0
votes

I am creating a game using C++ and Qt Creator. When my health gets below zero, I want to quit the current application and show a different screen that displays quit or play again.

I can do this by using the hide() method and then showing the new screen with the show() method.

However, if I do this while the game is still running in the background, I can still hear the music playing even though it is hidden. The main problem with this is that when I click the play again button and it loads up the game again in a new window, the score and health are affected by what's still going on in the old game that I have hidden.

Is there a way I can close the window completely so that it quits the game but then still loads the next window I want it to?

2
Just exit from Qt application stackoverflow.com/questions/8026101/…demonplus
That doesn't quite make sense. If you "quit the application", then any music playing in the application should go away, whether you shut down gracefully or not. hide() does not quit the application.CodeLurker

2 Answers

1
votes

Delete the window instance and create a new one for the next game. Or keep it and implement proper application states which control things like music to be off when the game is not in running state.

0
votes

It sounds like an implementation problem, here is an example of what you could do:

//in the main header declare a pointer to your game.
GameClass *game;

//in the main class when the game is supposed to start.
game = new GameClass(parent);
connect(game, SIGNAL(GameOver(int)), this, SLOT(cleanUpGame(int)));
game->show();

//define the slot in the main class
function cleanUpGame(int code){
    switch(code) .... //check the game over code
    case USER_KILLED:
        if(game != NULL){
            delete game;
            game=NULL;
        }

    break;
}

//in the game class header class description add
signals:
void GameOver(int);

//in the game class when you are finished.
 emit GameOver(USER_KILLED);
 this->close();