创建一堆对象,将它们添加到数组中,然后循环遍历数组:
var numOfObjects:int = 10;
var fallingObjectArray:Array = [];
var speed:Number = 10;
// Add 10 falling objects to the display and to the array
for(var i:int = 0; i < numOfObjects; i++) {
var fallingObject:Sprite = new Sprite();
fallingObject.graphics.beginFill(0xFF0000);
fallingObject.graphics.drawCircle(0, 0, 15);
fallingObject.graphics.endFill();
addChild(fallingObject);
fallingObject.x = Math.random() * stage.stageWidth;
fallingObjectArray.push(fallingObject);
}
addEventListener(Event.ENTER_FRAME, moveDown);
function moveDown(e:Event):void
{
// Go through all the objects in the array and move them down
for each(var fallingObject in fallingObjectArray) {
fallingObject.y += speed;
// If the object is past the screen height, remove it from display and array
if(fallingObject.y+fallingObject.height >= stage.stageHeight) {
removeChild(fallingObject);
fallingObjectArray.splice(fallingObjectArray.indexOf(fallingObject), 1);
}
}
// Once all objects have fallen off the screen, remove the listener
if(fallingObjectArray.length <= 0) {
removeEventListener(Event.ENTER_FRAME, moveDown);
}
}
上面的代码只是使用红色圆圈——你必须使用你拥有的任何图像(除非你喜欢红色圆圈......)。