KeyBoardEventManager for Games

January 2nd, 2012 by aabhaanjan

images
[xml]
package
{

import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class KeyBoardEventManager extends MovieClip
{
private var _parent:Object;
private var _isRightPressed:Boolean = false;
private var _isLeftPressed:Boolean = false;
private var _isUPpressed:Boolean = false;
private var _isDOWNpressed:Boolean = false;
/*
* @Comments : During the development of a game a number of times we need Key Board
* controls in that case we can use this class.
*
*
*/

public function KeyBoardEventManager()
{
super();

}

public function addKeyListners(__parent:Object):void {
_parent = __parent
_parent.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
_parent.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
}

private function keyDownHandler(evt:KeyboardEvent):void {
if(evt.keyCode == Keyboard.RIGHT){
_isRightPressed = true
}
else if (evt.keyCode == Keyboard.LEFT ) {
_isLeftPressed = true;
}
else if (evt.keyCode == Keyboard.UP) {
_isUPpressed = true;
}
else if (evt.keyCode == Keyboard.DOWN) {
_isDOWNpressed = true;
}

}

private function keyUpHandler(evt:KeyboardEvent):void {
if (evt.keyCode == Keyboard.RIGHT){
_isRightPressed = false;
}
else if (evt.keyCode == Keyboard.UP){
_isUPpressed = false;
}
else if (evt.keyCode == Keyboard.DOWN){
_isDOWNpressed = false;
}
else if (evt.keyCode == Keyboard.LEFT){
_isLeftPressed = false;
}
}

public function getLEFTkeyStatus():Boolean {
return _isLeftPressed;
}

public function getRIGHTkeyStatus():Boolean {

return _isRightPressed;
}
public function getUPkeyStatus():Boolean {
return _isUPpressed;
}
public function getDownkeyStatus():Boolean {
return _isDOWNpressed
}

public function resetValuse():void {
_parent.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
_parent.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
_isRightPressed = false;
_isLeftPressed = false;
_isUPpressed = false;
_isDOWNpressed = false;

}
}

}
[/xml]