gpt4 book ai didi

java - libgdx 使鼠标在几秒钟不活动后重新出现的问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:47:57 26 4
gpt4 key购买 nike

我在测试我的游戏时遇到了一个问题,该游戏是在我的 MacBook Pro 笔记本电脑 (OS X el Capitan) 上使用 LibGDX 制作的。我在 Windows PC 上没有遇到过这个问题。

问题

我的问题是,我编写了一些代码,这使得鼠标在不活动 2 秒后隐藏,如果再次拖动鼠标,那么它应该重新出现,直到不再移动,然后在不活动 2 秒后它应该再次隐藏。在 Windows PC 上玩时工作正常,但在使用我的 macbook 时,它无法正常工作,因为鼠标被隐藏后(不活动 2 秒后),它不会再次出现。

我正在使用 Input 中的方法 setCursorCatched(true)类,隐藏鼠标,并再次使用相同的方法,只是使用错误的输入,使其重新出现。为了检查鼠标是否相对于其最后一个已知位置移动,我使用方法 getDeltaX()getDeltaY()。我自己检查不活动的方法如下所示。

/* Time for checking mouse inactivity */
private float lastTimeMouseMoved = 0f;
private float elapsedTimeOfInactivity = 0f;

/**
* Check for mouse inactivity. If mouse is inactive for XX seconds or more, then hide it.
* If the mouse is moved show it, and then hide it again after XX seconds of inactivity.
*
* @param deltaTime the elapsed time between each frame.
* @param secondsInactive the time of inactivity in seconds
*/
private void hideMouseAfterInactivity(float deltaTime, float secondsInactive) {
/* If mouse has been moved */
if (Gdx.input.getDeltaX() != 0 || Gdx.input.getDeltaY() != 0) {
/* Show the mouse cursor */
Gdx.input.setCursorCatched(false);

/* Keep track of the last known time which the mouse was moved. */
lastTimeMouseMoved = deltaTime;
elapsedTimeOfInactivity = lastTimeMouseMoved;
} else {
/* check if the time of inactivity is XX seconds or more */
if (elapsedTimeOfInactivity >= lastTimeMouseMoved + secondsInactive) {
/* Hide the mouse cursor */
Gdx.input.setCursorCatched(true);
} else {
/* update the elapsed time of inactivity */
elapsedTimeOfInactivity += deltaTime;
}
}
}

我希望任何人都可以告诉我出了什么问题...在我使用setCursorCatched(true)后全屏玩游戏时,我也遇到了显示鼠标的问题。提前致谢。

最佳答案

虽然这不是问题的最佳解决方案,但它可以解决问题。我所做的是创建一个 1x1 像素的透明图像,然后我没有使用 setCursorCatched(boolean catched) 方法来使鼠标可见和不可见,而是在我的主游戏类中创建了以下方法,这样我就可以在所有其他游戏屏幕中访问它。

//Remember to dispose these on application exit.
private Pixmap normalMouseCursor = new Pixmap(Gdx.files.internal("normal-cursor.png"));
private Pixmap transparentMouseCursor = new Pixmap(Gdx.files.internal("transparent-cursor.png"));

/**
* Set the mouse cursor to either the normal cursor image or a transparent one if hidden.
*
* @param shouldHide true if the mouse should be invisble
*/
public void setMouseCursor(boolean shouldHide) {
if (shouldHide) {
Gdx.graphics.setCursor(Gdx.graphics.newCursor(transparentMouseCursor, 0, 0));
Gdx.app.debug(TAG, "Mouse Cursor is invisible");
} else {
Gdx.graphics.setCursor(Gdx.graphics.newCursor(normalMouseCursor, 0, 0));
Gdx.app.debug(TAG, "Mouse Cursor is visible");
}
}

这里唯一的问题是光标仍然可以点击游戏中的对象,尽管它是不可见的。我认为解决这个问题的方法是制作一个标志,设置该标志并用于确定鼠标光标是否不可见。

关于java - libgdx 使鼠标在几秒钟不活动后重新出现的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36127320/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com