0

问题:

我的 while 循环没有退出。在调试过程中,它非常明确地表示stateID等于 3。但是,当我添加std::cout << stateID;该值时1,总是将值写入控制台,无论调试器告诉我什么。

在代码中,我在输入循环之后渲染屏幕。这表明,毫无疑问,输入循环按预期正确退出。此外,如果这还不够,它必须首先退出才能更改 stateID。请不要再讨论嵌套循环是问题所在。而且我还在循环之后使用了正确命中的断点。

代码:

int main()
{

    stateID = 1;

    GameState* state = new GameStateTitle();

    sf::RenderWindow window(sf::VideoMode(1000, 600), "RPG");

    //GAME LOOP//
    while (stateID != 3)
    {//INPUT//
    sf::Event event;
    while (window.pollEvent(event))
    {
        switch (event.type)
        {
        //Window closed
        case sf::Event::Closed:
            set_next_state(3);
            break;
        }
    }

        change_state(state);

    //RENDERING//
    window.clear(sf::Color::Black);

    state->render(window);

    window.display();
    }
    /////////////

    window.close();

    return 0;
}

void set_next_state(int new_state)
{
    //Set the next state to take place
    next_state = new_state;
}

void change_state(GameState *current_state)
{
    //Check if the next state is null or exit
    if (next_state != 0)
    {
        if (next_state != 3)
            delete current_state;

        //Set the new state
        switch(next_state)
                ...
        }

        stateID = next_state;
        next_state = 0;
    }
}

下面是我的完整代码,以防我错过了一些重要的东西。

主文件

#include "GameState.hpp"

int main()
{

    stateID = STATE_TITLE;

    GameState* state = new GameStateTitle();

    sf::RenderWindow window(sf::VideoMode(1000, 600), "RPG");

    //GAME LOOP//
    while (stateID != STATE_EXIT)
    {
        //INPUT//
        sf::Event event;
        while (window.pollEvent(event))
        {
            switch (event.type)
            {
            //Window closed
            case sf::Event::Closed:
                set_next_state(STATE_EXIT);
                break;
            }
        }
        ///////////

        ////LOGIC//
        state->logic();
        ///////////

        change_state(state);

        //RENDERING//
        window.clear(sf::Color::Black);

        state->render(window);

        std::cout << stateID;

        window.display();
        /////////////
    }
    ///////////////

    window.close();

    return 0;
}

游戏状态.hpp

#pragma once

#include <SFML/Graphics.hpp>

//DEFINITIONS ETC//
enum GAME_STATES {
    STATE_NULL,
    STATE_TITLE,
    STATE_BATTLE,
    STATE_EXIT
};
///////////////////

//Game State Class//
class GameState
{
public:
    virtual void input(void) = 0;
    virtual void logic(void) = 0;
    virtual void render(sf::RenderWindow &window) = 0;
};
////////////////////

//FUNCTIONS//
void set_next_state(int new_state);
void change_state(GameState *current_state);
/////////////

//VARIABLES//
static int stateID;
static int next_state;
/////////////

//Title GameState Class//
class GameStateTitle : public GameState
{
private:
    sf::Texture img_title;
    sf::Sprite bgr_title;

public:
    GameStateTitle(void);
    ~GameStateTitle(void);

    void input(void);
    void logic(void);
    void render(sf::RenderWindow &window);
};
/////////////////////////

//Battle GameState Class//
class GameStateBattle : public GameState
{
private:
    sf::Image img_left;
    sf::Image img_right;

public:
    GameStateBattle(void);
    ~GameStateBattle(void);

    void input(void);
    void logic(void);
    void render(sf::RenderWindow &window);
};
//////////////////////////

游戏状态.cpp

#include "GameState.hpp"

//Game state general functions
void set_next_state(int new_state)
{
    //Set the next state to take place
    next_state = new_state;
}

void change_state(GameState *current_state)
{
    //Check if the next state is null or exit
    if (next_state != STATE_NULL)
    {
        if (next_state != STATE_EXIT)
            delete current_state;

        //Set the new state
        switch(next_state)
        {
        case STATE_TITLE:
            current_state = new GameStateTitle;
            break;
        case STATE_BATTLE:
            current_state = new GameStateBattle;
            break;
        }

        stateID = next_state;
        next_state = STATE_NULL;
    }
}

//The functions of the title state
GameStateTitle::GameStateTitle(void)
{
    //Load texture
    img_title.loadFromFile("title_screen.png");

    //Set texture to background
    bgr_title.setTexture(img_title);
}

GameStateTitle::~GameStateTitle(void)
{
}

void GameStateTitle::input(void)
{
}

void GameStateTitle::logic(void)
{
}

void GameStateTitle::render(sf::RenderWindow &window)
{
    window.draw(bgr_title);
}

//The functions of the battle state
GameStateBattle::GameStateBattle(void)
{
}

GameStateBattle::~GameStateBattle(void)
{
}

void GameStateBattle::input(void)
{
}

void GameStateBattle::logic(void)
{
}

void GameStateBattle::render(sf::RenderWindow &window)
{
}
4

3 回答 3

5

你有两个while循环。在内部循环退出之前,永远不会测试外部 while 中的条件。

考虑只使用一个循环,一个条件。

sf::Event event;
while ( (stateID != 3) && window.pollEvent(event)) {

}
于 2013-05-31T18:05:05.770 回答
1

你正在突破switch而不是突破while

下面的程序(尝试)复制您的代码。它按预期工作,两个循环都在它们应该结束的时候结束。

顺便说一句,这就是我们所说的简短、包含、正确的例子。在可编译中正确,使用诸如 in 之类的全局变量或诸如instateID之类的幻数是错误的形式,而不是专业人士的标志。3while (stateID != 3)

您的程序与下面的程序有何不同?

#define STATE_TITLE 2
#define STATE_BATTLE 3
#define STATE_NULL 0

int stateID;
int next_state;

struct Event
{
    Event(int Num) : type(Num){}

    Event() : type( 5 ){} // random number grater than 1

    enum Type
    {
        Closed = 1 // equivalent to sf::Event::Closed
    };

    int type;
};

class Window
{
public:

    int pollEvent( Event& Ev )
    {
        return --Ev.type;
    }
};

void set_next_state(int new_state)
{
    next_state = new_state;
}

void change_state()
{
    if (next_state != STATE_NULL)
    {
        stateID = next_state;
        next_state = STATE_NULL;
    }
}

int _tmain()
{
    stateID = STATE_TITLE;

    Window window;

    //GAME LOOP//
    while (stateID != 3)
    {//INPUT//
        Event event;
        // note that there is at least one event with a Closed type before window.pollEvent(event) return false (0)
        while (window.pollEvent(event))
        {
            switch (event.type)
            {
                //Window closed
            case Event::Closed:
                set_next_state(3);
                break;
            }
        }

        change_state();
    }

    return 0;
}
于 2013-05-31T18:01:09.167 回答
0

我将功能更改change_state为以下内容:

void change_state(GameState *current_state, int &ID)
{
    //Check if the next state is null or exit
    if (next_state != STATE_NULL)
    {
        if (next_state != STATE_EXIT)
            delete current_state;

        //Set the new state
        switch(next_state)
        {
        case STATE_TITLE:
            current_state = new GameStateTitle;
            break;
        case STATE_BATTLE:
            current_state = new GameStateBattle;
            break;
        }

        ID = next_state;
        next_state = STATE_NULL;
    }
}

问题一定在 的声明中stateID,因为它从未在此函数中更改。

于 2013-05-31T19:20:18.667 回答