Phaser: Keyboard input events still working when scene is paused

Created on 6 Jun 2018  路  3Comments  路  Source: photonstorm/phaser

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?

馃挅 Feature Request

Most helpful comment

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.

All 3 comments

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)?

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

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

halilcakar picture halilcakar  路  4Comments

samme picture samme  路  3Comments

BigZaphod picture BigZaphod  路  4Comments

frob picture frob  路  4Comments

Legomite picture Legomite  路  4Comments