With the Actions panel open, select Frame 1 of the Actions layer and add the following script:
this.onKeyDown = function() {
if(Key.getCode() == Key.RIGHT) {
trace("Move Right");
}else if(Key.getCode() == Key.LEFT) {
trace("Move Left");
}else if(Key.getCode() == Key.UP) {
trace("Move Up");
}else if(Key.getCode() == Key.DOWN) {
trace("Move Down");
}
}
Key.addListener(this);
In this code block we have created an
onKeyDown event handler for the
Key class. We started by creating a function called
onKeyDown. This function has an
if/else if statement that checks for the key that has been pressed. To add this handler as a listener to the
Key class we need to call the
addListener method after we have defined the handler function. We're using the
TRace statement in this example to send a message to our output panel. This will help us determine which case is being caught.
Hopefully, looking at this conditional logic has given you an idea of how this could be done a little better. This is the perfect situation to use a
switch/case statement. Let's go ahead and revise our event handler to use a
switch statement now.