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

问题描述 投票:0回答:1

我有一个带菜单的游戏,当我访问菜单时,它会带我到另一个sf::View,当我退出菜单时,它应该返回到sf::View我当前正在使用开关和case语句。我没有把我带回currentroom,当我在StartRoom并进入菜单时它应该设置为1,它让我进入LevelTwo并运行Room2()。因此,当我在第一个房间时,当时电流从1到2,所以当我通过按'q'访问菜单时它返回到room2而不是startroom。此外,当我用WhatRoom(currentroom);替换WhatRoom(1);同样的事情发生时,它运行Room2(),我被送到第二个房间。感谢您的帮助,如果您有任何疑问,我会尽力回答。

main.cpp中

#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);

        }
    }

EventManager.h

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;
    }
}
c++ switch-statement game-engine sfml
1个回答
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线。

© www.soinside.com 2019 - 2024. All rights reserved.