Jump to content

Switching states - error [REMOVE PLEASE]


Recommended Posts


I'm having a problem when switching states with Phaser.

I have one menu state and one game state.

In menu there are different options and different sub-menu options.

Something like this:


MenuOption1 - click calls function menu1Submenu() which creates sub menu which is overlayed over part of the Menu



I set groups and images inside menu1Submenu function this way:

this.menu1SubmenuGroup1 = this.menu1SubmenuGroup1 || this.add.group();

because you can go back and fort however you want I don't want to create a new group for submenu content everytime - when I already created one before. So I just change renderable to false/true instead and change inputEnabled to false/true depending whether the submenu is active or not.

One of the options in submenu is to start the game (thus changing states occurs). I first remove all events I set on all elements of my groups which I created then destroy them and the destroy group as well, to make sure everything is clean I set all the groups to undefined. Then I change state:


In the game I have a button which leads back to menu, that buttons simply does:


When I get back to menu animations start, everything seems ok but the moment I click on MenuOption1 to open submenu nothing happens or more precisely when I comment destroying, removing events and setting all groups to undefined I get errors:

phaser.js:40129 Uncaught TypeError: Cannot read property 'events' of nullPhaser.InputHandler.start
@ phaser.js:40129Phaser.Component.InputEnabled.inputEnabled.set
@ phaser.js:45418(anonymous function)
@ menu.js:1323Phaser.Timer.update
@ phaser.js:65567Phaser.Time.update
@ phaser.js:64890Phaser.Game.update
@ phaser.js:35146Phaser.RequestAnimationFrame.updateRAF
@ phaser.js:60028_onLoop
@ phaser.js:60012

83 phaser.js:40273 Uncaught TypeError: Cannot read property 'scale' of null

When I set my groups as:

this.menu1SubmenuGroup1 = this.add.group();

Everything works ok, but even when I destroy groups and all their content and set the property to undefined it brings this error.


Could someone advise me how to remove the stuff properly in this case?

I want to keep the groups but don't want to have them declared before because that just adds more content which is needed most of the time (imagine that you don't always open settings, and so on so there is no need to create them all the time), and I also don't want to stick only to this.menu1SubmenuGroup1 = this.add.group(); because this way everytime anyone clicks on any option adds more groups to phaser object (correct?), so a few times clicking back and forth can give dozens of groups which all link to same object (?) or at least polute the memory (I suppose I could always destroy groups set undefined when going through menu but that doesn't seems as a nice effective way).

I'm not sharing the entire code because it's too long and I managed to reduce the problem to lines mentioned above.


Thank you very much I really appreciate any advice.


PS: I used latest stable Phaser version 2.4.7.

Link to comment
Share on other sites

Ok, I tried to redownload (again) phaser and replace the file and it started working. I don't know why because it's the same version I was using before.

Can I please ask the moderator to delete this post?

Thank you very much.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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