The cocos2d-x has a problem that even if the CCScene operation is paused, the touch is still valid and some menus and buttons are still triggered.
So sometimes we need to manually shield the touch, especially when the billing screen is popped up or some of the built-in controls of the platform are used.
There are several methods:
The first solution is: Every class inherited from the CCLayer can close the touch and use this function.
- setIsTouchEnabled(false);
When suspending sence, we only need to disable those major cclayers, that is, the current CCScene master CCLayer and related CCMenu.
However, this method sometimes causes inexplicable crash. Debugging found that if you close the function in the same frame Before restoring the touch, crash may occur. The reason for crash is that when the engine sends a touch event, it finds that the list of response objects is empty and directly triggers assertions.
The second solution is to write a CCLayer, set all priorities to the highest, and directly overwrite the current CCLayer of the CCSence master.
After testing, this method is very simple, effective, and highly reusable.
- class NoTouchLayer : public cocos2d::CCLayer{
- public:
- // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
- virtual bool init();
-
- // implement the "static node()" method manually
- LAYER_NODE_FUNC(NoTouchLayer);
-
- virtualvoid registerWithTouchDispatcher();
-
- virtualbool ccTouchBegan (cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
- virtualvoid ccTouchMoved (cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
- virtualvoid ccTouchEnded (cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
-
- };
- Bool NoTouchLayer: init (){
- If (! CCLayer: init ())
- {
- Return false;
- }
- SetIsTouchEnabled (true );
- Return true;
- }
- Void NoTouchLayer: registerWithTouchDispatcher (){
- CCTouchDispatcher: sharedDispatcher ()-> addTargetedDelegate (this, numeric_limits <int>: min (), true); // use the int minimum value for the highest priority and swallow the event true
- CCLayer: registerWithTouchDispatcher ();
- }
- Bool NoTouchLayer: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent ){
- Return true;
- }
- Void NoTouchLayer: ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent ){
- }
- Void NoTouchLayer: ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent ){
- }
The usage of this class is also very simple. You can directly addChild and removeChild. Pay attention to cleaning. When addChild is used, a large enough Z axis depth value should be given.
This article is from the "Old G hut" blog, please be sure to keep this source http://4137613.blog.51cto.com/4127613/845269