6 Replies Latest reply on Aug 26, 2010 4:46 PM by peterguy1

    Card Game Output Error Message

    peterguy1 Level 1

      Hi, everyone,

       

      I'm making a card game with two shuffling decks. The cards from both decks are also supposed to flip and drag. My first deck is working fine, but in my second deck, the card won't flip. Every time I try to flip the card in the second deck, I get this output message:

       

      TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at Wicky/clickCard()

       

      I don't get any compiler errors at the moment, but I get that message every time I click on deck two. The card does drag, but it won't flip, and that error message pops up. Does anyone have an idea of where my mistake might be?

       

      I have two files, Wicky.as and Wicky.fla. Below is the code for Wicky.as. Thanks in advance for your help!

       

       

      Wicky.as

       


      package {
      import flash.display.*;
      import flash.events.*;

      public class Wicky extends MovieClip {
        // game constants
        private static const boardWidth:uint = 7;
        private static const boardHeight:uint = 7;
       
        private static const boardOffsetX:Number = 120;
        private static const boardOffsetY:Number = 45;

        private var firstCard:Card;
        private var secondCard:Card;
        private var thirdCard:Card2;
        private var fourthCard:Card2;
       
        public function Wicky():void {
         // make a list of card numbers
         var cardlist:Array = new Array();
         for(var i:uint=0;i<boardWidth*boardHeight;i++) {
          cardlist.push(i);
         
         }
         
         var cardlist1:Array = new Array();
         for(var j:uint=0;i<boardWidth*boardHeight;j++) {
          cardlist.push(j);
         
         
         }
        
         // create all the cards, position them, and assign a randomcard face to each
         for(var x:uint=0;x<boardWidth;x++) { // horizontal
          for(var y:uint=0;y<boardHeight;y++) { // vertical
           var c:Card = new Card(); // copy the movie clip
           c.stop(); // stop on first frame
          
           var r:uint = Math.floor(Math.random()*cardlist.length); // get a random face
           c.cardface = cardlist[r]; // assign face to card
           cardlist.splice(r,1); // remove face from list
           c.addEventListener(MouseEvent.CLICK,clickCard); // have it listen for clicks
           addChild(c); // show the card
          
          
           // create all the cards, position them, and assign a randomcard face to each
        
           var b:Card2 = new Card2(); // copy the movie clip
           b.stop(); // stop on first frame
          
           var s:uint = Math.floor(Math.random()*cardlist1.length); // get a random face
           b.cardface = cardlist1[s]; // assign face to card
           cardlist1.splice(s,1); // remove face from list
           b.addEventListener(MouseEvent.CLICK,clickCard); // have it listen for clicks
           b.x = 200
           b.y = 200
           addChild(b); // show the card
          
          
         // This function is called when the mouse button is pressed.
         function startDragging(event:MouseEvent):void
         {
              event.currentTarget.startDrag();
         }

          // This function is called when the mouse button is released.
          function stopDragging(event:MouseEvent):void
         {
             event.currentTarget.stopDrag();
         }

         c.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
         c.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
         b.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
         b.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
          
          }
         }
        }
       
        // player clicked on a card
        public function clickCard(event:MouseEvent) {
         var thisCard:Card = (event.target as Card); // what card?
          firstCard = thisCard; // note i
          firstCard.gotoAndStop(thisCard.cardface+2); // turn it over
        
         }
        
         public function clickCard1(event:MouseEvent) {
         var thatCard:Card2 = (event.target as Card2); // what card?
          thirdCard = thatCard; // note i
          thirdCard.gotoAndStop(thatCard.cardface+2); // turn it over
        
         }
        }
      }