Im trying to Dispatch an event, Anyone could help me?

#1

I’m trying to Dispatch an event somewhere in my class. But i couldn’t get it right., here’s what i did.

calling from class this

function classfunction():void{

          var evt:Event = new Event("testEvent", false, true);
          dispatchEvent(evt);

}

hoping to trigger from other class

function otherClassfunction():void{

        var dispatcher:IEventDispatcher = new EventDispatcher();
	dispatcher.addEventListener("testEvent", handleEvent);
 	

}
public function handleEvent(e:Event):Void{

		trace("stats update"); // NO LUCK!!

}
0 Likes

#2

I think, you should listen event from the same source you dispatching to.

I mean, if you listen event on dispatcher
dispatcher.addEventListener("testEvent", handleEvent);

you just need to dispatch event to it
dispatcher.dispatchEvent(evt);

This should work
package;

import openfl.events.Event;
import openfl.display.Sprite;
import openfl.events.EventDispatcher;

/**
 * ...
 * @author [email protected]
 */

class TestDispatch extends Sprite 
{
	
	private var dispatcher:EventDispatcher;

	public function new() 
	{
		super();
		
		dispatcher = new EventDispatcher();
		
		dispatcher.addEventListener('testEvent', handleEvent);
		
		dispatcher.dispatchEvent(new Event('testEvent', false, true));
	}
	
	private function handleEvent(e:Event):Void 
	{
		trace('stats update');
	}
	
}
0 Likes

#3

Another way is to extend EventDispatcher. DisplayObject classes (such as Sprite and Bitmap) already extend EventDispatcher

0 Likes