Java libgdx出现问题,使鼠标在数秒不活动后重新出现
我在测试我的游戏时遇到了一个问题,这个游戏是在我的MacBookPro笔记本电脑(OS X el capitan)上用LibGDX制作的。我还没有在我的windows PC上遇到过这个问题 问题 我的问题是,我制作了一些代码,使鼠标在2秒不活动后隐藏,如果鼠标再次被拖动,它应该重新出现,直到不再移动,然后在2秒不活动后它应该再次隐藏。在windows PC上播放时,该功能可以正常工作,但在使用我的macbook时,它无法正常工作,因为在隐藏鼠标后(2秒不活动后),它将不会再次出现 我正在使用类中的方法Java libgdx出现问题,使鼠标在数秒不活动后重新出现,java,macos,libgdx,mouse,Java,Macos,Libgdx,Mouse,我在测试我的游戏时遇到了一个问题,这个游戏是在我的MacBookPro笔记本电脑(OS X el capitan)上用LibGDX制作的。我还没有在我的windows PC上遇到过这个问题 问题 我的问题是,我制作了一些代码,使鼠标在2秒不活动后隐藏,如果鼠标再次被拖动,它应该重新出现,直到不再移动,然后在2秒不活动后它应该再次隐藏。在windows PC上播放时,该功能可以正常工作,但在使用我的macbook时,它无法正常工作,因为在隐藏鼠标后(2秒不活动后),它将不会再次出现 我正在使用类中
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");
}
}
这里唯一的问题是,光标仍然可以点击游戏中的对象,尽管它是不可见的。一个解决方案是制作一个标志,设置并使用该标志来确定鼠标光标是否不可见。虽然这不是解决问题的最佳方法,但它可以解决问题。我所做的是创建一个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");
}
}
这里唯一的问题是,光标仍然可以点击游戏中的对象,尽管它是不可见的。解决这个问题的一个方法是制作一个标志,该标志被设置并用于确定鼠标光标是否不可见