hi guys,
its me again, i recently learned “eventdispatcher” and how it is used to call custom event on various cases.
so i got this btn(button) within nav(mc) on stage which dispatch an event and call a function in the end.
(hierarchy: stage > nav > btn)
i have 3 bunch of codes in 3 location. here they are:
myEvent.as
package {
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.display.MovieClip;
public class myEvent extends EventDispatcher {
public static const GO:String = "go";
public function act():void {
dispatchEvent(new Event(myEvent.GO));
trace("act called");
}
}
}
main timeline (frame1)
var dispatcher:myEvent = new myEvent();
dispatcher.addEventListener(myEvent.GO, actionHandler);
function actionHandler(event:Event):void {
trace("Event Called");
}
nav (frame1)
var dispatcher:myEvent = new myEvent();
btn.addEventListener(MouseEvent.CLICK, buttonClickHandler);
function buttonClickHandler(event:MouseEvent):void {
dispatcher.act();
}
-
When you click btn, the “buttonClickHandler” fired and it calls act from dispatcher.
-
“act called” traced and its supposed to call the event, but its not.
do you know how to fix this problem?
- if you put the nav code in main timeline. and change the code to the following. it works
nav.btn.addEventListener(MouseEvent.CLICK, buttonClickHandler);
function buttonClickHandler(event:MouseEvent):void {
dispatcher.act();
}
but i don’t want that, i’m curious why the code inside nav doesnt work.
attached is the file, more understandable
thanks a bunch guys.