Key input (AS2 style)
as you'll no doubt have found out, the beloved isDown and isUp static functions are no longer existent, however, due to improvements with the Key event broadcasts, it is possible to make your own.
in AS2, tbh, the key event broadcasts were pathetic, example, hold down A, release, a KeyUp event is broadcasted, ... but, hold down A, then B, then release A, and no event is broadcasted, this is no longer the case in AS3.
basicly, i use the same technique i do in my C++ programming for simulating an isDown style function, hold a list of all key codes, with a boolean value for whether it is down or up, and access the list for the isDown isUp functions. in otherwords: below.
Feel free to use this class for you're own purposes, there is nothing majorly advanced about this, you can work out what is going on easily enough.
package delta.ui
{
import flash.events.KeyboardEvent;
public class Key
{
private var keys:Array;
private function downhandle(obj:KeyboardEvent):void
{
keys[obj.keyCode] = true;
}
private function uphandle(obj:KeyboardEvent):void
{
keys[obj.keyCode] = false;
}
public function isDown(keyCode:uint, ...keyCodes):Boolean
{
if(keyCodes.length==0)
return keys[keyCode];
else
{
if(!keys[keyCode]) return false;
for(var i:uint = 0; i<keyCodes.length; ++i)
{
if(!keys[keyCodes[i]]) return false;
}
return true;
}
}
public function isUp(keyCode:uint, ...keyCodes):Boolean
{
if(keyCodes.length==0)
return !keys[keyCode];
else
{
if(keys[keyCode]) return false;
for(var i:uint = 0; i<keyCodes.length; ++i)
{
if(keys[keyCodes[i]]) return false;
}
return true;
}
}
public function Key(stage:Object):void
{
keys = new Array(222);
stage.addEventListener(KeyboardEvent.KEY_DOWN ,downhandle,false,0,true);
stage.addEventListener(KeyboardEvent.KEY_UP,u phandle,false,0,true);
}
}
}
basicly, i assign a listener for the keyboard event KeyDown and KeyUp events, and retrieve the key that was broadcasted for, modify the array, and then access the array to determine if a key is down or not.
Usage is like this:
import delta.ui.Key;
import flash.ui.Keyboard;
var keyui:Key = new Key(stage); // YOU MUST PASS 'stage' TO THE CONSTRUCTOR
...
if(keyui.isDown(Keyboard.DOWN)){}
if(keyui.isUp(Keyboard.UP)){}
Also note, that the isDown, isUp take multiple parameters:
in other words:
keyui.isDown(Keyboard.DOWN,Keyboard.UP,Keyboa rd.LEFT);
This function call will only return true, if DOWN, UP AND LEFT are all currently being pressed, i think this is a nice little improvement upon AS2's function calls in my opinion