Lloyd Powell
Lloyd Powell

Reputation: 18770

dispatchEvent in static class - AS3

A nice simple one to start the day!

I can't use dispatchEvent in my static class, I was wondering if anyone knew how I can achieve similar functionality or if it's possible at all to call dispatchEvent from my static class?

I basically want to inform my action script code in my flash file when functionality in my static class is complete.

Thanks,

Upvotes: 8

Views: 5672

Answers (5)

Lloyd Powell
Lloyd Powell

Reputation: 18770

After reading the answers and gaining an understanding of what I can achieve, I have implemented the following (thought it would help users in the future if they could see some example code).

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 removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
   dispatcher.removeEventListener(type, listener, useCapture);
}

public static function dispatchEvent(event:Event):Boolean {
   return dispatcher.dispatchEvent(event);
}

public static function hasEventListener(type:String):Boolean {
   return dispatcher.hasEventListener(type);
}

Upvotes: 13

sajan
sajan

Reputation: 1370

Instead of using static methods from static class. You could use like this way.

import flash.events.EventDispatcher;
    import flash.events.Event;

    public class StaticClass extends EventDispatcher {
         public static var STATICCLASS:StaticClass = new StaticClass();
        public function StaticClass() {
            // constructor code

        }

        public function dispatchEventFromStaticClass():void
        {
            dispatchEvent(new Event("Event_Dispatched"));
        }

    }

you can listen somewhere else like this

StaticClass.STATICCLASS.addEventListener("Event_Dispatched", onHandler);
StaticClass.STATICCLASS.dispatchEventFromStaticClass()
function  onHandler(e:Event):void 
{
    trace("Hey !!")
}

Upvotes: 4

Pimgd
Pimgd

Reputation: 6043

One direct workaround I can see is to have a static variable be to your EventDispatcher - whenever you get addEventListener called, you attach it to your EventDispatcher - whenever you want to fire/dispatch an event, you tell so to your EventDispatcher.

Upvotes: 2

Constantiner
Constantiner

Reputation: 14221

Static classes (classes with static properties and methods) can't inherit ordinary instance-level methods and can't implement interfaces with static methods. So your public static function dispatchEvent can't take a part in EventDispatcher or IEventDispatcher.

You can create the same static methods as in IEventDispatcher and then create a static instance of IEventDispatcher to handle events but your static class itself can't be EventDispatcher but can only look the similar.

Upvotes: 4

Laurent Zuijdwijk
Laurent Zuijdwijk

Reputation: 611

You would use a private variable _dispatcher:EventDispatcher and implement the IEventDispatcher interface.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/IEventDispatcher.html

You can then route the events through the dispatcher. Let me know if this is enough to get you going.

Upvotes: 1

Related Questions