bounce basic

by majoraze forked from flash on 2010-11-22 (diff: 40)
♥0 | Line 64 | Modified 2010-11-22 01:15:37 | 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/abzr
 */

// forked from majoraze's flash on 2010-11-22
package {
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    
    public class Boundaries extends Sprite {
        private var ball:Ball;
        private var vx:Number;
        private var vy:Number;
        
        public function Boundaries() {
            init();
        }
        
        private function init():void {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            
            ball = new Ball();
            ball.x = stage.stageWidth/2;
            ball.y = stage.stageHeight/2;
            vx = Math.random() * 20 - 10;
            vy = Math.random() * 20 - 10;
            
            addChild(ball);
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        private function onEnterFrame(e:Event):void {
            ball.x += vx;
            ball.y += vy;
            
            var left:Number = 0;
            var right:Number = stage.stageWidth;
            var top:Number = 0;
            var bottom:Number = stage.stageHeight;
            
            if (ball.x + ball.radius > right) {
                ball.x = right - ball.radius;
                vx *= -1;
            } else if (ball.x - ball.radius < left) {
                ball.x = left + ball.radius;
                vx *= -1;
            }
            
            if (ball.y + ball.radius > bottom) {
                ball.y = bottom - ball.radius;
                vy *= -1;
            } else if (ball.y - ball.radius < top) {
                ball.y = top + ball.radius;
                vy *= -1;
            }




        }
    }
}




//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