3 Replies Latest reply on Jun 2, 2009 8:55 AM by David Beedle

    Referencing a movie clip by using a variable

    David Beedle

      I have been using this line of code to fade in a movie clip:

       

      new Tween(background_mc, "_alpha", None.easeNone, 0, 100, .5, true);

       

      Now, I want to accomplish the same thing, only this time by using an element from an array (bg_array[currentSlideNumber]) that stores the instance name "background_mc"

       

      I've tried the following, but it doesn't work:

       

      new Tween(bg_array[currentSlideNumber], "_alpha", None.easeNone, 0, 100, .5, true);

       

      What am I doing wrong?

       

      Thanks

        • 1. Re: Referencing a movie clip by using a variable
          Ned Murphy Adobe Community Professional & MVP

          Try:

           

          new Tween(this[bg_array[currentSlideNumber]], "_alpha", None.easeNone, 0, 100, .5, true);

           

          Instead of storing the string version, you could try storing the instance/name itself in the array and probably use what you tried for the tween.

          1 person found this helpful
          • 2. Re: Referencing a movie clip by using a variable
            Rothrock Level 5

            You are telling flash to look inside that array and tween the _alpha property of the string -- because instance names are strings -- "background_mc" from 0 to 100. But generally speaking a string doesn't have an _alpha property.

             

            What you really want to do is tell flash, look inside this array find the string then use that string to look inside another array -- the array of all objects on some timeline -- to find a clip and then tween its _alpha property. That sounds like a lot, but it only adds a little bit to your code.

             

            new Tween(this[bg_array[currentSlideNumber]], "_alpha", None.easeNone, 0, 100, .5, true);

             

            I'm assuming the code is on the same timeline as background_mc?

             

            Annother approach would be to store instance references instead of the names. I'm not sure how you are creating the background, but instead of putting the name into the array, put a reference. So if you were using attachMovie you could do something like this.

             

            var bg_array:Array=new Array()

            var tempVar:MovieClip=this.attachMovie("background","background_mc",100);

            bg_array.push(tempVar);

             

            Finally, and not to anything you've asked about, but don't you find the suffixes annoying? Why, bg_array and background_mc? If you type your variables you will still get code completion and you will have less to type and your code won't be so long. I just find that having to type those underscores and all makes me likely to make mistakes and makes it much harder to read the code. Just a thought.

            • 3. Re: Referencing a movie clip by using a variable
              David Beedle Level 1

              Thanks so much for the answers and advice. Awesome.