4 Replies Latest reply on Jul 10, 2007 8:33 AM by Ghost Of Death

    Applying action to many objects

    Ghost Of Death
      How can i apply an action to many instances of the same object or different objects without the need of writting the same code tens of times ???
      Like i have 10 copies of a circle and i want in the first frame to set their x positions to 100, can i do this in one step instead of writing the code for each one???

      Thanx
        • 1. Applying action to many objects
          Greg Dove Level 4
          If they have sequential instance names you can do it with a loop

          e.g. for circle1, circle2 etc.

          as2 example:

          for (var i =1; i<=10;i++) {
          this["circle"+i]._x =100;
          }

          in the code above "this" refers to the timeline that the code runs on.

          You can change "this" above (without the quotes) to be _root or _level0 or the name of a movieclip that contains all your circles if they're inside another clip and the code is running at the parent timeline level of that clip. e.g. circleHolder["circle"+i] would reference each "circle" instance inside a circleHolder clip where circleHolder exists on the same timeline as the code.

          • 2. Re: Applying action to many objects
            Ghost Of Death Level 1
            Thanks man for helping, and ur method worked with the example i gave ( the circles ). But the thing that i'm trying to do is the following:

            i have some walls and i want to test if a circle i made ( which the user can move by arrows ) hits the walls or not. i could made it for one wall by using hit test function. but the method u gave me didnt work with this. i wrote:

            for(var i=1; i<=5; i++){
            if ( this["wall" + i].hitTest(ball) ){
            ....................................................
            ....................................................
            ....................................................
            }
            }

            also i tries the opposite:

            for(var i=1; i<=5; i++){
            if (ball.hitTest(this["wall" + i]) ){
            ....................................................
            ....................................................
            ....................................................
            }
            }


            and THANX
            • 3. Re: Applying action to many objects
              Greg Dove Level 4
              Just tested it... this works fine (its not using the arrows but that part's not relevant):

              ball.onPress = function() {
              ball.startDrag();
              };
              ball.onRelease = function() {
              ball.stopDrag();
              };
              this.onEnterFrame = function() {

              for (var i = 0; i<=4; i++) {
              if (ball.hitTest(this["wall"+i])) {
              trace('hit wall '+i);
              trace('---');
              }
              }
              };
              • 4. Re: Applying action to many objects
                Ghost Of Death Level 1
                Thanx alot man, everything is working fine :)