Natman Posted March 26, 2014 Share Posted March 26, 2014 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: Uncaught TypeError: Cannot read property 'visible' of undefinedphaser.js:23839 Link to comment Share on other sites More sharing options...
Manifest Posted March 26, 2014 Share Posted March 26, 2014 I had this - it threw a new error every time I moved my cursor over the stage. Are you sure you're calling destroy() on every button? Additionally, try button.input.stop() Natman 1 Link to comment Share on other sites More sharing options...
rich Posted March 26, 2014 Share Posted March 26, 2014 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? Natman 1 Link to comment Share on other sites More sharing options...
Natman Posted March 26, 2014 Author Share Posted March 26, 2014 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. Natman 1 Link to comment Share on other sites More sharing options...
Manifest Posted March 26, 2014 Share Posted March 26, 2014 Unfortunately, my 'solution' was just to ensure that you're calling destroy() correctly - previously, when I was getting the error, I had done something likebutton.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. Natman 1 Link to comment Share on other sites More sharing options...
rich Posted March 26, 2014 Share Posted March 26, 2014 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. Natman 1 Link to comment Share on other sites More sharing options...
Natman Posted March 26, 2014 Author Share Posted March 26, 2014 OK, I found the problem. My Player object contained a Sprite as a field, but also had its own destroy() method. When the player died, I was only destroying the Sprite and not the Player, so the code to destroy the buttons was never used. My bad. Manifest 1 Link to comment Share on other sites More sharing options...
Recommended Posts