我正在尝试将触摸位置从 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 层..
最佳答案
唯一的解释是 self.parent
没有带有标签 kHudLayer
的子级。如果您在handleTouchAtPoint中设置断点,您会发现在执行getChildWithTag
行后,h1
为零。
关于ios - 正确链接 HUDLayer 到 GameplayLayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12783823/