Skip to main content
Inspiring
December 28, 2007
Answered

Custom Events and Listeners between classes

  • December 28, 2007
  • 1 reply
  • 402 views
I've got a project I've been working on for weeks that I'm having yet another problem with. I'm trying to learn AS3 which is why it's taking so long but that's not important for this post.

I wanted to create a custom event class so that I could make sure the event does not interfere with other "COMPLETE" events that are being passed between classes. In other words, I have a few things that need to complete prior to a function being called... one is some XML being loaded and another is a font loaded. So, I thought I would create a custom FontLoaded class that extends Event and make the type something like "FontLoadedEvent.LOADED". That way I could listen for the XML "Event.COMPLETE" and this font event also.

Please tell me if I'm going down the wrong path here but I don't seem to be getting the dispatched event for my new custom event. Also, how does one detect if it's being dispatched other than if the eventListener is fired? Any other ways to test this?
This topic has been closed for replies.
Correct answer scribintoons
You can trace the event to see if it dispatched.

Also, this is not a good case to create a new event. Custom events are used to store additional information. MouseEvent exists because Event doesn't have localX, localY, etc. properties. Since you don't seem to be throwing additional properties, you can use a regular event.

trace(dispatchEvent(new Event("panelFontsLoaded"));

addEventListener("panelFontsLoaded", onFontsLoaded);

Static consts are used to help debug typos. The event type is just a string, often stored in a const.

1 reply

scribintoonsCorrect answer
Inspiring
December 28, 2007
You can trace the event to see if it dispatched.

Also, this is not a good case to create a new event. Custom events are used to store additional information. MouseEvent exists because Event doesn't have localX, localY, etc. properties. Since you don't seem to be throwing additional properties, you can use a regular event.

trace(dispatchEvent(new Event("panelFontsLoaded"));

addEventListener("panelFontsLoaded", onFontsLoaded);

Static consts are used to help debug typos. The event type is just a string, often stored in a const.
aniebelAuthor
Inspiring
December 28, 2007
Thanks! I'll try that. I wasn't aware I could pass a custom string as an event.