5 Replies Latest reply on Oct 6, 2009 12:32 PM by Ned Murphy

    General Question About common Instance Names in Flash CS4 (AS3)

    Me2LoveIt2 Level 1

      I tried something interesting recently. I had code effecting an object with the instance name : object_mc.

      Now I added another object and gave it the same instance name.

       

      The result was, that the code only effected the most recent added object (no error).

       

      Is there a way in flash to have multiple objects with the same instance name all being affected by one piece of code?

       

      ~ Thanks for any thoughts, advice or facts. ~

        • 1. Re: General Question About common Instance Names in Flash CS4 (AS3)
          Ned Murphy Adobe Community Professional & MVP

          Advice: avoid it... if you want to control different instances, give them different instance names.

           

          It depends on what you mean by "one piece of code".  Directly using the instance name to target the movieclips will work as you have seen already... Last one added gets the attention.

           

          To use the instance name to take action is doable more along the lines of testing each movie to see if it has the name, but still, you would have to target each using a different means. 

           

          For the piece of code below I have some number of objects on the stage and I test to see if they have an instance name assigned (object_mc) that is shared by some of them, and I am able to move them if they do, but I cannot use the instance name to target each of them for action... in this case I use the getChildAt method.

           

          for(var i=0; i<this.numChildren; i++){
          if(this.getChildAt(i).name == "object_mc"){
            this.getChildAt(i).x += 200;

            object_mc.x += 25;  // only moves the last object added

          }
          }

          1 person found this helpful
          • 2. Re: General Question About common Instance Names in Flash CS4 (AS3)
            Me2LoveIt2 Level 1

            Thank you, this is really helpful to know!

            So you can actually affect all the objects with the same "piece of code" (like writing your code instead of writing "object_mc.x += 25;" for every single object, as long as they have the same instant name.

            This is exactly what I need.

            You said rather to avoid it. Why is that? Is there a catch, or simply for organisation purposes?

            • 3. Re: General Question About common Instance Names in Flash CS4 (AS3)
              Ned Murphy Adobe Community Professional & MVP

              It becomes easier to manage them individually if they have unique instance names or at least some means of isolating them by some unique property.  If you have no need to do that, then that's fine... there's all kinds of ways to approach designs.

               

              Usually when a series of instances are being used, they will be named sequentially, such as object_mc0, object_mc1, object_mc2, etc.  So you can single any one of them out if need be.  And if you want to act on all of them at the same time you can take a looping approach, though you don't have to take time to test anything....

               

              for(var i=0; i<numberOfobject_mcs; i++){
                this["object_mc"+String(i)].x += 200;

              }

               

              In that example the bracket/array notation is used in order to have a String value interpreted as an instance name.

              • 4. Re: General Question About common Instance Names in Flash CS4 (AS3)
                Me2LoveIt2 Level 1

                Thanks that's exactly what I wanted to know.