问题
I am trying to play around with touches in cocos2d-x . I have question on the priority of the touches for example , When I use CCMenuItemSprite
or CCControlButton
which are added on the Layer which has setTouchEnabled(true)
or even If I put a
What I found is touch is not first given to ccTouchesBegan
(...I mean Layer) instead it is taken by CCMenuItemSprite
or CCCOntrolButton
call backs .
Moreover , If I add an extra layer top on all the layers and setTouchEnable
(True) same results I get
touches are first given to menuitem and control button which after touch swallows the touches.
Is there any way by which we can change the priority of touches ? or
Is there any way by which I can override CCControlButton
or CCMEnuItemSprite
...ccTouchesBegan or moved
after all I want the touch location of CCMenuItem
or CControlButton
otherwise I have to reconsider the CCSprite
?
回答1:
overwrite your CCLayer's method
YOUR_LAYER::registerWithTouchDispatcher{
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, INT_MIN, true);
}
回答2:
You can find this in CCMenu.h
enum {
//* priority used by the menu for the event handler
kCCMenuHandlerPriority = -128,
};
so the default priority of the menu is set to -128, if you want to make some layers detect touch before CCMenu, try to set their priority to less than -128
回答3:
Which would first detect the touches only depends on the priority of the Widget, I think why you had been confused is that you may think it is depended on Zorder of the Widget.So, you can change the priority of the layer you added,and ensure the priority is less than the priority of the menuitems,which is -128 in the Cococs2d-x.
CCLayer.h
/** priority of the touch events. Default is 0 */
virtual void setTouchPriority(int priority);
CCMenu.h
enum {
//* priority used by the menu for the event handler
kCCMenuHandlerPriority = -128,
};
来源:https://stackoverflow.com/questions/12488180/touches-priority-in-cocos2d-cocos2d-x