GaryStanton / phaser3-merged-input

A Phaser 3 plugin to map input from keyboard & gamepad to player actions
MIT License
34 stars 5 forks source link

Phaser v3.60 - Issue with pointerdown event? #39

Closed Dan-Mizu closed 1 year ago

Dan-Mizu commented 1 year ago

Everytime the pointerdown event is called, I get this error:

Uncaught TypeError: this.players[0] is undefined
    pointerDown main.js:779
    boot main.js:79
    emit phaser.js:199
    processDownEvents phaser.js:103761
    update phaser.js:103443
    updateInputPlugins phaser.js:102156
    onMouseDown phaser.js:102357
    onMouseDown phaser.js:115867
    startListeners phaser.js:115939
    boot phaser.js:115746
    emit phaser.js:218
    boot phaser.js:101941
    emit phaser.js:218
    boot phaser.js:17086
    DOMContentLoaded phaser.js:30879
    Game2 phaser.js:17046
    <anonymous> index.ts:23
main.js:779:2

possibly due to new Phaser v3.60 version?

I create my player/merged inputs as so:

this.playerInput = this.mergedInput.addPlayer(0);
this.mergedInput
    .defineKey(0, "UP", "W") // move up
    .defineKey(0, "DOWN", "S") // move down
    .defineKey(0, "LEFT", "A") // move left
    .defineKey(0, "RIGHT", "D") // move right
    .defineKey(0, "START", "TAB") // inventory
    .defineKey(0, "SELECT", "ESC") // back/pause
    .defineKey(0, "RC_E", "F") // interact
    .defineKey(0, "LB", "Q") // rotate left/pagination left
    .defineKey(0, "RB", "E"); // rotate right/pagination right

They all seem to work with the controller but not the keyboard as well.

GaryStanton commented 1 year ago

Hi Dan,

I've been meaning to check out your Froguelike, as I'm also working on a Phaser game with frogs as main characters! 🐸 Thanks for the report. I'm at work now, but I'll take a look as soon as I can.

GaryStanton commented 1 year ago

@Dan-Mizu I updated the demo to v3.60 and did a bit of testing with the mouse input, but I can't seem to replicate your problem. I don't suppose you could set up a test in a pen? Or if you're able to provide some more information?

Dan-Mizu commented 1 year ago

@Dan-Mizu I updated the demo to v3.60 and did a bit of testing with the mouse input, but I can't seem to replicate your problem. I don't suppose you could set up a test in a pen? Or if you're able to provide some more information?

My most recent push has the issue on Froguelike. If you use VSCode you can launch it in dev mode using Client Dev, but I will try to launch it via code pen.

Dan-Mizu commented 1 year ago

CodeSandbox Link

GaryStanton commented 1 year ago

@Dan-Mizu

Not having much luck with this I'm afraid. I haven't been able to get FrogueLike running in my dev environment due to horrible node related versioning shenanigans. It runs in the CodeSandbox link you provided, but I wasn't able to make any changes - and once I signed in with GitHub it stopped me accessing it altogether!!

In any case, I didn't see the problem when running it in CodeSandbox. I can see that the interact button is disabled - but I couldn't enable it as no changes I made would be saved... so... I've seen the starting screen. ;)

Dan-Mizu commented 1 year ago

Did you open the web console to check for the errors whenever you clicked on the game? Not referring to the one in CodeSandbox, but your browser's console.

GaryStanton commented 1 year ago

Ahh, yes I see it... I'd thought the logs window was the console! It looks like it can't find a reference to the player object for some reason. The problem is I can't do any kind of debugging at all... I'm not able to make any changes to the code in CodeSandbox, and I still can't get it running on my dev environment.

It'd be really helpful if you could produce a test case that exhibits the problem, with as little extra code as possible. It's tough to try and debug a problem from inside your whole game! If not, I'll give running it locally another try on a new machine - I think it'll need a fresh install of Node. 🤷

GaryStanton commented 1 year ago

@Dan-Mizu I think I've got to the bottom of this... As you have the plugin running as a scene plugin, it's booting up for each scene and each time it's adding a pointerdown event - only one of the scenes' instances has a player object... so even though it's working, you're getting an error from another scene's instance.

I've pushed an update that only adds the pointer events when you specifically add a player object. Can you give it a try and let me know if that's working as you'd expect?

Also, I notice you had an issue with the 'interact' and 'inventory' actions. In your code, you're checking the interaction struct like this:

this.scene.sceneHead.playerInput.interaction.pressed.includes(
  "RC_E"
)

The interaction struct uses button numbers (e.g. B0 )instead of the friendly names. You want to be using interaction_mapped instead:

this.scene.sceneHead.playerInput.interaction_mapped.pressed.includes(
  "RC_E"
)
Dan-Mizu commented 1 year ago

Great news! Updating the module's main.js with the new version seemed to remove the pointer errors. Thanks for the help on that.

I saw interaction_mapped within the examples but when trying to implement that I got type errors (I'm using typescript). Perhaps the types are not updated in the current version for your Player class?

Code_0SAxAYRCoh

Dan-Mizu commented 1 year ago

Manually edited your modules main.d.ts with a duplicate of "interation" named "interaction_mapped" and that seemed to work.

Have a couple other questions. How would I go about mapping to the mouse like the scroll wheel and left/right/middle click. Also, how would i detect a mapped key being held down?

GaryStanton commented 1 year ago

@Dan-Mizu

Glad you got it working! I'm afraid I don't use TypeScript, and the main.d.ts was added by some kind contributors. If you can create a PR for your change, I'll merge it in.

Regarding the mouse buttons: In the pointer object you should find M1 - M5 flags, which correspond to mouse buttons. The scroll wheel button on mine is M3. You can interrogate those to see if a button is currently pressed during the update loop. You can also use the interaction object and call isPressed(), or check lastPressed, etc.

Currently there's no handling of the mousewheel's scrolling. I could look at adding that if that's what you're after.

It's worth noting that you can also pass a player's coordinates into the player object with setPosition(). When the player object knows its position, the pointer object will keep track of the mouse pointer in relation to it - i.e. angle, degrees and bearing.

To detect a key being held down, you can just check the corresponding flag in the buttons or buttons_mapped objects.

Dan-Mizu commented 1 year ago

when do you think a new update will be pushed for this plugin to npm? (so i don't have the edit the module manually in my different dev environments 😄)

GaryStanton commented 1 year ago

Hi Dan,

I was waiting to hear back from you before pushing a release. If you can make a PR for the typescript changes, I'll bundle it up this evening. If you want some interaction with the mousewheel, that may take a little while longer - but I can push that in a minor release separately.

Dan-Mizu commented 1 year ago

Ah right! I went ahead and updated the typings to the best of my ability and submitted a PR, using the demo you provide as reference. Also, scroll wheel functionality is not very critical to me.

GaryStanton commented 1 year ago

Thanks Dan, Just one thing - I notice you have a sprites struct in there. There aren't any sprites in the plugin itself, I presume you're referencing the demo, but I wouldn't expect that to make its way through into the typings. Are you using that for anything or can I remove it?

Dan-Mizu commented 1 year ago

Yes that must have automatically populated as I went through the demo. Should be free to remove it.

GaryStanton commented 1 year ago

Hi @Dan-Mizu v1.6.1 is now live on npm.

Let me know if you have any more issues, and thanks for the help!