Jump to content

Ticker.remove function not behaving as expected


SamuSan
 Share

Recommended Posts

Hi all, 

I am developing a simple 2D game with PIXI js and have created an FSM that controls game flow at a high level.
During some of the transitions between game states it would be desirable to have some animations in the game halt while others continue on or change. 
I have read the docs for PIXI.ticker.Ticker and my proposed solution was to use the remove function as outlined below. This is the most basic example I can give. 

class SomeState extends State {
  constructor(game){
    //does some stuff with the passed in game
  }
  enter(){
   //attach some animation loops to the app ticker
   this.game.app.ticker.add(this.game.someAwesomeFunction.bind(this.game));
  }

//...a bunch of things to do with updating a state etc

  exit(){
    //Here I would like to essentially pause a subset of the animation loops attached in the enter 
    //function like so:

    this.game.app.ticker.remove(this.game.someAwesomeFunction);

    //This doesn't behave as expected sadly, the function is not removed from the ticker and just 
    //continues on like nothing happened
  }
}


I have observed the function signature being passed to `remove` and it matches the signature being passed to `add`.
Has anyone got any ideas as to why I am unable to remove a listener instance from the game ticker? Or in fact have an opinion on this approach in general, may I am barking up the wrong tree.

Thank you in advance!

Link to comment
Share on other sites

Try caching the return value from bind(). Calling it twice is creating two separate functions maybe, one function that's passed to add() and a separate function that's passed to remove(). Or don't use bind() and pass the context instead like @themoonrat said. Either way should work since you'll be passing the same parameter/s to add() and remove(). -edit: that last line's more clear now I think;)

"The bind() method creates a new function," referenced from Mozilla MDN Docs

Link to comment
Share on other sites

3 minutes ago, magig said:

Try caching the return value from bind(). Calling it twice is creating two separate functions maybe, one function that's passed to add() and a separate function that's passed to remove(). Or don't use bind() and pass the context instead like @themoonrat said. Either should work I think since the parameter/s to add() and remove() will be the same using either method.

"The bind() method creates a new function," referenced from Mozilla MDN Docs

Oh! I get you, that is a pretty hilarious gotcha. I see what @themoonrat was speaking to now, I had no idea that bind would do that, I guess it makes sense in terms of partial application but is definitely not what I was going for here. 
I will try that out tonight and see if I have any luck. Thank you both!
 

Link to comment
Share on other sites

Success! Thank you so much @magig @themoonrat for your help and being a rubber ducky. I feel a bit stupid now lol. 
For anyone who has a similar problem:

class SomeState extends State {
  constructor(game){
    //does some stuff with the passed in game
  }
  enter(){
   //attach some animation loops to the app ticker
   //this.game.app.ticker.add(this.game.someAwesomeFunction.bind(this.game)); 
   // ^^^^ This creates a copy of the function ^^^^^ Thus when removing it there is no reference to 
   //use to find it. Prefer: 

    this.game.app.ticker.add(this.game.someAwesomeFunction, this.game); 
  }

//...a bunch of things to do with updating a state etc

  exit(){
    this.game.app.ticker.remove(this.game.someAwesomeFunction, this.game);
    // Passing the correct context here allows the ticker to find the correct function for removal.
  }
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...