1 Reply Latest reply on Jun 23, 2009 5:34 AM by Ned Murphy

# Incrementing several movieclip positions when adding to stage using for loop?

I'm adding several similar movieclips to the stage with a 'for' loop. However, I'd like to increment the position of each movieclip when added on to the stage. I know exactly where I want each movie clip positioned as well. At the moment I just have a small 'for' loop which setups the movieclip. Like so...

ActionScript Code:
```for (var i:uint = 0; i<=8; i++)
{
zone[i] = new Zone();
}```

I then have a long list adding and setting each mc position on the stage...

ActionScript Code:
```stage.addChild(zone[0]);
zone[0].x=267.6;
zone[0].y=120.5;
stage.addChild(zone[1]);
zone[1].x=596.5;
zone[1].y=120.5;
// code repeated for each movieclip until...
stage.addChild(zone[8]);
zone[8].x=925;
zone[8].y=364.6;```

What I'd like to do is make this code more efficient and somehow get the long list of code above into the for loop? For example...

ActionScript Code:
```for (var i:uint = 0; i<=8; i++)
{
zone[i] = new Zone();
// and start the position aspect of the loop with something like...
// zone[i].x=135 - so this will be x coordinates for zone[0]
// zone[i].x=30 - y coordinates for zone[0]
stage.addChild(zone[i]);
// then somehow increment the x and y positions for the next zone movieclips    //...so add 315 to the zone[1].x position and add 165 to zone[1].y
}```

Although I've just had a thought as I was typing that last bit of code. Will I need to setup a for loop for each row...so the y position remains constant and I just increment the x value for each movie clip, and then repeat this for loop for the next row with the new y coordinate?

Any pointers greatly appreciated!

• ###### 1. Re: Incrementing several movieclip positions when adding to stage using for loop?

If the x increments would be equally soaced, then you can have a starting x value and add the increment for each loop. If they are somewhat irregular, hen you can use and array that holds the x values for each instance.

var xplacement:Number = 267.6;

var yPlacement:Number = 120.5;

for (var i:uint = 0; i<=8; i++)
{
zone[i] = new Zone()
zone[i].x= xPlacement;
zone[i].y= yPlacement;
stage.addChild(zone[i]);
xPlacement += ?;
yPlacement += ?;
}