Skip to main content
Known Participant
July 10, 2007
Question

Applying action to many objects

  • July 10, 2007
  • 4 replies
  • 261 views
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
This topic has been closed for replies.

4 replies

Known Participant
July 10, 2007
Thanx alot man, everything is working fine :)
Inspiring
July 10, 2007
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('---');
}
}
};
Known Participant
July 10, 2007
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
Inspiring
July 10, 2007
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.