Acceleration
/**
* Copyright crossisland ( http://wonderfl.net/user/crossisland )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/9RNd
*/
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Acceleration extends Sprite {
private var ball:Ball;
private var vx:Number = 0;
private var vy:Number = 0;
private var ax:Number = 0;
private var ay:Number = 0;
public function Acceleration() {
init();
}
private function init():void {
ball = new Ball();
addChild(ball);
ball.x = 50;
ball.y = 100;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
private function onKeyDown(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.LEFT:
ax = -0.2;
break;
case Keyboard.RIGHT:
ax = 0.2;
break;
case Keyboard.UP:
ay = -0.2;
break;
case Keyboard.DOWN:
ay = 0.2;
break;
default:
break;
}
}
private function onKeyUp(event:KeyboardEvent):void {
ax = 0;
ay = 0;
}
private function onEnterFrame(event:Event):void {
vx += ax;
vy += ay;
ball.x += vx;
ball.y += vy;
}
}
}
import flash.display.Sprite;
class Ball extends Sprite {
public var radius:Number;
private var color:uint;
public function Ball(radius:Number=40, color:uint=0x000000) {
this.radius = radius;
this.color = color;
init();
}
public function init():void {
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}