【发布时间】:2018-05-23 18:09:30
【问题描述】:
前言:在桌面上一切正常。 当我在手机上测试我的项目时出现问题
我开始游戏。这是屏幕上的一些按钮:button_1、button_2、button_3。例如,我正在触摸 button_1:在应用程序启动后第一次触摸完全没有发生任何事情。如果我再次触摸 button_1,它工作正常 - > touchDown(按钮图像下降 10 像素)然后 touchUp(按钮图像上升 10 像素并运行按钮代码)。但是,如果我改为触摸另一个按钮,例如 button_2,则只会发生 button_1 的 touchDown(button_1 图像下降 10px 及以上),没有别的。每个按钮都会发生这种情况,所以我需要触摸按钮两次才能使其工作。
按钮类:
public class myButton {
private float x, y, width, height;
private Texture buttonUp;
public Rectangle bounds;
private boolean isPressed = false;
public myButton(float x, float y, float width, float height, Texture buttonUp) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.buttonUp = buttonUp;
bounds = new Rectangle(x, y, width, height);
}
public boolean isClicked(int screenX, int screenY) {
return bounds.contains(screenX, screenY);
}
public void draw(SpriteBatch batch) {
if (isPressed) {
batch.draw(buttonUp, x, y - 10, width, height);
} else {
batch.draw(buttonUp, x, y, width, height);
}
}
public boolean isTouchDown(int screenX, int screenY) {
if (bounds.contains(screenX, screenY)) {
isPressed = true;
return true;
}
return false;
}
public boolean isTouchUp(int screenX, int screenY) {
if (bounds.contains(screenX, screenY) && isPressed) {
isPressed = false;
return true;
}
isPressed = false;
return false;
}
}
输入处理程序:
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
screenX = (int) touchPos.x;
screenY = (int) touchPos.y;
playButton.isTouchDown(screenX, screenY);
return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
screenX = (int) touchPos.x;
screenY = (int) touchPos.y;
if (playButton.isTouchUp(screenX, screenY)) {
start();
return true;
}
}
在 create() 方法中我得到了:
touchPos = new Vector3();
Gdx.input.setInputProcessor(new InputHandlerer());
camera = new OrthographicCamera();
viewport = new FitViewport(1080, 1920, camera);
在 render() 方法中我得到了:
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
batch.setProjectionMatrix(camera.combined);
我再说一遍 - 在桌面上每个按钮都能正常工作,但在手机上却不行。可能是什么问题?
【问题讨论】:
-
InputHandler 中的
playButton是什么? -
@icarumbas 这是我的按钮,所以在 create() 方法中:
menuButtons = new ArrayList<myButton>(); playButton = new myButton(0, 560, 602, 180, playButtonUp); menuButtons.add(playButton); -
为什么不想使用 Scene2D 库?使用 Stage,将其设置为 InputProcessor,并为其添加 Buttons。没有理由实现您自己的 Button 类。 github.com/libgdx/libgdx/wiki/Scene2d.ui
-
@Arctic45 感谢您的建议,但问题是“在这种情况下可能是什么问题”。很抱歉我在 java/libgdx 方面的无能,我正在通过我的项目学习它,也许如果我放弃我将不得不重新考虑你的建议。除了这部分,我几乎已经完成了工作,制作那个简单的按钮类没什么大不了的。另外,正如我所说,它在桌面上运行良好,所以一定是我错过了一些东西,有人可能知道什么并可以帮助我。
标签: java android input libgdx touch