使用鼠标在 HTML 画布中控制游戏
我试图找出 JavaScript 和 HTML 是否有可能控制鼠标并引入相对移动控制 - 在第一人称游戏中获得的鼠标控制类型(其中鼠标可以向一个方向移动,并且玩家可以将无限转动直到鼠标停止)。
我认为实现这一目标的唯一方法是通过 JavaScript 控制鼠标,但我确信这是不可能的。有人对此有什么想法吗?
I am trying to find out if it is possible for JavaScript and HTML's to take control of the mouse and introduce relative movement control - the type of mouse control you get in first person games (where the mouse can be moved in one direction and the player will infinitely turn until the mouse stops).
The only way I can think this could be achieved is by taking control of the mouse via JavaScript, which I'm sure is not possible. Does anyone have any ideas on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好消息,老问题,随着鼠标锁的引入,https://developer.mozilla.org/en /API/Mouse_Lock_API ,可以捕获相对移动。
Good news old question, with the introduction of Mouse Lock, https://developer.mozilla.org/en/API/Mouse_Lock_API , you can capture relative movement.
根据你正在制作的内容,你可以做类似飞行模拟的事情。这意味着,变化率是由鼠标距中心/静止位置的距离决定的,而不是由鼠标的实际增量决定的。但这仅适用于车辆游戏。
Depending on what you are making you could do like flight sims. Meaning, the rate of change is determined by the distance of the mouse from the center/rest position, instead of the actual delta of the mouse. This only works well for vehicle games though.
并不真地。您可以模拟这一点:onmousemove,获取 X 和 Y 坐标,并据此计算光标相对于游戏视口的位置(例如,如果游戏发生在从 [100,100] 到 [200,200] 的正方形中,则将鼠标放在 [59,92 ] 表示“左转”,因为它位于游戏“中心”的左侧 [150,150])。
一个可能的问题:一旦鼠标位于浏览器窗口之外,您就会停止接收事件(要求播放器最大化窗口以获得最佳体验?)。
Not really. You could simulate this: onmousemove, get X and Y coordinates, and calculate from this the cursor's position relative to your game viewport (e.g. if the game happens in a square from [100,100] to [200,200], then mouse at [59,92] means "turn left", as it's to the left of the game's "center" at [150,150]).
A possible problem: you stop receiving events once the mouse is outside the browser window (ask player to maximize window for best experience?).