BliantFive Posted April 14, 2016 Share Posted April 14, 2016 Hi guys, in a little project i have an odd issue when trying to destroy buttons. I use buttons like this one: this.cashbutton = game.add.button(150, 220, 'cashbutton', function(){this.cashbutton.destroy()} , this, 'hover.png', 'normal.png', 'click.png'); Whenever i cick the button it destroys itself(which i want) but the console gives a warning: "Cannot set frameName: hover.png". I guess its trying to set the .frameName to hover.png after destroying it. Any ideas how to prevent that? I dont like warnings... I could use a timed event, but that feels like a bad solution. Link to comment Share on other sites More sharing options...
VitaZheltyakov Posted April 14, 2016 Share Posted April 14, 2016 You can not just destroy the object, to which are attached events. Just hide your button BliantFive 1 Link to comment Share on other sites More sharing options...
BliantFive Posted April 14, 2016 Author Share Posted April 14, 2016 Thx for your reply. That is a solution. But lets say i dont want to have the button in memory anymore. (Im using another close button for each window and like to destroy the window completely with anything in it. Which works great except for the warning.) What would be the right way to deattach events before destroying a phaser button? Link to comment Share on other sites More sharing options...
drhayes Posted April 14, 2016 Share Posted April 14, 2016 That's weird and is possibly a bug. Calling destroy on the Button should disconnect all the listeners. It's probably because you're destroying it in the middle of its update instead of just after. Try calling it like this: "setTimeout(this.cashbutton.destroy.bind(this.cashbutton), 10);" instead of calling it directly in the handler. That'll queue up its destruction after the update has finished and will (maybe) remove the warning. BliantFive 1 Link to comment Share on other sites More sharing options...
BliantFive Posted April 17, 2016 Author Share Posted April 17, 2016 On 14.4.2016 at 6:59 PM, drhayes said: That's weird and is possibly a bug. Calling destroy on the Button should disconnect all the listeners. It's probably because you're destroying it in the middle of its update instead of just after. Try calling it like this: "setTimeout(this.cashbutton.destroy.bind(this.cashbutton), 10);" instead of calling it directly in the handler. That'll queue up its destruction after the update has finished and will (maybe) remove the warning. Thats exactly what i think happens and i already thought about this solution. But it does look messy in the code and is another timer that just shouldnt be there. Link to comment Share on other sites More sharing options...
drhayes Posted April 18, 2016 Share Posted April 18, 2016 Well... philosophically, what's messy? Semantically, in a browser, this is the way you enqueue a function in the event loop in JavaScript. If this were Node it'd be "process.nextTick" but the functionality is the same. I recognize that aesthetically it might look gross, but if it makes you feel better you can make a wrapper function that does that for you elsewhere so you don't have to look at it. BliantFive 1 Link to comment Share on other sites More sharing options...
BliantFive Posted April 18, 2016 Author Share Posted April 18, 2016 12 hours ago, drhayes said: Well... philosophically, what's messy? Semantically, in a browser, this is the way you enqueue a function in the event loop in JavaScript. If this were Node it'd be "process.nextTick" but the functionality is the same. I recognize that aesthetically it might look gross, but if it makes you feel better you can make a wrapper function that does that for you elsewhere so you don't have to look at it. Yeah you are right. I might just add a ".close()" function to phaser button that does that. And BTW... if this were node I would, by now, be in the 5th hirarchy of callbacks and my posts would be full of curses that might get me banned. Link to comment Share on other sites More sharing options...
Recommended Posts