This week, I got around to adding some animation into the game and fixing a (again) thread-related bug.
Exiting the Application
After playing around with the application, I noticed a problem: After a couple of executions from within eclipse, my computer got pretty slow. It turned out that after closing the application, one Java thread always stayed alive. The reason for this is my architecture. In my starting class, I called the JavaFX application as follows:
The JavaFxApplication was as follows:
As you can see, the JavaFX application started in a new thread. When the user closes the (JavaFX) window, this thread stopped. But because the first thread that called the FX-thread never did something else as to start its counterpart, it was never exited. This way, it went on and on. To make it stop after the user closed the JavaFX window, I changed it as follows:
Now, the first (purely Java) thread starts the JavaFX thread and joins it. This means that only after the JavaFX thread finishes, the originaly thread continues. This gives me the chance to stop it in a proper way.
Animations!
I know that animations are a huge topic in JavaFX and there are many tutorials out there which show pretty impressing stuff. However, I programmed my own little animation for the harvesting colonies. Oh right: There are colonies in the game now! I’m not sure what they will do, but they sure will need some resources to do anything. Hence, I made them harvest existing resources:
The ultimate resource-gathering red laser beam is made as follows:
There are a few articles out there that describe how to remove a node from the group after its animation went through. However, the most articles I’ve seen didn’t work for JavaFX 2.2.
Get the Code
As always, I pushed the code in my repository on github. The codebase for this article is tagged as Codebase_ExitApplicationAndAnimation.
Next Steps
Because only having one colony is not very awesome, colonies will be able to expand soon. Most likely, they will be able to spawn a new colony if they have enough energy.