4 Replies Latest reply on Aug 16, 2007 9:28 PM by gbowhall

    Dynamic variable concatenation (eval vs array access operators)

    gbowhall Level 1
      Hello:

      Well it has been a long time since I've been emerged in Actionscript, and I'm struggling getting up to speed with some of the changes. I wrote a bunch of code that uses the "eval" function to dynamically reference variables and my movie is running slow. This may or may not be related to my "eval"s all over the place, but I was reading that eval isn't really recommended or supported these days.

      Here is what I am trying to convert from eval to array access operators
      --
      eval("_level600.NUM_FULLBAR.NUM_HIT_1_25.NUM_" + _level0.x + "_HIT.NUM_" + _level0.x + "_ACT")._alpha = 0;
      --
      I've tried something like this (with a trace to see what's happening, but it's coming back undefined)
      trace("OUTSIDE = " + _level600["NUM_FULLBAR.NUM_HIT_1_25.NUM_" + _level0.x + "_HIT"]._alpha);

      Does anyone have any ideas on how to get that to work?

      Thank you for any help you can offer! (I've been staring at this for a couple hours =(

      - Garrett
        • 1. Re: Dynamic variable concatenation (eval vs array access operators)
          Rothrock Level 5
          You need to replace the dots inside of the array. You could do this:

          _level600.NUM_FULLBAR.NUM_HIT_1_25["NUM_"+_level0.x]["_HIT.NUM_"+_level0.x+"_ACT"]._alpha

          I think. Your naming convention, the underscores, and the caps make it really hard to read and follow what is trying to go on there.
          • 2. Re: Dynamic variable concatenation (eval vs array access operators)
            gbowhall Level 1
            Thank you for such a quick reply. I still don't have this quite working, but I tried to clean this up to clarify what I'm asking (I didn't realize until I started stringing all of those movie clips together how illegible all those all caps and underscores were, sorry). Here is the cleaned up version:


            What I am trying to convert to use array access operators (where parent.child takes the place of those illegible letters from before)
            --
            _level600.parent.child.num_" + _level0.x + "_hit._alpha


            What I'm trying to get it to concatenate to (the x variable = 1 in this case)
            --
            _level600.parent.child.num_1_hit._alpha


            Thank you for the help!
            • 3. Re: Dynamic variable concatenation (eval vs array access operators)
              Rothrock Level 5
              The array notation says look inside this object for something with whatever is between the square braces. So if on the same timeline as the code there is a clip called myClip and it has a child called myChild you could get its _alpha like this:

              this.myClip.myChild._alpha
              this["myClip"].myChild._alpha
              this["myClip"]["myChild"]._alpha
              this["myClip"]["myChild"]["_alpha"]
              this.myClip["myChild"]._alpha
              and so on…

              In the above case nothing is dynamic so there is no need to use the concatenation +, but if you just think about each step in the path as its own array of objects it gets easier. Those are just strings above because we are looking for the name of something. If they weren't strings, Flash would think they are variables and try and find an object in that object with a name that matched the value of the variable. So if you need dynamic names, you you just start adding things together

              _level600.parent.child["num_"+_level0.x+"_hit"]._alpha

              I'm a bit confused by parent without the _ and then _alpha with it. Is this AS2 or 3?
              • 4. Re: Dynamic variable concatenation (eval vs array access operators)
                gbowhall Level 1
                Thank you for the help! I'm finishing converting all of my evals over to the [ ] operators and hoping that this will speed up the movie. In regards to your comment about the "parent", i only meant that loosely to describe the chain of movieclips being referenced (I had a confusing string of all caps before, so I replaced that to make it easier to read) It wasn't meant to be the official _parent

                Thank you so much for your help, I think you saved me at least another couple hours lost in stare...

                - Garrett