excaliburjs / Excalibur

🎮 Your friendly TypeScript 2D game engine for the web 🗡️
https://excaliburjs.com
BSD 2-Clause "Simplified" License
1.82k stars 189 forks source link

scenes consistently swallowing keyboard events after switching back and forth #3126

Closed andybarron closed 4 months ago

andybarron commented 4 months ago

NB: Thanks so much for this incredible project! I'm happy to take a stab at fixing this if anyone can point me in the right direction :)

Steps to Reproduce

Create two scenes that toggle back and forth with two different key presses:

import * as ex from "excalibur";

const a = new ex.Scene();
a.on("initialize", () => {
  a.input.keyboard.on("press", (e) => {
    if (e.key === ex.Keys.Key1) {
      a.engine.goToScene("b");
    }
  });
});

const b = new ex.Scene();
b.backgroundColor = ex.Color.DarkGray;
b.on("initialize", () => {
  b.input.keyboard.on("press", (e) => {
    if (e.key === ex.Keys.Key2) {
      b.engine.goToScene("a");
    }
  });
});

const game = new ex.Engine({
  scenes: { a, b },
});

game.goToScene("a");
game.start();

The issue does not appear if you target the same key code in both scenes.

Expected Result

Actual Result

Environment

Current Workaround

Haven't found one 😞

eonarheim commented 4 months ago

@andybarron Thanks for the issue, and the kind words!

Definitely feels like a bug to me, let me dig around quick with your example to see if anything jumps out.

eonarheim commented 4 months ago

@andybarron I think your intuition is spot on, I have some local tests that look like this is the problem. I should have a fix sometime today

It seems like the key gets "stuck" in the down state due to the scene changes, and the first press "releases" it.

eonarheim commented 4 months ago

@andybarron This is merged into main, should be deployed to the alpha npm package soon.

I have a few more bugs to fix before I cut the next v0.29.4, probably end of July?

andybarron commented 4 months ago

@eonarheim just installed the latest alpha and it works like a charm! thanks 😄 🙏🏻