获取LIBGDX中的光标位置 [英] Get cursor position in LIBGDX

查看:93
本文介绍了获取LIBGDX中的光标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取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 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中的光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆