8

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,

5 Answers 5

13

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);
}
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

4

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 !!")
}

1 Comment

Holy cow, you have no idea how much this helped me! Thnx!
2

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.