Basic Movement

by milchreis
WASD to move around
♥0 | Line 51 | Modified 2012-07-16 07:12:20 | MIT License
play

ActionScript3 source code

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

package {
    import flash.events.Event;
    import flash.geom.Point;
    import flash.display.Sprite;
    import flash.events.KeyboardEvent;
    
    [SWF(
        framerate = "40"
    )]
    
    public class FlashTest extends Sprite {
 
        private const SPEED:Number = 10;      
        private var velocity:Point;  
        private var ball:Sprite;
        
        public function FlashTest() {
            // write as3 code here..
            
            velocity = new Point();
            
            //some object
            ball = new Sprite();
            ball.graphics.beginFill(0xFF0000);
            ball.graphics.drawCircle(0, 0, 20);
            ball.graphics.endFill();
            addChild(ball);
            
            ball.x = ball.y = 150;
            
            
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
            
            addEventListener(Event.ENTER_FRAME, onLoop);
        }
        
        private function onLoop(e:Event):void
        {
            ball.x += velocity.x;
            ball.y += velocity.y;
        }

        
        private function onDown(e:KeyboardEvent):void
        {
            switch (e.keyCode)
            {
                case 87: velocity.y = -SPEED;break; //w
                case 83: velocity.y = +SPEED;break; //s
                case 65: velocity.x = -SPEED;break; //a
                case 68: velocity.x = +SPEED;break; //d
            }

        }
        
        private function onUp(e:KeyboardEvent):void
        {
            switch (e.keyCode)
            {
                case 87: velocity.y = 0;break;
                case 83: velocity.y = 0;break;
                case 65: velocity.x = 0;break;
                case 68: velocity.x = 0;break;
            }

        }

    }
}