Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package{
- import flash.display.Sprite;
- public class Ball extends Sprite{
- public var speed:int;
- public function Ball(){
- this.graphics.beginFill(0xABCDEF);
- this.graphics.drawCircle(0, 0, 5);
- this.graphics.endFill();
- speed = int(Math.random()*20+1);
- }
- public function destroy(){
- this.graphics.clear();
- }
- }
- }
- ------------------------
- Frame 1
- ------------------------
- import flash.events.Event;
- import flash.display.StageScaleMode;
- import flash.display.StageAlign;
- import flash.text.TextField;
- import flash.text.TextFormat;
- stage.scaleMode = StageScaleMode.NO_SCALE;
- stage.align = StageAlign.TOP_LEFT;
- var txt:TextField = new TextField();
- txt.setTextFormat(new TextFormat("Arial", 12, 0x000000));
- txt.x = txt.y = 5;
- this.addChild(txt);
- var balls:Vector.<Ball> = new Vector.<Ball>();
- var ball:Ball;
- var i:int;
- stage.addEventListener(Event.ENTER_FRAME, iterator);
- function iterator(event:Event){
- // Create a new ball every frame
- ball = new Ball();
- balls.push(ball);
- // Place ball at random location
- ball.x = stage.stageWidth*Math.random();
- this.addChild(ball);
- // Move existing balls
- for(i=0; i<balls.length; i++){
- balls[i].y += balls[i].speed;
- // Delete ball if out of bounds
- if(balls[i].y > stage.stageHeight){
- this.removeChild(balls[i]);
- balls[i].destroy();
- balls.splice(i, 1);
- }
- }
- txt.text = String(balls.length);
- }
Add Comment
Please, Sign In to add comment