默认情况下,CCLayer 注册为标准触摸代理。您必须将其注册为目标代表。在这种情况下,CCLayer 可以声明触摸,而其他可触摸元素将不会收到它。您可以通过覆盖 CCLayer 方法来做到这一点
-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority: self.priority swallowsTouches:YES];
}
在此之后,您必须用这些方法替换您的委托方法
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event;
@optional
// touch updates:
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event;
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event;
- (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event;
你的ccTouchBegan:withEvent:
方法应该是这样的
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
BOOL shouldClaimTouch = NO;
BOOL layerContainsPoint = // check if current layer contains UITouch position
if( layerContainsPoint )
{
shouldClaimTouch = YES;
}
// do anything you want
return shouldClaimTouch;
}
只是不要忘记将触摸的 UI 坐标转换为 GL。如果此方法返回 YES,则任何其他层都不会收到此触摸。