Guest User

Untitled

a guest
Apr 13th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.  
  4.     import flash.display.MovieClip;
  5.     import flash.text.*;
  6.     import flash.display.*;
  7.     import flash.display.Stage;
  8.     import flash.events.MouseEvent;
  9.     import flash.events.KeyboardEvent;
  10.     import flash.events.TextEvent;
  11.     import flash.events.Event;
  12.     import flash.ui.Keyboard;
  13.     import flash.net.URLLoader;
  14.     import flash.net.URLRequest;
  15.     import flash.media.Sound;
  16.     import flash.ui.Mouse;
  17.     import flash.events.Event;
  18.     import fl.controls.Button;
  19.     import flash.sampler.NewObjectSample;
  20.  
  21.     public class Main extends MovieClip
  22.     {
  23.  
  24.         //BUTTONS\\
  25.         private var startGame_btn:MovieClip = new playgame_btn  ;
  26.         private var playAgain_btn:MovieClip = new retry_btn ;
  27.         private var button_mc:MovieClip = new MovieClip();
  28.         private var buttonsArray:Array = new Array();
  29.         private var button_mcArray:Array = new Array();
  30.  
  31.         //TEXTFIELDS\\
  32.         private var word_txt:TextField = new TextField();
  33.         private var word_font:Font = new ingame_font  ;
  34.         private var word_txtFormat:TextFormat = new TextFormat();;
  35.         private var lives_txt:TextField = new TextField();
  36.  
  37.  
  38.         //GLOBAL VARIABLES\\
  39.         private var currentAnswer:String = new String();
  40.         private var currentAnswerArray:Array = new Array();
  41.         private var answerLoader:URLLoader = new URLLoader();
  42.         private var letter_btn:Button = new Button();
  43.         private var recentInput:String = new String();
  44.         private var correctLetters:String = new String();
  45.         private var currentLetter:int = new int();
  46.         private var lives:int = new int();
  47.         private var level:int = 1;
  48.         private var wordListURL:String = "wordlist" + 1 + ".txt";
  49.  
  50.        
  51.         //This is the first thing you will see when starting up the game
  52.         public function startScreen()
  53.         {
  54.             startGame_btn.addEventListener(MouseEvent.CLICK,gameStart);
  55.             startGame_btn.x = 40;
  56.             startGame_btn.y = 40;
  57.             startGame_btn.buttonMode = true;
  58.             addChild(startGame_btn);
  59.         }
  60.  
  61.  
  62.         //This function is called by the playgame_btn
  63.         public function gameStart(event:MouseEvent)
  64.         {
  65.  
  66.             trace("GAME STARTED");
  67.             removeChild(startGame_btn);
  68.             wordImporter();
  69.  
  70.  
  71.  
  72.  
  73.         }
  74.  
  75.         /*This loads the answers wich are in a external text file wordlist.txt*/
  76.         public function wordImporter()
  77.         {
  78.             var answerLoader:URLLoader = new URLLoader  ;
  79.             answerLoader.addEventListener(Event.COMPLETE,afterLoad);
  80.             answerLoader.load(new URLRequest(wordListURL));
  81.             trace("answers are being loaded");
  82.         }
  83.  
  84.         /*After the text file is loaded the answer is randomly chosen */
  85.         public function afterLoad(event:Event):void
  86.         {
  87.             trace("answers are loaded");
  88.             trace("picking answer");
  89.             var words:String = new String  ;
  90.             var answers:Array = new Array  ;
  91.             words = event.target.data;
  92.             words = words.toLowerCase();
  93.             answers = words.split(",");
  94.             var randomnu:int = Math.random() * answers.length;
  95.             currentAnswer = answers[randomnu];
  96.             currentAnswerArray = currentAnswer.split("");
  97.             trace("currentAnswerArray = " + currentAnswerArray);
  98.             trace("answer has been picked");
  99.             trace(currentAnswer);
  100.             addButtons();
  101.             placeWord();
  102.             lives = 3; //the number of lives the player has
  103.             placelives()
  104.            
  105.  
  106.         }
  107.  
  108.         public function addButtons()
  109.         {
  110.  
  111.             var alphabet:String = "abcdefghijklmnopqrstuvwxyz";
  112.             var alphabetArray:Array = alphabet.split("");
  113.             var randomletter:int = new int  ;
  114.  
  115.  
  116.  
  117.             //this loop generates 25 random letters for the buttons
  118.             for (var ii:int = 0; ii < 25; ii++)
  119.             {
  120.                 randomletter = Math.random() * alphabetArray.length;
  121.                 buttonsArray[ii] = alphabetArray[randomletter];
  122.             }
  123.  
  124.             //this loop sets the letterds in current answer in the buttons
  125.             var randomButton:int = new int();
  126.             var randomStart:int =  Math.random() * 4;
  127.  
  128.             for (var u:int = 0; u < currentAnswer.length; u ++)
  129.             {
  130.                 randomButton = randomStart + u * (buttonsArray.length / currentAnswer.length);
  131.                 buttonsArray[randomButton] = currentAnswerArray[u];
  132.             }
  133.            
  134.             //this loop shuffles the buttons
  135.  
  136.             //this loop generates the 25 buttons
  137.             for (var i:int = 0; i <25; i++)
  138.             {
  139.  
  140.                 var letter_btn:Button = new Button();
  141.                 letter_btn.label = String(buttonsArray[i]);
  142.                 letter_btn.width = 40;
  143.                 letter_btn.height = 40;
  144.                 letter_btn.setStyle("buttonColor", 0xFF0000);
  145.  
  146.  
  147.                 if (i < 5)
  148.                 {
  149.                     letter_btn.y = 30;
  150.                     letter_btn.x = 50 + i * 50;
  151.                 }
  152.  
  153.                 if (i >= 5 && i < 10)
  154.                 {
  155.                     letter_btn.y = 80;
  156.  
  157.                     letter_btn.x = (i - 4) * 50;
  158.                 }
  159.                 if (i >= 10 && i < 15)
  160.                 {
  161.                     letter_btn.y = 130;
  162.  
  163.                     letter_btn.x = (i - 9) * 50;
  164.                 }
  165.                 if (i>= 15 && i < 20)
  166.                 {
  167.                     letter_btn.y = 180;
  168.                     letter_btn.x = (i - 14) * 50;
  169.                 }
  170.  
  171.                 if (i>= 20 && i < 25)
  172.                 {
  173.                     letter_btn.y = 230;
  174.                     letter_btn.x = (i - 19) * 50;
  175.                 }
  176.  
  177.  
  178.                 letter_btn.addEventListener(MouseEvent.CLICK, letterClicked);
  179.                 addChild(letter_btn);
  180.  
  181.             }
  182.         }
  183.  
  184.  
  185.         //This function places the textfield where the word is shown
  186.         public function placeWord()
  187.         {
  188.  
  189.             word_txtFormat.size = 30;
  190.             word_txtFormat.font = word_font.fontName;
  191.             word_txt.defaultTextFormat = word_txtFormat;
  192.             word_txt.width = 300;
  193.             //word_txt.border = true;
  194.             word_txt.textColor = 0x000000;
  195.  
  196.             word_txt.x = 300;
  197.             word_txt.y = 125;
  198.             word_txt.text = currentAnswer;
  199.             addChild(word_txt);
  200.             trace("Textield = " + word_txt.text);
  201.         }
  202.         public function placelives()
  203.         {
  204.  
  205.             word_txtFormat.size = 20;
  206.             word_txtFormat.font = word_font.fontName;
  207.             lives_txt.defaultTextFormat = word_txtFormat;
  208.             lives_txt.width = 30;
  209.             //word_txt.border = true;
  210.             word_txt.textColor = 0x000000;
  211.  
  212.             lives_txt.x = 500;
  213.             lives_txt.y = 300;
  214.             addChild(lives_txt);
  215.             lives_txt.text = String(lives);
  216.         }
  217.  
  218.         //This function handles the click from the buttons
  219.         public function letterClicked(event:MouseEvent)
  220.         {
  221.             recentInput = event.target.label;
  222.             event.target.x = -100;
  223.             checkAnswer();
  224.             trace("clicked: " + correctLetters + " is het woord tot nu toe");
  225.         }
  226.  
  227.         public function checkAnswer()
  228.         {
  229.             if (recentInput == currentAnswerArray[currentLetter])
  230.             {
  231.                 trace("clicked good letter");
  232.                 correctLetters = correctLetters + recentInput;
  233.                 correctLetters + recentInput;
  234.                 currentLetter++;
  235.                 word_txt.text = correctLetters;
  236.                 checkWin();
  237.                 trace(correctLetters);
  238.             }
  239.             else
  240.             {
  241.                 trace("Wrong guess");
  242.                 lives = lives - 1;
  243.                 lives_txt.text = String(lives);
  244.                 checkDead();
  245.             }
  246.         }
  247.  
  248.         public function checkDead()
  249.         {
  250.             if (lives == 0)
  251.             {
  252.                 trace("GAME OVER");
  253.                 word_txt.text = "";
  254.                 clearStage();
  255.                 playAgain_btn.addEventListener(MouseEvent.CLICK, playAgainClick)
  256.                 addChild(playAgain_btn);
  257.                
  258.                
  259.             }
  260.         }
  261.  
  262.         public function checkWin()
  263.         {
  264.             if (correctLetters == currentAnswer)
  265.             {
  266.                 trace("WOOOHOO YOU GUESSED THE WORD");
  267.                 clearStage();
  268.                 choseWordTxt();
  269.                 correctLetters = "";
  270.                 currentLetter = 0;
  271.                 wordImporter();
  272.                 level ++;
  273.                 trace ("you proceeded to level " + level);
  274.                
  275.                
  276.             }
  277.         }
  278.        
  279.         public function choseWordTxt()
  280.         {
  281.             if (level > 5)
  282.             {
  283.                 wordListURL = "wordlist" + 2 + ".txt";
  284.             }
  285.         }
  286.        
  287.         public function clearStage()
  288.         {
  289.             while (this.numChildren > 1)
  290.                 {
  291.                     this.removeChildAt(this.numChildren - 1);
  292.                     if (this.numChildren == 1)
  293.                     {
  294.                         break;
  295.                     }
  296.                 }
  297.         }
  298.        
  299.         public function playAgainClick(Event:MouseEvent)
  300.         {
  301.             wordImporter();
  302.             removeChild(playAgain_btn);
  303.         }
  304.  
  305.         public function Main()
  306.         {
  307.             startScreen();
  308.  
  309.         }
  310.  
  311.  
  312.  
  313.     }
  314.  
  315.  
  316.  
  317. }
Add Comment
Please, Sign In to add comment