EventCustom im not understanding plz help me

I do not understand how the EventCustom someone help me with an example please. if i want to dispatch an event when my sprite positon be more than an value, how i could dispatch an event that output an string as result?

Can someone help me?

Here is a basic example of using a custom event listener.

Add a listener like this (you can do this in the onEnter function of a scene):

auto buttonlistener = getDispatcher()->addCustomEventListener(
   MyEventTypes::BUTTON_CLICKED, 
   [this](EventCustom* e) {
   auto data = static_cast<string*>(e->getUserData());
   onButtonClicked(*data);
});

You can remove the event listener when you leave the scene (onExit) like this:

getDispatcher()->removeCustomEventListeners(MyEventTypes::BUTTON_CLICKED);

To fire the event do this:

getEventDispatcher()->dispatchCustomEvent(MyEventTypes::BUTTON_CLICKED, "ButtonNameString");

Finally, you create the method that consumes the string:

void YourClass::onButtonClicked(string data) {
   // do something with the string
}

Hope this helps!

1 Like