Iphone Cocos2d输入层仅接收精灵顶部的触摸

Iphone Cocos2d输入层仅接收精灵顶部的触摸,iphone,cocos2d-iphone,Iphone,Cocos2d Iphone,我正在用Cocos2d为iPhone构建一个游戏,现在我正在尝试让触摸输入正常工作。我已经在多层场景中的一个独立控制层上启用了触控响应,它工作得很好——除非它仅在触控位于我在一个单独层上的精灵顶部时触发触控方法(单独层实际上只是一个节点)。除了那个精灵,我没有任何其他的屏幕内容 以下是我的控制层实现: #import "ControlLayer2.h" extern int CONTROL_LAYER_TAG; @implementation ControlLayer2 +(void)Cont

我正在用Cocos2d为iPhone构建一个游戏,现在我正在尝试让触摸输入正常工作。我已经在多层场景中的一个独立控制层上启用了触控响应,它工作得很好——除非它仅在触控位于我在一个单独层上的精灵顶部时触发触控方法(单独层实际上只是一个节点)。除了那个精灵,我没有任何其他的屏幕内容

以下是我的控制层实现:

#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

如何使控制层响应所有触摸输入?

在init方法中添加此代码

self.touchEnabled = YES;
然后用这个CCTouches开始

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    //handle touch
}
删除代码中的这一行:

  [[[CCDirector sharedDirector]touchDispatcher] addTargetedDelegate:self priority:0    swallowsTouches:YES];

更新:

ControlLayer2的父级是游戏场景。未在游戏场景中启用触摸。所以在游戏场景中放置self.touchEnabled=YES,并在同一场景中放置cctouchs开始…在游戏场景中放置所有触摸控件?我想我可以,但为了代码和组织的清晰,我想在一个单独的类中处理所有接触。或者你的意思是在游戏场景中启用self.touchEnabled,但触摸控件在ControlLayer2类中?下面是更新的代码:我知道,只要将触摸从游戏场景转发到你想要的任何地方。我想知道,如果ControlLayer2直接接收输入,我会看到更好的性能吗?每次调用一个方法,而不是两个。顺便说一句,我找到了解决问题的方法:我在GameScreenLayer上创建了一个没有纹理的精灵,并将其设置为屏幕大小。出于某种原因,控制层只有在点击屏幕上的内容时才会响应,因此它才能像我希望的那样有效地响应整个屏幕。它几乎不会浪费内存,因为没有纹理可以保存在内存中。