我试图弄清楚多个CCNode
s 上的触摸处理。
我有
Main CCLayer
------> z:2 Hud CCNode
在main layer
我选择一个对象时,在我想控制它的 hud 层上。我关注了这个 q&a 它非常有用的 Cocos2d 处理多层触摸
在Main Layer
触摸事件上,下面是 Hud 节点的工作:
-(void) registerWithTouchDispatcher
{
[[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event
{
//detects touched Object and sends it to hud
if (object != nil)
{
//sends it to hud
return true;
}
return false;
}
-(void) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent*)event
-(void) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent*)event
-(void) ccTouchCancelled:(UITouch*)touch withEvent:(UIEvent*)event
在 Hud CCNode 上,没有任何 ccTouchBegan/moved/ended 方法被触发
-(void) registerWithTouchDispatcher
{
[[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event
{
if (object!=nil) {
NSLog(@"Touch began");
return YES;
}
else
return NO;
}
编辑:我有一个按钮可以在 HUD Node 上设置对象的速度,这与它无关,object!=nil
因为当我放置断点时,我看到它-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event
从未被调用
-(void)speed1Tapped:(id)sender
{
if (object!=nil) {
NSLog(@"Moving Object is %@:",object!=nil);
}
}
On Log I get:
object is <ObjectATC: 0x13763850>
为什么在 CCNode 中不触发 ccTouchBegan/moved/ended 方法?
如何处理多个 CCNodes、CCLayers 上的触摸?