Here’s a tiny function I’m using in my current FP project. I like keeping as much as possible in data files instead of directly in code, so this is how I set up my input definitions.
public static function defineInput(xml:XML):void
{
for each (var input:XML in xml.children())
{
var keys:Array = [];
var keyNames:Array = String(input)
.replace(/[\s\r\n]+/gim, "")
.split(",");
for each (var key:String in keyNames)
{
keys.push(Key[key])
}
Input.define(input.name(), keys);
}
}
You give it an xml file that looks like this, using the static property names from the Key class. Comma-separated values will result in multiple options for the same input.
<keys>
<jump>UP,SPACE</jump>
<attack>X</attack>
<left>LEFT</left>
<right>RIGHT</right>
</keys>
And then you call the function like this:
defineInput(FP.getXml(YOUR_FILE_EMBED));
And voilà! Your inputs are automatically defined. You can use them by querying the Input class with the names you used in the file. Any time you want to add more keys or input types, just edit the file.
var direction:int = 0;
if (Input.check("left")) direction--;
if (Input.check("right")) direction++;
walk(direction);
if (Input.pressed("attack"))
shoot();
if (Input.pressed("jump") && onFloor)
jump();