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

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!!

}

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');
	}
	
}

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

Hi. I’m new to the community.
In this case, how could I dispatch this event from another class?

In my Main class, I’m wanting to leave some functions to be dispatched by methods of other classes. It’s possible?

Thanks!!!

If say you wanted to dispatch an event from a child class, to it’s parent, the child class would need to extend EventDispatcher, or extend a class that extends EventDispatcher, like Sprite (as per @singmajesty’s suggestion).

The basic child class might look like this:

import openfl.events.EventDispatcher;

class SomeChildClass extends EventDispatcher {

	public function new(target:IEventDispatcher = null) {
		super(target);
	}
}

Then in that child class, to dispatch an event, you might add a function like this:

private function announceSomeEvent():Void {
    var evt:Event = new Event("someEvent");
    dispatchEvent(evt);
}

In the parent class, to add a listener for that event:

var someChildClass:SomeChildClass = new SomeChildClass();
someChildClass.addEventListener("someEvent", someEventHandler);

private function someEventHandler(e:Event):Void {
    trace("Event received");
}

If you’d like to send data or object references with the event, create a custom event by extending the Event class. I like doing this in most cases anyway, because it allows me to centrally define the event name, rather than relying on repeated strings like "someEvent" (such as the example above), which can be prone to typos.

A custom event class might look like this. I’ve added the SOME_EVENT event type, and a couple variables I can use for sending bits of data and object references.

import openfl.events.Event;

class MyCustomEvent extends Event 
{
	static public inline var SOME_EVENT:String = "someEvent";

	public var someString:String;
	public var someSpriteReference:Sprite;

	public function new(type:String, bubbles:Bool=false, cancelable:Bool=false) 
	{
		super(type, bubbles, cancelable);		
	}
	
}

To use this custom event in the child class now, inserting some data too:

private function announceSomeEvent():Void {
        var evt:MyCustomEvent = new MyCustomEvent(MyCustomEvent.SOME_EVENT);
        evt.someString = "Some string data.";
        evt.someSpriteReference = someSprite;
        dispatchEvent(evt);
    }

And to listen for that event, and get the data it contains in the parent class:

var someChildClass:SomeChildClass = new SomeChildClass();
someChildClass.addEventListener(MyCustomEvent.SOME_EVENT, someEventHandler);

private function someEventHandler(e:MyCustomEvent):Void {
    trace("Event received");
    trace(e.someString);
    trace(e.someSpriteReference);
}
1 Like

It was exactly what I needed! Thank you!³

First time I ask a question, and I am promptly answered. Who knows, maybe one day I’ll be able to help in this way. :wink: