0

我正在尝试将触摸位置从 GameplayLayer 传递到 HUDLayer,以查看用户是否按下控制按钮。

HudLayer.mm

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

游戏玩法.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 在 GameplayLayer.h 中被导入。我收到了“Touch At Point”日志,但由于某种原因它没有进入 HUD 层。

4

1 回答 1

1

唯一的解释是self.parent没有带标签的孩子kHudLayer。如果您在 handleTouchAtPoint 中设置断点,您会注意到在该行执行h1后它是 nil 。getChildWithTag

于 2012-10-08T15:06:07.320 回答