Dynamically generated stars

In the movie the 10 stars are generated dynamically using a loop.
Get Adobe Flash player

In this movie, we will use a loop to create 10 stars dynamically. We want to be able to drag any of the shapes after we create them.

Draw a star and make it a MovieClip named Star, and export for ActionScript. Remove the instance on the stage, all of the stars are created dynamically:

The code is shown below:

for(var i:int=0;i<10;i++) {
   var str:Star=new Star();
   str.x=Math.random()*stage.stageWidth;
   str.y=Math.random()*stage.stageHeight;
   this.addChild(str);
}

Download the movie
INDEX, A simple drag and drop example, e.target: Using the same code for many shapes, Dynamically generated stars, Drag and drop dynamically generated shapes, Naming dynamically generated stars
Next lesson: Hit Test

Copyright © Zebra0.com
All rights reserved worldwide.

 
 

Dynamically generated stars