// Hangman Game Version 2 // // This version waits for the player to press the enter key // before reading the letter box text field. // This version draws the body dynamically. The movie clip // body parts have been exported to Action Script with // class names, so they can be created dynamically when needed. // // At the end of the game, a replay button is added dynamically // If the user presses the replay button, the body parts and // replay button are removed dynamically and // text fields and game variables are reinitialized // // It's difficult to debug and design correctly a program that // requires objects and listeners to be removed dynamically // Recommendation: Follow the method used in Version 1 package { import flash.display.*; import flash.events.*; import flash.ui.*; public class HangmanV2 extends MovieClip { // GLOBAL VARIABlES SECTION // Dynamic Replay Button for End of Game var replayBtn: MenuButton = new MenuButton( ); 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 HangmanV2 ( ) { initializeGame( ); // Set position of replay button, to be added later replayBtn.x = 300; replayBtn.y = 150; // Add lListener to stage for when key is pressed stage.addEventListener( KeyboardEvent.KEY_DOWN, readLetter ); } // 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 { initializeGame( ); for (var i = 0; i < alienParts.length; i++ ) { removeChild( alienParts[i] ); } alienParts = [ ]; // Remove the replay button and its listener replayBtn.removeEventListener( MouseEvent.CLICK, playAgain ); removeChild( replayBtn ); } // end playAgain // Every time key is pressed, checks whether it's the Enter key // If it is, calls the function that reads the text field and does next step function readLetter( evt: KeyboardEvent ) { if( evt.keyCode == Keyboard.ENTER ) 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?"; addChild( replayBtn ); replayBtn.addEventListener( MouseEvent.CLICK, playAgain ); } } // If letter is not in word, decrement score and add body part else { score = score - 1; guessesBox.appendText( " " + letter ); if( score == 6 ) drawHead( ); else if( score == 5 ) drawShirt( ); else if( score == 4 ) drawLeftArm( ); else if( score == 3 ) drawRightArm( ); else if( score == 2 ) drawPants( ); else if( score == 1 ) drawLeftShoe( ); else if( score == 0 ) { drawRightShoe( ); letterBox.alpha = 0; msgBox.text = "Sorry, you lost"; playAgainBox.text = "Play again?"; addChild( replayBtn ); replayBtn.addEventListener( MouseEvent.CLICK, playAgain ); } } } // 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 // Dynamic Drawing Functions function drawHead( ) { var head: AlienHead = new AlienHead( ); head.x = 200; head.y = 190; addChild( head ); alienParts.push( head ); } // end drawHead function drawShirt( ) { var shirt: AlienShirt = new AlienShirt( ); shirt.x = 190; shirt.y = 240; addChildAt( shirt, 0 ); alienParts.push( shirt ); } // end drawShirt function drawLeftArm( ) { var arm: LeftArm = new LeftArm( ); arm.x = 180; arm.y = 200; arm.rotation = -10; addChildAt( arm, 0 ); alienParts.push( arm ); } // end drawLeftArm function drawRightArm( ) { var arm: RightArm = new RightArm( ); arm.x = 135; arm.y = 210; arm.rotation = -10; addChildAt( arm, 0 ); alienParts.push( arm ); } // end drawRightArm function drawPants( ) { var pants: AlienPants = new AlienPants( ); pants.x = 190; pants.y = 210; addChildAt( pants, 0 ); alienParts.push( pants ); } // end drawPants function drawLeftShoe( ) { var shoe: LeftShoe = new LeftShoe( ); shoe.x = 215; shoe.y = 265; addChild( shoe ); alienParts.push( shoe ); } // end drawLeftShoe function drawRightShoe( ) { var shoe: RightShoe = new RightShoe( ); shoe.x = 135; shoe.y = 260; addChild( shoe ); alienParts.push( shoe ); } // end drawRightShoe } }