Tuesday, August 28, 2007

Loops and pulling movieclips out of library at runtime

Loops are pretty much the same. The only difference is that you have to put what the variable is (a number)...like this:

for(var i:Number = 0; i < 6; i++){ //you have to put "var" and that i is a number
trace(i);
}


Before if you wanted to place a bunch of movieclips on the stage at runtime using a loop, you would use attachMovie. Now you do this:

// this will pull six boxes out of the library and they will will each be placed up and to the right
var myBox:mcBox; //this creates a box variable. mcBox is what I put as the class in the movieclip properties
var myBoxX:Number = 127; //this is where the first box will be placed on the X axis
var myBoxY:Number = 271; //ditto but on the Y axis
var myBoxR:Number = 0; //start rotation at 0

for(var i:Number = 0; i < 6; i++){ //set the loop
myBox = new mcBox(); //this pulls an instance of the box mc outta the library each time the loop runs
addChild(myBox); //the new attachMovie. myBox is the variable I set above (will set 6 boxes on the stage)
myBox.x = myBoxX; //set the x position
myBox.y = myBoxY; //set y
myBox.rotation = myBoxR; //set rotation
myBoxY -= 35; //move each box up 35 pixels from the last one
myBoxX += 75; //move each 75 px over
myBoxR -= 45; //rotate each box 45 degrees clockwise
}
//Remember to click "export for actionscript" in the properties menu of the symbol in the library. In this case you have to put "mcBox" as the class name

No comments: