rosie
rosie

Reputation: 11

AS3 - Timer - Error #1009: Cannot access a property or method of a null object reference

I have created a simple math game for preschool using flash cs5.5. My problem is :

When I click Start to play a game for first time, everything is okay but when I click button option or help, then back to main screen to play again, my timer and my score are hidden but it's running. How do I solve that?

Here is my code for the timer :

var count:Number = 5; // amount of time
var myTimer:Timer = new Timer(1000,count);

myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();

    function countdown(event:TimerEvent):void {

    timer_txt.text = String((count)-myTimer.currentCount); //dynamic txt box shows current count
if (((count)-myTimer.currentCount) == 0) {
    gotoAndStop(1, "Scene 8");

}
}

And the error :

Error #1009: Cannot access a property or method of a null object reference.at FWM_MENU_fla::MainTimeline/countdown()[FWM_MENU_fla.MainTimeline::frame51:37]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()

Upvotes: 1

Views: 1622

Answers (1)

Amy Blankenship
Amy Blankenship

Reputation: 6961

Before going to Help:

myTimer.stop();
myTimer.removeEventListener(TimerEvent.TIMER, countdown);

If you wait until after, myTimer will be out of scope and probably permanently inaccessible. The 1009 is probably timer_txt, which likely doesn't exist wherever you are when the timer fires (likely in the Help).

Upvotes: 1

Related Questions