Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import flash.display.MovieClip;
- import flash.text.*;
- import flash.display.*;
- import flash.display.Stage;
- import flash.events.MouseEvent;
- import flash.events.KeyboardEvent;
- import flash.events.TextEvent;
- import flash.events.Event;
- import flash.ui.Keyboard;
- import flash.net.URLLoader;
- import flash.net.URLRequest;
- import flash.media.Sound;
- import flash.ui.Mouse;
- import flash.events.Event;
- import fl.controls.Button;
- import flash.sampler.NewObjectSample;
- public class Main extends MovieClip
- {
- //BUTTONS\\
- private var startGame_btn:MovieClip = new playgame_btn ;
- private var playAgain_btn:MovieClip = new retry_btn ;
- private var button_mc:MovieClip = new MovieClip();
- private var buttonsArray:Array = new Array();
- private var button_mcArray:Array = new Array();
- //TEXTFIELDS\\
- private var word_txt:TextField = new TextField();
- private var word_font:Font = new ingame_font ;
- private var word_txtFormat:TextFormat = new TextFormat();;
- private var lives_txt:TextField = new TextField();
- //GLOBAL VARIABLES\\
- private var currentAnswer:String = new String();
- private var currentAnswerArray:Array = new Array();
- private var answerLoader:URLLoader = new URLLoader();
- private var letter_btn:Button = new Button();
- private var recentInput:String = new String();
- private var correctLetters:String = new String();
- private var currentLetter:int = new int();
- private var lives:int = new int();
- private var level:int = 1;
- private var wordListURL:String = "wordlist" + 1 + ".txt";
- //This is the first thing you will see when starting up the game
- public function startScreen()
- {
- startGame_btn.addEventListener(MouseEvent.CLICK,gameStart);
- startGame_btn.x = 40;
- startGame_btn.y = 40;
- startGame_btn.buttonMode = true;
- addChild(startGame_btn);
- }
- //This function is called by the playgame_btn
- public function gameStart(event:MouseEvent)
- {
- trace("GAME STARTED");
- removeChild(startGame_btn);
- wordImporter();
- }
- /*This loads the answers wich are in a external text file wordlist.txt*/
- public function wordImporter()
- {
- var answerLoader:URLLoader = new URLLoader ;
- answerLoader.addEventListener(Event.COMPLETE,afterLoad);
- answerLoader.load(new URLRequest(wordListURL));
- trace("answers are being loaded");
- }
- /*After the text file is loaded the answer is randomly chosen */
- public function afterLoad(event:Event):void
- {
- trace("answers are loaded");
- trace("picking answer");
- var words:String = new String ;
- var answers:Array = new Array ;
- words = event.target.data;
- words = words.toLowerCase();
- answers = words.split(",");
- var randomnu:int = Math.random() * answers.length;
- currentAnswer = answers[randomnu];
- currentAnswerArray = currentAnswer.split("");
- trace("currentAnswerArray = " + currentAnswerArray);
- trace("answer has been picked");
- trace(currentAnswer);
- addButtons();
- placeWord();
- lives = 3; //the number of lives the player has
- placelives()
- }
- public function addButtons()
- {
- var alphabet:String = "abcdefghijklmnopqrstuvwxyz";
- var alphabetArray:Array = alphabet.split("");
- var randomletter:int = new int ;
- //this loop generates 25 random letters for the buttons
- for (var ii:int = 0; ii < 25; ii++)
- {
- randomletter = Math.random() * alphabetArray.length;
- buttonsArray[ii] = alphabetArray[randomletter];
- }
- //this loop sets the letterds in current answer in the buttons
- var randomButton:int = new int();
- var randomStart:int = Math.random() * 4;
- for (var u:int = 0; u < currentAnswer.length; u ++)
- {
- randomButton = randomStart + u * (buttonsArray.length / currentAnswer.length);
- buttonsArray[randomButton] = currentAnswerArray[u];
- }
- //this loop shuffles the buttons
- //this loop generates the 25 buttons
- for (var i:int = 0; i <25; i++)
- {
- var letter_btn:Button = new Button();
- letter_btn.label = String(buttonsArray[i]);
- letter_btn.width = 40;
- letter_btn.height = 40;
- letter_btn.setStyle("buttonColor", 0xFF0000);
- if (i < 5)
- {
- letter_btn.y = 30;
- letter_btn.x = 50 + i * 50;
- }
- if (i >= 5 && i < 10)
- {
- letter_btn.y = 80;
- letter_btn.x = (i - 4) * 50;
- }
- if (i >= 10 && i < 15)
- {
- letter_btn.y = 130;
- letter_btn.x = (i - 9) * 50;
- }
- if (i>= 15 && i < 20)
- {
- letter_btn.y = 180;
- letter_btn.x = (i - 14) * 50;
- }
- if (i>= 20 && i < 25)
- {
- letter_btn.y = 230;
- letter_btn.x = (i - 19) * 50;
- }
- letter_btn.addEventListener(MouseEvent.CLICK, letterClicked);
- addChild(letter_btn);
- }
- }
- //This function places the textfield where the word is shown
- public function placeWord()
- {
- word_txtFormat.size = 30;
- word_txtFormat.font = word_font.fontName;
- word_txt.defaultTextFormat = word_txtFormat;
- word_txt.width = 300;
- //word_txt.border = true;
- word_txt.textColor = 0x000000;
- word_txt.x = 300;
- word_txt.y = 125;
- word_txt.text = currentAnswer;
- addChild(word_txt);
- trace("Textield = " + word_txt.text);
- }
- public function placelives()
- {
- word_txtFormat.size = 20;
- word_txtFormat.font = word_font.fontName;
- lives_txt.defaultTextFormat = word_txtFormat;
- lives_txt.width = 30;
- //word_txt.border = true;
- word_txt.textColor = 0x000000;
- lives_txt.x = 500;
- lives_txt.y = 300;
- addChild(lives_txt);
- lives_txt.text = String(lives);
- }
- //This function handles the click from the buttons
- public function letterClicked(event:MouseEvent)
- {
- recentInput = event.target.label;
- event.target.x = -100;
- checkAnswer();
- trace("clicked: " + correctLetters + " is het woord tot nu toe");
- }
- public function checkAnswer()
- {
- if (recentInput == currentAnswerArray[currentLetter])
- {
- trace("clicked good letter");
- correctLetters = correctLetters + recentInput;
- correctLetters + recentInput;
- currentLetter++;
- word_txt.text = correctLetters;
- checkWin();
- trace(correctLetters);
- }
- else
- {
- trace("Wrong guess");
- lives = lives - 1;
- lives_txt.text = String(lives);
- checkDead();
- }
- }
- public function checkDead()
- {
- if (lives == 0)
- {
- trace("GAME OVER");
- word_txt.text = "";
- clearStage();
- playAgain_btn.addEventListener(MouseEvent.CLICK, playAgainClick)
- addChild(playAgain_btn);
- }
- }
- public function checkWin()
- {
- if (correctLetters == currentAnswer)
- {
- trace("WOOOHOO YOU GUESSED THE WORD");
- clearStage();
- choseWordTxt();
- correctLetters = "";
- currentLetter = 0;
- wordImporter();
- level ++;
- trace ("you proceeded to level " + level);
- }
- }
- public function choseWordTxt()
- {
- if (level > 5)
- {
- wordListURL = "wordlist" + 2 + ".txt";
- }
- }
- public function clearStage()
- {
- while (this.numChildren > 1)
- {
- this.removeChildAt(this.numChildren - 1);
- if (this.numChildren == 1)
- {
- break;
- }
- }
- }
- public function playAgainClick(Event:MouseEvent)
- {
- wordImporter();
- removeChild(playAgain_btn);
- }
- public function Main()
- {
- startScreen();
- }
- }
- }
Add Comment
Please, Sign In to add comment