Game Development Reference
In-Depth Information
Figure 3-8. Messages and game objects added to your stage
Now that each button has been registered for a mouse click event, you need to create the handler function that
will determine what action is needed when a player chooses a letter. Listing 3-17 shows this handler function.
Listing 3-17. wordGame.js - onLetterClick Function to Handle Button Clicks
function onLetterClick(e) {
var btn = e.target;
var txt = btn.txt;
btn.removeEventListener('click', onLetterClick);
checkForMatches(txt);
checkGame();
}
A reference to both the button that was clicked and the text object that it's tied to are held in local variables. The
mouse events are immediately removed to prevent the player from clicking the same letter twice, and a few functions
(see Listing 3-18) are then called to factor the result of the latest letter choice.
Listing 3-18. wordGame.js - checkForMatches and checkGame Functions Used to Check Letter Matches and
Game Status
function checkForMatches(txt) {
var letter = txt.text
var i, char, box, txtClone;
var match = false;
var l = answer.length;
 
Search WWH ::




Custom Search