Old Style Game Framework How To..
Creating a Game with Old Style Game Framework.
This walk through create a side scroller game, where the player runs across the screen and jumps over obstacles. The walk through uses the Old Style Game Framework (1.0.2), Game Doc (1.2.3) and Game Designer (1.2.2).
Updated versions: Old Style Framework (1.1), Game Doc (1.3) and Game Designer (1.3)
- Now that we have the Game document created, its time to start to build the game. Create a new project with the main class of SideScroller.
- Add the OldStyleGame.jar, GameDoc.jar and dom4j.jar to the projects libraries. (This project uses: Old Style Game version 1.0.2; Game Doc version 1.2.3; dom4j version 1.6.1)
- Now we need to copy the images and audio folders, and "Side Scroller.xml" file into our project source directory. Also, copy the HUNTER.TTF font over. The source folder is where the GameDoc library is looking for the files.
- We are going create our own SideScrollerCanvas,
extending GameCanvas, so we can add custom features.
- Create SideScrollerCanvas and override the constructor like
so:
Here we use the GameState framework. The Start GameState will display the title screen, until the player presses "Enter" Then it will change state (see below). - In the constructor we are going to load up our game document
using org.deken.gameDoc.document.DocumentManager.
DocumentManager documentManger = new DocumentManager(); gameDocument = documentManger.loadGameDocument( "Side Scroller.xml" );
- The DocumentManager has a feature that you can have it load resources as needed, by passing in true in the constructor.
- The GameDocument returned will give us access to all the game items we created in the GameDesigner program.
- The SideScroller class is the main class that creates the Game Frame.
- Create SideScrollerCanvas and override the constructor like
so:
- Next we'll setup up the Player: Create a new Jumper
class that extends BaseActor.
- The main feature of the Jumper class is the isNoTileBelow (line 89-102) method, here it check if there is a tile beneath the player. If there is not the player then falls.
- Create a JumpMovement class
that extends the TwoKeyMovement class. This class will use the base
two key movement of the parent, plus we will add code for making the
player jump
- The constructor will take the key Ids for right, left and space (for jumping). When the Actor gets added to the GameMap, the Movement will be queried for these Ids and wired into the input monitoring. (see getPlayer() in MapSetup.
- The collideVertical() method check if the player hits his head going up or lands on the ground.
- Create a FireBall class that extends
BaseActor.
- The main feature is the update (line 78-88) method. Here the FireBall checks if it has passed the players location. If so, it calls the map to increase the number of FireBalls missed.
- Next we are going to create a custom GameMap to control the Fireballs.
The AdventureMap extends the
TileMap. We need to override the constructor that takes a MapSize.
This constructor is used by the MapFactory.
- We have a method for adding additional FireBall's to the map (line 43). Note there is a separate method that adds the FireBall to the game (line 78-88). This method determines the player's location and sets the FireBall at the same Y coordinate as the player.
- We also keep track of the number of FireBall's the player passes (line 48).
- The removeFireBall (line 91-95), is called when the fireball reaches the left edge of the screen.
- In the update method (line 63-76), we check if there are FireBalls on the screen, if not we add one. Then we check if any FireBalls reach the left edge, if so, we remove it.
- I create a separate class for creating all our objects, called MapSetup.
The GameStates will use this to build the map. This will keep the
code cleaner in the GameCanvas and GameStates.
- In the buildBaseMap (line 52-60), we set a class mapping for BaseMap, to be the AdventureMap class. This method is used for the title screen and the game screen. We are going to use the Simple2DCollisionDetection. This class compares all objects for collision.
- The buildGameMap (line 62-75), gets the AdventureMap, adds the FireBall, player, backgrounds and layout for the game.
- The buildNullPlayer (line 78-88), is used for the title screen, since the GameMap requires a player sprite.
- The setupBackgrounds (line 90-109), gets all the backgrounds and adds them to a LayeredBackground to be set in the AdventureMap. For each background, we need to set the controlling sprite to the Player. The Houses and Trees backgrounds we need to set a speed at which they will scroll when the player moves. Set the houses to 0.4, which is farther back in the screen, and set the Trees to 0.8, which is closer and will move faster.
- The getFireBall (line 111-119) gets the FireBall sprite.
- The getPlayer (line 122-133), gets the player and set the keys to controller him. When the play gets added to the map, the GameMap will wire in the key events into the input monitor for us. I find a speed of .2 to work well for most games I've made so far. The higher the number this faster the player will move.
- Now to setup the GameStates. The GameState controls the flow of
the game. They determine when to display the Title screen, play the
game and end the game.
- We will start at the begining, the StartState game state. The constructor (line 40-54) builds the objects needed for the start screen.
- Override the activateState (line 57-59) method, this sets the GameMap into the GameCanvas.
- Override the deactivateState (line 62-66) method, this cleans up the GameMap, since the Start State will be re-used when the player ends the game. We remove the label.
- Override the update (line 69-75) method. This is called by the main game loop. It waits a second before displaying the Press Enter label.
- This class implements the InputListener. Implement all methods and add to the notifyListener (line 91-95) method. This gets called when the user presses the enter key. It calls the parent GameState method changeState. The next state, PlayingState, is passed in. Then it calls this GameState's deactivateState and the new GameState (PlayingState)'s activateState.
- The PlayingState, is the GameState
for when the player is playing the game.
- The activateState (line 39-45) sets up the GameMap. We put the GameStates (PlayingState and PlayerHitState) in the GameMap, since the GameMap monitors the player and fireballs.
- The other methods are not used.
- The PlayerHitState, is the GameState when the player gets hit by
a fireball, and ends the game.
- In the activateState (line 45-53) we deactivate the player, stop his movement and move him off the screen so he's not seen.
- In the update method, we wait a second and then display the Game Over label. Then wait four more seconds and display the Press Enter label.
- This class implements the InputListener, most of those methods do nothing, but the notifyInput (line 93-98) method, gets called when the player presses enter. Here we change the state back to the StartState.
Next -> Main Menu