Game Development Reference
In-Depth Information
var index:int = shuffledList.push(_cardList[rand])-1;
_cardList[rand].cardNumber = Math.floor(index / 2)+1;
_cardList.splice(rand, 1);
}
_cardList = shuffledList;
}
protected function activateCards():void {
for each (var card:MemoryCard in _cardList) {
card.addEventListener(MouseEvent.CLICK,
selectCard, false, 0, true);
card.buttonMode = true;
}
}
protected function deactivateCards():void {
for each (var card:MemoryCard in _cardList) {
card.removeEventListener(MouseEvent.CLICK,
selectCard);
card.buttonMode = false;
}
}
The shuffledCards method does very much what you would
expect; it creates a new list having randomly pulled from the
original list. The two activation methods enable and disable mouse
input, respectively. This is so the game can manage user input
more easily and prevent impatient clicking from breaking the game
logic.
protected function selectCard(e:MouseEvent):void {
deactivateCards();
if (_selectedCards[0] == null) { //NO CARD SELECTED
_selectedCards[0] = e.target as MemoryCard;
flipCard(_selectedCards[0]);
} else if (_selectedCards[0] == e.target) { //SAME CARD
SELECTED
flipCard(_selectedCards[0], false);
_selectedCards[0] = null;
activateCards();
} else { //NEW CARD SELECTED
_selectedCards[1] = e.target as MemoryCard;
flipCard(e.target as MemoryCard);
}
}
protected function flipCard(card:MemoryCard, show:Boolean =
true):void {
Search Nedrilad ::




Custom Search