Building a Hangman iPad App with Flash - Programming Gameplay

Welcome to the second part in our tutorial series on building a Hangman game for the iPad with Flash. While the first tutorial primarily focused on getting setup to code iOS apps with Flash, this tutorial will focus on the ActionScript and game logic necessary to actually program hangman. Let's get started!

Where We Left Off. . .

In the first part of this tutorial series, we went through the device provisioning process, adding an app icon and loading screen, and how to sync the app to your iPad during development. In this part of the tutorial, we will finish coding the gameplay.

Add the TweenLite Project

This tutorial will incorporate the TweenLite project, so go ahead and grab a copy. Once you have downloaded it, extract it and add the "com" folder to the same folder your hangman project is in. Finally, add the following along with the rest of your project imports:


Step 15: Adding a Guessword Text Field

Select the Text Tool, make sure the TextField is not selectable, and that the following properties are set:

  • Size: 45pt
  • Color: #0000FF
  • Align: Center

Draw a TextField out on the stage and set the following properties on it:

  • X: 98.00
  • Y: 110.00
  • W: 540.95
  • H: 54.00

Give it the instance name "guess_word_text".


Step 16: Creating the Game Buttons

Select the Oval Tool and make sure the following properties are set on it:

  • Fill Color: #000000
  • Stroke Color: #0000FF
  • Stroke: 3.00
  • Style: Solid

Hold "Shift" to conform the oval to a circle and drag out a circle onto the stage.

With the "Selection Tool" drag around the circle to select both the fill and stroke. You can tell they are both selected by a "Hatch Pattern".

With both the fill and the stroke selected give the circle the following properties:

  • W: 51.00
  • H: 51.00

Now right click on the circle and choose "Convert To Symbol". Give it the name "GameButton", make sure "Export For Actionscript" is checked and
that the class is set to "GameButton".

Now double click on it to go into editing mode. Select the Text Tool and make sure the following properties are set:

  • Color: White
  • Size: 20pt

Drag out a TextField into the MovieClip and set the following properties on it:

  • X: 11.00
  • Y: 10.00
  • W: 29.00
  • H: 28.00

Make sure the TextField is not selectable:

Give the TextField the instance name "game_btn_text".

Now Delete the Circle off the stage.

Step 17: Game Button Class

Go to File > New and choose "Actionscript File". Save this as "GameButton.as" in the same folder as your "Hangman.fla".

Enter the following code into the "GameButton.as".

Here we import the classes we need, set the game_button_text to not be selectable, set mouseChildren to false so the
TextField cannot recieve Mouse Events, added some getter and setter methods to set the text on the TextField, and a disable() function
that will disable the buttons from receiving Mouse Events. Get and set the text on the game_btn_text.


Step 18: Imports for the Main Game

Open "Main.as" which you should have available from part 1. Add the following imports to the top, underneath the package declaration:

These are the classes we will need througout this tutorial


Step 19: Creating the Variables

Enter the following code just below public function Main extends MovieClip:


Step 20: Creating the Game Buttons

Enter the following code below the public function Main() constructor:

We first declare two variables xPos and yPos. These hold the initial positions of our GameButtons. We then instantiate the
gameButtonArray
to a new Array. Next we loop through the alphabetArray, creating a new GameButton. We Check if i is equal to
8 or 16, and if it is we increment the yPos variable by 65, and reset the xPos variable to 150. We then check if i is equal
to 24, and if it is we increment yPos by 65 and set xPos to 330. This lays out our buttons in a nice grid.

We then create a GameButton and set it's text to the current letter in the alphabet array by using (alphabetArray[i]) we then
set the button's .x and .y by using xPos and yPos. We then push the button into the gameButtonArray
add it to the stage and add an EventListener to it.

Last, we increment the xPos by 60 so the next time through the loop the buttons will be staggered instead of on top of each other.


Step 21: checkLetter Function

Enter the following code below the setupGameButtons() you created above.

Here we set up a tempButton, and a theLetter variable which gets the buttons text and converts it to lowerCase. Then we trace theLetter to
the output panel.

Add the following within the public function Main().

Go ahead and test the Movie and you should see the Game Buttons nicely align on the stage, and if you click on them, the correct letter should
be traced to the output panel.


Step 22: Loading in a List of Words

We will be loading in an external .txt file that has the words used in this game, seperated by a newline character.

I downloaded a wordlist from SourceForge and am using the "variant_1-words.95". I renamed it to
"wordlist.txt".

Enter the following code below the checkLetter function you created above.

Here we set wordList to a new Array, set up a URLLoader, load the "wordlist.txt", and then setup an Event.COMPLETE listener
to the loader.


Step 23: wordListLoaded Function

Add the following below the doRequest function you created in the step above:

Add the following to public function Main().

Go ahead and test the movie again. You should see the words from "wordlist.txt" traced to the output window.


Step 24: Creating the Guess Word

We will take the original word and replace all the characters with a "?". However, some of the words contain apostrophes ('), so we will replace those
where appropriate.

Add the following below the wordListLoaded() function you created in the step above:

Since Strings are immutable in AS3, we will store each character in an array and then convert the array into a string. By using an array we can easily
change each individual character.

The first thing we do is set guessWord to a new Array. We then chose a random element from our wordList array and set theWord
equal to the random element from the wordlist, which in turn gives us a random word.

Next we use the substring method to chop off the last character of the word. While testing I noticed the words had an extra space at the end of them. This takes care of that extra space.

We then loop through theWord character by character, check if the current character is an apostrophe('), and if it is we set the current index of the guessWord
array to an apostrophe('), otherwise we set it to a "?".

Finally, we create a newGuessWord by using the arrays built in the join method which takes the elements of an array and turns them
into a string. Finally, we set the guess_word_text TextField's text to the new GuessWord.


Step 25: Reset Game

The resetGame() function is where we will reset all of our game elements for a new game. For now we are just using it create the guessWord and setup our buttons.

Enter the following code below the createGuessWord function you created in the step above.

Change the code within public function Main() to the following.

Next add the following line to the wordListLoaded function.

Here we add the words to the wordList array using the Strings built in split() method, passing in "\n" since our words
are separated by newline chracters, then we call the resetGame function we created above.

Go ahead and test the Movie and you should see that guessWord has been replaced by "?" and "'" where appropriate.


Step 26: Drawing the Hangman Container

Our hangman is dynamically drawn using AS3's drawing API. The first thing we need to do is draw the container for the hangman. Enter the following code
below the resetGame() function:

Here we set the hangmanContainer to a new Sprite, set it's .x and .y properties, and use the drawing
API to set the lineStyle to a 3px black line. Next we draw a rectangle using the drawRect() method and add it to the stage.

Add the following to the resetGame() function:

We first check to make sure hangmanContainer does not equal null. If it does not, then we remove it from the stage and set it to null, then we
use addHangmanContainer to add it to the stage.

Test the movie and you should see the hangman container positioned nicely above the buttons.

Step 27: drawHangman() Function

The drawHangman() function will be used to draw the individual hangman pieces as the game progresses. Enter the following code below the
addHangmanContainer() function:

This function will take an int as a parameter and use a switch statement to decide which part of the hangman to draw.

Step 28: drawGallows() Function

The following code uses the drawing API to draw the gallows inside the hangmanContainer sprite. Add the following below the drawHangman() function.

Step 29: drawHead() Function

This code is used to draw the head of the hangman.Add the following code below the drawGallows() function.

Step 30: drawBody() Function

This code draws the body of the hangman. Add the following code belows the drawHead() function:

Step 31: drawArm Functions

These two functions are used to draw the left and right arms. Add the following functions below the drawBody() function:

Step 32: drawLeg Functions

These two functions are used to draw the left and right legs. Add the following functions below the drawArm2() function.

Step 33: Creating the You Win Movie Clip

Go to Insert > New Symbol. Give it the name YouWin and make sure "Export for ActionScript" is checked and that the class is set to "YouWin"

Select the Text Tool and make sure the following properties are set:

  • Size: 14pt
  • Color: #00FF00

Drag out a TextField into the MovieClip and enter the words "YOU WIN!!" into it.

Next set the following properties on the TextField.

  • X; -49.00
  • Y: -8.00
  • W: 104.00
  • H: 19.00

We need to create a variable to hold our YouWin MovieClip so enter the following at the bottom of your variable declarations:

Step 34: Creating the You Lose Movie Clip

Go to Insert > New Symbol. Give it the name YouLose and make sure "Export for ActionScript" is checked and that the class is set to "YouLose".

Select the Text Tool and make sure the following properties are set.

  • Size: 14pt
  • Color: #FF0000

Drag out a TextField into the MovieClip and enter the words "YOU LOSE!!" into it.

Set the following properties in the TextField

  • X; -49.00
  • Y: -8.00
  • W: 104.00
  • H: 19.00

We need a variable to hold our YouLose MovieClip, so enter the following at the bottom of your variable declaration.

Step 35: disableButtons() Function

Add the following code below the drawLeg2() function.

This code simply loops through the gameButtonArray and disables all the buttons.

Step 36: Finishing checkLetter()

In this step we will finish off the checkLetter() function which is where the heart of the game lies. Enter the following code
inside the checkLetter() function you created earlier.

The tempButton and theLetter variables were explained in the previous steps. The correctGuess variable is set to
false at first, if the user chose a correct letter we set this to true. The newGuessWord is a temporary variable we will use to construct the new word to display.

Next, we remove the EventListener from the button and remove it from the stage.

We loop through theWord character by character and check if the current character in theWord is equal to theLetter, and
if it is, we set the appropriate letter in the guessWord array to theLetter.

We then use the join method of the String Class to turn our guessWord array into a String (newGuessWord), and set the guess_word_text to newGuessWord.

We check if correctGuess is false, and if it is we increment numWrong and call our drawHangman function passing in numWrong.

We then check if newGuessWordM is equal to theWord and if it is, we disable the buttons, instantiate our youwin MovieClip, set its .x
and .y, add it to the stage and call TweenLite to animate it with an onComplete function.

Lastly, we check if numWrong is equal to six and if it is, we loop through theWord and set every letter in the guessWord
array equal to the current character in theWord. We then use join as explained above and show the word. We disable the buttons, instantiate our youLose MovieClip,
add it to the stage and once again call TweenLite to animate with an onComplete function.

Step 37: resetTheGame() Function

The resetTheGame() functions are called when the animations of the youWin and youLose MovieClips are finished. Enter the following code
below the drawLeg2() function:

This function checks whether the youWin and youLose are not null, and if they are not we call removeChild to remove
them from the stage and set them to null. Finally we call the resetGame function.

Step 38: Finishing resetGame()

We need to do some final cleanup in the resetGame function. Add the following code to the resetGame() function:

We first reset numWrong to 0, then we loop through the gameButtonArray, check if the buttons are on the stage, and if they
are we remove them.

Step 39: Adding the WordList

You will need to include the "wordlist.txt" to the included files. So go to the "AIR for IOS" settings and on the "General" tab click on the "+" button
and browse to the "wordlist.txt" adding it to the included files.

This completes our game! The next step is to publish for the App Store, and upload through iTunes Connect.

Step 40: Preparing for the App Store

You will first need to generate a "Distribution" Provisioning Profile. Follow the steps in Step 4 of the first part of the tutorial, but
make sure you are under "Distribution" instead of development.

Next you will need to download the "Distribution" certificate and convert it to a .P12 following the example from Step 5 of the first part of this series.

Then, go back to the "Air For IOS" and under the "DEPLOYMENT" tab, swap out the devlopement .P12 with the distribution .P12, and swap out the devloper provisioning file
with the distributuion profile, make sure you tick off "Deployment - Apple App Store" and click publish.

Lastly, log into iTunes Connect, Go to "Manage Applications" and add a new App, follow the instructions, the upload your app with "App Loader".

Conclusion

This series gave a good introduction to Mobile Development with Flash CS5.5. Hopefully you've learned something useful and thank you for reading! If you want to see more Flash CS 5.5 iOS development content, leave a comment below!

Tags:

Comments

Related Articles