Jump to content

Removing buttons


Natman
 Share

Recommended Posts

I create several buttons when gameplay begins for controlling the player, but I want them to be removed after the game ends. Then if the player starts playing again in the same session, the buttons return.

 

When game over happens, I call destroy() on each of the buttons. This seems to work, because the buttons disappear, but then this error starts triggering repeatedly, and no more input is accepted by the game: 

  1. Uncaught TypeError: Cannot read property 'visible' of undefinedphaser.js:23839
Link to comment
Share on other sites

Hmm, this test works ok (tested in 2.0.1 but same as in 2.0.0 too):

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create });function preload() {    game.load.spritesheet('button', 'assets/buttons/button_sprite_sheet.png', 193, 71);}var button;function create() {    button = game.add.button(game.world.centerX - 95, 400, 'button', nukeIt, this, 2, 1, 0);}function nukeIt() {	button.destroy();}

What are you doing differently to the above so we can replicate it?

Link to comment
Share on other sites

I am using the buttons' onInputDown and onInputUp signals to start player movement when they are pressed and end it when they are released.

The buttons are fields of my player, so I add the events like this:

this.moveLeftButton.onInputDown.add(moveLeftCallback, this);

this.moveLeftButton.onInputUp.add(stopLeftCallback, this);

And since I'm adding events like this, I don't want to pass in an event on button creation so I construct like this:

game.add.button(x, y, key, null, null, 0, 0, 1);

These are the only differences I can think of. Manifest is correct about the error triggering when the mouse moves across the window, but his solution did not fix things.

Link to comment
Share on other sites

Unfortunately, my 'solution' was just to ensure that you're calling destroy() correctly - previously, when I was getting the error, I had done something like

button.input.stop();button.kill();

but changing it to button.destroy() fixed it for me =/ chuck in a super.destroy() somewhere, if applicable?

 

Alternatively, try saving those SignalBindings (returned when you add the listeners) to a member variable and explicitly removing them. 

Link to comment
Share on other sites

Ok I've updated my code to do it the same way you said, and it still doesn't error for me (this is using 2.0.1 release version)

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create });function preload() {    game.load.spritesheet('button', 'assets/buttons/button_sprite_sheet.png', 193, 71);}var button;function create() {    button = game.add.button(game.world.centerX - 95, 400, 'button', null, null, 2, 1, 0);    button.onInputDown.add(nukeIt, this);}function nukeIt() {	button.destroy();}

Does the above work for you as well? If so it must be something else triggering this in your code, although it's hard to say where without seeing more I suspect.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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