AS 3 tips: addEventListener
I was typing some AS3 one day and was a bit lazy. I did the following:
Actionscript:
-
// I did not specify the object to add the event
-
addEventListener(Event.ENTER_FRAME,some_fct);
-
addEventListener(MouseEvent.MOUSE_MOVE,some_fct_2);
-
-
// but actually the above is equivalent to the following
-
-
this.addEventListener(Event.ENTER_FRAME,some_fct);
-
this.addEventListener(MouseEvent.MOUSE_MOVE,some_fct_2);
The EnterFrame work, but not the Mouse event. This is because the mouse event does not respond to the timeline ("this" refers to the timeline).
Therefore, to make the mouse respond to the movement, I add the event to some object e.g. stage in my case.
Actionscript:
-
this.addEventListener(Event.ENTER_FRAME,some_fct);
-
stage.addEventListener(MouseEvent.MOUSE_MOVE,some_fct_2);
This is also true for Keyboard events.
What i struggle most is accessing stage is a null object. ie cant addEventListener on it -_-
stage is not available until FlexEvent.ApplicationComplete in Flex
waste a lot of time , frustrating
Comment by Paul Wong — January 25, 2007 @ 6:09 pmThis post and additional comment was greatly helpful to me. Thanks both of you.
Comment by Keith H — May 3, 2007 @ 2:27 am