Huge strides have been made in Actionscript 3.0 vs. 2.0. Events are now handled within specific flash-defined scopes, such as the FocusEvent, KeyboardEvent, TextEvent, IOErrorEvent, IMEEvent, NetStatusEvent, TimerEvent, etc... These events are essentially static constants that we hook into to catch the events.
For example, if we wanted to catch a RollOver of a textbox.
Code:
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
public class Gallery extends Sprite {
private var _textBox:TextField;
public function Gallery() {
_textBox = new TextField();
_textBox.text = "Hello World";
_textBox.addEventListener(MouseEvent.ROLL_OVER, onTextBoxRollOver);
addChild(_textBox);
}
private function onTextBoxRollOver(e:MouseEvent):void {
trace("I just rolled over the textbox!");
}
}
} or if you wanted to detect a keyboard event you could add the import the KeyboardEvent scope and listen for it.
Code:
package {
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.text.TextField;
public class Gallery extends Sprite {
private var _textBox:TextField;
public function Gallery() {
_textBox.addEventListener(MouseEvent.ROLL_OVER, onTextBoxRollOver);
_textBox.addEventListener(KeyboardEvent.KEY_UP, onTextBoxKeyUp);
addChild(_textBox);
}
private function onTextBoxKeyUp(e:KeyboardEvent):void {
trace("I just pressed a key in the textfield");
}
private function onTextBoxRollOver(e:MouseEvent):void {
trace("I just rolled over the textbox!");
}
}
} This is how you listen to elements that dispatch already pre-defined Flash events. Next I'll show you how to dispatch your own Custom Events... and possibly get into the internals and extras beautiful options of Events
