Jump to content

Destroying Buttons - Best Practices?


Recommended Posts

I have a button with a callback that effectively completes a state and moves on to the next state. The world remains but I remove the UI (including that button).


The problem is, it seems that the callback fires on mouseup, but before the button changes back to the up state. So, when the button is removed using button.destroy(), it causes null reference errors:

Uncaught TypeError: Cannot read property 'cache' of null

It is referencing this.game.cache, but this.game is null.


I could fork Phaser and fix this, but I was wondering if maybe I'm just doing it wrong.  :)




Link to comment
Share on other sites

  • 5 months later...

I encountered this issue now myself. Below is the code from my project.

var PointEditScreen = function(game) {  Phaser.Image.call(this, game, 600, 50, 'background');  var exitButton = game.add.button(360, 5, 'exit-button', this.closeScreen, this, 1, 0, 2, 0);  this.addChild(exitButton);PointEditScreen.prototype.closeScreen = function() {  this.destroy();};

The code works, but results in the same error as with the original poster: "Uncaught TypeError: Cannot read property 'cache' of null".


The error does not happen if don't I give any values to downFrame and upFrame when creating the button. Also, if I set the downFrame and upFrame to the same value, in my case the frames would then be 1, 0, 2, 2, the error does not occur.


So is this a bug or am I doing something wrong?


Edit: Seems to be fixed in Phaser 2.3.0. My problem was with 2.2.2. Great :)

Link to comment
Share on other sites

  • 3 weeks later...

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...