Is it possible to get events (subscribe) from certain sprite?

I want to check my physicsbody sprite is resting.
Instead manually checking sprite isResting, sprite should emit resting event.
Does sprites can emit events?

Hi. You can use Custom Events anywhere you need. Custom Events has a user data property that can be anything (in your case your sprite).

for example:
auto mySprite = Sprite::create(“myImage”);
Director::getInstance()->getEventDispatcher()->dispatchCustomEvent(“myEventName”, &mySprite);

and somewhere else you need a EventListenerCustom for this event.

Search the Doc for move info.

1 Like

thank you I will try this

I googled but there is no result for dispatchCustomEvent implementation.

and dispatchCustomEvent second argument is userData not sprite.
I am guessing there is no support getting events for certain sprite otherwise tons of post should be somewhere

Hi. the userDate is a Void Pointer. The Void Pointer can be anything (include sprites).

This is example of EventListenerCustom.

 auto myListener = EventListenerCustom::create("myEventName", [this](EventCustom*        event)
{
	auto mySprite = static_cast<Sprite*>(event->getUserData());
//.      now you have access to mySprite and doing stuff.
	
});

_eventDispatcher->addEventListenerWithSceneGraphPriority(myListener, this);
1 Like

Cocos event system is very generic and useful.
You can sent custom events in any lines of code and listen those events in any lines of code in any class.

I got it thank you, but I point different thing, can i get event when sprite scaled or rotated?

I think cocos2d-x not automatically firing events from sprites to let us subscribe to this events when we need.

use a custom event and set your own flags to fire it? We don’t have built-in events for every single transformation that you can possible do.

thank you. I will manually dispatch event