flash on 2010-11-22

by majoraze
♥0 | Line 62 | Modified 2010-11-22 00:34:52 | MIT License
play

ActionScript3 source code

/**
 * Copyright majoraze ( http://wonderfl.net/user/majoraze )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/6taz
 */

package {
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    
    public class Boundaries extends Sprite {
        private var count:int = 20;
        private var balls:Array;
        
        public function Boundaries() {
            init();
        }
        
        private function init():void {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            balls = [];
            for (var i:int = 0; i < count; i++) {
                var ball:Ball = new Ball();
                ball.x = Math.random() * stage.stageWidth;
                ball.y = Math.random() * stage.stageHeight;
                ball.vx = Math.random() * 2 - 1;
                ball.vy = Math.random() * 2 - 1;
                addChild(ball);
                balls.push(ball);
            }
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        private function onEnterFrame(e:Event):void {
            for (var i:int = balls.length - 1; i > 0; i--){
                var ball:Ball = balls[i] as Ball;
                ball.x += ball.vx;
                ball.y += ball.vy;
                if (ball.x - ball.radius > stage.stageWidth ||
                    ball.x + ball.radius < 0 ||
                    ball.y - ball.radius > stage.stageHeight ||
                    ball.y + ball.radius < 0) {
                        removeChild(ball);
                        balls.splice(i,1);
                        if (balls.length <=0) {
                            removeEventListener(Event.ENTER_FRAME, onEnterFrame);
                        }
                }
            }
        }
    }
}




//ball class

import flash.display.Sprite;

class Ball extends Sprite {
    public var radius:Number;
    private var color:uint;
    public var vx:Number = 0;
    public var vy:Number = 0;    
    
    public function Ball(radius:Number = 40, color:uint = 0xff0000) {
        this.radius = radius;
        this.color = color;
        
        init();
    }
    
    public function init():void {
        graphics.beginFill(color);
        graphics.drawCircle(0,0,radius);
        graphics.endFill()
    }
}

Forked