11 Replies Latest reply: Apr 14, 2010 9:11 PM by Kartik Mehta RSS

    Error #1009: Cannot access a property or method of a null object reference.

    sergey.landar Community Member

      can anybody help please?

       

      in output i have:

      +++ Welcome screen +++
      +++ Levels screen +++
      >>> array: 0,0,1,1
      >>> random numbers: 3
      >>> random numbers: 2
      >>> random numbers: 1
      >>> random numbers: 0
      ****** Icons creationg finished ******
      Level: 1 started
      TypeError: Error #1009: Cannot access a property or method of a null object reference.
           at MyMatching/stopwatch()[D:\#_Flash_Games\MatchingThings\MyMatching.as:70]
      Icon 0 was clicked
      Icon 0 was clicked
      + added 20 points because matching of 2 icons. Now player has: 20 points
      --- deleted 2 icons ---
      Icon 1 was clicked
      Icon 1 was clicked
      + added 20 points because matching of 2 icons. Now player has: 40 points
      --- deleted 2 icons ---
      +++ Gameover Screen +++
      

       

      code:

      package 
      {
           import adobe.utils.CustomActions;
           import flash.display.MovieClip;
           import flash.events.MouseEvent;
           import flash.text.TextField;
           import flash.events.*;
           import flash.utils.getTimer;
      
           public class MyMatching extends MovieClip
           {
                public var xIconsContainer;//all icons start to draw from this x
                public var yIconsContainer;//all icons start to draw from this y
                
                private var iconsVertical=2;//icons columns
                private var iconsHorizontal=2;//icons rows
                
                private var firstIcon;//player click to first icon
                private var secondIcon;//player click to second icon
                
                private var deletedIcons:int = 0;//if firstIcon==secondIcon we will delete 2 icons
                public var numberOfLevels:int = 2;//2 for 1 level; 4 for 1, 2 levels; 6 for 1, 2, 3 levels
                
                public var startTime:int = getTimer();
                public var timeString:String;
                
                public var playerScoreSum:int = 0;//sum of all players points
                
                public var iconsList:Icon;
                public var iconsArray:Array;
                
                public var iconWidth:int = 51;//width of icon
                public var iconHeight:int = 51;//height of icon
                
                public var levelNumber:int = 1;
                
                public var matchPoints:int = 20;//if player match 2 icons we add 20 points to his playerScoreSum
                public var missPoints:int = 1;//if miss we delete 1 points from playerScoreSum
                
                //constructor
                public function MyMatching()
                {
                     welcomeScreen();
                }
                
                public function welcomeScreen():void
                {
                     trace("+++ Welcome screen +++");
                     gotoAndStop('WelcomeScreen');
                     playBtn.addEventListener(MouseEvent.CLICK, goToLevels);
                }
                
                public function startingStopwatch():void
               {
                     addEventListener(Event.ENTER_FRAME, stopwatch);
               }
                
               public function stopingStopwatch():void
               {
                     removeEventListener(Event.ENTER_FRAME, stopwatch);
               }
                
              public function stopwatch(event:Event) 
              {
                     var timePassed:int = getTimer()-startTime;
                     var seconds:int = Math.floor(timePassed/1000);
                     var minutes:int = Math.floor(seconds/60);
                     seconds -= minutes*60;
                     timeString = minutes + ":" + String(seconds + 100).substr(1, 2);
                     stopwatchPreview.text = String(timeString);
              }
                
                public function goToLevels(event:MouseEvent)
                {
                     xIconsContainer=stage.stageWidth/2 - (iconWidth*iconsHorizontal)/2;//all icons start to draw from this x
                     yIconsContainer=stage.stageHeight/2 - (iconHeight*iconsVertical)/2;//all icons start to draw from this y
                     startingStopwatch();
                     
                     trace("+++ Levels screen +++");
                     iconsArray = new Array();//creating new object iconsArray     
                     
                     gotoAndStop('Levels');
                     
                     for (var i:int; i < iconsVertical*iconsHorizontal/2; i++)
                     {
                          iconsArray.push(i);
                          iconsArray.push(i);
                     }
                     trace(">>> array: " + iconsArray);               
                     
                          
                     
                     for (var x:int = 0; x < iconsVertical; x++)
                     {
                          for (var y:int = 0; y < iconsHorizontal; y++)
                          {
                               iconsList = new Icon();
                               addChild(iconsList);
                               iconsList.buttonMode = true;//if we move cursor to icon we can see hand
                               iconsList.stop();//stoping our icons on Flash IDE, because on one layer we have all our icons
                               iconsList.x=x*iconWidth+xIconsContainer;
                               iconsList.y=y*iconHeight+yIconsContainer;
                               var myRandom:int=Math.floor(Math.random()*iconsArray.length);//creates a random number that will be related to an index of the array named "iconsArray"
                               //var showIcon;//cast as whatever type of element the array is holding
                               iconsList.showIcon=iconsArray[myRandom];//assigns the randomly selected element of iconsArray to the variable showIcon
                               iconsArray.splice(myRandom,1);//removes the randomly selected element (now showIcon) from the array (not the last one, the random one) //we use the splice command to remove number from the array so that it won’t be used again
                               //iconsList.gotoAndStop(iconsList.showIcon+2);//showIcon+2 would be the next frame in the timeline after the frame for the random item deleted, so that code is essentially moving in the timeline to the frame just after the frame for the item that was removed from the array
                               trace(">>> random numbers: " + myRandom);
                               iconsList.addEventListener(MouseEvent.CLICK, clickToIcon);
                               deletedIcons++;//when we draw icons we every time add +2 icons to our deletedIcons variable, in future we will deleted 2 icons from this variable if firstIcon==secondIcon
                          }
                     }
                     
                     //added score to the sceen
                     trace("****** Icons creationg finished ******");
                     trace("Level: " + levelNumber + " started");
                     //levelNumberPreview.text = String(levelNumber);               
                }          
                
                public function clickToIcon(event:MouseEvent)
                {
                     var thisIcon:Icon = (event.currentTarget as Icon);//what icon player clicked...
                     trace("Icon " + thisIcon.showIcon + " was clicked");//trace clicked icon to the output pannel
                     if (firstIcon==null)
                     {
                          firstIcon=thisIcon;
                          firstIcon.gotoAndStop(thisIcon.showIcon + 2);
                     } 
                     else if (firstIcon == thisIcon)
                     {
                          firstIcon.gotoAndStop(1);
                          firstIcon=null;
                     } 
                     else if (secondIcon==null)
                     {
                          secondIcon=thisIcon;
                          secondIcon.gotoAndStop(thisIcon.showIcon + 2);
                          
                          if (firstIcon.showIcon==secondIcon.showIcon)
                          {
                               playerScoreSum += matchPoints;// add matchPoints for all existing player points
                               scoreGamePreview.text = String(playerScoreSum);
                               trace("+ added " + matchPoints + " points because matching of 2 icons. " + "Now player has: " + playerScoreSum + " points");
                               
                               removeChild(firstIcon);
                               removeChild(secondIcon);
                               deletedIcons -= 2;
                               trace("--- deleted 2 icons ---");
                               if (deletedIcons == 0)
                               {
                                    if (iconsVertical >= numberOfLevels)
                                    {
                                         iconsVertical = 2;
                                         iconsHorizontal = 2;
                                         levelNumber = 1;
                                         MovieClip(root).playerScoreSum = playerScoreSum;
                                         stopingStopwatch();
                                         startTime = getTimer();
                                         gotoAndStop('GameoverScreen');
                                         trace("+++ Gameover Screen +++");
                                    }
                                    else
                                    {
                                         iconsVertical += 2;
                                         iconsHorizontal += 2;
                                         levelNumber += 1;
                                         xIconsContainer+=2;
                                         yIconsContainer+=2;
                                         goToLevels(null);
                                    }                              
                               }
                          }
                          else
                          {
                               playerScoreSum -= missPoints;// delite missPoints from all existing player points
                               scoreGamePreview.text = String(playerScoreSum);
                               trace("- deleted " + missPoints + " points because missing of 2 icons. " + "Now player has: " + playerScoreSum + " points");
                          }
                     }
                     else
                     {
                          firstIcon.gotoAndStop(1);
                          secondIcon.gotoAndStop(1);
                          firstIcon = null;
                          secondIcon = null;
                          firstIcon=thisIcon;
                          firstIcon.gotoAndStop(thisIcon.showIcon + 2);
                     }
                }
                
                
           }
      }