Sawyer

Remove keyboard callback

6 posts in this topic

I have an opening screen to my game where the use can press any key to start.

I implemented this using:

  this.game.input.keyboard.addCallbacks(this, function() {
      this.state.start("Menu");
  });

My problem is that this callback persists over into other states, so if I press any key it always take me back to my Menu state.

I've looked at the Documentation and tried both:

  this.game.input.keyboard.addCallbacks(this, null, null, null);

and

  this.game.input.keyboard.reset(true);

Neither of which did the trick.

I then simply replaced the callback with:

this.game.input.keyboard.addCallbacks(this, function() {});

Which seems to have solved the problem.

2 questions here:

  1. Is there a cleaner way to do this?
  2. Would having an empty callback like this impact performance in any way?
brentstrandy likes this

Share this post


Link to post
Share on other sites

Maybe you could instead use this.game.input.keyboard.onDownCallback, and nullify it when done with it?

By the way, states have a shutdown() method called when the state is shutdown, so I guess it would be quite clean to nullify the onDownCallback method there.

drhayes likes this

Share this post


Link to post
Share on other sites

A bit late to the party, but...

this.game.input.keyboard.stop();

Will stop the keyboards events from being fired. 

Share this post


Link to post
Share on other sites

It looks like you should just do

var keyboard = this.game.input.keyboard;
keyboard.onDownCallback = keyboard.onUpCallback = keyboard.onPressCallback = null;

 

lewster32 likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.