所以我想用一个非常干净的代码和良好的组织来制作游戏。查看游戏状态,我找到了这个网站:http://gamedevgeek.com/tutorials/managing-game-states-in-c/
使用他的模板,到目前为止我有这个:
GameState.hpp
#ifndef Rect_Game_GameState_hpp
#define Rect_Game_GameState_hpp
#include <SFML/Graphics.hpp>
#include "GameEngine.hpp"
class GameEngine;
class GameState {
public:
virtual void Init() = 0;
virtual void Cleanup() = 0;
virtual void Pause() = 0;
virtual void Resume() = 0;
virtual void HandleEvents(GameEngine* game) = 0;
virtual void Update(GameEngine* game) = 0;
virtual void Draw(GameEngine* game) = 0;
virtual void ChangeState(GameEngine* game, GameState* state);
private:
GameState() { }
};
#endif
TitleScreenState.hpp
#ifdef Rect_Game_TitleScreenState_hpp
#def Rect_Game_TitleScreenState_hpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "GameState.hpp"
#include "GameEngine.hpp"
class TitleScreenState : public GameState {
public:
void Init();
void Cleanup();
void Pause();
void Resume();
void HandleEvents(GameEngine* engine);
void Update(GameEngine* engine);
void Draw(GameEngine* engine);
void ChangeState(GameEngine* engine, GameState* state);
static TitleScreenState* Instance();
private:
TitleScreenState() {}
static TitleScreenState* titleScreenInstance;
sf::RenderWindow* window;
int mouseX;
int mouseY;
Button* playButton;
};
#endif
然后每当我尝试实现这些功能时出现错误“使用未声明的标识符'TitleScreenState'”。它也不会自动完成“TitleScreenState”。有什么建议吗?
TitleScreenState.cpp
#include <iostream>
#include "GameEngine.hpp"
#include "GameState.hpp"
#include "TitleScreenState.hpp"
#include "ResourcePath.hpp"
void TitleScreenState::Init()
{
// Initialize values
leftClick = false;
mouseX = 0;
mouseY = 0;
// Load title screen image
sf::Texture titleImage;
if (!titleImage.loadFromFile(resourcePath() + "TitleScreen.png"))
printf("could not load TitleScreen.png");
sf::Sprite titleScreen;
titleScreen.setTexture(titleImage);
playButton = new Button("Play", 350, 220);
}
答案 0 :(得分:1)
这里的问题是TitleScreenState.hpp:
#ifdef Rect_Game_TitleScreenState_hpp
#def Rect_Game_TitleScreenState_hpp
首先,#ifdef
阻止编译器查看文件的其余部分,因为尚未定义Rect_Game_TitleScreenState_hpp
。因此,应该是#ifndef
。此外,#def
应该是#define
。这应该可以解决问题。
答案 1 :(得分:0)
我有一段时间没有触及c ++,但似乎你声明默认构造函数是私有的:
private:
TitleScreenState() {}
如果我没弄错,这意味着你无法逃脱:
void TitleScreenState::Init()
因为它是一个非静态函数,需要实例化TitleScreenState
并且默认控制器是私有的:S,所以在你new TitleScreenState()
的任何地方你都会得到那个错误,因为有效的没有公共{ {1}}可用,提供错误TitleScreenState
您是否尝试将其移至公开?
Use of undeclared identifier 'TitleScreenState'