Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 394
Lloyd Powell Avatar asked Jan 06 '12 10:01

Lloyd Powell


2 Answers

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);
}
like image 76
Lloyd Powell Avatar answered Oct 08 '22 14:10

Lloyd Powell


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.

like image 25
Constantiner Avatar answered Oct 08 '22 14:10

Constantiner