当语句切换到案例1时,将语句切换到案例2

时间:2018-02-27 00:15:30

标签: c++ switch-statement game-engine sfml

我有一个带菜单的游戏,当我访问菜单时,它会将我带到另一个sf::View,当我退出菜单时,它会让我回到我当前正在使用的sf::View一个开关和案例陈述。当我在currentroom并访问菜单时,我将其带回StartRoom并运行LevelTwo,而不是将我带回Room2(),而WhatRoom(currentroom);应设置为1。因此,当我在第一个房间时,当时电流从1到2,所以当我通过按'q'访问菜单时它返回到room2而不是startroom。此外,当我用WhatRoom(1);替换#include <iostream> #include <SFML/Graphics.hpp> #include "Character.h" #include "Sprite.h" #include "Computer.h" #include "Window.h" #include "EventManager.h" //Use std using namespace std; //Boolean to determine if screen will scroll Sprite computer("/Users/username/Desktop/Xcode /NewGame/ExternalLibs/Sprites/CompSprite.png", 1200, 100); Sprite battery("/Users/username/Desktop/Xcode /NewGame/ExternalLibs/Sprites/battery4.png", 0, 0); Sprite wooddoor("/Users/username/Desktop/Xcode /NewGame/ExternalLibs/Sprites/wood_door.png", 1200, 1350); //boolean for whether to show weapon or not bool showweapon; //main loop int main() { window.setKeyRepeatEnabled(false); bool showBox = false; //Setting up the dungeon back-round dungeon.loadFromFile("/Users/username/Desktop/Xcode /NewGame/ExternalLibs/Sprites/DungeonBack.png"); if (LevelOne == true){ backround.setTexture(dungeon); //Replace with function that determines backround } while (window.isOpen()){ // check all the window's events that were triggered since the last iteration of the loop sf::Event event; while (window.pollEvent(event)){ // "close requested" event: we close the window if (event.type == sf::Event::Closed) window.close(); } //Movement if (moveChar == true){ if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){ player.left(); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){ player.right(); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){ player.forward(); } if (sf:: Keyboard::isKeyPressed(sf::Keyboard::Down)){ player.backward(); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)) { player.Highspeed(); } else{ player.Lowspeed(); } } if (batstat == 4){ battery.changeimage("/Users/username/Desktop/Xcode /NewGame/ExternalLibs/Sprites/battery4.png"); } if (batstat == 3){ battery.changeimage("/Users/username/Desktop/Xcode /NewGame/ExternalLibs/Sprites/battery3.png"); } if (batstat == 2){ battery.changeimage("/Users/username/Desktop/Xcode /NewGame/ExternalLibs/Sprites/battery2.png"); } if (batstat == 1){ battery.changeimage("/Users/username/Desktop/Xcode /NewGame/ExternalLibs/Sprites/battery1.png"); } if (batstat == 0){ battery.changeimage("/Users/username/Desktop/Xcode /NewGame/ExternalLibs/Sprites/battery0.png"); } if (player.getSprite().getGlobalBounds().intersects(computer.getSprite().getGlobalBounds())){ show = false; player.hascomp = true; } if (player.getSprite().getGlobalBounds().intersects(wooddoor.getSprite().getGlobalBounds()) and show == false){ WhatRoom(2); showBox = true; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){ moveChar = true; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q) and InMenu == false){ WhatMenu(2); } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q) and InMenu == true){ InMenu = false; WhatRoom(currentroom); } //draw and window stuff window.clear(sf::Color(0, 0, 0)); window.draw(backround); if (show == true){ window.draw(computer.getSprite()); } if (show == false){ window.draw(battery.getSprite()); window.draw(wooddoor.getSprite()); } window.draw(player.getSprite()); window.display(); window.setFramerateLimit(70); } } 时,同样的事情发生了,它运行Room2(),然后我被送到第二个房间。感谢您的帮助,如果您有任何疑问,我会尽力回答。

main.cpp

enum Levels{
    StartRoom, LevelTwo
};
Levels room = StartRoom;
int currentroom = 1;

void Room2(){
    window.setView(leveltwo);
    backround.setPosition(0, 5000);
    generalback.loadFromFile("/Users/username/Desktop/Xcode /NewGame/ExternalLibs/Sprites/DungeonBackGeneral.png");
    backround.setTexture(generalback);
    player.relocate(0, 5000);

    down_limit = 6410;
    right_limit = 2410;
    up_limit = 5000;
    left_limit = 0;
}

void WhatRoom(int TheLevel){
    switch (room){
        case StartRoom:
            currentroom = 1;
            window.setView(start);
            backround.setTexture(dungeon);
            if (TheLevel == 2){
                LevelOne = false;
                cout << "hello";
                room = LevelTwo;
                break;
            }

        case LevelTwo:
            cout << "HELLO";
            currentroom = 2;
            backround.setTexture(generalback);
            Room2();
            break;



    }
};


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

void CompMenu(){
    window.setView(ComputerScreen);
    InMenu = true;

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

EventManager.h

@Bean
//@Primary
@ConfigurationProperties(prefix="spring.datasource")
public DataSource fDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix="spring.secondDatasource")
public DataSource sDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean
public JdbcTemplate jdbcTemplateOne(@Qualifier("fDataSource") DataSource ds) {
    return new JdbcTemplate(ds);
}

@Bean
public JdbcTemplate jdbcTemplateTwo(@Qualifier("sDataSource") DataSource ds ) {
    return new JdbcTemplate(ds);
}

1 个答案:

答案 0 :(得分:0)

您在break分支的末尾错过了case StartRoom:。 进入这个功能:

void WhatRoom(int TheLevel){
    switch (room){
        case StartRoom:
            currentroom = 1;
            window.setView(start);
            backround.setTexture(dungeon);
            if (TheLevel == 2){
                LevelOne = false;
                cout << "hello";
                room = LevelTwo;
                break;
            }
        /*
            At this point you should break this branch, otherwise
            case LevelTwo brach will be executed too
        */
        case LevelTwo:
            cout << "HELLO";
            currentroom = 2;
            backround.setTexture(generalback);
            Room2();
            break;



    }
};

这是switch语句中的常见错误。我建议你总是写下所有可能的案件及其各自的休息时间。

有些special cases您可能希望删除这些break行。