I’m working on the card matching game from flashgameu, in which a deck of cards with pairs of identical cards are layed out face down and the player selects pairs in an attempt to match them. (Same project as in a post from last night.)
The project is originally set up such that, when two selected cards match, they are removed from the stage. I changed this a bit – I leave the cards in place, and change their alpha to 0.25.
The problem is that simply changing the alpha value and keeping them on the stage maintains the player’s ability to click them. When clicked, they turn back over.
I want to take these matched cards “out of play” somehow while keeping them on the stage.
Here’s the code:
package {
import flash.display.MovieClip;
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.utils.getTimer;
import flash.utils.Timer;
import flash.media.Sound;
import flash.media.SoundChannel;
public class Matching10 extends MovieClip {
private static var numRows:uint = 3;
private static var numCols:uint = 4;
private static var numCards:uint = numRows * numCols;
private static var cardWidth:uint = 85;
private static var cardHeight:uint = 85;
private static var gap:uint = 10;
private static var horizSp:uint = 95;
private static var vertSp:uint = 95;
private static var horizScrOff:uint = 800 / 2 -((numCols * cardWidth) + (numCols - 1) * gap) / 2;
private static var vertScrOff:uint = 600 / 2 -((numRows * cardHeight) + (numRows - 1) * gap) / 2;
private static const matchPoints:int = 100;
private static const missPoints:int = -10;
private var scoreField:TextField;
private var score:int;
private var bestScoreField:TextField;
private var bestScore:int;
private var textStyle1:TextFormat;
private var textStyle2:TextFormat;
private var textStyleClock:TextFormat;
private var gameStartTime:uint;
private var gameTime:uint;
private var gameTimeField:TextField;
private var gameScore:uint;
private var firstCard:Card;
private var secondCard:Card;
private var cardsRemaining:uint;
private var flipBackTimer:Timer;
private var waitToFinishTimer:Timer;
var firstCardSound:FirstCardSound = new FirstCardSound();
var matchSound:MatchSound = new MatchSound();
var missSound:MissSound = new MissSound();
public function Matching10():void {
init();
}
public function init():void {
// set background
var bkgd:Sprite = new Sprite();
bkgd.graphics.beginFill(0xD8BFD8);
bkgd.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
bkgd.graphics.endFill();
addChild(bkgd);
// make a list of card numbers
var cardlist:Array = new Array();
for (var i:uint = 0; i < numCols * numRows / 2; i++) {
cardlist.push(i);
trace("cardlist 1 = " + cardlist);
cardlist.push(i);
trace("cardlist 2 = " + cardlist);
trace("cardlist.length = " + cardlist.length);
}
// add card placeholders showing only the back (first frame of Card)
cardsRemaining = 0;
for (var x:uint = 0; x < numCols; x++) {
for (var y:uint = 0; y < numRows; y++) {
var c:Card = new Card();
c.stop();
c.x = x * horizSp + horizScrOff + c.width/2;
c.y = y * vertSp + vertScrOff + c.height/2;
var s:uint = Math.floor(Math.random() * cardlist.length);// get a random card face
c.cardface = cardlist[s];// assign the face to the card
cardlist.splice(s, 1);// remove that face from the list
// c.gotoAndStop(c.cardface + 2);
c.buttonMode = true;
addChild(c);
cardsRemaining++;
c.addEventListener(MouseEvent.CLICK, clickCard);
}
}
scoreField = new TextField();
addChild(scoreField);
score = 0;
scoreField.x = 15;
scoreField.y = 15;
showGameScore();
gameTimeField = new TextField();
gameTimeField.x = 650;
gameTimeField.y = 15;
addChild(gameTimeField);
gameStartTime = getTimer();
gameTime = 0;
addEventListener(Event.ENTER_FRAME, showTime);
}
public function clickCard(event:MouseEvent):void {
var thisCard:Card = (event.target as Card);// what card?
if (firstCard == null) {
firstCard = thisCard;
thisCard.startFlip(thisCard.cardface+2); // turn card over
playSound(firstCardSound);
} else if (firstCard == thisCard) { // clicked the first card again
firstCard.startFlip(1); // turn it back over
firstCard = null;
playSound(missSound);
} else if (secondCard == null) {
secondCard = thisCard;
thisCard.startFlip(thisCard.cardface+2);
// compare first and second
if (firstCard.cardface == secondCard.cardface) {
playSound(matchSound);
firstCard.alpha = 0.25;
secondCard.alpha = 0.25;
firstCard = null;
secondCard = null;
cardsRemaining -= 2;
score += matchPoints;
showGameScore();
if (cardsRemaining == 0) {
MovieClip(root).gameScore = score;
MovieClip(root).gameTime = clockTime(gameTime);
waitToFinishTimer = new Timer(2000, 1);
waitToFinishTimer.addEventListener(TimerEvent.TIMER_COMPLETE, goToEndFrame);
waitToFinishTimer.start();
}
} else {
score += missPoints;
showGameScore();
flipBackTimer = new Timer(1000, 1);
flipBackTimer.addEventListener(TimerEvent.TIMER_COMPLETE, returnCards);
flipBackTimer.start();
playSound(missSound);
}
} else {
returnCards(null);
firstCard = thisCard; // select the first card in the next pair
firstCard.startFlip(thisCard.cardface+2);
}
}
public function returnCards(event:TimerEvent):void {
firstCard.startFlip(1);
secondCard.startFlip(1);
firstCard = null;
secondCard = null;
flipBackTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, returnCards);
}
public function startOver(event:MouseEvent):void {
MovieClip(root).gotoAndStop("start");
}
public function showGameScore():void {
scoreField.text = "SCORE: " + String(score);
textStyle2 = new TextFormat();
textStyle2.font = "Myriad Pro";
textStyle2.size = 16;
textStyle2.bold = true;
textStyle2.color = 0x0000FF;
textStyle2.align = TextFormatAlign.LEFT;
scoreField.setTextFormat(textStyle2);
bestScoreField = new TextField();
bestScoreField.text = "BEST SCORE: " + String((matchPoints * numCards / 2));
addChild(bestScoreField);
bestScoreField.x = scoreField.x;
bestScoreField.y = scoreField.y + 20;
bestScoreField.setTextFormat(textStyle2);
bestScoreField.autoSize = TextFieldAutoSize.LEFT;
}
public function showTime(event:Event):void {
gameTime = getTimer() - gameStartTime;
gameTimeField.text = "Time: " + clockTime(gameTime);
textStyleClock = new TextFormat();
textStyleClock.font = "Myriad Pro";
textStyleClock.size = 16;
textStyleClock.bold = true;
textStyleClock.color = 0x0000FF;
textStyleClock.align = TextFormatAlign.RIGHT;
gameTimeField.setTextFormat(textStyleClock);
}
public function clockTime(ms:int) {
var seconds:int = Math.floor(ms/1000);
var minutes:int = Math.floor(seconds/60);
seconds -= minutes * 60;
var timeString:String = minutes + ":" + String(seconds + 100).substr(1,2);
return timeString;
}
public function goToEndFrame(event:TimerEvent):void {
MovieClip(root).gotoAndStop("gameover");
}
public function playSound(soundObject:Object) {
var channel:SoundChannel = soundObject.play();
}
}
}
My first thought is that maybe? the card can be converted from a movieclip to a simple graphic? A bitmap? I have no experience with this so I don’t know if it’s possible. Perhaps the answer is much simpler. But I’m at a loss.
Thanks,
Tim