there is a Keyboard class in flash.ui package which has all the
constant you're using.
instead of an array, you can use a dictionary which looks more adapted
for your use than an array
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/ui/Keyboard.html
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/Dictionary.html
i had to do the same event listener than you and that's what i did :
addEventListener(KeyboardEvent.KEY_DOWN, keyboardHandler);
function keyboardHandler(e:KeyboardEvent):void
{
switch(e.keyCode)
{
case Keyboard.SPACE:
break;
// blabla....
default:
trace("this keycode is free to use :", e.keyCode);
}
}
Hope this helps :)
Le 10 juil. 09 à 09:51, Fabrice3D a écrit :
just store the keycode on down.
on up, if keycode is same trash and change state.
Fabrice
On Jul 10, 2009, at 6:42 AM, justaddice wrote:
I guess this might be more of a general Flash AS3 question, however
it
applies while I'm attempting to make a game using Away3D, so I'll
post
it here and hope someone can help me out...
My game is designed such that I use the arrow keys and space bar.
I've
noticed that if I'm pressing UP and RIGHT at the same time, and then
press SPACE, everything is ok (my ship fires), however using UP LEFT
and then SPACE wont trigger the keydown event (my ship dosn't
fire)...
If it helps here is the code I'm using (nicely wrapped in a package):
package
{
import flash.events.KeyboardEvent;
public class keys
{
public const LEFT_ARROW:int = 37;
public const RIGHT_ARROW:int = 39;
public const UP_ARROW:int = 38;
public const DOWN_ARROW:int = 40;
public const SPACE_BAR:int = 32;
public var keysTracked:Array;
public function keys(movieclip)
{
keysTracked = new Array();
movieclip.stage.addEventListener(KeyboardEvent.KEY_DOWN,
keyDownHandler);
movieclip.stage.addEventListener(KeyboardEvent.KEY_UP,
keyUpHandler);
}
public function trackKey(keyCode:int):void
{
keysTracked[keyCode] = false;
}
public function unTrackKey(keyCode:int):void
{
keysTracked[keyCode] = undefined;
}
public function isKeyDown(keyCode:int)
{
return keysTracked[keyCode];
}
function keyDownHandler(event:KeyboardEvent)
{
if(keysTracked[event.keyCode] != undefined)
keysTracked[event.keyCode] = true;
}
function keyUpHandler(event:KeyboardEvent)
{
if(keysTracked[event.keyCode] != undefined)
keysTracked[event.keyCode] = false;
}
}
}