I'm new to JavaFX and I wanted to create a simple 2D game using JavaFX. I read this JavaFX game tutorial which I found very useful. I wrote a simple game with the following structure:
public class MyGame extends Application
{
(...)
public void start(Stage theStage)
{
(...)
Group root = new Group();
Scene theScene = new Scene( root );
theStage.setScene( theScene );
Canvas canvas = new Canvas( 800, 600 );
root.getChildren().add( canvas );
(...)
theScene.setOnMouseClicked(
new EventHandler<MouseEvent>()
{
public void handle(MouseEvent e)
{
//mouse events
}
});
final GraphicsContext gc = canvas.getGraphicsContext2D();
(...)
final LongValue lastNanoTime = new LongValue( System.nanoTime() );
new AnimationTimer()
{
public void handle(long currentNanoTime)
{
double elapsedTime = (currentNanoTime - lastNanoTime.value) / 1000000000.0;
lastNanoTime.value = currentNanoTime;
//drawing all sprites with gc etc.
}
}.start();
theStage.show();
}
}
And I was quite happy with the result. There was no menu etc. in the game, just one screen with the game itself. The problem is, I now want to expand the application and include a few different games in it, which means I somehow need to handle a few different screens (the main menu and each of the games) and switch between them. I looked for help in the Internet, but all I found were some examples with FXML which I don't really use.
My question is: how can I easily create a few other screens and switch between them, given the structure that I have now?