0

所以我想制作一款代码非常简洁且组织良好的游戏。调查游戏状态我发现了这个网站: http: //gamedevgeek.com/tutorials/managing-game-states-in-c/

使用他的模板,到目前为止我有这个:

游戏状态.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);

}
4

2 回答 2

1

这里的问题是 TitleScreenState.hpp:

#ifdef Rect_Game_TitleScreenState_hpp
#def Rect_Game_TitleScreenState_hpp

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

于 2012-06-30T19:24:35.667 回答
0

我有一段时间没有接触 C++,但您似乎已经声明默认构造函数是私有的:

private:
   TitleScreenState() {}

如果我没记错的话,这意味着您无法运行:

void TitleScreenState::Init()

因为它是一个需要TitleScreenState实例化的非静态函数,并且默认控制器是私有的:S,所以在任何地方new TitleScreenState()你都会得到那个错误,因为实际上没有TitleScreenState可用的公共给出错误Use of undeclared identifier 'TitleScreenState'

您是否尝试将其公开?

public:
   TitleScreenState() {}
于 2012-06-30T19:19:31.027 回答