【问题标题】:Enter and Exit menu using enum and switch sfml使用枚举进入和退出菜单并切换 sfml
【发布时间】:2018-02-24 14:04:56
【问题描述】:

现在在我的游戏中,我正在尝试制作一个可以在按“q”时访问的菜单,但目前我遇到了一些问题。我认为它切换到CompScreen 视图然后很快又回到currentroom 视图,我可能错了。我得到了 cout CompMenu、HELP 和 hello 读数,所以我知道它正在通过程序运行,但是当我按 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";
        }

如果您有任何问题或需要查看更多代码,请告诉我。谢谢。

【问题讨论】:

    标签: c++ game-engine sfml


    【解决方案1】:

    我认为您在第一个if 块之后错过了else,因此它可能会立即执行,因为此时InMenu 可能会更改为true

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多