Jump to content

Simple button issue


BliantFive
 Share

Recommended Posts

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

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

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.

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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

Loading...
 Share

  • Recently Browsing   0 members

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