Game Development Reference
In-Depth Information
function evalCardsFlipped() {
if (cardsFlipped[0].key === cardsFlipped[1].key) {
matches++;
evalGame();
}
else {
setTimeout(resetFlippedCards, 1000);
}
}
Flipping a card is simple. As previously mentioned, the card is revealed by simply hiding its top-most graphic,
which was named
back
. A conditional is first set to see if there are currently two cards already being evaluated; if not,
the mouse events are immediately disabled on the card that was clicked. This prevents clicking the same card twice.
Figure
5-16
shows the first two cards after they have been clicked.
Figure 5-16.
Two cards after being clicked to flip over
Once two cards are flipped, the
evalCardsFlipped
function is called to check if they match. The
key
properties
on each card are used to determine this, and if a match was found, the match count is increased and the game is
evaluated with the function
evalGame
. If they don't match, a small timeout is created to reset the flipped cards.
Listing 5-28 shows these two functions,
resetCardsFlipped
and
evalGame
.