How can touch priority be set in v3.8 JS?
I older versions, you could use “setTouchPriority”.
Currently, I have several layers. Each layer has an event listener. I want the layers “on top” to swallow the event. event: cc.EventListener.TOUCH_ALL_AT_ONCE, swallowTouches: true,
How can I set the priority, such that only the top layer will get the event?
To put it simply, if you have multiple EventListenerTouchOneByOne event listeners in your scene, setting setSwallowTouches to true for each means only the first one that returns true from onTouchBegan will get calls to onTouchMoved and onTouchEnded for that touch. I think it is set to false by default.
When you touch the screen, the event dispatcher will call the onTouchBegan function of each EventListenerTouchOneByOne. Each event listener that returns true from onTouchBegan will also get calls to onTouchMoved and onTouchEnded for that touch.
If swallowTouches is set to true for an EventListenerTouchOneByOne, if it returns true from onTouchBegan then no other event listener after it will get the call to onTouchBegan and so they won’t get calls to onTouchMoved or onTouchEnded either.
Thanks for that detailed explanation. But could you give me an example of when (and why) someone would use more than one EventListenerTouchOneByOne event listener in the same scene?
Say for example my game is a racing game and the user can touch anywhere on the left side of the screen to turn left and anyone on the right side of the screen to turn right. How would you go about handling these touches?
You might use one event listener for the UI and one for controlling the game. You wouldn’t want the game to steer your car when you are touching something in the UI. So the UI would get the event first and would “swallow” the touch so it doesn’t get passed to the game controls listener as well.
Awesome thanks for the reply!
So I have a UI button like this
var closeButton = new ccui.Button();
closeButton.loadTextures(res.closeBtn_n_png, res.closeBtn_s_png);
closeButton.addTouchEventListener(this.onClose, this);
Where and how would I make this swallowTouches? I’m not sure what the syntax is exactly.
Thanks a lot! Much appreciated