The declaration above is really important. For example, let’s say that in our game, when the user hits the space bar, the ship fires. Basically, the
CStatePattern_GameView will receive a key event. The view will simply redirect the event to the game manager. The game manager does not know anything about the game logic, so it redirects this event to the current state object. If
PlayState is the current state, the
KeyEvent function of that state will check which key was struck. In this case, it’s going to fire. If the current state would have been
IntroductionState, this event could have had a totally different effect.
State transitions are another important part of the pattern.
For example, the
Update function of the
CPlayState could check if the player has any ships left. If this is not the case, this function would call the
ChangeState function of the
CGameManager like so:
pGameManager->ChangeState(CGameOverState::Instance() /* singleton pattern */);<TABLE cellSpacing=1 cellPadding=0 width="100%" bgColor=#ff9900 border=0>
|The states are not changed by the game manager, but by the state itself.|
The declaration above might look strange, but when you think about it, it’s just logic. Let’s say that the player needs to press F1 to start a game while he is in the introduction page. The introduction page knows what to do when the user presses F1. In that case, it will tell the game manager to change the current state to
In my demo project, I simply demonstrate some state transitions. When the game starts, we are in Introduction state. This state will wait for the user to press a key. According to the key pressed, the Introduction state will change the
CGameManager’s state. F1 will switch to the
CPlayState, and F2 will switch to the
In my demo project, I have made some
TRACE calls to show you the details of each transition.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.