7 days of WordPress themes, graphics & videos - for free!* Unlimited asset downloads! Start 7-Day Free Trial
  1. Game Development
  2. Game Design

Use Citrus to Build a Simple Flash Platformer

Scroll to top
Read Time: 11 mins

Ever wanted to make a platformer, but not too keen on writing all the code from scratch? The Citrus Engine doesn't eliminate code entirely, but it makes it much simpler, with plenty of useful 2D game features built right in. In this tutorial, we'll use it to build a simple platform game.

Step 1: Brief Overview

In this tutorial we'll use a popular Flash Game Engine, called Citrus, and a series of ActionScript classes to create a platformer game.

Step 2: Flash Document Settings

Open Flash and create a 420x280px document. Set the Frame rate to 30fps.

Step 3: Interface

We'll use these great pixel graphics by Guillaume Moreau. You can download them from opengameart.org.

Step 4: Background

For the background, change the stage color to #99D9EA, or use the Rectangle Tool (R) to draw a rectangle of that color.

Step 5: Alert

An alert will be shown when the player completes the level or dies; it will display a game over message and the score. Use the Rectangle Tool to create it and set its instance name to AlertView. Mark the Export for ActionScript box and give it the same class name.

There are many other objects in the game (for example: a title screen, the various symbols for each sprite, the terrain). Rather than explaining how to create them here, I suggest you download the source files and look at what objects are in the FLA's Library. You can either use the same symbols in your own game, or create new ones!

Step 6: Embed Font

We'll use a bitmap font in the game; since we are going to use the font dynamically we will need to embed it in the application. I'm using the fonts 04b11 and Arcade Classic.

Select a dynamic text field with the font of your preference and click the Embed... button in the Properties Panel.

Select/add all the necesary characters and click OK.

Step 7: Sounds

We'll use Sound Effects to enhance the feeling of the game. The sounds used in this example were generated using as3sfxr and are included in the source files.

Step 8: TweenNano

We'll use a different tween engine from the default included in Flash. This will increase performace as well as being easier to use.

You can download TweenNano from its official website.

Step 9: Citrus Engine

We'll use the Citrus Engine to power our game.

What is the Citrus Engine? Learn more from its website:

The Citrus Engine is a professional-grade, scalable Flash game engine built for industry-quality games. It is built upon modern Flash programming practices, allowing you to focus on making your game awesome! It comes built-in with a "platformer" starter-kit, which you can use to easily make awesome 2D sidescrolling games.

Step 10: Set Up Citrus Engine

Go to the download page and get the necessary files. Open Flash Preferences, select ActionScript from the list on the left and click on ActionScript 3.0 settings..., then add the path to the sources as shown in the image above.

Step 11: Set Document Class

We'll make our application interactive by using an external class. Add its name (Main) to the Class field in the Publish section of the Properties panel to associate the FLA with the Main document class.

Step 12: Create a New ActionScript Class

Create a new (Cmd + N) ActionScript 3.0 Class and save it as Main.as in your class folder.

Step 13: Class Structure

Create your basic class structure to begin writing your code.

Step 14: Main.as

The Main class will prepare the Citrus Engine to handle the first level.

This class extends the CitrusEngine class and sets the game's "state" to Level, which is the name of the class that will contain all our game's behaviour.

Step 15: Level.as

The State class is one of the core classes of Citrus Engine, and you should extend this class to create the game logic of your levels or states. In this example the Level class extends State to create the game's first level.

Create a new (Cmd + N) ActionScript 3.0 Class and save it as Level.as in your class folder.

Step 16: Required Classes

These are the classes we'll need to import for our Level class to work. The import directive makes externally defined classes and packages available to your code.

Step 17: Variables

These are the variables we'll use; read the comments in the code to know more about them. (Some of their names are self-explanatory, so there's no comment.)

Step 18: Constructor

The constructor is a function that runs when an object is created from a class, this code is the first to execute when you make an instance of an object, loads once the game starts if part of the document class.

It calls the necessary functions to start the game. Check those functions in the next steps.

Step 19: Pause Game

We'll start by pausing the game; this will prevent the engine from adding the graphics while the Level View sprite is on stage.

Step 20: Add Level View

This will add the Level View sprite to the stage, as well as a mouse listener to remove it.

Step 21: Start Level

The next function runs when the Level View is clicked, and will handle the logic to start the game.

Step 22: Remove Level View

This code destroys the Level View instance.

Step 23: Start GameState (Unpause)

Now we unpause the game and initialize the level.

Step 24: Start Box2D

The following code starts the Box2D engine that handles the physics of Citrus Engine.

Step 25: Add Graphic Objects

These lines will create the entire level, read through the code to understand the Citrus Engine instantiation system (which is really easy!).

(All view properties are the class names of symbols from the FLA Library: Bg, TerrainPart1, WaterX3, etc.)

As you can see, in each case you create an instance of the type of object you want and use the parameters to specify its position, size, graphic or skin (this is the view parameter) and other useful elements. Then we add it to the Citrus stage using the add() method.

Let's stop here to make a quick test and make sure that our game code is working:

Remember that the Milestones are included in the source files, so if for some reason your file doesn't mimic this one take a look at the source to see what can be causing that. (And keep in mind that some lines must be commented out as some functions haven't been created yet.)

Step 26: Add Signals

Citrus Engine uses Signals to handle event type interaction. You can learn more about signals on this Activetuts+ tutorial.

Step 27: Stop Hero Animation

Our Hero MovieClip will play by default unless we prevent it. This code handles that, and you will also learn how to access the MovieClip that serves as the art for your Citrus Object.

Step 28: Game Listeners

This code adds EnterFrame and Keyboard listeners that will be used in our game. You can read about the corresponding handler functions in the next steps.

Step 29: Add Hearts

Hearts will represent our Hero's health. The next code adds three hearts to the stage and stores them in a Vector to use them later outside this function.

Step 30: Add Score

The Score TextField is created by this code. We use filters to add the black stroke around the letters.

Step 31: Handle Camera

Here we set up the camera, which will follow our Hero if its position is past the center X of the stage.

Step 32: Check for Fall

This code checks if our hero has fallen into the water, and if so it plays the lose sound and calls an alert.

Step 33: Animate Hero Walk

Our hero walking animation is started when the left or right arrow keys are pressed.

Step 34: Stop Walk Animation

When the keys are released the animation stops.

Let's stop here to make another test and check that our game code is working:

Keep in mind again that some lines have been commented out as not all functions have been created yet.

Remember that the Milestones are included in the source files, so if for some reason your file doesn't mimic this one take a look at the source to see what could be causing that.

Step 35: Hurt Hero

The hero should take damage if the baddy touches him; the next lines remove one heart and play the hurt sound. An alert is called when the hero is out of health.

Step 36: Kill Baddy

You can kill a baddy by jumping on it. When this happens the score increases.

Step 37: Level Complete

The level ends when the hero reaches the door. A sound will be played and an alert will be called; you can see the alert code in the next step.

Step 38: Alert

This function will stop the game and show the game over message, and also adds a mouse listener to reset the game when clicked.

Step 39: Restart

This code will reload the SWF when the game over message is clicked, restoring all initial values and returning to the initial screen.

Step 40: Final Test

We are now ready to do a final test to our game and check that everything works as expected.


Experiment with this powerful game engine and create your own games!

I hope you liked this tutorial, thank you for reading!

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Game Development tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.