Game Development Reference
In-Depth Information
for (i = 0; i < l; i++) {
char = answer[i];
if (char == ' ' || char == '&') {
continue;
}
box = stage.getChildByName('box_' + i);
if (box.key == letter) {
lettersNeeded--;
match = true;
txtClone= txt.clone();
txtClone.color = "#000";
txtClone.x = box.x;
txtClone.y = box.y;
stage.addChild(txtClone);
}
}
stage.removeChild(txt);
if (!match) {
lives--;
livesTxt.text = "LIVES: " + lives;
}
}
function checkGame() {
if (lettersNeeded == 0) {
win = true;
gameOver();
}
else if (lives == 0) {
win = false;
gameOver();
}
}
A reference to the button's corresponding text object is passed to checkForMatches , and its text value will be used
to check for matches in the puzzle. You then create a variable defaulted to false that will be updated to true if you
find any matches in the function's loop. This loop is set up to iterate through each letter in the answer while checking
for a match in each puzzle box. If that character is either a space or a symbol used for formatting, you ignore it and
continue on with the loop. Using your iterator, you can reference each box in the puzzle and the keys you assigned to
them.
If you find a match, you first subtract the amount of letters needed to win the game and set the match variable to
true . You need to then display this letter on the appropriate box to indicate that a match was made and further reveal
the secret phrase (see Figure 3-9 ). The Text object has a clone method that will create a new text instance, using all of
the same properties assigned to it. This is much faster than having to create a new one from scratch. After changing its
color and coordinates, it's added to the stage in the center of the appropriate box.
 
Search WWH ::




Custom Search