这可能是我身边的一个使用错误,但我无法让CCControlButton在hud图层内捕获任何触摸。我用这种方式实现HUD图层:http://www.raywenderlich.com/4666/how-to-create-a-hud-layer-with-cocos2d 在主游戏图层中,启用了触摸(self.touchEnabled = YES;)
在hudlayer.m内部我只是添加一个新的CCControlButton作为孩子。 像这样:
CCLabelTTF* titleRight = [CCLabelTTF labelWithString:@"→" fontName:@"Helvetica" fontSize:FONTSIZE];
titleRight.color = ccRED;
CCControlButton* buttonRight = [CCControlButton buttonWithLabel:titleRight backgroundSprite:[CCScale9Sprite spriteWithFile:@"switch-mask.png"]];
buttonRight.position = ccp(ORIGIN.x + SIZE.width, ORIGIN.y);
buttonRight.userObject = [NSValue valueWithCGPoint:ccp(1, 0)];
[buttonRight addTarget:self action:@selector(actionPan:) forControlEvents:CCControlEventTouchUpInside];
[self addChild:buttonRight];
使用CCMenu创建的按钮可以正常工作。