Advertisement
ActionScript

AS3 101: Quick Tip - Dispatching Events Without Extending EventDispatcher

by

It's rare, but it happens. Sometimes you absolutely need to extend a class, and that class does not already extend EventDispatcher. At the same time, you absolutely need your class to be an EventDispatcher. Because ActionScript 3 does not allow multiple inheritance, how do you reconcile this?

In other situations, perhaps you would like to create a class that has a bunch of static methods on it, and isn't really meant to be instantiated. You would like to be able dispatch events from this class as well, but you can extend EventDispatcher because that enables instance methods, not class methods.

Let's take a look at how to achieve these goals.


Step 1: How To Do It

The solution in both cases is to instantiate, hang onto, and use an EventDispatcher object. That is, a new EventDispatcher() object, not necessarily any of its subclasses. In the case of needing to extend something else, you can also have your class implement the IEventDispatcher interface.

The process goes something like this:

  1. Implement IEventDispatcher (not for the static class).

    package {
        import flash.events.*;
        public class ArrayDispatcher extends Array implements IEventDispatcher {
            public function ArrayDispatcher() {
            }
        }
    }
  2. Create a new EventDispatcher object, and store it in a property.

    private var _dispatcher:EventDispatcher
    public function ArrayDispatcher() {
        _dispatcher = new EventDispatcher();
    }
  3. Implement the methods defined by IEventDispatcher, and simply wrap around the matching method on your EventDispatcher instance.

    public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void {
        _dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
    }
    public function dispatchEvent(event:Event):Boolean {
        return _dispatcher.dispatchEvent(event);
    }
    public function hasEventListener(type:String):Boolean {
        return _dispatcher.hasEventListener(type);
    }
    public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
        _dispatcher.removeEventListener(type, listener, useCapture);
    }
    public function willTrigger(type:String):Boolean {
        return _dispatcher.willTrigger(type);
    }

Step 2: A Static EventDispatcher

For reference, implementing this in a static class might look like this:

package {

    import flash.events.*;

    public class StaticDispatcher {

        private static var _dispatcher:EventDispatcher = new EventDispatcher();

        public static function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void {
            _dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
        }
        public static function dispatchEvent(event:Event):Boolean {
            return _dispatcher.dispatchEvent(event);
        }
        public static function hasEventListener(type:String):Boolean {
            return _dispatcher.hasEventListener(type);
        }
        public static function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
            _dispatcher.removeEventListener(type, listener, useCapture);
        }
        public static function willTrigger(type:String):Boolean {
            return _dispatcher.willTrigger(type);
        }
    }
}

Note that there is no extends or implements, all members are now static, and the _dispatcher property is now created directly on the property, instead of in the constructor (because there is no constructor).


That's All

Although quick to explain, this is definitely an advanced technique, and you probably won't require it if you're at a point where dispatching your own events is still a new concept. But file this away somewhere in your mind so you can come back to this example when you need it.

Related Posts
  • Code
    Android SDK
    Create a Music Player on Android: User Controls0d63m preview image@2x
    We are building a simple music player app for Android in this series. So far, we have presented a list of the songs on the device and allowed the user to make selections from it, starting playback using the MediaPlayer class in a Service class. In this final part of the series, we will let the user control playback, including skipping to the next and previous tracks, fast-forwarding, rewinding, playing, pausing, and seeking to particular points in the track. We will also display a notification during playback so that the user can jump back to the music player after using other apps.Read More…
  • Code
    Android SDK
    Create a Music Player on Android: Song Playback0d63m preview image@2x
    In this series, we are creating a music player on Android using the MediaPlayer and MediaController classes. In the first part, we created the app and prepared the user interface for playback. We presented the list of songs on the user device and specified a method to execute when the user makes a selection. In this part of the series, we will implement a Service class to execute music playback continuously, even when the user is not directly interacting with the application.Read More…
  • Code
    Android SDK
    Create a Music Player on Android: Project Setup0d63m preview image@2x
    The Android platform provides resources for handling media playback, which your apps can use to create an interface between the user and their music files. In this tutorial series, we will create a basic music player application for Android. The app will present a list of songs on the user device, so that the user can select songs to play. The app will also present controls for interacting with playback and will continue playing when the user moves away from the app, with a notification displayed while playback elapses.Read More…
  • Code
    Android SDK
    Create a Hangman Game: User InteractionAndroid hangman game preview retina
    In this series, we are creating a Hangman game for the Android platform. So far, we've built the application's user interface, including images, drawables, and layouts. In this third and final installment, we are going to focus on user interaction.Read More…
  • Code
    Android SDK
    Consuming Web Services with kSOAPEd4e2 preview image@2x
    In this tutorial, you'll learn how to consume web services using the popular kSOAP library in an Android application. kSOAP is a reputable library loved by developers who have the need to parse WSDL (Web Service Definition Language) and SOAP (Simple Object Access Protocol) messages.Read More…
  • Game Development
    Implementation
    Make a Neon Vector Shooter in jMonkeyEngine: Enemies and SoundsMonkeyblaster 2 preview big
    In the first part of this series on building a Geometry Wars-inspired game in jMonkeyEngine, we implemented the player's ship and let it move and shoot. This time, we'll add the enemies and sound effects.Read More…