Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CCNode as child of CCScene Touch Handler cocos2d v3

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 :(

like image 923
SpellChucker Avatar asked Feb 23 '14 23:02

SpellChucker


2 Answers

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.

like image 93
bitraven Avatar answered Nov 01 '22 23:11

bitraven


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.

like image 2
Dany Avatar answered Nov 01 '22 23:11

Dany