Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Using MovieClip(root) Problem

Community Beginner ,
Oct 11, 2013 Oct 11, 2013

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

TryClass has function of updateScore..  so my code is

public function updateScore(amount:int):void

                    {

                              score += amount;

                              if(score < 0) score = 0;

 

                              realNumber = score;

 

                              setInterval(updateDisplayedScore, 10);

                              var displayedNumber:Number = 0;

 

                              function updateDisplayedScore():void

                              {

                              displayedNumber += Math.round((realNumber-displayedNumber)/5);

                              if (realNumber - displayedNumber < 5 && realNumber - displayedNumber > -5)

                              {

                              displayedNumber = realNumber;

                              }

                              addZeros();

                              }

 

                              function addZeros():void

                              {

                              var str:String = displayedNumber.toString();

                              MovieClip(root).scoreTxt.text = str; <------ i use MovieClip(root) as i saw from internet..its working fine.. but

                              }

                    }

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");

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..

TOPICS
ActionScript
2.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 11, 2013 Oct 11, 2013

The first thing you should do is unnest those two functions and give them their own space in the class.  If you are not familiar with the term, that means do not put functions inside other functions.

If you are getting an error, you should include the full error message.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 11, 2013 Oct 11, 2013

okay Sir. I'll try first the first thing that you've said..

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 11, 2013 Oct 11, 2013

its already been un nest sir. is this correct sir?

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;

               }

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()

          atTumba/updateDisplayedScore()

          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..

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 11, 2013 Oct 11, 2013

Try adding the following lines just before line 686...

trace( "root: "+MovieClip(root));

trace( "scoreNa: " + MovieClip(root).scoreNa);

Then run the file to get the error again and see what displays in the output before the error message.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 11, 2013 Oct 11, 2013

okay.. ive tried.. when it hits the right target.. it traces

root: [object MainTimeline]

scoreNa: [object TextField]

but when i go to the next frame it still tracing but with this.. but before i gotoAndStop.. im removing the gameLoop which makes the updateScore loops..

root: null

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

          atTumba/addZeros()

          atTumba/updateDisplayedScore()

          atFunction/http://adobe.com/AS3/2006/builtin::apply()

          atSetIntervalTimer/onTimer()

          atflash.utils::Timer/_timerDispatch()

          atflash.utils::Timer/tick()

it seems when i go to "Main" frame.. the root becomes null.. and the target of MovieClip(root).scoreNa.text cannot be find cause that scoreNa.text is only at the frame 3... urrgh i really dont know..

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 11, 2013 Oct 11, 2013

I guess I am not clear on how you have things arranged and managed in the timeline.  You say you remove the gameLoop, and that might be the source of the problem, but I can't say for sure.  A 1009 error indictaes something that your code is trying to target does not exist as far as the code sees it.  Are you able to skip removing the gameLoop just to see if the error goes away?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 11, 2013 Oct 11, 2013

i skip the removing of gameloop..  after reaching the "Main" frame..

it traces ..its not anymore null.. but the scoreNa.text is null cause the scoreNa.text is at the frame 3 only? what should i do?

root: [object MainTimeline]

scoreNa: null

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

          atTumba/addZeros()

          atTumba/updateDisplayedScore()

          atFunction/http://adobe.com/AS3/2006/builtin::apply()

          atSetIntervalTimer/onTimer()

          atflash.utils::Timer/_timerDispatch()

          atflash.utils::Timer/tick()

this is my whole code of the class.. namely Tumba.as

package {

 

          import flash.events.Event;

          import flash.display.Stage;

          import flash.display.MovieClip;

          import flash.utils.Timer;

          import flash.events.TimerEvent;

          import flash.events.MouseEvent;

          import flash.media.Sound;

          import fl.transitions.*;

          import fl.transitions.easing.*;

          import flash.text.TextField;

          import flash.utils.setInterval;

 

          public class Tumba extends MovieClip

          {

                    //public var scoreNa:MovieClip = new TextField();

                    public var player:MovieClip = new HandPlayer();

                    public var myNum:MovieClip = new NumberMovie();

                    public var myNumBig:MovieClip = new NumberMovie();

 

                    public var SlipperArray:Array = new Array();

                    public var HeelsArray:Array = new Array();

                    public var ShoesArray:Array = new Array();

                    public var BakyaArray:Array = new Array();

                    public var BootsArray:Array = new Array();

 

                    public var leftRightArray:Array = new Array();

                    public var canArray:Array = new Array();

                    public var PowerUpsTimeArray:Array = new Array();

                    public var PowerUpsScoreArray:Array = new Array();

 

                    public var sec:int = 16;

                    public var powerUpstimer:Timer = new Timer(1000, sec);

                    public var ScorePowerUps:Timer = new Timer(3000);

                    public var timerPowerUps:Timer = new Timer(3000);

                    public var leftOrRightTimer:Timer = new Timer(4000);

                    public var timerGame:Timer = new Timer(100);

                    public var timerCount:int = 0;

 

 

                    public var timeMinutes:String;

                    public var timeSeconds:String;

                    public var myScoreClassic:String;

                    public var bonusClassic:String;

                    public var bonus:int;

 

                    public var canFire:Boolean;

                    public var timePower:Boolean;

                    public var endGame:Boolean = false;

 

                    public var canSound:Sound = new Can();

                    public var peopleSound:Sound = new People();

                    public var timeSound:Sound = new HitTime();

                    public var scoreSound:Sound = new HitScore();

 

 

                    public var score:int;

                    public var realNumber:Number = 00000;

                    public var displayedNumber:Number = 0;

 

                    public function Tumba()

                    {

                              timerGame.addEventListener(TimerEvent.TIMER, timerTickHandler);

                              timerGame.start();

                              leftOrRightTimer.addEventListener(TimerEvent.TIMER, addleftRight);

                              leftOrRightTimer.start();

                              addEventListener(Event.ENTER_FRAME, gameLoop);

                    }

                    public function gameLoop(event:Event):void

                    {

  SlipperLoop();

                              checkEnemiesOffTheScreen();

                              checkTimeOffTheScreen();

                              checkScoreOffTheScreen();

                    }

 

          //Slippers

                    public function SlipperLoop():void

                    {

                              slipperHitLeftOrRight();

                              slipperHitCan();

                              slipperHitScore();

                              checkSlippersOffTheScreen();

                    }

 

                    public function slipperHitCan():void

                    {

                                        for(var l:int= 0; l < SlipperArray.length; l++)

                                        {

                                                  var currentSlipper:weaponSlipper = SlipperArray;

                                                  for(var k:int= 0; k < canArray.length; k++)

                                                  {

                                                            var currentCan:canProgramLevel = canArray;

                                                            if(currentSlipper.hitTestObject(currentCan.canTimerLevel.canLevel.tipCanLevel) && currentCan.canTimerLevel.canLevel.y < 31)

                                                            {

                                                                      canSound.play();

                                                                      currentCan.canTimerLevel.gotoAndPlay(53);

                                                                      canArray.splice(k,1);

                                                                      currentSlipper.destroySlipper();

                                                                      SlipperArray.splice(l, 1);

                                                                      updateScore(100);

                                                            }

                                                  }

                                        }

                    }

 

                    public function slipperHitLeftOrRight():void{

                              for(var l:int= 0; l < SlipperArray.length; l++)

                              {

                                        var currentSlipper:weaponSlipper = SlipperArray;

                                        for(var k:int= 0; k < leftRightArray.length; k++)

                                        {

                                                  var currentleftOrRight:leftOrRight = leftRightArray;

                                                  if(currentSlipper.hitTestObject(currentleftOrRight))

                                                  {

                                                            peopleSound.play();

                                                            var newExplosion:mcExplosion = new mcExplosion();

                                                            stage.addChild(newExplosion);

                                                            newExplosion.x = currentleftOrRight.x;

                                                            newExplosion.y = currentleftOrRight.y;

 

                                                            currentSlipper.destroySlipper();

                                                            SlipperArray.splice(l, 1);

 

                                                            currentleftOrRight.destroyLeftOrRight();

                                                            leftRightArray.splice(k, 1);

 

                                                            lessLives();

                                                  }

                                        }

                              }

                    }

 

 

                    public function slipperHitScore():void

                    {

                              for(var l:int= 0; l < SlipperArray.length; l++)

                              {

                                        var currentSlipper:weaponSlipper = SlipperArray;

                                        for(var k:int= 0; k < PowerUpsScoreArray.length; k++)

                                        {

                                                  var currentPowerUpsScore:plusScore = PowerUpsScoreArray;

                                                  if(currentSlipper.hitTestObject(currentPowerUpsScore))

                                                  {

                                                            scoreSound.play();

                                                            currentSlipper.destroySlipper();

                                                            SlipperArray.splice(l, 1);

 

                                                            currentPowerUpsScore.destroyScore();

                                                            PowerUpsScoreArray.splice(k, 1);

 

                                                            updateScore(500);

                                                            bonus += 500;

                                                  }

                                        }

                              }

 

                    }

                    public function checkSlippersOffTheScreen():void

                              {

                                        for(var r:int = 0; r < SlipperArray.length; r++)

                                        {

                                                  var currentSlipper:weaponSlipper = SlipperArray;

                                                  if(currentSlipper.y < 98)

                                                  {

                                                            SlipperArray.splice(r, 1);

                                                            currentSlipper.destroySlipper();

                                                  }

                                        }

                              }

 

                    public function addleftRight(event:TimerEvent):void

                    {

                              var newleftOrRight:leftOrRight = new leftOrRight();

                              newleftOrRight.width = 150;

                              newleftOrRight.height = 186;

                              stage.addChild(newleftOrRight);

                              leftRightArray.push(newleftOrRight);

                    }

 

 

                    public function checkEnemiesOffTheScreen():void

                    {

                              for(var u:int = 0; u < leftRightArray.length; u++)

                              {

                                        var currentleftOrRight:leftOrRight = leftRightArray;

                                        if(currentleftOrRight.mDirection == "L" && currentleftOrRight.x < -(currentleftOrRight.width / 2))

                                                  {

                                                            leftRightArray.splice(u, 1);

                                                            currentleftOrRight.destroyLeftOrRight();

                                                  }

                                        else if(currentleftOrRight.mDirection == "R" && currentleftOrRight.x > (stage.stageWidth + currentleftOrRight.width / 2))

                                                            {

                                                                      leftRightArray.splice(u, 1);

                                                                      currentleftOrRight.destroyLeftOrRight();

                                                            }

 

                              }

                    }

 

                    public function checkTimeOffTheScreen():void

                    {

                              for(var u:int = 0; u < PowerUpsTimeArray.length; u++)

                              {

                                        var currentTime:PowerUpsTime = PowerUpsTimeArray;

                                        if(currentTime.mDirection == "L" && currentTime.x < -(currentTime.width / 2))

                                                  {

                                                            PowerUpsTimeArray.splice(u, 1);

                                                            currentTime.destroyTime();

                                                  }

                                        else if(currentTime.mDirection == "R" && currentTime.x > (stage.stageWidth + currentTime.width / 2))

                                                            {

                                                                      PowerUpsTimeArray.splice(u, 1);

                                                                      currentTime.destroyTime();

                                                            }

 

                              }

                    }

 

 

                    public function checkScoreOffTheScreen():void

                    {

                              for(var u:int = 0; u < PowerUpsScoreArray.length; u++)

                              {

                                        var currentPlus:plusScore = PowerUpsScoreArray;

                                        if(currentPlus.mDirection == "L" && currentPlus.x < -(currentPlus.width / 2))

                                                  {

                                                            PowerUpsScoreArray.splice(u, 1);

                                                            currentPlus.destroyScore();

                                                  }

                                        else if(currentPlus.mDirection == "R" && currentPlus.x > (stage.stageWidth + currentPlus.width / 2))

                                                            {

                                                                      PowerUpsScoreArray.splice(u, 1);

                                                                      currentPlus.destroyScore();

                                                            }

 

                              }

                    }

 

                    public function updateScore(amount:int):void

                    {

                              score += amount;

                              if(score < 0) score = 0;

 

                              setInterval(updateDisplayedScore, 10);

 

                    }

 

                    public function updateDisplayedScore():void

                              {

                                        realNumber = score;

                                        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();

                                        trace( "root: "+MovieClip(root));

                                        trace( "scoreNa: " + MovieClip(root).scoreNa);

                                        MovieClip(root).scoreNa.text = str;

                              }

 

                    public function lessLives():void

                    {

                              MovieClip(root).lives.nextFrame();

                              if(MovieClip(root).lives.currentFrame == MovieClip(root).lives.totalFrames)

                              {

                                        endGame = true;

                                        checkEndGame();

                              }

                    }

 

                    public function checkEndGame():void

                    {

                              if(endGame == true)

                              {

                                        leftOrRightTimer.stop();

                                        timerGame.stop();

                                        MovieClip(root).ScorePowerUps.stop();

                                        MovieClip(root).timerPowerUps.stop();

                                        MovieClip(root).powerUpstimer.stop();

 

                                        for each(var eenemy:leftOrRight in leftRightArray)

                                        {

                                                  eenemy.destroyLeftOrRight();

                                                  leftRightArray.splice(0,1);

                                        }

 

                                        for each(var eepower:PowerUpsTime in PowerUpsTimeArray)

                                        {

                                                  eepower.destroyTime();

                                                  PowerUpsTimeArray.splice(0,1);

                                        }

 

                                        for each(var eescore:plusScore in PowerUpsScoreArray)

                                        {

                                                  eescore.destroyScore();

                                                  PowerUpsScoreArray.splice(0,1);

                                        }

 

                                        for(var i:int = 0; i < canArray.length; i++)

                                                  {

                                                            canArray.visible = false;

                                                            canArray[1].enabled = false;

                                                  }

 

                                        if(leftRightArray.length == 0 && PowerUpsTimeArray.length ==0 && PowerUpsScoreArray.length ==0 && (SlipperArray.length == 0 || HeelsArray.length == 0 || ShoesArray.length == 0 || BakyaArray.length || BootsArray.length == 0))

                                        {

                                                  for each(var eecan:canProgramLevel in canArray)

                                                  {

                                                            eecan.destroyCan2();

                                                            canArray.splice(0,1);

                                                  }

 

                                                  //removeEventListener(Event.ENTER_FRAME, gameLoop);

                                                  captureScore();

                                                  captureBonus();

                                                  trace("REEEEEEEEEEEEEEEEEEMMMMMMMMMM");

                                                  trace( "root: "+MovieClip(root));

                                                  MovieClip(root).gotoAndStop(1);

                                                  //removeChild(this);

 

                                        }

 

                              }

                    }

 

                    //timer for game

                    public function timerTickHandler(Event:TimerEvent):void

                    {

                              timerCount += 100;

                              toTimeCode(timerCount);

                    }

 

                    public function toTimeCode(milliseconds:int) : void

                    {

                    

                              var time:Date = new Date(milliseconds);

                              var minutes:String = String(time.minutes);

                              var seconds:String = String(time.seconds);

                              var miliseconds:String = String(Math.round(time.milliseconds)/100);

 

                              minutes = (minutes.length != 2) ? '0'+minutes : minutes;

                              seconds = (seconds.length != 2) ? '0'+seconds : seconds;

 

                              trace(minutes + ":" + seconds+"." + miliseconds);

                              timeMinutes = minutes;

                              timeSeconds = seconds;

                    }

 

                    public function captureScore():void {

                       myScoreClassic = String(score);

                    }

                    

                    public function captureBonus():void {

                              bonusClassic = String(bonus);

                    }

 

          }

 

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 11, 2013 Oct 11, 2013

the MovieClip(root).scoreNa.text = str; was the first error..

i know that it has also many errors cause i also used

MovieClip(root).lives.nextFrame();

MovieClip(root).ScorePowerUps.stop();

MovieClip(root).timerPowerUps.stop();

MovieClip(root).powerUpstimer.stop();

which are alll timers on the timeline.. and the lives is also movie clip on the stage.

after that MovieClip(root).scoreNa.text = str; error.. im sure that this four will be next error.. cause it has also MovieClip(root)..

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 11, 2013 Oct 11, 2013

Hi Ned

i got it.. i convert the text to symbol then export to action script then use it in the class. im so happy..  its now working and no errors when i go to the next frame. but the loop is stil going.. its looping even thought im on the next frame..i know that its looping cause all the tracing is not stopping... it makes the player lag... when i test it.. what should i do?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 11, 2013 Oct 11, 2013
LATEST

owww.. i didnt see.. it still has some problem.. after i convert it to symbol... its working fine till i it reaches the next frame.. but i noticed that the score is not moving.. but im tracing it that it has score..

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines