Define onCardClick():
function onCardClick(cardClip:MovieClip):Void {
cardClip._visible = false;
if(selected == null) {
selected = cardClip;
}
else {
if(clips[cardClip._name].id == clips[selected._name].id) {
correct += 2;
clips[cardClip._name].clip._alpha = 50;
clips[selected._name].clip._alpha = 50;
selected = null;
if(correct == linkageArr.length) {
trace("you win");
}
}
else {
setTimeout(resetclips, 1000, cardClip, selected);
selected = null;
}
}
}
The
onCardClick() function first tests whether selected is null. If so, it assigns a reference to the card that was clicked to
selected. However, if
selected is not null, it means that the user already clicked a card. In that case, the code tests whether the corresponding linkage IDs are equal for the card referenced by selected and the card that was just clicked. If they are, it means that the user has correctly selected two cards. In that case, the code adds 2 to the score, sets the
_alpha property of the two cards to 50, and resets
selected to null. If the correct variable is equal to the number of elements in
linkageArr, the user has won. In the event that the IDs aren't equal, use
setTimeout to reset the cards in one second.