Building a Foundation For Your Games and Handling Screens
This article is part 1 of a series: The Structure of a Game. Read the Introduction here.
What does a game foundation consist of?
The starting point for any well built structure is a strong foundation. It is important that the foundation of your game is able to handle the basic functionality that will apply across all of your games, and does it simply with little to no extra effort on your part.
Also, what do all games have in common? They consist of various screens (Oh yes they do, or your game would be considered a tech demo my friend). With that in mind, our structure should be able to handle the following:
- Context Menu Setup
- Access to the Stage.
- Tweaking of game speed
- Frame Rate Tracking
- Sending updates to your game
- Custom Game Cursor ( if applicable to your game )
- Global game pause
- Ability to step through code 1 update at a time
- Screen switching
In order to implement all of these in a manner that we can easily and quickly transfer over to each and every project and use without any real need of changing the code at all we will create our structure using the following 3 classes:
- IScreenItem - Anything that will be considered a screen, such as MenuScreen, GameScreen, etc...
- AScreen - An Abstract class which handles screen switching.
- Root - Our meat and potatoes of the foundation. Handles the "Basic" functionality discussed above.
Lets build it!
A few concepts for me to mention now before we start are that almost all of our game objects will use an update and dispose method. Updates will filter down from the Root class so as to keep greater control of the game and eliminate the inconvenience and speed hog of creating multiple frame event listeners to the stage. Also it is good practice to clean up your code on removal such as removing any event listeners and nulling any outside references.
Also we will be using a constant time step to update our code. It is quite simple to interchange actually which I can show you, but in my experience a non-constant time step can cause inconsistencies that are difficult to debug.
When I refer to "Global Game Pause" this means that all updates to the game objects and rendering. You may have seperate pause logic within your game screen that pauses only certain features, still allowing menus or other GUI to run.
The IScreenItem and AScreen Classes
Okay, so let's begin with the IScreenItem class. This will be the interface for any screen used in the game... Read the rest of this entry »





