7
votes

I've been beating my head over this for a while now. I know in cocos2d v3 it changed so that CCNode can accept touches as long as you set the contentSize and set self.userInteractionEnabled = YES.

This is not working for me. I have a CCNode that I add as a child to a CCScene, but any touch is not getting registered.

Here's the CCNode code:

-(id) initWithPortName:(NSString *)portName andDesc:(NSString *)desc {
    self = [super init];
    if (!self) return(nil);

    CGSize winSize = [[CCDirector sharedDirector] viewSize];

    self.contentSize = winSize;
    self.portName = portName;
    self.desc = desc;

    self.descLabel = [[CCRPGLabel alloc] initWithString:desc fontName:@"Arial" fontSize:18.0f dimensions:CGSizeMake(300, 150)];
    self.descLabel.color = [CCColor blackColor];
    self.descLabel.position = ccp(winSize.width/2, -200);

    [self addChild:self.descLabel];

    return self;
}

- (void) onEnter {
    self.userInteractionEnabled = YES;
    [super onEnter];
}

- (void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    NSLog(@"here");
}

And the CCScene:

self.portNode = [[MainPort alloc] initWithPortName:@"Santa Maria Port" andDesc:@"This port is soweeeet"];
self.portNode.position = ccp(0, winSize.height);
self.portNode.contentSize = winSize;

[self addChild:self.portNode];

I get no log for the touchBegan function. Am I doing something wrong? Remember this is cocos2d v3, not much documentation on this version yet :(

3
Hey, I'm having the same problem. How did you figure this problem out? Thankslionserdar

3 Answers

3
votes

Set a breakpoint inside CCResponderManager.m touchesBegan:withEvent:

It loops through all the CCNodes that have userInteractionEnabled and checks for hit. First thing you can do is see if your target CCNode is in the _responderList. If it is, you can trace into the hitTestWithWorldPos: for that CCNode and see why it returns false.

2
votes

I had the same problem. In my CCScene I add CCNode *map using [self addChild:map z: -1]; and when I changed z: option to 0 or more my touchBegan function responds. I'm not so good to explain that but now it works.

0
votes

Is the CCNode being read in the same .m file or a different one as your scene? This is how it might look if you were reading from a different class file (I took out your titles to simplify what you're trying to accomplish):

header:

#import "MainPort.h";

at your scene:

CGSize screenSize = [[CCDirector sharedDirector]viewSize];
CCNode *santaMaria = [MainPort node];
santamaria.contentSize = screenSize;
[self addChild:santaMaria];

in your MainPort node:

- (void)onEnter
{
    [super onEnter];
    self.userInteractionEnabled = YES;
}

If it is in the same class file, the z order will determine which touch should be registered first as Dany pointed out. The greater the z order, the higher the touch priority.