In this detailed lesson made for Flash 8 and ActionScript 2.0, I will show you how to create a completely dynamic hangman game. This lesson is intended for intermediate or advanced users of ActionScript. If you are completely new to ActionScript, I suggest that you learn more about ActionScript and come back to this tutorial later. Before continuing with this lesson, you should already know some fundamental ActionScript concepts, such as variables, if/else conditional logic, for loops, function basics, paths, dynamic text fields and movie clip events.
Below is the example of the hangman game. Try it! Click play and then try to guess the hidden word by pressing on different letters.
What you will learn in this tutorial
In this tutorial, I will show you:
- How to create a simple text file from which a single value will be loaded,
- What is an XML file,
- How to create an XML file where all the game data (words) will be stored,
- How to create and use a LoadVars object to load the data from the text file,
- How to create and use an XML object in ActionScript to retrieve data from the XML file,
- How to parse the XML data and place it into an Array object,
- How to dynamically pull out a movie clip from the Library several times and display a different letter in each movie clip instance (this will create the letter buttons),
- How to create, read data from and manipulate a String object,
- How to compare two different String objects,
- How to create a function that will pick a random word that has to be guessed and much more along the way.
So, roll up your sleeves and follow my guidance. Let's go!
Preparing the environment
1 Open a new Flash document and save it in a new folder — make sure that this folder is completely empty. Only Flash files and data files (text and XML) will be stored inside it. This is a good practice, to avoid confusion. Don't save your Flash file somewhere where there are other .fla files. Keep it clean and simple.
2 Download the zip file that I prepared for you. Unpack it. You will find two files that were inside it: guessword.txt and words.xml. Place both of them in the folder where you saved your new Flash document. If you want, take a look at them now, but really there is no need — I will discuss and explain them in extensive detail later.
3 Back to your Flash document, select Modify > Document to access the Document Proprties panel. In the Dimensions section, enter 500 pixels for both width and height fields. Click OK.
Of course, if you want, you can pick any other dimensions for your Flash movie to suit your needs, but for the sake of simplicity and easier work, make them as I suggested above. These dimensions will make it easier for you to follow this tutorial and place the graphic elements accordingly.
Creating the hangman animation
4 Select the Pencil tool (Y). In the Property inspector, choose black for the color, Solid as line type and a line thickness of 5.
5 Draw a hangman, complete with gallows. Its total dimensions (top to bottom, left to right) should be about 130 pixels by 150 pixels.
Pay attention to the following: the drawing should be composed of 9 separate lines, like this:
It isn't strictly necessary to do this, but if you follow my suggestion, you will find the animation work that comes next much easier to follow. Also, if you want, you can make more parts for the hangman: the one used in this lesson is made up of nine parts, which allows the player to make nine errors before the game is over. But I will leave that up to you — decide the level of difficulty that you want to make for this game, and then draw the figure accordingly.
6 No matter how you drew your hangman, do the following now: select the whole drawing with the Selection tool (V).
7 Press F8 (or select Modify > Convert to Symbol) to make a symbol out of this drawing. In the window that shows up, select Movie clip as type, call it hanging sequence and click OK.
8 Position your new movie clip (using the arrow keys or your mouse) in the upper right part of your document.
9 Double-click on the hanging sequence movie clip with the Selection tool (V) to enter inside it. To be sure that you are really inside the movie clip symbol, take a look above the stage, it is clearly written there:
10 Right-click on frame 2 of the only (so far) layer of the movie clip and select Insert Keyframe. Your movie clip's timeline should now look like this:
11 On the keyframe that you have just created, click once on an empty part of the stage to unselect the drawing. Then, select a leg of the figure by clicking on it with the Selection tool (V), and then hit Delete or Backspace to erase it.
12 Insert a new Keyframe in frame 3 of the current layer, just like you did in step 10.
13 Repeat the procedure you did in step 11, this time erasing the second leg of the character.
14 Repeat this procedure, until you have 6 new keyframes, meaning 9 keyframes in total. On each new keyframe you should erase a part of the drawing. The image below shows how the drawing should look like in each of the new keyframes.
15 Once you have finished the previous step, select all the nine keyframes.
16 Now, right-click on any of the selected (blackened) keyframes and select Reverse Frames from the menu that shows up. Apparently, nothing has changed. But wait just a little more, you will see why this was done.
17 With all the keyframes still selected, click on them and move them forward by 1 frame, like the sequence of images below shows:
Ok, let me explain you now why all of this had to be done. When a player begins playing the game, the drawing must not be visible at all, because no mistake has been made yet. And when (if) the first mistake is going to happen, the first part of the drawing — the vertical line of gallows — has to appear.
And that's why you had to reverse the order of the keyframes — you began by drawing the full image, on the first keyframe. That was done because it is much, much easier to create the complete drawing first and then just erase it part by part, until you arrived at a single line. Imagine drawing the entire thing frame by frame: it is a waste of time. By doing as I had laid the procedure out, you save time and just click and delete as you go along.
18 Lock this layer (you are still inside the movie clip, remember) and call it sequence. Make a new layer above it and call it actions.
19 Click on the first (and only) keyframe of the actions layer to select it.
20 Select Window > Actions (shortcut key: F9) to open the Actions panel. Insert the following line of code inside:
stop() action will prevent this movie clip from going forward. As you know, all the animations in Flash loop endlessly by default, unless you tell them otherwise via ActionScript. And this movie clip should advance forward only if the player made a mistake. You will make this possible later, when the main code is going to be written.
21 Click on the Scene 1 link above the layers to return to the main scene.
22 The hanging sequence movie clip which you just exited will be selected by default now. Go to the Property inspector and give it the Instance name hangman_mc.
23 Call the current layer (the only one so far) on the main scene hangman and lock it.
You shouldn't see anything inside your document right now, just the empty stage. This is perfectly ok, since the first frame of the hanging sequence movie clip is empty.
Proceed to see how to create the remaining objects in your Flash document.
Creating the dynamic text field
24 Make a new layer and call it text field.
- Choose Dynamic Text on the panel's left side.
- Select the generic _sans font type (you will find it at the top of the fonts list).
- Select a big font size, around 24.
- Pick black as color or any other if you have a specific design for your game in mind.
- Select left alignment for the text field.
- In the rendering options menu, select the Use device fonts option.
- If you don't want the player to be able to select the text in the field, leave the Selectable option turned off.
- Your text field should be of the Single line type.
Most of these options are self-explanatory. I just want to point out two options: the _sans generic font type and the Use device fonts rendering option.
The first one is a wise thing to do, because it makes the user's operating system pick the default sans serif font. Basically, this will allow for all users to see almost the same thing. If you had chosen the Arial font for example, some Mac users wouldn't see it. Likewise, if you are working on a Mac and you want to select Helvetica, most Windows users won't see this font, because it doesn't come pre-installed with the operating system. The _sans option automatically selects the default sans serif font on any system.
The Second option, for using the device fonts, means that your final SWF file intended for the Web will be much smaller in size. Had you selected any other option, like Anti-alias for animation or readability for example, you would have to select a non-generic font family and after that, embed the characters in your SWF document. Embedding makes the SWF file larger in size, because Flash must store the vector shape data of the font's characters into itself.
Sure, if you need a specific design with a specific font in mind, you may want to embed the font data into your Flash movie. If you choose this method, every single user will see the font you chose, regardless if it's installed on their system or not.
26 Click and drag out a long text field on the stage, somewhere along its vertical middle point. It should span the whole stage width. When done, press Esc on your keyboard to exit the text field.
Now, you should be aware of a property here: the number of characters that can appear in the text field. If you have made it as wide as I did, with the same characteristics such as font type an size, it should be able to display about 34 characters. Most of the words used in a hangman game don't have such a high letter count, so you should be safe with the text field that you just created.
On the other hand, if you plan to make your game include some less commonly used words, like latin animal species names, words used in science, etc, you should either make your text field (and the size of your document) wider, or pick a smaller font size. If you go for the second option, don't pick a font size that is too small, because the word and the guessed letters must be clearly visible.
27 With the dynamic text field still selected, go again to the Property inspector and give it an Instance name: call it guessWord_txt.
That's it for the text field. It's good and ready to be manipulated with via ActionScript.
Making the movie clip with a dynamic text field, for runtime manipulation
The title of this section mens the following: you will create a movie clip which will be used at runtime. At runtime means while the SWF movie is running. Instead of creating each letter button (for the player to press — to guess a letter in the word) separately on stage, you will save lots of time by making a single movie clip, and then writing some ActionScript code that will create all the buttons for the entire alphabet.
28 Lock the current layer and make a new one: call it start screen.
This layer will in fact host the starting screen for the game. It is here that you will create the movie clip with the text field, since it will be erased anyway. This movie clip is going to be stored in the Library, so it really isn't necessary to create a separate layer for it.
29 Select the Rectangle tool (R) and draw a 31 by 31 pixel borderless square on the stage. If you prefer buttons with borders, by all means do make a border. Pick any color you like.
30 Select the square and press F8 to convert it into a symbol. Pay attention to the following: select Movie clip as type and make sure to select the upper left corner for the Registration point. Enter letter button as name and click OK.
Selecting this particular registration point will make the positioning of the many letter buttons much easier later.
31 Double-click the newly made movie clip on the stage to enter inside it.
32 Once inside it, call the first layer background and lock it. Make a new layer and call it text field.
33 Choose the Text tool (T). In the Property inspector, all the options should be the same as the ones in step 25 of this tutorial, except one: the font size. Make it smaller, change it to 18.
Click and drag over the square and make a text field that covers almost all of it. Press Esc to exit the text field.
34 Go to the left side of the Property inspector and assign an Instance name to the text field: name it letter_txt.
35 Click on the Scene 1 link to go back to the main scene and timeline.
36 Delete the movie clip that you have just made from the stage. It is stored in the Library, so there is no need to have it on the scene and/or assign it an Instance name. You will pull it out dynamically, thanks to an identifier. You will see how to do just that in a moment.
37 Open the Library if it isn't open already (Window > Library). You will see your letter button movie clip nicely sitting there, along with the hanging sequence movie clip.
38 Right-click on the letter button movie clip and select Linkage from the menu.
39 The Linkage Properties window will appear. Check the Export for ActionScript option. The Export in first frame option will automatically follow. In the Identifier field write letterButton and click OK.
This movie clip is now ready to be manipulated via ActionScript, thanks to the Identifier name you gave it and the Export for ActionScript option.
The Export in first frame option means (if checked, like in this case) that this movie clip will begin to load before any other content in your SWF. This can cause problems with large, graphics-intensive movie clips, because if you happen to have a preloader in your SWF, it won't be visible until that particular movie clip has completely loaded. But there is a simple solution to that: make a SWF with just a preloader in it, which in turn loads the main SWF with the movie clip in question inside it. Here, you don't have to worry about that because the letter button movie clip's size is around 180 bytes 🙂 !
Creating the game start/game over screen
40 Still in the start screen layer, select the Rectangle tool (R). Draw a big square on the stage, the width and height of which should be about 430 pixels.
41 Convert this square into a Movie clip symbol, call it start screen and click OK. The registration point for this one isn't important.
42 Double-click on the newly made movie clip to enter inside it.
43 Inside the start screen movie clip, call the first layer (containing the square) background and lock it. Make a new layer and call it play button.
44 In this layer, create a 160 by 50 pixel rectangle, with round corners if you like, choosing any colors you like. Place it over the lowest third of the background square and center it horizontally.
45 Use the Text tool (T) to write the message "Play!" on top of it. This time, choose any font you like and make sure that you have selected Static text for your text field. This is a design element, not a dynamic one.
46 Using the Selection tool (V), select both the rectangle and the text field. Press F8 to convert it into a Movie clip — call it play it again sam or whatever you like 🙂 and click OK.
47 This movie clip will need an Instance name, so that you can tell it via ActionScript what to do when a player presses it. So do just that: name it play_mc.
48 Lock the play button layer and make a new one above it, with the name message.
49 Select the Text tool (T) again. Make sure that you have selected Dynamic text. Make the same choices for this tool as you did earlier in step 25, with one change: select the Multiline option, NOT the Single line one.
50 Create a big text field on the stage, that goes from the top of the background square, down to the play button and spans the square from side to side. Hit Esc to exit the text field. You can see the text field's blue border in the image below.
51 This text field will display the welcome message, with a piece of dynamic data inside it, so it needs an Instance name. Call it message_txt.
52 Go back to the main scene and give the start screen movie clip the Instance name startScreen_mc.
53 Lock the start screen layer.
Fine! That's it concerning the graphical elements of the hangman game. Whew! 🙂 What follows is pure ActionScript. Go the the next page, to see how to load data into your SWF file using the LoadVars object.