Wednesday, 14 October 2015

Code development for IP3

To let the interaction of IP3 could be stood alone by users themselves without the interaction of the developer, an additional hinting box would be added to the main interface, specifically, below the game board. The hint for a guessing word would appear in the box when the word is randomly selected through the function. With the hints shown on the screen, the word guessing part of the game is fully finished.

First of all, a new array was made as a public variable for the latter inputting of the hints.

 public var wordArray: Array = new Array();  

After that, the existing word array was changed from
 public var words:Array =   
      [  
           "tiger",  
           "lion",  
           "deer",  
           "bear",  
           "horse",  
           "eagle"  
      ];  
to
 public var words: Array =   
      [  
           ["turtle", "I have four legs and a tail. I have no teeth. I can swim and dive underwater. I carry my house around with me. I am a..."],  
           ["bear", "I live in the woods. I'm very big and furry. I have a big nose, a little tail and four legs. I like to eat fish and berries.I am a..."],  
           ["whale", "I live in the ocean. I swim wherever I want. I sing to my family. I can breathe through a hole in the top of my head. I am a..."],  
           ["parrot", "I have a tail. I can fly. I'm covered in colorful feathers. I can whistle and I can talk. I am a..."],  
           ["horse", "I have four legs and a long tail. I eat oats and hay. I love to run fast. I let people ride on my back. I am a..."],  
           ["frog", "My skin is green and slippery. I have four legs and webbed feet. I eat bugs and little fish. I can swim under water and hop on land. I am a..."]  
      ];  

The reason for doing this is that each word would be matched from itself with its hints by calling words[x][0] and word[x][1] (x is the randomly chosen word).
*Alternative way of doing this is to create another array with only the hints, but eventually, this combining array method was used for the hinting system.

A new function called "setUpHints()" was added into the list of startGame function (underlined below)
 public function startGame() {  
           chooseWord();  
           //trace(wordArray[chosenWord]);  
           setUpInstructions();  
           setUpHints();  
           getGuess();  
      }  

Before adding the setUpHints function, a text box was added into the main stage, given the name "hintbox", which would be used as the elements to show the hints at the stage to users.


In the existing function setUpWordArray(), new variables for hints are required to be added into the function. Moreover, as the word array was changed, the existing variables also needs to be adjusted. This would ensure that the hints would show up to the stage with the matched randomly selected word.
Specifically, the code was adjusted from:

 public function setUpWordArray()  
      {  
           for(var k:int = 0; k < words.length; k++)  
           {  
                var thisWord = words[k];  
                var thisWordArray:Array = new Array();  
                for(var l:int = 0; l < thisWord.length; l++)  
                {  
                     thisWordArray.push(thisWord.charAt(l));  
                }  
                wordArray.push(thisWordArray);  
           }  
           trace(wordArray.length);  
      }  
to:
 public function setUpWordArray() {  
      for (var k: int = 0; k < words.length; k++) {  
           var thisWord = words[k][0];
           var thisHint = words[k][1];  
           var thisWordArray: Array = new Array();  
           var thisHintArray: Array = new Array();  
           var l: int = 0;  
           for (l; l < thisWord.length; l++) {  
                thisWordArray.push(thisWord.charAt(l));  
           }  
           wordArray.push(thisWordArray);  
           hintArray.push(thisHint);  
      }  
      trace(wordArray.length);  
 }  
The added and adjusted part has been highlighted by underlines. An additional line was added to push the hinting text into the hintArray which would be later pushed into the main stage.

Eventually, the setUpHint function was added:
 public function setUpHints() {  
      trace("Show hint");  
      hintbox.text = hintArray[chosenWord];  
 }  
This would push the hint to the main stage which is matched with the randomly chosen word.

No comments:

Post a Comment