# Creating a score for a Matching Card Game

Page 1 of 1

## 1 Replies - 5294 Views - Last Post: 08 May 2010 - 05:52 PM

### #1 beebeekun

Reputation: 0
• Posts: 1
• Joined: 10-February 10

# Creating a score for a Matching Card Game

Posted 10 February 2010 - 01:52 PM

Hi, Im not that good with AS3 but I have created a Matching Cards game so far which works ok, but I would like to add a score function with +2 everytime a pair is correct, how can I do this?

Any help would be much appreciated! I'v tried searching everywhere for tutorials but I can't seem to follow them..

here is the code fo far..
```package {
import flash.display.*;
import flash.events.*;
import flash.text.TextField;

public class MatchingGame1 extends MovieClip {

private static const boardWidth:uint = 4; //number of cards
private static const boardHeight:uint = 4;
private static const cardHorizontalSpacing:Number = 70; //space between cards
private static const cardVerticalSpacing:Number = 70;
private static const boardOffsetX:Number = 185; // positioning of cards
private static const boardOffsetY:Number = 110;

private var firstCard:Card;
private var secondCard: Card;

public function MatchingGame1():void {

var cardlist:Array = new Array();
for(var i:uint=0; i<boardWidth*boardHeight/2;i++){
cardlist.push(i);
cardlist.push(i);

}

for(var x:uint=0;x<boardWidth;x++) {
for(var y:uint=0;y<boardHeight;y++){
var c:Card = new Card();
c.stop();
c.x = x*cardHorizontalSpacing+boardOffsetX;
c.y = y*cardVerticalSpacing+boardOffsetY;
var  r:uint = Math.floor(Math.random()*cardlist.length);
c.cardface = cardlist[r];
cardlist.splice(r,1);
}
}
}
public function clickCard(event:MouseEvent) {
var thisCard:Card = (event.currentTarget as Card);

if (firstCard == null) {
firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardface+2);

}else if (firstCard == thisCard) {
firstCard.gotoAndStop(1);
firstCard = null;

}else if (secondCard == null) {
secondCard = thisCard;
secondCard.gotoAndStop(thisCard.cardface+2);

if (firstCard.cardface == secondCard.cardface) {

removeChild(firstCard)
removeChild(secondCard);

firstCard = null;
secondCard = null;

}

} else {
firstCard.gotoAndStop(1);
secondCard.gotoAndStop(1);
secondCard = null;

firstCard = thisCard;
firstCard.gotoAndStop(thisCard.cardface+2);
}

}
}
}

```

This post has been edited by beebeekun: 10 February 2010 - 01:56 PM

Is This A Good Question/Topic? 0

## Replies To: Creating a score for a Matching Card Game

### #2 MattTheroux

Reputation: 0
• Posts: 9
• Joined: 08-May 10

## Re: Creating a score for a Matching Card Game

Posted 08 May 2010 - 05:52 PM

First, you need a variable to track your score.
"var score:int=0;" toward the top should suffice.
Next, when you compare your two cards and find that they match:
" else if (firstCard == thisCard) {
firstCard.gotoAndStop(1);
firstCard = null; "

...update the score.
" score +=2; "

Also, if you want to actually display your score, you'll probably need a dynamic text box from your toolbar. Name the instance something like "score_lbl" to keep it seperate from the actual score counter.

Then, you would update its display after adding to the score:
"score_lbl.text = String(score);"

That should be it.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }