2 Replies Latest reply on Aug 27, 2013 1:44 PM by sinious

    Building a simple TIMER

    Charine -FB buggoop

      In my word game, I have 30 Levels of letter tiles falling out of the sky.

       

      I want to control these arrays(of movie clips) with a timer, so that I can start, pause and stop it at specific times.

       

      I'm doing something wrong with the timer code. This is the error message I get:

       

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

          at WordArray/startTileTimer()

          at WordArray/activeLevelofTiles()

          at WordArray/tileGenerator()

          at WordArray/levelGenerator()

          at WordArray()

          at wordText/initWordText()

          at flash.display::DisplayObjectContainer/addChild()

          at BugGoopFSGame/startTimer()

       

      How do I fix the timer? Should I have added it as an event listener somehow?

       

      Relevant code below:

       

      package

       

      {

          import flash.display.*;

          import flash.text.TextField;

          import flash.events.Event;   

          import flash.utils.Timer;

          import flash.events.*;

          import flash.events.MouseEvent;

          import flash.media.Sound;

          import flash.media.SoundChannel;

         

          public  dynamic class WordArray extends MovieClip

              {

                     public var wordtext:wordText =  new wordText;

                      var activeWordArray:Array;

                      var activeLevelofTenWordsArray:Array;

                      var activeLevelofTilesArray:Array;

                      public var letterArray:LetterArray;

                      public var tileTimer:Timer;

                 

           

              //Movie clips of letter tiles to be used

              public var a:A = new A();

              public var f:F =  //...etc

       

              private var wordArray:WordArray;

             

              private var letterTiles:Array = [a,f,g,h,i,...etc];

                                    private var  lettersL01:Array = [a,a,f,f,f,g,g,h,h,i,i,n,n,n,o,o,o,o,s,s,t,t,t];

                                    private var  lettersL02:Array = [th,th,  //...etc

                                 

                                  public var letterLevels:Array = [lettersL01,lettersL02,lettersL03,...etc];

                 

      public var  wordsL1W1:Array = ["a"];

      public var  wordsL1W2:Array = ["h","a","s"];

      //...etc

       

      public var  wordsL01:Array = [wordsL1W1,wordsL1W2,wordsL1W3,wordsL1W4,wordsL1W5,wordsL1W6,wordsL1W7,wordsL1W8,wordsL1W 9,wordsL1W10];

      public var  wordsL02:Array = [wordsL2W1,  //...etc.

      public var wordLevels:Array = [wordsL01,wordsL02,wordsL03,...etc]

                                                     

                      private var tf:TextField;

                     

                      // ***constructor code

                      public function WordArray(_tf:TextField)

                      {   

                         tf = _tf;

                          levelGenerator();

                          var tileTimer = new Timer(500,384);

                      }

                     

                      function levelGenerator():void

                      {

                         //**To get next level of 10 words

                          if(wordLevels.length>0)

                          {

                              activeLevelofTenWords();

                              tileGenerator();

                         }

                          else

                          {

                          //Game is complete

                          }

                      }

                     

                      function activeLevelofTenWords():void

                      {

                          //**To get next word

                          activeLevelofTenWordsArray = wordLevels.shift();

                          nextWordF();

                      }

                     

                      function tileGenerator():void

                      {

                          if(letterLevels.length>0)

                          {

                              activeLevelofTiles();

                          }

                          else

                          {

                          //Game is complete

                          }

                       

                          }

                         

                          function activeLevelofTiles():void

                          {

                              activeLevelofTilesArray = letterLevels.shift();

                              shuffleTiles(activeLevelofTilesArray);

                              trace(activeLevelofTilesArray);

                              startTileTimer();

                          }

                         

                          function shuffleTiles(a:Array)

                          {

                              var p:int;

                                      var t:*;

                                      var ivar:int;

                                      for (ivar = a.length-1; ivar>=0; ivar--)

                                          {

                                          p=Math.floor((ivar+1)*Math.random());

                                          t = a[ivar];

                                          a[ivar] = a[p];

                                          a[p] = t;

                                          }

                              }

                             

                          function startTileTimer():void

                              {

                                  tileTimer.start();

       

                                  //Place the tiles to run

       

                                  /*activeLevelofTilesArray.x = 399;

                                  activeLevelofTilesArray.y= 30;

                                  addChild(activeLevelofTilesArray);*/

                                  trace("Letters should run");

                              }

                             

                     //rest of code not applicable

                      }

              }

        • 1. Re: Building a simple TIMER
          Ned Murphy Adobe Community Professional & MVP

          Declare the timer outside of any function, otherwise, by declaring it inside the WordArray function as you do its scope is limited to within that function - meaning it cannot be accessed outside the function.

          1 person found this helpful
          • 2. Re: Building a simple TIMER
            sinious Most Valuable Participant

            You created that Timer in a function which means as soon as you leave the function it no longer exists because it's not class-level. Make it class-level like some of your other vars:

             

            private var myTimer:Timer;

             

            Then yes, you attach a TimerEvent listener to it and specify a function.

             

            myTimer.addEventListener(TimerEvent.TIMER, _handleTimer);

             

            function _handleTimer(e:TimerEvent):void

            {

                 // do what you want

            }