Sign in to follow this  
ITpaolo

Game Over Screen - Destroy complete state

Recommended Posts

Hi Community,

I want to make an Game Over screen for my Top Down Test Game.

My Code is in states.

I did load the state with

"game.state.add('gameover'), gameoverState);"

on my play.js 

gameover: function () {

        if (sprite.body.y > 700) {
            game.state.start('gameover');
        }

gameover.js

var gameoverState = {
    create: function () {

        var gameoverLabel = stateText = game.add.text(game.world.centerX, game.world.centerY, ' ', {font: '84px Arial', fill: '#F2F2F2'});
        stateText.anchor.setTo(1.1, 0.2);

    },

    update: function () {

        game.world.removeAll();
        stateText.text = " GAME OVER \n Click to restart";
        stateText.visible = true;

        //the "click to restart" handler
        game.input.onTap.addOnce(function () {
        game.state.restart();}, this.start);
    },

    start: function () {
        game.state.start('menu');
    }
};

Everytime I die this will pop up, no text just all the obj and layer resize in 16x16 or 32x32.

 

Regards,

ITpaolo

 

btw. Sorry, i'm nooby in Phaser. :-P

 

Unbenannt.PNG

 

 

EDIT:

 

I know now that this was the problem for it:

play.js

/*
        if (sprite.body.x < -30) {
            sprite.body.x = this.game._width - 30;
        }
        if (sprite.body.x > 650) {
            sprite.body.x = this.game._width - 650;
        }
        if (sprite.body.y < -50) {
            sprite.body.y = this.game._height - 50;
        }
        if (sprite.body.y > 660) {
            sprite.body.y = this.game._height - 650;
        }
*/

But now Idk how to do the Game Over screen, any help?

 

EDIT2: Did now:

play.js

if (sprite.body.y > 650) {
            stateText.text = " GAME OVER \n Click to restart";
            stateText.visible = true;

            //the "click to restart" handler
            game.input.onTap.addOnce(function () {
            game.state.restart(this.start);});
        }
    },
    start: function () {
        game.state.start('menu');
    }

Its now restarting the 'play.js', if I click "restart" but I want that he start the "menu.js" state, any help 2.0? ^^

 

EDIT3: I fixed it, hope I can help anyone.

play.js

if (sprite.body.y > 650) {
            stateText.text = " GAME OVER \n Click to restart";
            stateText.visible = true;

            //the "click to restart" handler
            game.input.onTap.addOnce(function () {
            game.state.start('menu');});
        }
    },
    start: function () {
        game.state.start('menu');
    }

 

 

EDIT4: The last edit, now I fixed it really, he now start the 'gameover.js' when the sprite die.^^

play.js

if (sprite.body.y > 650) {
            game.state.start('gameover');
        }

gameover.js

var gameoverState = {
    create: function () {

        var gameoverLabel = stateText = game.add.text(500, 300, ' ', {font: '50px Arial', fill: '#F2F2F2'});
        stateText.anchor.setTo(1.1, 0.2);

    },

    update: function () {
        if (sprite.body.y > 650) {
            stateText.text = " GAME OVER \n Click to restart";
            stateText.visible = true;

            //the "click to restart" handler
            game.input.onTap.addOnce(function () {
            game.state.start('menu');});
        }
    }
};

 

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.