使用enum和switch sfml进入和退出菜单

时间:2018-02-24 14:04:57

标签: c++ game-engine sfml

在我的游戏中,我正在尝试制作一个菜单,当您按“q”时可以访问该菜单,但目前我遇到了一些问题。我认为它会切换到CompScreen视图,然后快速返回currentroom视图,我可能错了。我得到了cout CompMenu,HELP和你好读数,所以我知道它正在运行程序,但是当我按q时我仍然在同一个地方,没有任何事情发生。

EventManager.h

#ifndef EventManager_h
#define EventManager_h


#endif /* EventManager_h */


int windowWidth = 5000;//width of window
int windowHeight = 5000;//height of window
sf::View leveltwo(sf::FloatRect(x, y, 5000, 5000));
sf::View start(sf::FloatRect(0, 0, 2500, 1500));
sf::View ComputerScreen(sf::FloatRect(50000, 50000, 5000, 5000));
sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight ), "Awesome Game" );
Character player("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/Sprites/Player.png");

bool InMenu = false;

enum Levels{
    StartRoom, LevelTwo
};
Levels room = StartRoom;
int currentroom;
void WhatRoom(int TheLevel){

    switch (room){
        case StartRoom:
            currentroom = 1;
            window.setView(start);
            if (TheLevel == 2){
                room = LevelTwo;
            }
            break;
        case LevelTwo:
            currentroom = 2;
            window.setView(leveltwo);
            break;



    }
};


enum States{
    compmenu, mainmenu, NoMenu
};
States menu = NoMenu;

void CompMenu(){
    window.setView(ComputerScreen);
    cout << "HELP";
    InMenu = true;

}
void WhatMenu(int TheMenu){
    switch (menu){
        case compmenu:
            cout << "CompMenu";
            CompMenu();
            break;
        case mainmenu:
            break;
        case NoMenu:
            if (TheMenu == 2){
                menu = compmenu;
            }
            break;
            if (TheMenu == 3){
                menu = mainmenu;
            }
            break;
    }
}

main.cpp(在int main中)

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q) and InMenu == false){
            WhatMenu(2);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q) and InMenu == true){
            InMenu = false;
            WhatRoom(currentroom);
            cout << "hello";
        }

如果您有任何疑问或需要查看更多代码,请告知我们。感谢。

1 个答案:

答案 0 :(得分:0)

我认为您在第一次else阻止后错过if因此可能会立即执行,因为此时InMenu可能会更改为true:< / p>

else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q) and InMenu == true){

此外,您应该处理按键长时间(仅在进入按下状态时作出反应)并摆脱所有全局变量的情况,因为它们已经造成了相当混乱。