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 ...

Get haXe 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.