I’m not sure if the way I did makes the garbage collector remove the timer. Here are my two functions:
public function newWave() {
var callTimer:Timer = new Timer(800);
callTimer.start();
leftToSpawn = 4;
callTimer.addEventListener(TimerEvent.TIMER,waveCall);
}
public function waveCall(e:TimerEvent) {
leftToSpawn--;
if(leftToSpawn <= 0){
e.target.stop();
e.target.removeEventListener(TimerEvent.TIMER_COMPLETE,waveCall);
}
spawnEnemy();
}
Thanks
To remove an event listener you need to remove it with the exact same signature.
If you do:
Then you need to use the same event type and function to remove it:
Using
TimerEvent.TIMER_COMPLETEhere will try to remove a listener that doesn’t exist, which is silently ignored.Using
targethere is ok, for other listener types you may need to usecurrentTarget, which is always the object the listener got added to. For example in a mouse click event,targetcould be a child of a MovieClip and without any listeners.