Event handling
Event handling in AS3 is pretty simple; it basically works by registering listeners to an event.
To do so, we will call the addEventListener
function on the object we want to listen to the event on. We will have to pass the name of the event we want to listen to (this is done by passing a String
, these strings are stored as statics inside classes in the flash.events
package) and a function to handle the event.
The following is an example:
public static function main(): Void { flash.Lib.current.stage.addEventListener(flash.events.KeyboardEvent.KEY_DOWN, keyDown); } private static function keyDown(args : flash.events.KeyboardEvent) { trace(args.keyCode); switch(args.keyCode) { case flash.ui.Keyboard.LEFT: //Left trace("Left"); horizontalSpeed = -10; case flash.ui.Keyboard.RIGHT: //Right trace("Right"); horizontalSpeed = 10; } }
In this example, the keyDown
function will...