forked from: forked from: flash on 2009-12-3

by terra1119 forked from forked from: flash on 2009-12-3 (diff: 1)
♥0 | Line 71 | Modified 2010-01-09 16:53:59 | MIT License
play

ActionScript3 source code

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

// forked from terra1119's forked from: flash on 2009-12-3
// forked from terra1119's flash on 2009-12-3
package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;

	public class Bouncing extends Sprite {
		
		private var ball:Ball;
		private var vx:Number;
		private var vy:Number;
		private var left:Number;
		private var right:Number;
		private var top:Number;
		private var bottom:Number;
		private var bounce:Number = -0.7;
		
        public function Bouncing() {
			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() * 10 - 5;
			vy = Math.random() * 10 - 5;
			addChild(ball);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		private function onEnterFrame(e:Event):void 
		{
			ball.x += vx;
			ball.y += vy;
			left = 0;
			right = stage.stageWidth;
			top = 0;
			bottom = stage.stageHeight;
			if (ball.x + ball.radius > right) {
				ball.x = right - ball.radius;
				vx *= bounce;
			} else if (ball.x - ball.radius < left) {
				ball.x = left + ball.radius;
				vx *= bounce;
			}
			if (ball.y + ball.radius > bottom) {
				ball.y = bottom - ball.radius;
				vy *= bounce;
			} else if (ball.y - ball.radius < top) {
				ball.y = top + ball.radius;
				vy *= bounce;
			}
		}
		
    }
}
import flash.display.Sprite;
class Ball extends Sprite {
	public var radius:Number;
	public var color:uint;
	public var vx:Number;
	public var vy:Number;
	
	public function Ball(radius:Number=40,color:uint=0xff0000){
		this.radius = radius;
		this.color = color;
		init();
	}
		
	private function init():void{
		graphics.beginFill(color);
		graphics.drawCircle(0, 0, radius);
		graphics.endFill();
	}
}