Looping "For" in Actionscript 3.0 (Part 2)

This time the update statement increments the iteration variable by 5. The loop will stop once is no longer less than 20.

   for(var i:int=0; i<20; i+=5){
      trace(i);
   }
   //output 0 5 10 15


The for loop in the function below instantiates a new instance of a TextField duing each iteration. The iteration variable is used to set the x and y position of each instance. Because I changes each iteration, the x and y position will be different for each instance.

   public function makeMessages():void{
      for(var i:int=0; i<8; i++){
         textMessage = new TextField();
         textMessage.x = i * 10;        
         textMessage.y = i * 30;        
         textMessage.text = "hello";        
         addChild(textMessage);    
      }
   }
   //output for x and y
   0, 0
   10, 30
   20, 60
   30, 90
   40, 120
   50, 150
   60, 180
   70, 210

source : www.adobe.com

Share this

Related Posts

Previous
Next Post »