Pambudi
Pambudi

Reputation: 1

AS3 stopping a timer from another class

Hi I'm trying to stop a timer in stopwatch class from MainMenu class. But my code doesn't work, here is my code:

in MainMenu class i've method:

public function pauseGame (e:MouseEvent){
    timestop = new Stopwatch();
    timestop.Stoptimer();
    }

in class Stopwatch i try to stop my timer with:

 public function Stoptimer(){
    timer.stop();
        timer.removeEventListener(TimerEvent.TIMER, timeFun);
    return;
}

Upvotes: 0

Views: 173

Answers (1)

Daniel
Daniel

Reputation: 35704

without seeing more code I can only guess, but I think the issue is that you are creating a new stopwatch when you are pausing the game

public function pauseGame (e:MouseEvent){
    timestop = new Stopwatch();  // <-- a new instance with a new timer inside
    timestop.Stoptimer();
}

the timestop should be a global variable so you shouldn't need to instantiate it again, so this should be enough:

public function pauseGame (e:MouseEvent){
    timestop.Stoptimer();
}

Upvotes: 1

Related Questions