/**
* Keyboard Control class
* The keyboard button action format, the general use of games. Author: Flying Rain Shadow
* Usage: control.initstage (stage), initialization stage; Control.addlistener (function), add listener;
* Change control settings: Control.changecontrol (array); array as set KeyCode
*/
Package
{
Import Flash.display.Stage;
Import flash.events.KeyboardEvent;
Import Flash.events.EventDispatcher;
Import flash.events.Event;
public class control
{
/**
* State is the button status, whether to press Isdown to get the status
* Controlkeycode sets the array for button control,
* Defaults to w=, s=, A= left, d=, u,i,j,k to four action buttons
*/
static private var State:array = new Array (), controlkeycode:array=new Array (87,83,65,68,85,73,74,75);
Static private var controlkey:array=new array (), controlname:array=new array (' Upper ', ' lower ', ' left ', ' right ', ' light fist ', ' punch ', ' light foot ', ' heavy foot ');
Static private Var eventdispatcher:eventdispatcher=new eventdispatcher ();
static public Function Initstage (stage:stage): void
{
var len:uint=controlkeycode.length;
for (Var i:uint=0;i<len;i++)
{
Controlkey[controlkeycode[i]]=controlname[i];
}
Stage.addeventlistener (Keyboardevent.key_down, OnKeyDown);
Stage.addeventlistener (keyboardevent.key_up, onKeyUp);
}
static public Function Isdown (code:uint): Boolean
{
Return state[Code] = = true;
}
static public Function Changecontrol (keycode:array): void
{
Controlkeycode = KeyCode;
}
static public Function AddListener (listener:function): void
{
var len:uint=controlkeycode.length;
for (Var i:uint=0;i<len;i++)
{
Eventdispatcher.addeventlistener (Controlname[i],listener);
Eventdispatcher.addeventlistener (controlname[i]+ ' release ', listener);
}
}
Static Private Function OnKeyDown (event:keyboardevent): void
{
var Code:uint=event.keycode
if (!isdown (code))
{
state[code] = true;
Trace (Code, "Press");
if (Controlkey[code]) eventdispatcher.dispatchevent (new Event (Controlkey[code));
}
}
Static Private Function OnKeyUp (event:keyboardevent): void
{
var Code:uint=event.keycode
state[Event.keycode] = false;
Trace (Event.keycode, ' release ')
if (Controlkey[code]) eventdispatcher.dispatchevent (new Event (controlkey[code]+ "release"));
}
}
}