Additional feature to the match game
So I just got done with the Match game part of the book and one thing that I wanted to add, to dabble a litle, was a timer that would turn the cards over automatically if a match was not found. However, the second card never turns face up. The game just pauses for a second, like its supposed to, and then the first card is turned back face down. Also, the trace statement that outputs what card was clicked doesn't execute until the first card is turned back face down. Below is the code that I've got with the additions that I made to the book code being in bold. Anyone have any thoughts on how to get this working? Thanks!
public function clickCard(event:MouseEvent){
var thisCard:Card = (event.currentTarget as Card);
trace(thisCard.cardFace); // test which card is selected
if (firstCard == null){ // first card in a pair
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardFace + 2); // turn it over
} else if (firstCard == thisCard) { // clicked the first card again
firstCard.gotoAndStop(1); // turn back over
firstCard = null; // starting back over in state 1
} else if (secondCard == null) { // second card in pair
secondCard = thisCard;
secondCard.gotoAndStop(thisCard.cardFace + 2); // turn it over
// compare the two cards to see if they match
if (firstCard.cardFace == secondCard.cardFace){
//remove the match
removeChild(firstCard);
removeChild(secondCard);
// reset selection
firstCard = null;
secondCard = null;
} else { // the two cards didn't match
// setup our timer for delay
var lastTime:int = 0;
lastTime = getTimer(); // time since the Flash player started
var timeWaited:int = 0;
// makes the game pause for 1 second before turning over
do{
timeWaited = getTimer() - lastTime;
} while (timeWaited < 1000);
firstCard.gotoAndStop(1);
secondCard.gotoAndStop(1);
firstCard = null;
secondCard = null;
}
} else {
//reset the previous pair
firstCard.gotoAndStop(1);
secondCard.gotoAndStop(1);
secondCard = null;
// select first card in the next pair
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardFace + 2);
}
} // end clickCard method
Copyright Gary Rosenzweig
