[Phaser 3] Need double keyboard input for function

Hello hello, been trying to troubleshoot an issue I’ve been having regarding scene transitions and keyboard inputs, to no avail.

So basically I want to have a main menu scene and an options screen, and if I press enter while a line of text is selected on the main menu, the main menu scene will sleep/pause/shutdown/anything to make it disappear, and then the options scene will appear, and vice versa with options screen --> main menu.

After some experimenting with the scene transitions on offer, I settled on using switch() for both transitions, which also saves any changed values in the options scene. However this causes an issue where I need to press enter twice to switch to the options scene, after I go main menu --> options --> main menu. The first time transitioning from main menu to options only requires one press of enter like I intended, but afterwards it always requires two presses of enter to open the options screen.

This issue also only occurs in the main menu --> options transition, not the other way around. It also happens with some transitions between the menu and other scenes, but I figured that I’d be able to fix those if I can fix the main menu --> options transition first.

All functions are called in create(), and keyEnter is a variable defined in create():

 keyEnter = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.ENTER);

Function that calls the transition to the options page:

  menuOptionEnter() {
    // Does certain things when enter is pressed on a certain selection

    keyEnter.on('down', function() {
      if (mainMenuSelection == 1) {
        // Starts game
        console.log('start game');
      } else if (mainMenuSelection == 2) {
        // Continues the previous game if there was one in progress
        console.log('continue');
      } else if (mainMenuSelection == 3) {
        // Starts the tutorial
        console.log('tutorial');
      } else {
        // Moves to the options page
        this.scene.switch('options');
      }
    }, this);
  }

Options --> main menu:

  optionsExit() {
    // Exits to main menu when escape is pressed
    this.input.keyboard.on('keydown', function(event) {
      switch (event.key) {
        case 'Escape':
        console.log('menu');
        this.scene.switch('gameMenu');
        break;
      }
    }, this);
  }

Another function main menu --> another scene, also affected by the double press issue:

  menuExit() {
    // Escapes to splash screen when 'escape' is pressed
    this.input.keyboard.on('keydown', function(event) {
      switch (event.key) {
        case 'Escape':
        this.scene.switch('splashScreen');
        break;
      }
    }, this);
  }

Help would be super appreciated, spent a couple of days trying to figure this out and I’m at a complete dead end, perhaps I wasn’t transitioning between scenes correctly?
I’m not sure how much of my code I need to post to have sufficient detail for a solution, but I hope this will be of some use if the issue is obviously about scene transition stuff.

Can you try

// Moves to the options page
this.scene.switch('options');
keyEnter.reset();
1 Like

Or

this.input.keyboard.resetKeys()
1 Like

Both work great, far simpler than I expected, thanks :))

I couldn’t reproduce the issue :woman_shrugging: but if it works :smile: