Apr 11, 2007

Make Setting up Input Easier.

Alright. Well here's a short tut about how to do keyboard input without having to remember or lookup ASCII codes. What are ASCII codes? ASCII codes are the numbers that were in Ivan's keyboard input tutorial for the Key.isDown(37) for example. 37 is an ASCII number that stands for the left arrow key. Like in this example:

onEnterFrame = function()
{
if(Key.isDown(37))
{
//left arrow has been pressed
trace("left arrow has been pressed!');
}
}

run it, when you press Left arrow key it should output that.

Lets say you're creating a game that takes keyboard input WASD. It would be so easy if we defined the ASCII codes for each key and then not use "magic" numbers.
Key.isDown(W) instead of Key.isDown(87) . Makes programming a lot easier.

So let's define some variables.
W = 87;
A = 71;
D = 74;
S = 83;
UP = Key.UP;
DOWN = Key.DOWN;
LEFT = Key.LEFT;
RIGHT = Key.RIGHT;
And use that. In fact, how about we create a .as script that defines all of these codes.
Just create a new .as file and put that code into there. Now put that as file in the same folder as your game. Then in your game put #include "INPUT.as" without a ';' . INPUT.as is the file name that holds the key codes. Now if you wanted to do some keyboard input you'd do something like Key.isDown(W) and then it has the key code.

No comments:

FlashGrounds

FlashGrounds
Kevin Stubbs, Ivan Alvarez Malo