PDA

View Full Version : Dispatching Events not working


loganbest
12-12-2009, 01:04 AM
I have an event dispatcher in an irrelevant part of the fla and it works great, but when I go to set it up in this part of the fla it doesn't work. In the irrelevant part it dispatches from an MC to the main timeline which has a listener on it in the first frame then it does it's thing.

maintimeline with listener -> MC with dispatcher

In this part I have the dispatcher dispatching an event from inside an MC to a nested MC.

maintimeline -> MC with dispatcher -> MC with listener

Dispatcher:
stop();
import flash.events.EventDispatcher;
import flash.events.Event;

inperformanceBtn.addEventListener(MouseEvent.CLICK ,inperf);
function inperf(evt:MouseEvent):void {
dispatchEvent(new Event("inperfplay", true));
trace("perf clicked");
}
pieBtn.addEventListener(MouseEvent.CLICK,inpie);
function inpie(evt:MouseEvent):void {
dispatchEvent(new Event("inpie", true));
trace("pie clicked");
}



Listener:
stop();

addEventListener("closebtn", closebtn);
addEventListener("inperfplay", inperfPlay);
addEventListener("inpie", inpie);// listen for the custom event dispatched by button

function inperfPlay(e:Event):void
{
trace("perf rec");
popupMC.gotoAndStop(1);
gotoAndPlay("popIn");

}
function inpie(e:Event):void
{
popupMC.gotoAndStop(1);
gotoAndPlay("popIn");
}
function closebtn(e:Event):void
{
gotoAndPlay("popOut");
}

It's setup pretty much the exact same way as the other one. I'm lost.

Mazoonist
12-12-2009, 04:39 AM
In the first block of code, your buttons are not dispatching those events, it is the enclosing scope that is dispatching the events (the MC that contains these buttons), although this event dispatch is being triggered by clicks on the buttons.

So for a nested movie clip within this one to register for those dispatched events, it would have to use parent, for example in your case:

this.parent.addEventListener("inpie", inpie);

You have to always add event listeners to the same object that is dispatching them (there is one exception but I probably shouldn't confuse you with bubbling events right now).

The events are being dispatched by the nested movie clip's parent, so that is the object you must add the listeners to.

henke37
12-12-2009, 11:57 AM
You got lucky, the event propagated thrught the displaylist. But that does not happen for events targeted at parent objects.

Mazoonist
12-12-2009, 03:33 PM
henke37,

I don't know if that reply was intended for me or the other person. But I was just trying to explain how dispatching events work. Sometimes I answer people in the context of what they have already set up, even if I don't agree with it, or it's not the way I would do it. Understand?

henke37
12-12-2009, 04:39 PM
It was for loganbest.

loganbest
12-12-2009, 05:15 PM
well now it gets to the other function correctly but now its not registering the MC label that I manually typed it, not dynamically.

perf rec
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at site_fla::popupContainer_6/inperfPlay()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at site_fla::contentMC_5/inperf()
perf clicked

henke37
12-12-2009, 06:16 PM
Oh, it found the property alright. The property was just null. The object did not exist at that point in time.

henke37
12-12-2009, 06:18 PM
The property was found, but it's value at that point in time was null. This is due to the instance not existing at that point in time.

loganbest
12-12-2009, 06:22 PM
That's weird...it was there but it wasn't... FIXED.