// Hangman Game Version 1 // // This version waits for the player to deselect the letter box // text field after typing in it before reading it, using FOCUS_OUT // It also detects when the user selects the letter box // using FOCUS_IN, and erases any text in the box // // In this version, the body parts and replay button are dragged // on the stage and placed to the side where they're not visible // They are then moved to be visible when needed // // At the end of the game, if the user presses the replay button, // the body parts and replay button are moved out of sight and the // text fields and game variables are reinitialized package { import flash.display.*; import flash.events.*; import flash.ui.*; public class HangmanV1 extends MovieClip { // GLOBAL VARIABLES SECTION var score: int; // Number of wrong guesses allowed // List of words, from which a random one // will be chosen for the user to guess var wordList: Array = [ "aardvark", "basket", "swift", "quark", "silver", "zeppelin", "kangaroo" ]; // Variable to hold word that user will guess var word: String; // Variable to hold same number of dashes as number of // letters in word. Will gradually replacen dashes with // correct letters guessed by user var wordSoFar: String; // Array to hold the body parts added dynamically via addChild // so that the variable number of parts added during a game // can be removed once it's over by going through the array var alienParts: Array = new Array( ); // CONSTRUCTOR FUNCTION function HangmanV1 ( ) { // Initialize game variables and text fields initializeGame( ); // Add Listener to stage for when letter box is selected letterBox.addEventListener( FocusEvent.FOCUS_IN, setToBlank ); // Add Listener to stage for when letter box is deselected letterBox.addEventListener( FocusEvent.FOCUS_OUT, readLetter ); // Add listener for replay button to let user play again replayBtn.addEventListener( MouseEvent.CLICK, playAgain ); } // end constructor // GAME INITIALIZATION FUNCTION that sets up game variables and text // To be called at the start of each new game function initializeGame( ) { // Set text boxes with initial values msgBox.text = "Next Letter: "; guessesBox.text = "Guessed Letters: "; playAgainBox.text = ""; letterBox.text = ""; letterBox.alpha = 1; // Set game variables with initial values score = 7; var randNum = Math.round( Math.random( ) * wordList.length - 1 ); word = wordList[ randNum ]; wordSoFar = createDashedWord( word ); wordBox.text = wordSoFar; } // end initializeGame // EVENT HANDLERS SECTION // Function to be called when replayButton is clicked, // after it's added to scene at end of game function playAgain( evt: MouseEvent ): void { // Initialize game variables and text fields initializeGame( ); // Move the alien body parts off stage head.x = 600; head.y = 600; shirt.x = 600; shirt.y = 600; leftArm.x = 600; leftArm.y = 600; rightArm.x = 600; rightArm.y = 600; pants.x = 600; pants.y = 600; leftShoe.x = 600; leftShoe.y = 600; rightShoe.x = 600; rightShoe.y = 600; // Move the replay button off stage replayBtn.x = 600; replayBtn.y = 600; } // end playAgain // Erases any characters in the letter box function setToBlank( evt: FocusEvent ) { letterBox.text = ""; } // end setToBlank // After user types in the letter box text field and then deselects it // by selecting anything outside of it with the mouse, // calls the function that reads the text field and does next step function readLetter( evt: FocusEvent ) { processLetter( ); } // end readLetter // Reads the letter text field and processes the letter the user typed function processLetter( ): void { var letter : String = letterBox.text; // If letter is in word, substitute it for dash(es) in word if( word.indexOf( letter ) >= 0 ) { wordSoFar = addLettersToMatchWord( wordSoFar, word, letter ); wordBox.text = wordSoFar; // If whole word has been guessed, the user has won if( removeSpaces(wordSoFar) == word ) { letterBox.alpha = 0; msgBox.text = "You've won!" playAgainBox.text = "Play again?"; // Move replay button to be visible replayBtn.x = 300; replayBtn.y = 150; } } // If letter is not in word, decrement score and add body part else { score = score - 1; guessesBox.appendText( " " + letter ); if( score == 6 ) // Add head //drawHead( ); { head.x = 200; head.y = 190; } else if( score == 5 ) // Add shirt // drawShirt( ); { shirt.x = 190; shirt.y = 240; } else if( score == 4 ) // Add left arm // drawLeftArm( ); { leftArm.x = 180; leftArm.y = 200; leftArm.rotation = -10; } else if( score == 3 ) // Add right arm // drawRightArm( ); { rightArm.x = 135; rightArm.y = 210; rightArm.rotation = -10; } else if( score == 2 ) // Add pants // drawPants( ); { pants.x = 190; pants.y = 210; } else if( score == 1 ) // Add left shoe //drawLeftShoe( ); { leftShoe.x = 215; leftShoe.y = 265; } else if( score == 0 ) // Add right shoe, game over { //drawRightShoe( ); rightShoe.x = 135; rightShoe.y = 260; letterBox.alpha = 0; msgBox.text = "Sorry, you lost"; playAgainBox.text = "Play again?"; // Move replay button to be visible replayBtn.x = 300; replayBtn.y = 150; } } } // end processLetter // STRING PROCESSING FUNCTION DEFINITIONS // Create and return a string of dashes with the same number of // dashes as the number of letters in w function createDashedWord( w: String ): String { var dashedWord: String = ""; for( var i: int = 0; i < w.length; i++ ) dashedWord = dashedWord + "_ "; return dashedWord; } // end createDashedWord // Find occurrences of char in theWord and place that letter in // same position(s) in dashedWord to replace dash(es) // Return the changed string function addLettersToMatchWord( dashedWord: String, theWord: String, char: String ): String { for (var i: int = 0; i < theWord.length; i ++ ) { if( theWord.charAt( i ) == char ) { var firstPart: String = dashedWord.substring(0,i*2); var secondPart: String = dashedWord.substring(i*2+1); dashedWord = firstPart + char + secondPart; } } return dashedWord; } // end addLettersToMatchWord // Returns s with all spaces removed function removeSpaces( s: String ) : String { var word : String = ""; var i: int; for( i = 0; i < s.length; i++ ) { var character = s.charAt(i); if( character != " " ) word = word + character; } return word; } // end removeSpaces } }