0

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?

Tay2510
  • 5,748
  • 7
  • 39
  • 58
  • Just exit from Qt application http://stackoverflow.com/questions/8026101/correct-way-to-quit-a-qt-program – demonplus Apr 10 '15 at 11:37
  • 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 Nov 07 '16 at 05:02

2 Answers2

1

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.

Frank Osterfeld
  • 24,815
  • 5
  • 58
  • 70
  • How do I delete the window instance? – user3019111 Apr 10 '15 at 11:31
  • 1
    @user3019111 `delete window;`, with the type being `QWidget * window`. If the window is on the stack, you need to move it to the heap. Generally speaking, your design is broken and the window should be aware of what state the application is in - it should know how to go quiet when you want it to be quiet. – Kuba hasn't forgotten Monica Apr 10 '15 at 13:43
0

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();
buster
  • 1,038
  • 7
  • 16
  • It is not necessary to test for game being NULL. "delete 0" is defined to do nothing. It is a good habit to get into to set game=0; NULL; or nullptr, if you want to get fancy with c++11. – CodeLurker Nov 07 '16 at 05:01