Our game will have various layers to show some kind of depth. The layers are in this order:
- scrolling background
- lower clouds
- bullets
- playfield (player, enemies)
- upper clouds
- score
- debug information
The code for this is straightforward:
Pane backgroundLayer;
Pane lowerCloudLayer;
Pane bulletLayer;
Pane playfieldLayer;
Pane upperCloudLayer;
Pane debugLayer;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
try {
// create root node
Group root = new Group();
// create layers
backgroundLayer = new Pane();
lowerCloudLayer = new Pane();
bulletLayer = new Pane();
playfieldLayer = new Pane();
upperCloudLayer = new Pane();
debugLayer = new Pane();
// add layers to scene root
root.getChildren().add( backgroundLayer);
root.getChildren().add( lowerCloudLayer);
root.getChildren().add( bulletLayer);
root.getChildren().add( playfieldLayer);
root.getChildren().add( upperCloudLayer);
root.getChildren().add( debugLayer);
// create scene
Scene scene = new Scene( root, Settings.SCENE_WIDTH, Settings.SCENE_HEIGHT, Color.BLACK);
// show stage
primaryStage.setScene(scene);
primaryStage.show();
...
// start the game
startGame();
} catch(Exception e) {
e.printStackTrace();
}
}
We introduced a special Settings class so that we can change the game's attributes at one place.
public class Settings {
public static double SCENE_WIDTH = 400;
public static double SCENE_HEIGHT = 800;
}
This way it's easy to vary the scene dimensions depending on your playfield size.
No comments:
Post a Comment