Listener removal from within callback [Solved]

Hi there.

Using Phaser3, coding in Typescript.

  • I’m trying to run some code once per frame for a few frames.
  • I want to do this repeatedly when a specific action happens from the user.
  • These “coroutines-sort-of” (lingo from Unity) will be possibly running concurrently and will have different durations (number of iterations to be a bit more specific).

I’ve got some code similar to the following:

//I have a custom class. In its constructor I do the following...
this.iterator = 0;
this.sourceScene.events.on('update', this.cb, this); //sourceScene is the scene where my custom class does its work

//in my class I have this method
private cb(): void {
  if(this.iterator < someLimit){
    //do something
    this.iterator++;
  }
  else{
    this.sourceScene.events.off('update', this.cb);
  }
}

The issue is that when I call events.off, all of the concurrent “coroutines-sort-of” are stopped. Not just the one that is linked to the object where each one was created.

Is there some other way to do this that I haven’t found/thought of?

Note that I’ve considered just having the callback happen on update and checking if I should execute the inner code with an if statement, but I find that a bit too costly for my needs.

Any help would be much appreciated!

For anyone who might come across this, I found the answer to my own question.
I was just missing the context. Specifically it should be…

this.sourceScene.events.off('update', this.cb, this);

Can’t believe I missed it :slight_smile: