Jump to content

Game.destroy() error


phaserdev97
 Share

Recommended Posts

Hello, can anyone help me, please ? 
I don't understand why when I use game.destroy() inside the update function of my state, I get an error "Cannot read property 'update' of null" ? 
It seems that the game continue to run the updateLogic loop, after the destroy call, which call the update function on the variable stage, which has been set to null in the destroy function. Isn't the game supposed to break of the updateLogic loop when I call the game.destroy() function ? If not, how am I supposed to destroy my phaser game when reacting to an event in my update loop ? I wasn't able to find a working solution yet during my research.
Thank you.

Link to comment
Share on other sites

Destroying your game is most definitely not the way to go on this lol. Instead of destroying your game, why not call another game state? 

If you really want to destroy your game state you might have to call an external function to delete the game variable, but you shouldn't be able to use game.destroy() because .destroy() is a function of the game itself. You cant destroy something with the means of destroying it inside. You probs have to just set it to null or delete the variable. Either way, this path is probably not the best way. 

Link to comment
Share on other sites

Thanks for your answer,

I don't want to just change my game state from the main state to a "game over" state, I want to destroy the entire game (canvas, sprite, cache, ... everything) when a certain event occurs in my main state.

In fact, I don't have one phaser game, but two. The first game is a normal phaser game and it switches to the second one. The second phaser game is each time a different mini-game. After the mini-game is complete, I switch back to the first phaser game and destroy the mini-game. I could have done that by switching between states in a unique phaser game, but I wan't both, the main game and all the mini-games, to be fully independent. (I don't want mini-games to make hazardous change to the main game and possibly destroying it, because it might be too complicated to clear the possibly mess that is left behind by a mini-game when returning to the main game and because I'm not the one making the mini-games).

Link to comment
Share on other sites

9 hours ago, phaserdev97 said:

Thanks for your answer,

I don't want to just change my game state from the main state to a "game over" state, I want to destroy the entire game (canvas, sprite, cache, ... everything) when a certain event occurs in my main state.

In fact, I don't have one phaser game, but two. The first game is a normal phaser game and it switches to the second one. The second phaser game is each time a different mini-game. After the mini-game is complete, I switch back to the first phaser game and destroy the mini-game. I could have done that by switching between states in a unique phaser game, but I wan't both, the main game and all the mini-games, to be fully independent. (I don't want mini-games to make hazardous change to the main game and possibly destroying it, because it might be too complicated to clear the possibly mess that is left behind by a mini-game when returning to the main game and because I'm not the one making the mini-games).

Lol i think you might be confused as to how game states work. Game states literally set up separate phaser games inside of one game, with complete independence of each other (except global vars/funcs obviously). Setting up game states is the way to go here, you will eventually have to do it, you might as well do it now. 

Link to comment
Share on other sites

Yes, of course, the best way is to just use states. But, as you said, states aren't fully independent of the game. If something change in the mini-game state (for example with a call to a global function) likes physics, the loading bar, plugins, etc... or even if the cache is cleared ; I don't want it to impact the main game.

Link to comment
Share on other sites

On 7/29/2016 at 2:59 AM, phaserdev97 said:

Yes, of course, the best way is to just use states. But, as you said, states aren't fully independent of the game. If something change in the mini-game state (for example with a call to a global function) likes physics, the loading bar, plugins, etc... or even if the cache is cleared ; I don't want it to impact the main game.

You cant "accidentally" change a global variable lol, if you dont want those variables possibly changed by a player making their own minigame, they shouldnt be in the global scope to begin wiht. On top of that, physics arent global, calling a function shouldn't change anything you didn't feed to it, and using a different state wont clear the cache. Literally destroying the game is by far the worst way to handle this and I dont think its even possible besides just setting the value to null. Dunno how to be any more clear. Best of luck! 

Link to comment
Share on other sites

But, I want those global variable to be possibly change by the player, even clearing the cache if it's what he wants to do. I don't have any control on what could be done in the mini-game, even the worst.
Currently, my solution is to create a state which creates the new game and hide/pauses itself. And, when the second game is over, it resumes the main game which switches to a new state whose goal is to destroy it by calling an outside destroy function on the second game. 
I don't understand why there is no way to properly destroy a phaser game and be sure that nothing is left ?

Oh ok, I thought that the physics was somewhat global, since you can start/set it in different states.

Anyway, thanks for your help :).

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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