问题描述
如何在 libgdx 中获取光标位置并将其应用于 sprite?像这样?
How to get cursor position in libgdx and apply it to sprite? Like this?
我希望我的精灵的方向指向屏幕中光标所在的任何位置.我搜索了 libgdx 示例,但找不到任何与此相关的示例.
I want my sprite to have the direction pointed wherever my cursor is in the screen.I search libgdx examples but i cant find any examples related to that.
推荐答案
获取光标位置
如果要轮询输入,请使用 Gdx.input.getX()
和 Gdx.input.getY()
获取当前鼠标 x 和 y 坐标.(文档说它只与触摸有关,但代码 看起来它报告原始鼠标值不管按钮状态如何.)
Get cursor position
If you're polling for input, use Gdx.input.getX()
and Gdx.input.getY()
to get the current mouse x and y coordinates. (The doc says its only relevant for touch, but the code looks like it reports raw mouse values regardless of button state.)
如果您使用的是 InputProcessor
,您可以使用以下之一:
If you're using an InputProcessor
you can use one of:
- touchMoved 回调(开启较旧的 libGDX 版本)
- mouseMoved 回调(在较新的 libGDX 版本上)
- touchMoved callback (on older libGDX versions)
- mouseMoved callback (on newer libGDX versions)
在没有按下按钮的情况下接收来自鼠标的输入事件.
to receive input events from a mouse with no buttons pressed.
更新 Vector2
从当前精灵位置指向光标位置.这可以是您的 Sprite 的标题.您需要旋转精灵的标题以匹配此向量.
Update a Vector2
that points from the current sprite position to the cursor position. This can be your Sprite's heading. You'll want to rotate the sprite's heading to match this vector.
使用 Vector2.angle()
来计算这个向量的角度,并将你的精灵的旋转设置为此.(这是相对于正 X 轴的,所以如果你想要它相对于 Y 轴,你可能需要添加一个常数.)
Use Vector2.angle()
to compute the angle of this vector, and set your sprite's rotation to this. (This is relative to the positive X axis,so you may need to add a constant if you want it relative to the Y axis.)
这篇关于获取 LIBGDX 中的光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!