#JavaFX scene isnt switching once number hits 0

1 messages · Page 1 of 1 (latest)

harsh drum
#

So i want once the HP hits 0, for it to switch to the gameover scene but it never does, it drops to 0 then just freezes

long marshBOT
#

<@&987246487241105418> please have a look, thanks.

lime aurora
#

Your error is about multi-threading.

The timer task runs on its own thread which is different from the thread that creates the UI and renders it.

In most UI frameworks there is a technical need for the change of a value on the UI to occur in the thread that runs the UI.

Because of that most frameworks implement a stack of actions to be executed on the UI thread and give the user some convenient way to add items to it.

In JavaFX that is Platform.runLater() which adds whatever action you send into it to the stack of actions the UI thread will execute soon.

Here the thing you set on the ui thread obviously enough is setting the scene, so instead that needs to happen in an action you send to the UI thread via Platform.runLater

#

Its actually an issue with whatever was line 106 like your error message says. I have no idea whether your .attack() modifies something on the ui thread

harsh drum
#

Heres the attack function @lime aurora

lime aurora
#

The important thing to understand here is that any time you set a setting on something in the ui from another thread, you need to wrap i in a runLater call

#

this attack fn probably qualifies since i'm assuming circle is a ui element

harsh drum
#

so how would i do that? ive never used timers or runeLaters before

#

so its all new to me

lime aurora
#

Google it?

You just give it a lambda to wrap your code like

Platform.runLater(()->{
//Whatever you were already doing here
});

#

You need to gogle a tutorial about javafx runlater if you want to get a deeper understanding.