Cocos2d-x Study Notes outside Article 05: How to quickly shield touch

Source: Internet
Author: User

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.

 
 
  1. 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.

 
 
  1. class NoTouchLayer : public cocos2d::CCLayer{ 
  2. public: 
  3. // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone 
  4. virtual bool init();   
  5.      
  6.     // implement the "static node()" method manually 
  7.     LAYER_NODE_FUNC(NoTouchLayer); 
  8.      
  9.     virtualvoid registerWithTouchDispatcher(); 
  10.      
  11.     virtualbool ccTouchBegan (cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); 
  12.     virtualvoid ccTouchMoved (cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); 
  13.     virtualvoid ccTouchEnded (cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); 
  14.      
  15. }; 


 
 
  1. Bool NoTouchLayer: init (){
  2. If (! CCLayer: init ())
  3. {
  4. Return false;
  5. }
  6. SetIsTouchEnabled (true );
  7. Return true;
  8. }
  9. Void NoTouchLayer: registerWithTouchDispatcher (){
  10. CCTouchDispatcher: sharedDispatcher ()-> addTargetedDelegate (this, numeric_limits <int>: min (), true); // use the int minimum value for the highest priority and swallow the event true
  11. CCLayer: registerWithTouchDispatcher ();
  12. }
  13. Bool NoTouchLayer: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent ){
  14. Return true;
  15. }
  16. Void NoTouchLayer: ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent ){
  17. }
  18. Void NoTouchLayer: ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent ){
  19. }

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.