Hi!
How can i make all expect the buttons and the other images clickable so that it’ll run a callback function when it gets clicked?
?
Much appreciated!<3
Hi!
How can i make all expect the buttons and the other images clickable so that it’ll run a callback function when it gets clicked?
Much appreciated!<3
Making the background clickable is easy.
Just add an EventListener to your scene.
For example like this:
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
Of course you’ll have to implement HelloWorld::onTouchBegan… 
Do the same to your sprites but make sure they swallow their touches.
For example like this:
listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [](Touch* touch, Event* event) {
// returning true swallows the touch:
return true;
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, yourSprite);
Deciding when to swallow the touch could be the tricky part though if you need precision (by default Touch is using the bounding box)… AFAIK there isn’t any easy way to evaluate where your sprite is transparent…
Nice tho! One thing, so i have to add the shallows to all the sprites right?
Thanks!
You only have to add an event listener to the objects you want to catch the event for.
If you don’t want events on the sprites, but only the background, just add an event listener to the background.
There is no need to add an event listener to the sprites and swallow the event.
A Sprite wont catch an event if it doesn’t have a listener. Just don’t use them if you can. If you have to, then @mausmausgames has the right idea.
There is no point in adding a listener, swallowing it’s event and just returning true.
Swallowing an event is just used to stop propagating the event to objects underneath the object and receiving the same event.
what about a menuSpriteItem then?? hmmmmm
what about it?
no no, i got it fixed!
Thanks anyway!