Game Development Reference
In-Depth Information
Character states
In order to correctly handle states, we will expand our character code to handle
different states that can occur. Firstly, we need to identify the states, which are
as follows:
Running state : This is the default state when the character is running on
the ground
Jumping state : This is the state when we press a button to jump, but it
should be limited so that we don't continue the jumping state when we
are in the air
In air state : This is the state when the character is still in the air following
a jump
In order to use these states, let's define them in ERGPlayer.h :
typedef enum playerState {
playerStateRunning = 0,
playerStateJumping,
playerStateInAir
} playerState;
This code creates a new type of variable that is internally a usual integer, and we
use that to identify the state of the character. We could use integer, character,
or even string, but these can lead to problems. We will have to remember what
state corresponds to what integer, and here we just write the state as it is.
Now, we need to store this state in our character data. Add the following line
to ERGPlayer.h :
@property (assign, nonatomic)playerState animationState;
In this case, we want to execute some custom code when the animationState
property is changed, so we implement our own setAnimationState: method.
It is called each time we change the animationState property:
- (void) setAnimationState:(playerState)animationState
{
switch (animationState) {
case playerStateJumping:
if (_animationState == playerStateRunning) {
[self stopRunningAnimation];
[self startJumpingAnimation];
}
 
Search WWH ::




Custom Search