Using MovieClip(root) in ActionScript 3

Earl Burdeos picture Earl Burdeos · Oct 11, 2013 · Viewed 8.3k times · Source

im at frame 3.. i have text field on the stage name scoreTxt.. at frame 3 i added TryClass..

 var Try:TryClass = new TryClass();

TryClass has function of updateScore.. this is working fine if im on frame 3. so my code is

     public function updateScore(amount:int):void
     {
           score += amount;
           if(score < 0) score = 0;

           realNumber = score;

           setInterval(updateDisplayedScore, 10);

     }

     public function updateDisplayedScore():void
           {
               displayedNumber += Math.round((realNumber-displayedNumber)/5);
               if (realNumber - displayedNumber < 5 && realNumber - displayedNumber > -5)
               {
                     displayedNumber = realNumber;
               }
               addZeros();
           }

     public function addZeros():void
           {
               var str:String = displayedNumber.toString();
               MovieClip(root).scoreNa.text = str;
           }

but then if for example .. the user died or he reaches the required score.. im suppose to go a certain frame using this code..

MovieClip(this.root).gotoAndStop("Main"); this code is on the class..

its reaching the frame "Main" but its pointing errors to this -->

MovieClip(root).scoreTxt.text

that "Main" frame is on frame 4.. which i did not yet added the TryClass.. should i add to all my frames the TryClass? and how is that?

Sorry for the question.. i dont know yet how to perfectly code in the class.. and accessing the timelines and other external class.. Please dont use deeper language of actionscript.. on a beginner way only..

here is the full error message when i go to the frame "Main"

TypeError: Error #1009: Cannot access a property or method of a null object reference.
          atTumba/addZeros()[C:\Documents and Settings\Chrissan\Desktop\Game and Docs\Game\Tumba.as:686]
          atTumba/updateDisplayedScore()[C:\Documents and Settings\Chrissan\Desktop\Game and Docs\Game\Tumba.as:680]
          atFunction/http://adobe.com/AS3/2006/builtin::apply()
          atSetIntervalTimer/onTimer()
          atflash.utils::Timer/_timerDispatch()
          atflash.utils::Timer/tick()

this is the line 686 of Tumba.as - MovieClip(root).scoreNa.text = str;

public function updateDisplayedScore():void
               {
                   displayedNumber += Math.round((realNumber-displayedNumber)/5);
                   if (realNumber - displayedNumber < 5 && realNumber - displayedNumber > -5)
                   {
                         displayedNumber = realNumber;
                   }
                   addZeros(); -->> this is the line 680 of Tumba.as
               }

about the setInterval sir.. its working fine cause i imported the flash.utils.* ..its working fine on frame 3 which i added the class.. but on "Main" fram. it isnt..

Answer

Zoron19 picture Zoron19 · Oct 15, 2013

Try using (root as MovieClip) instead of MovieClip(root)