Jump to content

"Game Over" text only appears on first game over


heisenthurg
 Share

Recommended Posts

Building a platformer. When the player runs out of lives, I have a function that resets the number of lives, the score, and restarts the state. When they are out of lives, text is displayed along the lines of "Game Over, press enter to restart". When playing for the first time, this text displays, and when the player presses enter it restarts correctly. However, if the player gets a Game Over again, the text doesn't show. The function is still working though, as pressing enter restarts the state again. 

In my create function I have:

 

        //Game Over 
        goText = this.game.add.text(game.world.centerX,game.world.centerY - 200,' ', { font: '40px Arial', fill: '#D80000', align: 'center' });
        goText.anchor.setTo(0.5, 0.5);
        goText.font = 'Press Start 2P';
        goText.visible = false;
        goText.fixedToCamera = true;

 

Then a separate function (stored in my game.js file as a global function) for when the player is killed to check number of lives and show Game Over if neccessary:

 

function playerKill() {

    if (lives > 1) {
            lives -= 1;
            livesText.text = 'Lives: ' + lives;

            player.kill();
            player.reset(150, 650);

        } else {

            player.kill();

            goText.text="GAME OVER!\nPress Enter to try again...";
            goText.visible = true;

            var restartButton = game.input.keyboard.addKey(Phaser.Keyboard.ENTER);
            restartButton.onDown.addOnce(restartGame);

    }

}

 

And the restartGame function called when Game Over shows and the player presses enter:

 

function restartGame() {
    lives = 3;
    score = 0;
    game.state.start('level1', false, true);
}

 

At first I thought it was something to do with the goText.visible boolean not setting to true on the second play, but console log shows that whenever the player gets game over, it is setting to true, and the text is being added in to the object. It just doesn't appear!

Any idea what I'm missing??

Thanks!

Link to comment
Share on other sites

34 minutes ago, heisenthurg said:

Thanks for replying so quickly! I'm new to JS, and dont really know much about OOP at the moment, could you give a little more detail as to how I would create a class please? 

Forget classes for now. Try to destroy the text object completely and reinitialize it at every Game Over see what happens.

Link to comment
Share on other sites

I tried destroying the object, that worked, but I've now found the cause of the issue. The text was always appearing, but it seems like its position relative to the camera was not resetting on Game Over. The following line was the problem.

goText = this.game.add.text(game.world.centerX,game.world.centerY - 200,' ', { font: '40px Arial', fill: '#D80000', align: 'center' });

Instead of using "game.world.centerX", I am now giving it a pixel value and it always shows up in the center of the screen as I need it.

Thanks for the help!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...