how to fix error 1009 in as3

404 Views Asked by At

hi i have been searhing the net for hours and have not found a solution to my problem and i have no idea how to fix it as i am only new to flash so if you know anything that might help me just comment below please all help is greatly appreciated

so here is the code

Quit.addEventListener(MouseEvent.CLICK, func2);

function func2(event:MouseEvent):void
{
    gotoAndStop(2);
}

Help.addEventListener(MouseEvent.CLICK, func4);

function func4(event:MouseEvent):void
{
    gotoAndStop(4);
}

var myTimer:Timer = new Timer(2000,0);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener(e:TimerEvent):void {
Hungry_bar.scaleX-=0.05;
if(Hungry_bar.scaleX<=0.05){
        gotoAndStop(12)
    }
}
myTimer.start();

var myTimer2:Timer = new Timer(3000,0);
myTimer.addEventListener(TimerEvent.TIMER, timerListener2);
function timerListener2(e:TimerEvent):void {
Fun_bar.scaleX-=0.05;
if(Fun_bar.scaleX<=0.05){
        gotoAndStop(13)
    }
}
myTimer2.start();

Feed.addEventListener(MouseEvent.CLICK,feed)
function feed(e:MouseEvent){
    Hungry_bar.scaleX+=0.05
    if(Hungry_bar.scaleX>=1.5){
        gotoAndStop(14)
    }
}

Fun.addEventListener(MouseEvent.CLICK,happy)
function happy(e:MouseEvent){
    Fun_bar.scaleX+=0.05
    if(Fun_bar.scaleX>=1.5){
        gotoAndStop(15)
    }
}

And here is the error

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at petgame_fla::MainTimeline/timerListener()[petgame_fla.MainTimeline::frame5:19]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at petgame_fla::MainTimeline/timerListener2()[petgame_fla.MainTimeline::frame5:29]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
1

There are 1 best solutions below

0
On BEST ANSWER

You use gotoAndStop() to move off a frame, in the meantime you have timers that refer to objects that do not exist on other frames than your current frame (5). gotoAndStop() triggers destruction of previously current frame, thus, once you go to another frame, your Hungry_bar becomes void, but timers still tick, because they are frame independent, and when they trigger the timer event, your functions assume that your MC's components are there while they no longer are in place. You should stop the timers and remove their listeners when you change the frame via gotoAndStop().