Reputation: 11
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..
Upvotes: 1
Views: 9041
Reputation: 311
My guess would be that "root" is undefined, because I'm guessing TryClass is not inherited from a MovieClip or other DisplayObject that lives in the existing heirarchy.
To fix it, I'd add a parameter to the class's constructor. Then, I would send it a movieclip that you can access. For instance, if you are instantiating your class from within a movieclip, just send it the "this" for that movie.
public class TryClass {
...
static var myroot:MovieClip = null;
...
public function TryClass(someclip:MovieClip=null) {
...
if (this.myroot == null && someclip != null && someclip.root != undefined) {
this.myroot = someclip.root;
}
...
}
...
}
Then from within a movie clip:
var something = new TryClass(this);
Anyway, this is the technique I'm using for a class that I'm making. For mine, I have it adding an instance of an external movie clip if the class detects that the root does not already have it loaded. In my case, a universal loading-bar called from my loading wrapper class. If a particular movie I put it in already has a custom loading bar, then it won't do anything, but for any I don't have one in yet, it'll add it.
Upvotes: 0