user1078385
user1078385

Reputation: 344

Generate random falling objects in AS3

I want to create a set of random objects to fall down the stage in a loop.

So far I have created a test object to fall at a random x coordinate. I am having trouble working out how to loop the falling function so multiple instances of the object continuously fall.

var randomX:Number = Math.random() * 800;

test_mc.x = randomX;
test_mc.y = 0;

var speed:Number = 10;

test_mc.addEventListener(Event.ENTER_FRAME, moveDown);

function moveDown(e:Event):void
{
 e.target.y += speed; 

 if(e.target.y >= 480)
 {
      test_mc.removeEventListener(Event.ENTER_FRAME, moveDown);
 }
}

Upvotes: 0

Views: 11332

Answers (2)

bitmapdata.com
bitmapdata.com

Reputation: 9600

refer my a falling snow effect code.

The starting position of the snow is all random, almost same effect that real snow falling circumstance. If you run You'll be amazed.

The Snow is My Custom MovieClip (white circle shape, width 15, height 15)

here is my demo: SnowEffect

here is my source: SnowEffect Down

this.addEventListener( Event.ENTER_FRAME, onEnter );

function onEnter( e: Event ):void {

    var s: Snow = new Snow();
    s.x=550*Math.random();
    s.y=-20;
    s.width=s.height=1+9*Math.random();// 1 ~ 9

    s.xSpeed=-2+4*Math.random();// -2 ~ 2
    s.ySpeed=1+4*Math.random();// 1 ~ 5

    s.at = -0.001 -0.001*Math.random();
    s.vt = 0;
    this.addChild( s );

    s.addEventListener( Event.ENTER_FRAME, onSnowEnter );
}

function onSnowEnter( e: Event ):void {
    var s:Snow=e.currentTarget as Snow;
    s.x+=s.xSpeed;
    s.y+=s.ySpeed;


    if (s.y>=480) {
        s.addEventListener( Event.ENTER_FRAME, onMeltingEnter );
    }
}

function onMeltingEnter( e: Event ): void {
    var s:Snow=e.currentTarget as Snow;
    this.addChild( s );
    s.removeEventListener( Event.ENTER_FRAME, onSnowEnter );
    s.vt += s.at;
    s.alpha += s.vt;
    if ( s.alpha <=0){
        s.removeEventListener( Event.ENTER_FRAME, onMeltingEnter );
        this.removeChild( s );
    }

}

Upvotes: 2

Pixel Elephant
Pixel Elephant

Reputation: 21383

Create a bunch of objects, add them to an array and then loop through the array:

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);
   }
}

The code above is just using red circles - you will have to use whatever image you have instead (unless you like red circles...).

Upvotes: 1

Related Questions