Skip to main content
Known Participant
December 14, 2010
Question

A simple custom event

  • December 14, 2010
  • 2 replies
  • 1360 views

Hello everyone, I am dispatching an event from one document  class and listening for it via another document class.

My code in class A.

this.dispatchEvent(new MYEvent(MyEvent.APERTURE_DONE));


trace("Dispatching APERTURE_DONE");

In my code in class B.

addEventListener(MyEvent.APERTURE_DONE, onDoorsOpen,true);

                  trace("Lisetning for APERTURE_DONE");

private function onDoorsOpen(event:MyEvent):void

{

trace("Open doors");

}

My listener is registering before the event is dispatched, based on my output window, however I never get the "Open Doors" trace statement to fire.

Does anyone have any suggestions?

Thanks, I appreciated it.

This topic has been closed for replies.

2 replies

5SystemsAuthor
Known Participant
December 15, 2010

That is understandable, though I am going to figure out how to buy you a beer at some point, for all your help.

Yes aperture is A and doors is B?

kglad
Community Expert
Community Expert
December 15, 2010

you must ensure that aperture doesn't play that frame until door is ready to receive that event.  you can always stop the aperture timeline until door is ready, that's what you must do.  there's no way to work around that potential issue except to prevent it from occurring.

once you've done that you can call dispatchEventF() in Aperture to dispatch your event to Door.

//////////////////////////////////////////

package{
   
    import flash.display.MovieClip;
    import flash.events.Event;
   
    public class Aperture extends MovieClip{

var doorRef:MovieClip
       
        function Aperture(){
        }
        function addlistenerF(mc:MovieClip){

doorRef=mc
        }

function dispatchEventF(whatever){

if(doorRef){

doorRef.dispatchEvent(new Event("ce"));

}

}
    }
}

/////////////////////

package{
   
    import flash.display.MovieClip;
    import flash.events.Event;
   
    public class Door extends MovieClip{
       
        function Door(){
            this.addEventListener(Event.ADDED_TO_STAGE,init);
        }
        function init(e:Event){
            this.addEventListener("ce",listenerF);
            MovieClip(MovieClip(this.parent.parent).loaderB.content).addlistenerF (this);
        }
        function listenerF(e:Event){
            trace(e)
        }
    }
}

5SystemsAuthor
Known Participant
December 16, 2010

cool,

thanks, I will play around with it and see what I can do.

thanks again.

kglad
Community Expert
Community Expert
December 14, 2010

only the class instance that dispatches an event can receive the event.

so, if you wanted  B to be notified of some event in A, you would have to pass a reference to A to B and have that reference dispatch the event.  A could then receive the dispatched event.

5SystemsAuthor
Known Participant
December 14, 2010

kglad,

Are you referring to "use weak references" in the event listener?  If not can you provide an example of what you mean. I did some googling on reference and event dispatching and no much seems to come up that would explain what you mean.  I also looked in some of my books and got the following info on references, but not sure how it  applies to what you are saying.

Info from book:

assignment where the source variable’s value is an instance of a

custom class or an instance of a built-in class other than String, Boolean, Number,

int, or uint, ActionScript associates the second variable directly with the first variable’s

value. After the assignment, only one copy of the value exists in memory, and

both variables refer to it.

Thanks for the help.

kglad
Community Expert
Community Expert
December 14, 2010

what's the relationship between the swfs that have those document classes?  is one loaded into the other?