Today we finally implement scenes. Scenes are entities that correspond to a single state (screen) in a game. This can be a menu screen, the main game, or credits screen. A game made using Artenus has a single stage, on which scenes are switched to create the flow of the game. Having implemented the building blocks, there is not much work left for us to do today.
The last day we created sprite collections, which we saw how they are going to act as layers of our scene. So, we start our implementation by plugging them in. Create a class called
Scene as a sibling to
Stage, and add the following fields to it:
protected Stage stage; private final SpriteCollection layers;
As our scene is part of a stage, we need to keep track of the parent for later. That’s what the first field is for. The second field contains an array of sprite collections. When we add a sprite to a scene, we do so by indirectly adding it to one of these layers. We could also use an
ArrayList instead of an array. But since the cases where we want to push an extra layer in the middle of a scene are rare (and can almost always be replaced by an improvisation), we stick to the array.