加速度を変化させる(左右)

by _wonder forked from 加速するボール (diff: 20)
♥0 | Line 52 | Modified 2010-04-27 19:08:45 | MIT License
play

ActionScript3 source code

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

// forked from _wonder's 加速するボール
// forked from _wonder's base
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 ax:Number = 0;
    		
        public function Acceleration() {
            init();            
        }
        
        private function init():void {
        		ball = new Ball();
        		addChild( ball );
        		ball.x = stage.stageWidth / 2;
        		ball.y = stage.stageHeight / 2;
        		addEventListener(Event.ENTER_FRAME, onEnterFrame);
        		stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
        		stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
        }
        
        private function onKeyDown(e:KeyboardEvent):void {
        		if(e.keyCode == Keyboard.LEFT){
        			ax = -0.2;
        		} else if( e.keyCode == Keyboard.RIGHT){
        			ax = 0.2
        		}
        }
        
        private function onKeyUp(e:KeyboardEvent):void {
        		ax = 0;
        }
        
        private function onEnterFrame(e:Event):void {
        		vx += ax;
        		ball.x += vx;
        }
    }
}

import flash.display.Sprite;

class Ball extends Sprite {
	public var radius:Number;
	public var color:uint;
	
	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