0
votes

I'm building a game with Cocos2d for iPhone, and right now I'm trying to get Touch input working. I've enabled touch response on an independent control layer in a multi-layer scene, and it's working fine - except it ONLY fires the touch methods if the touch was on top the sprite that I have on a separate layer(the separate layer is actually just a node). I don't have any other on-screen content aside from that sprite.

here's my control Layer implementation:

#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

and here's the layer with the child node that has a sprite inside it:

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

and finally, the scene that contains them both:

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

how can I make it so that the control layer responds to ALL touch input?

1

1 Answers

1
votes

In init method add this code.

self.touchEnabled = YES;

And use this ccTouchesBegan

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

    //handle touch
}

Remove this line in your code:

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

UPDATE: HERE IS FULL CODE