Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyboard input events still working when scene is paused #3733

Closed
JoeBerkley opened this issue Jun 6, 2018 · 3 comments
Closed

Keyboard input events still working when scene is paused #3733

JoeBerkley opened this issue Jun 6, 2018 · 3 comments

Comments

@JoeBerkley
Copy link

JoeBerkley commented Jun 6, 2018

I've tried several solutions for this, it sems sprite .on('pointerdown') events are not able to be fired once a scene has been paused. It also seems as if scene.input.on(gameobjectdown) events are also not able to be fired if a scene is paused.

class Level1Scene extends Phaser.Scene {
 
  constructor() {
    super({
      key: 'Level1Scene'
    });
  }
 
  create() {
    console.log('creating Level1');
    this.setupPauseMenu();
    this.events.on('pause', this.pause, this);
    this.events.on('resume', this.resume, this);
 
    this.input.keyboard.on('keydown_X', () => { this.togglePause(); }, this); //This will work regardless of paused state
  }
 
  setupPauseMenu() {
    this.pauseMenuResume = this.add.image(this.sys.game.config.width/2, this.sys.game.config.height/2 -30, 'atlas', 'ui/pause/resume.png');
    this.pauseMenuResume.setInteractive();
    this.pauseMenuResume.on('pointerdown', () => { this.togglePause(); }, this); //This can only be fired if not paused

    this.pauseMenuResume.visible = false;
  }
 
  pause() {
    console.log('Before pause scene');
    this.scene.pause('Level1Scene');
    console.log('After pause scene');
    //Display pause menu
    this.pauseMenuResume.visible = true;
  }
 
  resume() {
    console.log('In Resume');
    this.pauseMenuResume.visible = false;
    console.log('Hidden menu');
    this.scene.resume('Level1Scene');
    console.log('resumed scene');
  }

  togglePause() {
    console.log('Toggling Pause');
    if (!this.scene.isActive('Level1Scene')) {
        console.log("Trying to resume");
        this.scene.resume('Level1Scene');
    } else {
      console.log("Trying to pause");
        this.scene.pause('Level1Scene');
    }
  }
}

As mentioned, I've also tried the following in the create() and while paused got nothing back

this.input.on('gameobjectup', function(pointer, gameObject) {
      console.log(gameObject);
    });

I've also tried a pointer down on a text object, however got the same result

The issue here is probably more likely that keydowns are working, rather than pointerdown/gameobjectdown are not working, as if the scene is paused, it should prevent all user interaction until unpaused?

@JoeBerkley JoeBerkley changed the title pointerdown and gameobject down not working whilst paused Keyboard input events still working when scene is paused Jun 6, 2018
@samid737
Copy link
Contributor

samid737 commented Jun 6, 2018

fiddle 3.9.0.

I guess the first title describes the issue better? Having key input enabled could be desirable for some devs(pause menus)?

@JoeBerkley
Copy link
Author

We discussed this on the community Discord when when we realized what was going on, if a scene is paused there should be a UI scene to manage its pause/unpausing/whatever else, still having any kind of input could let the users effect the game state while it should be paused.

The title works either way, depending on what way you view the problem, I originally pointerdown/gameobjectdown should be triggerable when paused, however after some discussion I thought it made more sense for a UI/Paused scene to handle this instead

photonstorm added a commit that referenced this issue Jun 8, 2018
The `KeyboardManager` class has been removed. It has been replaced with `KeyboardPlugin` which is now an Input level plugin, that registers itself with the new `InputPluginCache`. The Input Plugin class (which belongs to a Scene) will now automatically inject registered plugins into itself on boot. Every Scene has its own instance of the Input Plugin (if enabled in the scene plugins), which in turn has its own instance of the KeyboardPlugin. The `InputManager` no longer has any reference to the Keyboard class at all. The benefits of this are two-fold: First, it allows you to now entirely exclude all of the keyboard classes from a custom build, saving a lot of space if not required. Secondly, it means that the Scenes themselves are now responsible for keyboard events, where-as before they were entirely global. This means a Scene can be paused and stop processing keyboard events, and stop having its Key objects updated, while another Scene can still carry on doing this. It also prevents key related callbacks in sleeping Scenes from being fired (which resolves issue #3733, thanks @JoeMoov2)
@photonstorm
Copy link
Collaborator

This actually required a complete rewrite of the KeyboardManager. It was a global class before, not bound to a Scene, and therefore objects and events created on it applied to any Scene in your game, not just those in which they were defined.

I can see how it would be useful to have this on a Scene level though. So I have created an Input Plugin system, and moved the keyboard class to it, so it's now one of the new input plugins and self-registers with the Scene input system. This means every Scene has its own instance of the keyboard plugin, which means it won't emit events (or process Key objects) for sleeping / inactive Scenes, giving full separation. It also uses more resources, but you can disable it on a per-Scene basis using the Scene config, so if you've got a Scene that doesn't need input, or keyboard input, you can disable either / or.

This is now in the master branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants