cocos2d ccTouchBegan不被调用

转自:http://blog.csdn.net/yang3wei/article/details/7924793


参考自:http://blog.sina.com.cn/s/blog_61e26bcb0100xwqe.html

CCLayer 里面的 ccTouchBegan 和 ccTouchesBegan 到底调用哪个?

默认调用的是 ccTouchesBegan 方法~

添加了如下代码的话

[cpp]  view plain copy
  1. /** 
  2.  * 2012。08。30。12。56~ 
  3.  * 启用 ccTouchEnded,禁用默认的 ccTouchesEnded~ 
  4.  */  
  5. -(void) registerWithTouchDispatcher {  
  6.     [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:true];  
  7. }  

就会调用 ccTouchBegan 方法~

而且,ccTouchBegan 里面返回 NO,触摸事件就不会继续往下传递


为什么呢?

下面是我在 CCTouchHandler.m 里面扒出来的两块儿代码:

[cpp]  view plain copy
  1. -(id) initWithDelegate:(id)del priority:(int)pri  
  2. {  
  3.     if( (self=[super initWithDelegate:del priority:pri]) ) {  
  4.         if( [del respondsToSelector:@selector(ccTouchesBegan:withEvent:)] )  
  5.             enabledSelectors_ |= kCCTouchSelectorBeganBit;  
  6.         if( [del respondsToSelector:@selector(ccTouchesMoved:withEvent:)] )  
  7.             enabledSelectors_ |= kCCTouchSelectorMovedBit;  
  8.         if( [del respondsToSelector:@selector(ccTouchesEnded:withEvent:)] )  
  9.             enabledSelectors_ |= kCCTouchSelectorEndedBit;  
  10.         if( [del respondsToSelector:@selector(ccTouchesCancelled:withEvent:)] )  
  11.             enabledSelectors_ |= kCCTouchSelectorCancelledBit;  
  12.     }  
  13.     return self;  
  14. }  

[cpp]  view plain copy
  1. - (id)initWithDelegate:(id)aDelegate priority:(int)aPriority swallowsTouches:(BOOL)swallow  
  2. {  
  3.     if ((self = [super initWithDelegate:aDelegate priority:aPriority])) {     
  4.         claimedTouches = [[NSMutableSet alloc] initWithCapacity:2];  
  5.         swallowsTouches = swallow;  
  6.           
  7.         if( [aDelegate respondsToSelector:@selector(ccTouchBegan:withEvent:)] )  
  8.             enabledSelectors_ |= kCCTouchSelectorBeganBit;  
  9.         if( [aDelegate respondsToSelector:@selector(ccTouchMoved:withEvent:)] )  
  10.             enabledSelectors_ |= kCCTouchSelectorMovedBit;  
  11.         if( [aDelegate respondsToSelector:@selector(ccTouchEnded:withEvent:)] )  
  12.             enabledSelectors_ |= kCCTouchSelectorEndedBit;  
  13.         if( [aDelegate respondsToSelector:@selector(ccTouchCancelled:withEvent:)] )  
  14.             enabledSelectors_ |= kCCTouchSelectorCancelledBit;  
  15.     }  
  16.       
  17.     return self;  
  18. }  
观察后发现,其实就是做一些配置,然后就会注册各自的一批关于触摸的方法~
最后,使用的是默认的配置调用 ccTouchesBegan 等方法的话,

可以采用如下写法的:

[cpp]  view plain copy
  1. UITouch* touch = [touches anyObject];  

不一定非要快速遍历一趟的

[cpp]  view plain copy
  1. for(UITouch* touch in touches) {  
  2.     CGPoint location = [touch locationInView:[touch view]];  
  3.     location = [[CCDirector sharedDirector] convertToGL:location];  
  4. }  

(如果不在 RootViewController.m 中添加开启多点触控特性的代码的话,touches 里面是不会有多个触摸点对象的)~


你可能感兴趣的:(cocos2d ccTouchBegan不被调用)