Return to Snippet

Revision: 40482
at February 2, 2011 23:04 by JaaQ


Updated Code
private var goRight:Boolean = false;
private var goLeft:Boolean = false;
private var goDown:Boolean = false;
private var goUp:Boolean = false;


public function Main() 
{
	stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
	stage.addEventListener(KeyboardEvent.KEY_UP, upHandler);
}
	
private function keyHandler(e:KeyboardEvent):void 
{
	switch (e.keyCode) 
	{
		case Keyboard.UP:
			goUp = true;
		break;
		
		case Keyboard.DOWN:
			goDown = true;
		break;
		
		case Keyboard.LEFT:
			goLeft = true;
		break;
		
		case Keyboard.RIGHT:
			goRight = true;
		break;	
		
		default:
			trace(e.keyCode);
		break;
	}
}
	
	
private function upHandler(e:KeyboardEvent):void 
{
	switch (e.keyCode) 
	{
		case Keyboard.UP:
			goUp = false;
		break;
			
		case Keyboard.DOWN:
			goDown = false;
		break;
			
		case Keyboard.LEFT:
			goLeft = false;
		break;
	
		case Keyboard.RIGHT:
			goRight = false;
		break;	
	
		default:
			trace(e.keyCode);
		break;
}

Revision: 40481
at February 2, 2011 23:02 by JaaQ


Initial Code
private var goRight:Boolean = false;
	private var goLeft:Boolean = false;
	private var goDown:Boolean = false;
	private var goUp:Boolean = false;


	public function Main() 
	{
		stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
		stage.addEventListener(KeyboardEvent.KEY_UP, upHandler);
	}
		
	private function keyHandler(e:KeyboardEvent):void 
	{
		switch (e.keyCode) 
		{
			case Keyboard.UP:
				goUp = true;
			break;
			
			case Keyboard.DOWN:
				goDown = true;
			break;
			
			case Keyboard.LEFT:
				goLeft = true;
			break;
			
			case Keyboard.RIGHT:
				goRight = true;
			break;	
			
			default:
				trace(e.keyCode);
			break;
		}
	}
		
		
	private function upHandler(e:KeyboardEvent):void 
	{
		switch (e.keyCode) 
		{
			case Keyboard.UP:
				goUp = false;
			break;
				
			case Keyboard.DOWN:
				goDown = false;
			break;
				
			case Keyboard.LEFT:
				goLeft = false;
			break;
		
			case Keyboard.RIGHT:
				goRight = false;
			break;	
		
			default:
				trace(e.keyCode);
			break;
	}
}

Initial URL

                                

Initial Description
Listen to the Navigation keys and save their values in local Booleans

Initial Title
Listening to navigation keys

Initial Tags
actionscript, navigation

Initial Language
ActionScript 3