package { import flash.display.*; import flash.events.*; import flash.utils.Timer; public class deck extends MovieClip{ var multiplier; var startDeck = new Array(); var theStack = new Array(); var deckRows:uint = 4; var deckCols:uint = 5; private var firstCard:card; private var secondCard:card; private var flipBackTimer:Timer; //Constructor receives the multiple method. public function deck(myMultiple) { // assign multiplier multiplier = myMultiple; //Event Listener for flipped card comparisons this.addEventListener (MouseEvent.CLICK, dog); // fill start deck's array with twenty cards for (var i:int=1; i<11;i++) { var myTotal = i*myMultiple var myQ:String = i+" X "+myMultiple; var myA:String = myTotal; var myQcard = new card(myQ, myTotal); var myAcard = new card(myA, myTotal); this.startDeck.push(myQcard); this.startDeck.push(myAcard); } // Shuffle cards and set to theStack while (startDeck.length>0) { var r:int = Math.floor(Math.random()*startDeck.length); theStack.push(startDeck[r]); startDeck.splice(r,1); } this.deal(); } public function returnCards(event:TimerEvent) { firstCard.startFlip(); secondCard.startFlip(); firstCard = null; secondCard = null; flipBackTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, returnCards); } //Game Logic Goes Here public function compareCards() { if (firstCard.total == secondCard.total) { firstCard.suicide(); secondCard.suicide(); firstCard = null; secondCard = null; } else { flipBackTimer = new Timer(1500,1); flipBackTimer.addEventListener(TimerEvent.TIMER_COMPLETE, returnCards); flipBackTimer.start(); } } //testing the click public function dog(event:MouseEvent) { var thisCard:card = (event.target.parent as card); //what card? if (secondCard != null) { secondCard.startFlip(); firstCard.startFlip(); secondCard = null; if (thisCard.showingFace == thisCard.myBack) { firstCard = thisCard; } else { return; } } else if (thisCard.showingFace == thisCard.myBack) { if (firstCard == null) { firstCard = thisCard; trace (firstCard.total); trace ("First Card Total = "+firstCard.total); return; } else if (secondCard == null) { secondCard = thisCard; trace ("Second Card Total = "+secondCard.total); this.compareCards(); return } } } //The following loop deals out the random deck of cards public function deal() { for (var i:int=0; i<5;i++) { for (var j:int=0; j<4;j++) { theStack[(4*i+j)].x = (80 + (160*i)); theStack[(4*i+j)].y = (75 + (150*j)); addChild(theStack[(4*i+j)]); } } } } }