# Using Compound Objects to Track Character State

1
• 5 favourites

### Stats

799 visits, 1,017 views

### Translations

This tutorial hasn't been translated.

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

## Track a Character State Using Compound Objects

Sometimes, calculating seemingly complex things can be simplified simply by thinking about the problem as a series of problems. Implementing behaviors where many things can happen is much easier if you break it down into tiny behaviors. These behaviors can be turned on or off based on a character's state. Anything from variables to objects can be used to calculate what a character state is.

If we use Mario as an example, you could say that he has many obvious states that determine how he behaves. If he is in water, then he acts very differently than when on land. To track that, all one would need is a single Boolean variable. If Mario is in water, then that variable, or flag, would be set to true.

### Using a second object to calculate state...

Lets say you wanted to make a platformer game. In many cases you may need to know where the player object is in relation to the environment. Is the player on the ground? Is the player on a wall?

Without using a built in behavior, these questions can be easily answered.

First you can create an object that matches the player object's collision dimensions (simply clone the object and remove any variables and behaviors attached to it. You should name it something like "player_state". Add the following variables: ("up","down","Left", and "Right").

Every frame of the game, set this player state object to the same position as the player object. Set all Boolean variables to false. For each direction, set the position of the object at an offset of the player's position and then check for collisions. If there are any, then set the appropriate variable to true.

At a later point, these variables can be checked in meaningful ways. You could use this to add wall jumps to a platformer game, for example.

• Order by