Search code examples
ioscocos2d-iphonelayerscene

Linking HUDLayer to GameplayLayer properly


I'm trying to pass the touch location from GameplayLayer to HUDLayer to see if user presses control buttons.

HudLayer.mm

-(void) handleTouchAtLocation:(CGPoint) location {
NSLog(@"Touch passed to HUD");
}

Gameplay.mm

enum {
   kHudLayer = 2;
};

+(CCScene *) scene {
    CCScene *scene = [CCScene node];
    HudLayer *hud = [HudLayer node];
    [scene addChild:hud z:kHudLayer];
    GameplayLayer *layer = [GameplayLayer node];
    [scene addChild:layer];
    return scene;
}

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   for( UITouch *touch in touches ) {
      CGPoint location = [touch locationInView: [touch view]];
      location = [[CCDirector sharedDirector] convertToGL: location];
      [self handTouchAtPoint:(location)];
   }
}

-(void) handleTouchAtPoint:(CGPoint)location {
   NSLog(@"Touch At Point");
   HudLayer *h1 = (HudLayer *)[self.parent getChildWithTag:kHudLayer];
   [h1 handleTouchAtLocation:location];
}

HudLayer.h is imported in GameplayLayer.h. I'm getting the "Touch At Point" log but it's not going through to HUD layer for some reason..


Solution

  • The only explanation is that self.parent has no child with the tag kHudLayer. If you set a breakpoint in handleTouchAtPoint you'll notice h1 being nil after the getChildWithTag line did execute.