Attached Files

The following files have been attached to this tutorial:



Download now 1.89 MB


4,994 visits, 7,313 views



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.

I made a simple space shooter "Boss Battle" type mini game to demonstrate a basic finite state machine for behaviors.

What's a State Machine?

Some video game enemies are stateless, like the UFO in Asteroids... a space invader or a typical zombie. They're always in the same state, like chasing the player or following a predetermined path.

The state may be complex, it may involve path finding, or various speeds or complicated maneuvers, but a stateless game entity will always have one thing only on its mind.

State List

All "state machine" means is that sometimes an entity is doing one thing, and sometimes it's doing something else. What it's doing is called its "state." Finite state machines have a list of states things they can be in. An enemy state list may look something like this:

Chase player

Stop and yell "Welcome to die!"

Run away from player

Look for power up

Shoot mega missile

shoot small weapon

Do pretty spin maneuver


Each state has an action and a way of getting out of that state into another state.

States can have states within them, but for this demo the boss just has a few basic states.

It's easier to use words for the states

I have a text variable in the boss sprite called "state," and populate it with exactly what it is in plain language. You might be tempted to use numbers like 1,2,3 with 1 meaning "chase player," 2 meaning "run away" and so on --and there's nothing wrong with that, but I like to just use words.

For the boss battle type of object, I use a timer to determine when to go to the next state. So for the boss sprite there is state (text) and stateTimer (number). After a certain amount of time in one state, the boss goes into the next.

Don't forget the "dying" state

Not all states are fun to be in for your game enemies. Some might be "appearing" where a zombie is coming out of the ground or a spaceship is warping in... another might be "dying" with sub-states like "spinningOutOfControl" or "Exploding." This Boss has a "dying" state where it's just spinning slowly, falling and spawning explosions.

Other Tricks

"The Alien Ship's Only Weak Point

For the boss battle, I made a kind of "hitbox" or vulnerable point that bosses often have. An easy way to accomplish this is to have an opening in the sprite's collision polygon:

It's literally just a point...

Add a point to the object called "hit" and score a hit on the boss by checking for a collision



less than


That way when the bullet hits the boss it MUST hit within 20 pixels of wherever I put that "hit" point before any damage is done!

Voila: Hitboxes (sort of). This can work for head-shots or alien boss "brains" or whatever. You can have multiple points on a single sprite, so go nuts. No need for connected sprites just to do selective hit detection.

Player and boss made from remasterd Tyrian graphics by Daniel Cook: lostgarden.com/2007/04/free-game-graphics-tyrian-ships-and.html

I just made the space background and other objects.

Song "Cool Adventure Intro" Music by Eric Matyas soundimage.org

There are other tricks in this demo, like scrolling backgrounds, health bars, coins... but the capx code makes it pretty clear what's going on.

I hope this helps! Enjoy!

Happy gamedeving!



Download now 1.89 MB

  • Order by
Want to leave a comment? Login or Register an account!