In AS3.0, and to summarize, all events are handled with the following logical pattern:
function eventListener(eventObject:EventType) { // Actions performed in response to the event go here. } eventTarget.addEventListener(EventClass.EVENT_NAME, eventListener); |
Let's put that into practise with a couple of examples.
1. Mouse events
In AS3.0 all interactivities based on mouse clicks, mouse wheel and mouse moves are now going to be handled with that new event system.
if you want to to detect a mouse click anywhere on the stage you will need to attach and to define a listener to the stage object like below:
function myClick(eventObject:MouseEvent) { trace("mouse click detected"); } stage.addEventListener(MouseEvent.CLICK, myClick); |
The name of the event is defined by the constant associated with the event class. In our case CLICK is one of the events associated with the MouseEvent class, the events defined for the MouseEvent class are CLICK, DOUBLE_CLICK, MOUSE_DOWN, MOUSE_UP, MOUSE_MOVE, MOUSE_OUT, MOUSE_OVER, MOUSE_WHEEL, ROLL_OUT, ROLL_OVER.
To attach a behaviour to a button in AS 2.0 you would attach the following code to that button:
on(release){ // do something } |
or if the button is a movie clip you will attach:
onClipEvent(load){ this.onPress = function(){ // do something } } |
or you in both cases you could define the behaviour in a frame of the timeline:
myButton.onPress = function(){ // do something } } |