JavaFX 8 - Custom events

Hello JGO!
I searched the forum and google but couldn’t find a solution.
Basically what I try to achieve is the following with JavaFX:

  • If e.g. a Sprite “dies” i want to fire an event.
  • Another class (which handles scoring) should listen and wait for that event getting fired.

Basically I want to “wire” two classes without one having a reference to the other.

Hi buddy!

//Sprite.class
if(died() == true){
    Main.state = DIED;//where Main.class is your main class for example
}
//Event.class
if(Main.state == DIED){
    //fire event
}

That does not seem too difficult does it? :wink:
Of course that would depend on your code :stuck_out_tongue:

(Same goes with second question)

Good luck :smiley:

J0

That is a absolutely terrible idea. The moment you need more than one sprite, the system breaks down.

A much better way is to have a method in the class of your ‘thing’ that is called when the ‘thing’ dies.
This method could then fire a event, or put a message in some queue that is continously read.

EDIT: A very quick google search that should tell you what you want:

But… He wanted to wire the two classes without referring to each other .____.
What I understood was, any sprite could fire the event by setting Main.state to true, so that, for example, you could fire a game over event from anywhere in the program.