As defines keyboard key actions

Source: Internet
Author: User

/**
* 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"));
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.