所以我想用一个干净的代码和良好的组织能力来制作一款游戏。调查游戏状态后,我发现此站点: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);

}

最佳答案

这里的问题是TitleScreenState.hpp:

#ifdef Rect_Game_TitleScreenState_hpp
#def Rect_Game_TitleScreenState_hpp


首先,#ifdef阻止编译器查看文件的其余部分,因为尚未定义Rect_Game_TitleScreenState_hpp。因此,它应该是#ifndef。另外,#def应该是#define。那应该解决问题。

关于c++ - 游戏状态的Xcode C++错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11276935/

10-10 18:43