Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In general, you want the class that dispatches the event to either extend <code>EventDispatcher</code> or implement <code>IEventDispatcher</code>. (All <code>DisplayObject</code>s do, so if your classes are <code>DisplayObject</code>s, you don't need to do anything extra.)</p> <p>In the dispatching class:</p> <pre><code>class DispatchingClass extends Event Dispatcher { function doSomething() { // do stuff dispatchEvent(new Event("FOO")); } } </code></pre> <p>In the listening class:</p> <pre><code>class ListeningClass { function startListening(dispatcher:DispatchingClass) { dispatcher.addEventListener("FOO", handleFoo); } function handleFoo(evt:Event) { // do stuff } } </code></pre> <p>EventDispatchers work fine with custom events.</p> <hr> <p>If for some reason your listening class doesn't have and can't get an instance of your dispatching class, you can make a global event broadcaster. Basically, make a universally-accessible class that extends <code>EventDispatcher</code> (or implements <code>IEventDispatcher</code>) and listens for and dispatches events to anything that tells it to.</p> <p>Here is a bare-bones implementation of an event broadcaster:</p> <pre><code>import flash.events.EventDispatcher; public class EventBroadcaster extends EventDispatcher { private static var _instance:EventBroadcaster = new EventBroadcaster(); public function EventBroadcaster() { if (_instance != null) { trace ("Error: an instance of EventBroadcaster() already exists."); } } public static function getInstance():EventBroadcaster { return EventBroadcaster._instance; } } </code></pre> <p>You use it pretty much the same way:</p> <pre><code>class DispatchingClass { function doSomething() { // do something EventBroadcaster.getInstance().dispatchEvent(new Event("FOO")); } } class ListeningClass { function startListening() { EventBroadcaster.getInstance().addEventListener("FOO", handleFoo); } function handleFoo(evt:Event) { // do stuff } } </code></pre> <p><code>dispatchEvent()</code> and <code>addEventListener()</code> are just the functions from the built-in <code>EventDispatcher</code>.</p> <p>There's some discussion at <a href="http://www.actionscript.org/forums/showthread.php3?t=112186" rel="nofollow noreferrer">Event Broadcaster - Simple events solution...</a> on ways to make an event broadcaster and how to add useful features. The article <a href="http://www.actionscript.org/resources/articles/610/1/Centralized-Event-Management-in-Actionscript-20/Page1.html" rel="nofollow noreferrer">Centralized Event Management in Actionscript 2.0</a> has a good introduction to the concept.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload