我的 C++ 游戏中有大约 10 个场景类。它类似于常规游戏屏幕(菜单屏幕、游戏屏幕、排行榜、选项等)。所以我需要一些技巧来将一个场景替换为另一个场景。我已阅读您在此线程中发布的所有评论并尝试实施它。现在我有以下代码:
// -----[ main.cpp ]----- //
#include "SceneManager.h"
#include "Scene.h"
int main( int argc, const char * argv[] ) {
SceneManager *sceneManager = new SceneManager();
sceneManager->changeScene( 0 );
delete sceneManager;
return 0;
}
// -----[ Scene.h ]----- //
class SceneManager;
class Scene {
public:
SceneManager *sceneManager;
void start( SceneManager *sceneManager );
};
// -----[ SceneManager.h ]----- //
#include "Scene.h"
#include "MenuScene.h"
#include "GameScene.h"
class Scene;
class MenuScene;
class GameScene;
class SceneManager {
public:
Scene* scene;
void changeScene( short id ) {
if ( scene != NULL ) {
delete scene; // to prevent a memory leak
}
if ( id == 0 ) {
scene = new MenuScene();
} else if ( id == 1 ) {
scene = new GameScene();
}
if (scene) {
scene->start( this );
}
}
};
// -----[ MenuScene.h ]----- //
class MenuScene: public Scene {
public:
void start( SceneManager *sceneManager ) {
this->sceneManager = sceneManager;
}
};
// -----[ GameScene.h ]----- //
class GameScene: public Scene {
public:
void start( SceneManager *sceneManager ) {
this->sceneManager = sceneManager;
}
};
由于错误(XCode 4.6,MacOS X),它不起作用:
Apple Match-O Linker (id) Error
Undefined symbols for architecture x86_64:
"Scene::start(SceneManager*)", referenced from:
SceneManager::changeScene(short) in main.o
ld: symbol(s) not found for architecture x86_64
我做错了什么?如何解决?也许有人知道这个主题的一些热门问题?