Wednesday, April 20, 2011

AS3 Code: For Loop

//loops are for processing really fast, not animating
//loops work well with arrays
var myArray:Array = ["one", "two", "three", "four", "five"];

for(var i:Number=0; i< myArray.length; i++){
    //repeats until done
    //i is a variable to keep track of iterator
    //i<# checks if i is less than #; if so, jump to brackets
    //i++ means to add 1 to i
   
    trace(myArray[i]);
    //one
    //two
    //three
    //four
    //five
}


//add Bubble clip to stage from library
//see also: Class Linkage
for(var i:Number=0; i<100; i++){ //generate 100 clips
    var clip:MovieClip = new Bubble(); //Bubble is Class name
    addChild(clip); //each iteration is added to stage
    clip.x = Math.random() * stage.stageWidth; //random placement
    clip.y = Math.random() * stage.stageHeight;

//change to random colors
//add this code inside loop brackets
    var myColor:ColorTransform = clip.transform.colorTransform;
    myColor.color = Math.random() * 0xFFFFFF;
    clip.transform.colorTransform = myColor;


//also can add inside brackets
   clip.alpha = Math.random();


0 comments:

Post a Comment