Thursday, October 16, 2008

Call an Event function indirectly in AS3 with dispatchEvent

Event listeners are a great way to work but if you have an event function, you can't just call it directly in the script anymore (ala AS2), well you can try, but you will get an argument error. You can, however, dispatch a matching event to trigger listener and fires that function.

Perhaps you have a timer event that calls a function and you want to call without involving the timer or, a more common one, you have a script to do stuff when the stage is resized, but you want to execute that function when the movie first loads:

stage.addEventListener(Event.RESIZE, resizeHandler);
stage.dispatchEvent(new Event(Event.RESIZE));

No comments: