在我的代码中,我制作了一个 GameManager 单例,它有一个负责改变场景的方法。我调用的第一个场景是 MenuScene,之后我将其替换为 GameScene。当我这样做时,控制台输出显示:
2013-10-07 19:40:55.895 MyGame[56164:a0b] -[MenuScene distance]: unrecognized selector sent to instance 0xb460690
2013-10-07 19:40:56.011 MyGame[56164:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MenuScene distance]: unrecognized selector sent to instance 0xb460690'
从日志中,我不明白为什么我会-[MenuScene distance] ...
因为距离是 GameScene 而不是 MenuScene 的属性。
下面是用于切换场景的 GameManager 方法:
-(void)runSceneWithID:(SceneTypes)sceneID {
SceneTypes oldScene = currentScene;
currentScene = sceneID;
//NSString* str;
id sceneToRun = nil;
switch (sceneID)
{
case kBeginScene:
sceneToRun = [BeginScene node];
break;
case kGameScene:
sceneToRun = [GameScene node];
break;
case kMenuScene:
sceneToRun = [MenuScene node];
break;
default:
CCLOG(@"Unknown ID, cannot switch scenes");
return;
break;
}
if (sceneToRun == nil) {
// Revert back, since no new scene was found
currentScene = oldScene;
return;
}
if ([[CCDirector sharedDirector] runningScene] == nil) {
[[CCDirector sharedDirector] runWithScene:sceneToRun];
} else {
[[CCDirector sharedDirector] replaceScene:sceneToRun];
}
}
此外,替换场景的调用位于作为 MenuScene 一部分的图层类中。见下文:
-(void)startGameScene {
[[GameManager sharedGameManager] runSceneWithID:kGameScene];
}
请帮忙。