In this exercise, we will make a Flash version of the brutal game "hangman." I'm not sure if it's PC, but it is fun. This exercise is based on the one found in Actionscript for Flash 8 published by Friends of Ed.
Hint: you could add more steps if you like, just move the "lose" keyframe and label over to make room for your additional steps.
Okay there is a lot to this script, but it all goes in frame 1 on the actions layer. I'll break it down into several discreet chunks.
wordList_array = new Array("gorilla", "elephant", "orangutan", "leopard", "cougar", "chimpanzee", "zebra", "spaghetti", "pizza", "meatloaf", "pudding", "cake", "candy", "popcorn", "caramel");
lettersNeeded_array = new Array();
lettersGuessed_array = new Array();
Feel free to change the words in the first array. Another cool thing about using arrays is you can easily add new words just by tacking them on to the list.
randomNumber = 0;
selectedWord = "";
lettersLeftToGo = 0;
foundLetter = false;
notGuessed = false;
wrong = false;
This creates six separate variables that we will make use of. The first three are given an initial value of zero or "" which means "none" or "empty." The last three are set to an initial value of "false." basically we're turning everything off at the start.
interface_in._visible = false;
hangman_in.stop();
stop();
The first line hides the interface movie clip (temporarily). The second line stops the hangman sequence from playing. The third line, well, you should know what it does.
play_btn1.onRelease = function() {
hangman_in.gotoAndStop("play");
play_btn1._visible = false;
interface_in._visible = true;
interface_in.display_txt.text = "";
Note that we're not done with this handler yet, thus the missing curly-bracket. So when the button is pressed, the hangman movie clip jumps to the first frame (if it is not there already), the play button disappears, the interface appears, and the display text box in the interface is emptied of any text that might be there (from a previous round, for example).
randomNumber = Math.round(Math.random()*(wordList_array.length-1));
selectedWord = wordList_array[randomNumber];
lettersLeftToGo = selectedWord.length;
The first line generates a random number from zero to (one less than) the length of the array (so, if the array contains 15 items as ours does, it will generate a random number from 0 to 14. Why 0-14 and not 1-15? Computers start counting at zero). The second line figures out which word in our array the number corresponds to and assigns it to the variable "selectedWord." So zero would be "gorilla," 1 would be "elephant," etc. The third line determines how long the selected word is, and sets that number as the value of "lettersLeftToGo."
for (i=0; i<selectedWord.length; i++) {
lettersNeeded_array[i] = selectedWord.charAt(i);
}
This code is a loop. Loops have 3 parts in the "for" statement: 1) a variable is created and given a numeric value (in this case "i" is given the value "0"), 2) a comparison is made (is the value of "i" less than the length of the word?), and an action is prescribed (if so, add one to the value of "i"). The line that follows (in the curly-brackets) contains an action that will repeat every time line 1 loops. In this case, the "lettersNeeded" array (which we created earlier but did no fill), becomes filled with the letters that make up the chosen word. So "gorilla" would cause the loop to repeat seven times, filling the array with the letters g, o, r, i, l, l, and a.
for (i=0; i<selectedWord.length; i++) {
lettersGuessed_array[i] = "?";
interface_in.display_txt.text += "?";
}
};
Very similar to the previous one, but this time we're filling the array with nothing but question marks. When the mystery word appears on screen it will look like this : "???????" at least until the player makes a successful guess. Also very important—note the last curly bracket; this closes the statement we started back in step 4.
interface_in.enter_btn1.onRelease = function() {
wrong = true;
interface_in.display_txt.text = "";
When the enter button is pressed, a function is created that does many things. First, the variable called "wrong" is reset to "true," which assumes that the user has guessed incorrectly (we'll deal with right answers later). The third line clears out the text in the display text box (to be replaced by the updated text later on in this code).
for (i=0; i<selectedWord.length; i++) {
foundLetter = lettersNeeded_array[i] == interface_in.input_txt.text;
if (foundLetter) {
wrong = false;
lettersLeftToGo--;
lettersGuessed_array[i] = interface_in.input_txt.text;
}
interface_in.display_txt.text += lettersGuessed_array[i];
}
Okay this is a bit tricky, so stay with me. First a loop is set up to run through the following comparisons one time for every letter in the word (so a 4-letter word would loop 4 times). In line 2 it checks to see if the user has entered a correct letter in the input_txt field. If it matches the first of the lettersNeeded, then foundLetter is considered "true." If there is no match, foundLetter will remain "false." When the loop repeats, it will check the second letter, then the third, and so on.
If the user has found a correct letter, three more things happen: 1) the variable "wrong" is changed to false (because the user is right, not wrong), 2) one is subtracted from the "lettersLeftToGo" total (so, if it's a 4 letter word, we're now down to 3 letters left to go), and 3) the question mark in the text box is replaced by the appropriate letter. If the user has chosen incorrectly, the variables remain as they are, and a question mark is written in place of the letter once again.
But there is a problem...let's say one of the correct letters is "a." If the player guesses "a" all of those things happen. What if she guesses "a" again? Well, it's still a correct answer, so the "lettersLeftToGo" would drop by one again. If she kept entering "a" eventually the "lettersLeftToGo" would hit 0, and the game would end, even though she'd only guessed one correct letter. So, we need to add some code that checks to make sure that the letter guessed correctly has not been guessed before. Note the new code in red:
for (i=0; i<selectedWord.length; i++) {
foundLetter = lettersNeeded_array[i] == interface_in.input_txt.text;
notGuessed = lettersGuessed_array[i] != interface_in.input_txt.text;
if (foundLetter && notGuessed) {
wrong = false;
lettersLeftToGo--;
lettersGuessed_array[i] = interface_in.input_txt.text;
}
interface_in.display_txt.text += lettersGuessed_array[i];
}
This line checks to see if the letter guessed matches one of the letters stored in the "lettersGuessed" array. If it does not (the "!=" character means does not equal), then "notGuessed" is set to true. If it does match, "notGuessed" remain false.
Now for the "if" statement to work, both "foundLetter" AND "notGuessed" have to be true. "if (foundLetter && notGuessed)" is a compact way of writing "if ((foundLetter == true) and (notGuessed == true))". If one or both statements is false, then the "if" statement is not executed. Confused? Don't worry, it works. Trust me.
interface_in.input_txt.text = "";
if (wrong ) {
hangman_in.nextFrame();
if (hangman_in._currentFrame == 10) {
interface_in._visible = false;
play_btn1._visible = true;
}
}
If the selected letter is wrong, the hangman movie clip advances one frame. Once frame 10 is hit (the last frame in out hangman sequence), the interface disappears and the play button reappears, so that the player can play again. Note: if you added more steps to your hangman animation, you'll have to adjust the "if (hangman_in.currentFrame...)" line to reflect your total.
if (lettersLeftToGo == 0) {
hangman_in.gotoAndStop("win");
interface_in._visible = false;
play_btn1._visible = true;
}
};
If lettersToGo hits zero, that means the player has successfully guessed all of the letters, solving the puzzle. The hangman movie clip jumps to the "win" screen, the interface vanishes, add the play button appears so the user can play again.
That's it! Try it.