CCNode as a child of CCScene Touch Handler cocos2d v3

For some time now I hit my head about this. I know that in cocos2d v3 it has changed, so CCNode can take strokes while you set contentSize and set self.userInteractionEnabled = YES .

This does not work for me. I have a CCNode that I am adding as a child of CCScene, but any touch is not logged.

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 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 do not have a log for the touchBegan function. Am I doing something wrong? Remember that this is cocos2d v3, there is not much documentation on this version :(

+6
source share
3 answers

Set a breakpoint inside CCResponderManager.m touchhesBegan: withEvent:

It iterates over all CCNodes that have userInteractionEnabled and checks for hits. The first thing you can do is see if your target CCNode is in _responderList. If so, you can trace in hitTestWithWorldPos: for this CCNode and see why it returns false.

+2
source

I had the same problem. In my CCScene, I add a CCNode * map using [self addChild: map z: -1]; and when I changed the z: option to 0 or more, my touchBegan function will respond. I don't explain it that well, but now it works.

+2
source

Is CCNode read in the same .m file or different as your scene? Here's what it might look like if you were reading from another class file (I took your headers to simplify what you are trying to do):

Title:

 #import "MainPort.h"; 

on your stage:

 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 Dani pointed out. The greater the order of z, the higher the priority of the touch.

0
source

All Articles