我正在使用 Cocos2d for iPhone 构建游戏,现在我正在尝试让 Touch 输入正常工作。我已经在多层场景中的独立控制层上启用了触摸响应,并且它工作正常 - 除了它仅在触摸位于我在单独层上的精灵顶部时触发触摸方法(单独层是实际上只是一个节点)。除了那个精灵之外,我没有任何其他屏幕内容。
这是我的控制层实现:
#import "ControlLayer2.h"
extern int CONTROL_LAYER_TAG;
@implementation ControlLayer2
+(void)ControlLayer2WithParentNode:(CCNode *)parentNode{
ControlLayer2 *control = [[self alloc] init];
[parentNode addChild:control z:0 tag:CONTROL_LAYER_TAG];
}
-(id)init{
if (self=[super init]){
[[[CCDirector sharedDirector]touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
return self;
}
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
CCLOG(@"touchbegan");
return YES;
}
@end
这是带有子节点的图层,其中有一个精灵:
extern int PLAYER_LAYER_TAG;
int PLAYER_TAG = 1;
@implementation PlayerLayer
//reduces initializing and adding to the gamescene to one line for ease of use
+(void)PlayerLayerWithParentNode:(CCNode *)parentNode{
PlayerLayer *layer = [[PlayerLayer alloc] init];
[parentNode addChild:layer z:1 tag:PLAYER_LAYER_TAG];
}
-(id)init{
if(self = [super init]){
//add the player to the layer, know what I'm sayer(ing)?
[Player playerWithParentNode:self];
}
return self;
}
@end
最后,包含它们的场景:
int PLAYER_LAYER_TAG = 1;
int CONTROL_LAYER_TAG = 2;
@implementation GameScene
+(id)scene{
CCScene *scene = [CCScene node];
CCLayer *layer = [GameScene node];
[scene addChild:layer z:0 tag:0];
return scene;
}
-(id)init{
if(self = [super init]){
//[[[CCDirector sharedDirector]touchDispatcher] addTargetedDelegate:[self getChildByTag:0] priority:0 swallowsTouches:YES];
//add the player layer to the game scene (this contains the player sprite)
[ControlLayer2 ControlLayer2WithParentNode:self];
[PlayerLayer PlayerLayerWithParentNode:self];
}
return self;
}
@end
如何使控制层响应所有触摸输入?