0

I have a game with a menu, when I access the menu it takes me to a different sf::View and when I exit the menu it is supposed returns me to the sf::View I was currently on using a switch and case statements. Instead of taking me back to the currentroom which should be set to 1 when I am in the StartRoom and access the menu it puts me in LevelTwo and runs Room2(). So somehow currentroom is going from 1 to 2 when I am in the first room, so when I access the menu by pressing 'q' it returns me to room2 instead of the startroom. Also when I replace WhatRoom(currentroom); with WhatRoom(1); the same thing happens, It runs Room2() and I am sent to the second room. Thanks for any help and if you have any questions I will do my best to answer.

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;
    }
}
DapperDaniel
  • 75
  • 1
  • 10

1 Answers1

0

You're missing a break at the end of your case StartRoom: branch. Into this function:

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;



    }
};

This is a common mistake in switch statements. I recommend you always to write down all possible cases with its respective breaks.

There are some special cases where you may want to remove those break lines.

alseether
  • 1,889
  • 2
  • 24
  • 39