A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Problem with Timer function and AS3

  1. #1
    Senior Member
    Join Date
    Feb 2004
    Posts
    782

    Problem with Timer function and AS3

    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?
    Flash Ninja Clan - Games and cartoons

  2. #2
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    It looks like you nullify firstCard and secondCard in a couple of places. 1) it's probably not necessary to do this if you're reusing those variables throughout the app and 2) you need to make sure they have a new value before their accessed again.

    If flipcard runs one time, it nullifies those values, if it runs again, they're still nullified.

  3. #3
    Senior Member
    Join Date
    Feb 2004
    Posts
    782
    I actually do need to set them to null through or blank out the values after each run.

    the clickCard function should set the values each time it runes and the flipcard one should Nullify them
    Last edited by Archbob; 08-22-2012 at 07:46 PM.
    Flash Ninja Clan - Games and cartoons

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center