|
Reusing actions: How to emulate functions in ActionScript
This slot machine game was built in Flash 4 using actions that emulate functions.
ActionScript gives you the ability to execute an action several times throughout your movie without having to write the code more than once. In programming languages, this type of code module is known as a function or subroutine.
In ActionScript, you can emulate functions by assigning actions to labeled frames, and then executing those actions elsewhere with a Call statement.
In our slot machine game, each of the three major actions is executed multiple times, but is defined only once. This strategy makes it much easier to create and troubleshoot complex movies. The less code there is to write, the less opportunity there is to make a mistake. Using less code also results in a smaller movie file.
The slot machine game also features multiple instances of nested movie clips. Like reusing actions, reusing movie clips lets you work more efficiently and reduces the size of your Flash movie. The slot machine game weighs in at a tiny 17k.
To play the game, click the red button. Watch what happens when you get three of a kind.
To view this demo, you need the latest version of the Macromedia Flash Player.
Download the free Macromedia Flash Player now.
To see how the slot machine was put together, download the source file, open it in Flash, and follow the steps in this article. Optionally, you can create your own artwork and file as you work through the steps, using the source file as a reference. To make the best use of this article, you should understand the basic principles of drawing, using layers, creating motion tweens, and adding actions to objects and frames in Flash.
Download the Windows source file slots.zip (168K)
Download the Macintosh source file slots.sea.hqx (360K)
In the steps below, we examine the major components of the source file: the actions and movie clips.
- Step 1: Build the Blur Loop movie clips
- Step 2: Build the Slot Reel movie clip
- Step 3: Create instances of the Slot Reel movie clip
- Step 4: Assign the Initialize actions
- Step 5: Assign the Randomize actions
- Step 6: Assign the Compare actions
- Step 7: Assign actions to the Pull Button
|