heisenthurg Posted January 13, 2016 Share Posted January 13, 2016 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 More sharing options...
Batzi Posted January 13, 2016 Share Posted January 13, 2016 Honestly I would make a game over class and initialize the text object among other things in the constructor and in that class I'd call the showGameOver() function. This should fix your problem. Link to comment Share on other sites More sharing options...
heisenthurg Posted January 13, 2016 Author Share Posted January 13, 2016 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? Link to comment Share on other sites More sharing options...
Batzi Posted January 13, 2016 Share Posted January 13, 2016 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 More sharing options...
heisenthurg Posted January 14, 2016 Author Share Posted January 14, 2016 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! mduSenthil 1 Link to comment Share on other sites More sharing options...
Recommended Posts