Jump to content

Destroy Phaser Game Completely


Recommended Posts

Hi Guys,

I am new to this community and this is actually my second post, but my first one didn't appear. So I hope I am not posting my question twice. If it does appear, my apologies!  :wacko:


Anyhow, I am pretty stuck on a problem. I found a dirty work around, which I would really like to get rid of though.

I think the problem is a little bit more complex and I don't know if I am making the right assumptions considering my solution, but I hope somebody tried the same with a different solution approach.


So here we go: 

Basically, I am trying to switch in and out of my game, over states with Angular. I set up a simple controller, which is been given my <div> container in a template, which I add my game into when I switch to that route. The way I load my game into the container when I hit the state/route is that I wrote my own lazy loading function that loads my game setup:



(function() {
    var game = new Phaser.Game(screen.width, screen.height, Phaser.CANVAS, 'gameCanvas');
    // add our game states
    game.state.add('boot', MMM.Boot);
    game.state.add('preload', MMM.Preload);
    game.state.add('main-game', MMM.MainGame);
    // start with the booting state
When I exit my game and return to my menu controller, I remove my script again from the <head> and destroy the phaser game and I try to clear the cache with this.game.cache.destroy()
If I want to enter my game again I just load the above script again with my lazy load function. 
It enters my game just fine, but i get the following error:
Uncaught TypeError: Cannot read property 'cache' of null
I tried reloading all of my game related scripts including my preload script, but the error stays the same.
The assumption that I made was that the game is not actually completely destroyed and thus has some remains which cause errors.
My work around is to do a $window.location.reload(false); and load everything again from cache. Works without any errors and loads quickly, too, but I really don't like how I am going about this.
Any recommendations? 
Thanks in advance!



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.

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.


  • Recently Browsing   0 members

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