1. Code
  2. Game Design

Create a Three Shell Game in Flash


In this tutorial, you'll learn how to design and code a Flash version of the classic street-side shell game. The main difference is, ours won't cheat!

Step 1: Brief Overview

In this tutorial we'll use a series of ActionScript classes to create a classic three shell game. The objective of the game is to follow the shell that has the hidden ball inside it, and then click it after they have been shuffled. You will be able to configure the speed and movements performed by the shells.

Step 2: Flash Document Settings

Open Flash and create a 480 pixels wide, 320 pixels tall document. Set the Frame rate to 24fps.

Step 3: Interface

A simple brown and gold interface will be used, featuring several shapes, buttons and MovieClips; continue to the next steps to build this GUI.

Step 4: Background

This background was created in Photoshop using a simple Radial Gradient from #3F2C1D to #2E180D and applying a Water Paper filter followed by a Sprayed Strokes filter. If you don't have Photoshop, you can right-click the above image and download it.

Step 5: Title

To create the Title, select the Text Tool (T) and change the color to #EAAA0C, write your title text and apply the Gold effect of my Exclusive Text FX Freebie.

Step 6: Buttons

Use again the Text Tool to create two buttons as shown in the image above. Convert them to buttons and give them descriptive instance names to easily use them later in the code. Convert the graphics in stage to a single MovieClip and name it TitleView, remember to check the Export for ActionScript box.

Step 7: Game Screen

This is the Game Screen, simple shapes are used for the Shells and Ball MovieClips, and Dynamic TextFields for the text elements. You can see the instance names in the image above.

Step 8: Credits Screen

The Credits screen will appear in front of the Title Screen, use the graphics and fonts used before to create it. Name it CreditsView and remember to check the Export for ActionScript box.

Step 9: Alert

An alert will be shown when all the bank credits are gone, it will display the game over message and instructions. Use the Rectangle Tool to create it and set its instance name to AlertView, mark the Export for ActionScript box.

Step 10: TweenMax

We'll use a different tween engine from the default included in Flash, this time it will be TweenMax as we'll need Bezier curves to perform the shells' movement animations.

You can download TweenMax from its official website.

Step 11: Set Document Class

We'll make our application interactive by using an external class called Main; add its name 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


Step 13: Class Structure

Create your basic class structure to begin writing your code.

Step 14: Required Classes

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

Step 15: Variables

These are the variables we'll use - all pretty self-explanatory:

Step 16: 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, or runs when the SWF is loaded, if belonging to the Document Class.


Step 17: Add Title View

We start by adding the TitleView from the Library to the stage.

Step 18: Add Shells Sprites to Vector

The shells in the stage are then added to the shells vector to use them later in the code.

Step 19: Start Buttons Listeners

This will add the mouse listeners to the buttons in the title view, which in turn will take us to the game or credits screen.

Step 20: Show Credits

The Credits screen is shown when the user clicks the credits button; a mouse listener is added to the full MovieClip to remove it.

Step 21: Hide Credits

When the Credits screen is clicked it will be tweened back and removed from the stage.

Step 22: Show Game View

The following lines remove the Title screen and leave the Game Screen visible. It also adds an event listener to activate the button on the stage.

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

Keep in mind that some lines have been commented as some functions haven't 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 figure out the reason.

Step 23: Place Bet

This function runs when the BetBtn is pressed. The part we are adding in this step subtracts a credit from the bank and removes the listener from the button to avoid pressing it while the shells are moving.

Step 24: Change Message

The message at the bottom will change depending on the current game state, and the following line changes that message.

Step 25: Reset Total Movements

The shells' movements are calculated by this variable; resetting its value will make the shells make the correct moves every time.

Step 26: Hide Ball

The next line of code performs the tween that hides the ball using the shell. The function that moves the shells is called when the animation is complete.

Step 27: Calculate Random Shell

This code uses a simple math function to generate a number that will be used in the Vector to select two random shells every movement.

Step 28: Hide Ball

There's really no need to animate the ball with the shell so we hide it. This function will be executed the times that the totalMoves variable indicates; we decrease that value every time until it reaches 0.

Step 29: Tween Shells

These lines of code are very important as they perform the curved path animation of the shells. They use the bezier plugin of the TweenMax library to calculate where the curve point will be.

The value is generated by calculating the middle X point between the two shells selected to shuffle; the Y value was introduced manually by testing the shells in the stage. There can't be an easy way to perform a curved tween!

Step 30: Check Moves Left

When the animation is complete, we check the moves left in our variable to determine if another shell movement should be made.

Step 31: Add Shell Mouse Listeners

If the shuffle part is complete its time to add mouse interactivity to the shells and change the message instructions.

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

Keep in mind again that some lines have been commented as some functions haven't been created yet.

Step 32: Remove Shell Listeners

Any shell the player clicks will run this function. This part will remove the shell mouse listeners to avoid unwanted behavior.

Step 33: Move Ball to Correct Position

Next we move the ball to the correct shell position and make it 'visible' (though we won't see it, because it is behind the shell).

Step 34: Give Credits and Reveal Ball

Here we test if the correct shell was clicked and add the credits to the bank. The message is also changed according to the shell clicked and finally an animation is made to reveal where the ball is.

Step 35: Check for Enough Credits

After that we check whether any bank credits are left and call an alert if not.

Step 36: Alert

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

Step 37: Restart Function

The next function will reload the SWF, restarting all variables and returning to the first screen.

Step 38: Test Game

We are now ready to test our game and see whether everything works as expected.

Step 39: Change Movements

You can change the number of shuffle movements that are performed by the shells by simply changing two lines in the code:

Change the number value on each line and the game will be automatically make the amount of shuffle movements specified!

Step 40: Change Speed

You can also change the shuffle speed!

To do it, change the following line:

This is the seconds that takes the first shell to replace the second one so the minimum value should be 0.1 (although that would be invisible!).


Use these last values to customize your game and have fun with it.

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

Looking for something to help kick start your next project?