flash on 2011-6-28

by yama3
♥0 | Line 65 | Modified 2011-06-28 13:19:12 | MIT License
play

ActionScript3 source code

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

package {
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    [SWF(backgroundColor="0x000000", width=465, height=465)]
    public class FlashTest extends Sprite {
        private var ball:Array = [];
        private var n:int = 100;
        
        public function FlashTest() {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            for(var i:int=0; i<n; i++) {
                ball.push(new Ball(Math.random()*30+15, Math.random()*30+15, Math.random()*0xffffff));
                ball[i].x = stage.stageWidth/2;
                ball[i].y = stage.stageHeight/2;
                addChild(ball[i]);
            }
            addEventListener(Event.ENTER_FRAME, onEnterFrame);            
        }
        private function onEnterFrame(e:Event):void
        {
            for(var i:int=0; i<n; i++)
            {
                ball[i].move();
            }
        }
    }
}

import flash.display.Sprite;

class Ball extends Sprite {
    private var vx:Number;
    private var vy:Number;
    private var r:Number = Math.random()*10+5;
    private var g:Number = 0.98;
    private var e:Number = 0.9;
    public function Ball(_vx:Number, _vy:Number, color:Number) {
        vx = Math.cos(Math.random()*Math.PI*2/3+Math.PI/6)*_vx;
        vy = -Math.sin(Math.random()*Math.PI*2/3+Math.PI/6)*_vy;
        
        graphics.beginFill(color);
        graphics.drawCircle(0, 0, r);
        graphics.endFill();
    }
    
    public function move():void {
        this.x += vx;
        this.y += vy;
        
        if(this.x - r < 0) {
            vx *= -e;
            this.x = r;
        }
        if(this.x+r > this.stage.stageWidth) {
            vx *= -e;
            this.x = this.stage.stageWidth-r;
        }
        if(this.y -r < 0) {
            vy *= -e;
            this.y = r;
        }
        if(this.y + r > this.stage.stageHeight) {
            vy *= e;
            this.y = this.stage.stageHeight - r;
        }
        vy += g;
    }
}