简体   繁体   English

从侦听器获取信息并将其发送到游戏引擎的最佳方法是什么?

[英]What is the best way from grabbing information from a listener and getting it to the game engine?

[Lance] What is the best way from grabbing information from a listener and getting it to the game engine? [Lance]从收听者那里获取信息并将其发送到游戏引擎的最佳方法是什么? Currently trying to make the Spaaaceship follow my mouse. 目前正试图让Spaaaceship跟随我的鼠标。 I have made a MouseControls.js that listens for mouse movement and records the X and Y of the cursor. 我制作了一个侦听鼠标移动的MouseControls.js并记录了光标的X和Y. I then used the following code in the client engine to get it to the game engine 然后我在客户端引擎中使用以下代码将其引入游戏引擎

this.sendInput('mouseMove', {
 cursorX: this.mouseControls.cursorPos.cursorX, 
 cursorY: this.mouseControls.cursorPos.cursorY
});

then in game engine I tried reading the second parameter in the processinput method like so: 然后在游戏引擎中我尝试在processinput方法中读取第二个参数,如下所示:

inputData.inputOptions.cursorY

but I get the error "Cannot read property 'cursorY' of undefined". 但我收到错误“无法读取未定义的属性'cursorY'”。 I get that each key for the other controls does the same thing every time, but I don't know how to pass variable information around (cursorX/Y). 我得到其他控件的每个键每次都做同样的事情,但我不知道如何传递变量信息(cursorX / Y)。 This is all modifying the spaaace tutorial btw. 这都是修改spaaace教程btw。 Should I be making a mouse object instead? 我应该制作一个鼠标对象吗?

UPDATE: I have dug deeper and learned a bit more, so I think I have narrowed down my problem. 更新:我已经挖得更深,学到了更多,所以我想我已经缩小了我的问题。 It is as follows: 它如下:

At the moment the game engine processes the inputs, it only has the name of the input with no additional information, which is fine for key presses that do the exact same thing every time. 在游戏引擎处理输入的那一刻,它只有输入的名称而没有附加信息,这对于每次执行完全相同的按键操作是很好的。 However, with the mouse movement, once I receive the "mouseMove" input, I also need to get the mouse X and Y position from my Mouse Controller, which isn't visible from game engine (as far as I can tell). 但是,随着鼠标移动,一旦我收到“mouseMove”输入,我还需要从我的鼠标控制器获取鼠标X和Y位置,这在游戏引擎中是看不到的(据我所知)。 So how do I get those values in that moment? 那么如何在那一刻获得这些价值呢?

Since you cannot see my code, it is the equivalent of getting the "activeInput.up" value from KeyboardControls from GameEngine's processinput method 由于您无法看到我的代码,因此它相当于从GameEngine的processInput方法获取KeyboardControls中的“activeInput.up”值

You're very close! 你非常接近! :) :)

If you send input like this from the ClientEngine : 如果您从ClientEngine发送这样的输入:

document.addEventListener('mousemove', (e)=>{
    this.sendInput('mousePos', { x: e.clientX, y: e.clientY });
})

then in the GameEngine you can access it via the options property: 然后在GameEngine您可以通过options属性访问它:

processInput(inputData, playerId) {
    super.processInput(inputData, playerId);
    console.log(inputData.options.x, inputData.options.y);
}

Reference: Lance docs 参考: Lance docs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM