Jump to content

how to restart/reload a state?


Lotti
 Share

Recommended Posts

Hey :D

 

So I am trying to do this, I call:

this.game.state.restart(true, false)

 

And I go into an infinite loop.  My first question, is, does anyone know what would make this occur? My second is, how do you debug an infinite loop in JS??

Thanks!

 

EDIT:

Additional strangeness:  

this.game.state.destroy() appears to do nothing, I would expect a blank screen  

this.game.state.start("game", true, false); //inf loop same as restart

Link to comment
Share on other sites

StateManager.destroy = Removes all StateManager callback references to the State object, nulls the game reference and clears the States object.

 

It doesn't clear the display list.

 

I just tested StateManager.restart() on a game I'm building and it re-ran the State without hitting an infinite loop, which I'd definitely notice :) I'm not using TS though (are you?). Also the restart was called from a click event, although I'm not sure it should matter, but I'll test it from in an update function too.

 

Needless to say this is 2.0.3-dev only.

Link to comment
Share on other sites

This is my method. When a game state starts (game.state.starts('mainGame') ;), it runs the create function of that state, but all the variables that where setup don't change. What I do is call on create a function call restart(); where I initialize the values of all my variables.

create: function() {        this.ll = 0;        this.canShoot = true;        this.restart();}
restart: function() {        this.gfunc;        this.player;        this.enemie;        this.powerUp;        this.cursors;        this.circle;        this.d = 270;        this.p1;        this.p2;        this.p3;        this.ORBIT_RADIUS = 60;        this.enemies = new Array();        this.counter = 0;        this.counter1 = 0;        this.counter2 = 0;        this.eToDelete;        this.level1 = [            [18, 19, 0, 1, 2], 101, 102        ];        this.ll = 0;}

So that works for me, I also stop and start the timers of my game, in order to not get errors.

Link to comment
Share on other sites

  • 7 months later...
// Exit to menuif (playTime) {  game.input.onUp.addOnce(function() {    playTime = false;    game.state.clearCurrentState();    game.state.start("main");  });  game.input.keyboard.onUpCallback = function () {    playTime = false;    game.state.clearCurrentState();    game.state.start("main");  }}

I'm having a game over screen when the time runs out and I want to press any key or click to go back to the main menu. The problem is that in the main menu those key presses from my gameplay state are still listened to, so I can end up sort of running the game on top of itself, which is awful. How the heck do I properly kill the gameplay state and stop making it listen to events and have my gameplay state reset when starting again?

Link to comment
Share on other sites

You can use the shutdown function on a state to clean up after yourself when it's shut down, i.e. to remove listeners etc, as the state doesn't (and almost certainly couldn't) attempt to do much more than clear the display list and destroy a few things it set up itself.

var MyState = {  create: function(game) {    // set stuff up here  },  update: function(game) {    // do stuff here  },  shutdown: function(game) {    // unset, destroy, unlisten etc here  }};
Link to comment
Share on other sites

 

You can use the shutdown function on a state to clean up after yourself when it's shut down, i.e. to remove listeners etc, as the state doesn't (and almost certainly couldn't) attempt to do much more than clear the display list and destroy a few things it set up itself.

var MyState = {  create: function(game) {    // set stuff up here  },  update: function(game) {    // do stuff here  },  shutdown: function(game) {    // unset, destroy, unlisten etc here  }};

 

Seen mention of that in the docs but it seemed kinda redundant at the time as I was hoping there's an easier way. Guess I gotta clean up manually after myself :D Thanks, will give it a go soon if I can figure out how to actually unlisten to those events in my code.

Link to comment
Share on other sites

If using Signals (no doubt you are) it'll probably be a case of calling removeAll or dispose on them.

 

Tried a few things, it either disables the listener right away and not reaching the main menu (while fixing my game over text tween to not show up laggy) or screws up the listener from the main menu, not being able to start playing again hmm...

Link to comment
Share on other sites

Hard to say without seeing it and the code, obviously the order you do things is pretty important in your particular scenario.

 

Here's my project for who wants to take a look https://www.dropbox.com/sh/b47ubqtt8ei7ev2/AAASkwTGfDzHnYm2Z_xN1ls2a?dl=0

It's an adventure game started out in a game jam so that I can learn game coding. So far most things I try in phaser don't go very well.

Link to comment
Share on other sites

  • 1 year later...

Apologies for reviving a dead thread but this came up on the earlier Google searches.

I wanted to restart the state via game.state.restart but when I did, it would seem the cache got wiped no matter what I provided as an argument to prevent this. Cue an awful lot of warning messages and then a legitimate crash in trying to find images that don't exist. Has anybody else noticed this (2.5.0)

To get around this I start another interim state and instantly start the original state from within the interim state

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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