Configure keyboard inputs once for all scenes to use

Nick, I was just reading your example of “Play and skip intro video” earlier and noticed the use a plugin for your controls, a thing I was unaware of in Phaser.

I realized it might well be a solution, thanks for writing the detailed code above, it helps a ton!
Maybe it’s still a bit complicated but I can’t think of a better way in Phaser as of now.

It felt quite simple in the first build of my game in basic JS though, something along those lines for the main script:

        Game.KEYS = {};
        Game.JUMP = "Space";
        Game.ATTACK = "KeyA";

        window.addEventListener("keydown", e => {
            Game.KEYS[e.code] = true;
        });

        window.addEventListener("keyup", e => {
            Game.KEYS[e.code] = false;
        });

And then in the update() method for any scene I like:

            if(Game.KEYS[Game.JUMP])
            {
                // Do jumpy stuff
            }
1 Like