Free ActionScript

Flash AS2 & AS3 Tutorials, Game Code, Effects, Source Files & Sample Downloads

8-way keyboard player movement

9 Comments »

This is an optimized AS3 version of the old movement code.

8-way keyboard player movement script for flash games.

Click and use Arrow Keys to move:

View Code ACTIONSCRIPT
/**
 * Player Movement - 8-way keyboard
 * ---------------------
 * VERSION: 1.0
 * DATE: 9/23/2010
 * AS3
 * UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com
 **/
package  
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
 
	public class Main extends MovieClip
	{		
		// player
		private var _player:MovieClip;
 
		// player settings
		private var _playerSpeed:Number = 4;
 
		// movement flags
		private var _movingUp:Boolean = false;
		private var _movingDown:Boolean = false;
		private var _movingLeft:Boolean = false;
		private var _movingRight:Boolean = false;
 
		/**
		 * Constructor
		 */
		public function Main() 
		{
			createPlayer();
 
			// add listeners
			stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, myOnPress);
			stage.addEventListener(KeyboardEvent.KEY_UP, myOnRelease);			
		}
 
		/**
		 * Creates player
		 */
		private function createPlayer():void
		{
			_player = new Player();
			_player.x = stage.stageWidth / 2;
			_player.y = stage.stageHeight / 2;
			stage.addChild(_player);
		}
 
		/**
		 * EnterFrame Handlers
		 */
		private function enterFrameHandler(event:Event):void
		{
			// Move up, down, left, or right
			if ( _movingLeft && !_movingRight )
			{
				_player.x -= _playerSpeed;
				_player.rotation = 270;
			}
			if ( _movingRight && !_movingLeft )
			{
				_player.x += _playerSpeed;
				_player.rotation = 90;
			}
			if ( _movingUp && !_movingDown )
			{
				_player.y -= _playerSpeed;
				_player.rotation = 0;
			}
			if ( _movingDown && !_movingUp )
			{
				_player.y += _playerSpeed;
				_player.rotation = 180;
			}
 
			// Move diagonally
			if ( _movingLeft && _movingUp && !_movingRight && !_movingDown )
			{
				_player.rotation = 315;
			}
			if ( _movingRight && _movingUp && !_movingLeft && !_movingDown )
			{
				_player.rotation = 45;
			}
			if ( _movingLeft && _movingDown && !_movingRight && !_movingUp )
			{
				_player.rotation = 225;
			}
			if ( _movingRight && _movingDown && !_movingLeft && !_movingUp )
			{
				_player.rotation = 135;
			}
		}
 
		/**
		 * Key Press Handlers
		 */
		public function myOnPress(event:KeyboardEvent):void
		{
			switch( event.keyCode )
			{
				case Keyboard.UP:
					_movingUp = true;
					break;
 
				case Keyboard.DOWN:
					_movingDown = true;
					break;
 
				case Keyboard.LEFT:
					_movingLeft = true;
					break;
 
				case Keyboard.RIGHT:
					_movingRight = true;
					break;
			}
		}
 
		/**
		 * Key Release Handlers
		 */
		public function myOnRelease(event:KeyboardEvent):void
		{
			switch( event.keyCode )
			{
				case Keyboard.UP:
					_movingUp = false;
					break;
 
				case Keyboard.DOWN:
					_movingDown = false;
					break;
 
				case Keyboard.LEFT:
					_movingLeft = false;
					break;
 
				case Keyboard.RIGHT:
					_movingRight = false;
					break;
			}
		}
 
	}
 
}
Download Fla Sample

Download Fla Sample

9 Responses

I’m trying to remap the controls to wasd and i’m getting this:

“C:\Users\Jason\Documents\Work\flash tutorials\player-movement-8-way-keyboard\player-movement-8-way-keyboard\Main.as, Line 107 1119: Access of possibly undefined property W through a reference with static type Class.”

and for all other lines where the key is defined.

its also not bringing up the Player movieclip

  • Hi jazun33,
    You need to check for key presses this way:

    stage.addEventListener(KeyboardEvent.KEY_UP, onKeyEvent);
    function onKeyEvent(e:KeyboardEvent):void
    {
    var character:String = String.fromCharCode(e.charCode);
    trace(character);
    if (e.keyCode == 66)
    {
    //do something
    }
    }

  • Is there a slick way to get the rotation to have smooth easing? So instead of turning so aburptly there is kind of a realistic turning motion? That would be pretty cool to see! Thanks for the help with the code btw

  • Kinda like that but there is a distinct propulsion by pressing the up key… what if all keys propelled but in their respective directions. Pressing the up left key while pressing the up key wouldnt make you go in a circle, but it would just rotate 45 degrees smoothly and continue in a northwest direction. does that make sense?

  • I’m trying to make it so that it is a 4-way movement code. I thought I could just remove the “//move diagonally” section but then I realized that only controls the rotation. Which section controls the ability to move in 8 ways?

  • Rob,

    After removing the //move diagonally‚ÄĚ section, change the movement code to this:
    // Move up, down, left, or right
    if ( _movingLeft )
    {
    _player.x -= _playerSpeed;
    _player.rotation = 270;
    }
    else if ( _movingRight )
    {
    _player.x += _playerSpeed;
    _player.rotation = 90;
    }
    else if ( _movingUp )
    {
    _player.y -= _playerSpeed;
    _player.rotation = 0;
    }
    else if ( _movingDown )
    {
    _player.y += _playerSpeed;
    _player.rotation = 180;
    }

  • what if i’m using the diagonal movements in this sprite sheet?
    http://i52.photobucket.com/albums/g12/MidnightMagik/Sprites/Sora/Mikails_Spriteszoom.png

    Credits’ in the file.

  • Leave a Reply

    You must be logged in to post a comment.