So I have this code:
Code:
package {
import flash.display.*;
import flash.events.*;
import flash.display.MovieClip;
import flash.utils.*;
public class MatchingGame1 extends MovieClip
{
//places some game constants
var myDelay:Timer = new Timer(1500);
private static const boardWidth:uint = 6;
private static const boardHeight:uint=6;
private static const cardHorizontalSpacing:Number=54;
private static const cardVerticalSpacing:Number=72;
private static const boardOffsetX:Number =120;
private static const boardOffsetY:Number = 45;
public var firstCard:card; //declare flip cards
public var secondCard:card;
public function MatchingGame1(): void
{
//make a list of card numbers
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); //random face
c.cardface=cardList[r];
cardList.splice(r,1); //remove face from list
//c.gotoAndStop(c.cardface+2);
c.addEventListener(MouseEvent.CLICK,clickCard);
addChild(c);
}
}
}
public function flipcard(event:TimerEvent):void
{
firstCard.gotoAndStop(1);
secondCard.gotoAndStop(1);
firstCard=null;
secondCard=null;
}
public function clickCard(event:MouseEvent)
{
var thisCard:card = (event.target as card); //card that is clicked on
if(firstCard == null)
{
firstCard=thisCard;
firstCard.gotoAndStop(thisCard.cardface+2); // turn card over
}
else if (firstCard == thisCard)
{ // clicked first card again
firstCard.gotoAndStop(1); // turn back over
firstCard = null;
}
else if(secondCard == null)
{
secondCard=thisCard; //this card is the second card flipped
secondCard.gotoAndStop(thisCard.cardface+2); //turn card over
if(firstCard.cardface == secondCard.cardface)
{
removeChild(firstCard);
removeChild(secondCard);
firstCard = null;
secondCard = null;
}
else
{
myDelay.addEventListener(TimerEvent.TIMER, flipcard);
myDelay.start();
}
}
}
}
}
There are no compile error however when I run it and test it after I click on a few cards I get:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at MatchingGame1/flipcard()[C:\as3stuff\matchingcardgame\MatchingGame1.as:47]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
Doesn't happen on the first click. Line 47 is
Code:
public function flipcard(event:TimerEvent):void
{
firstCard.gotoAndStop(1);
secondCard.gotoAndStop(1);
firstCard=null;
secondCard=null;
}
I declared firstcard and secondCard as public so I shouldn't have this problem. Any ideas?