libgdx:在电话上进行错误的触摸处理



前言:在桌面上,一切正常。当我在电话上测试我的项目时,问题

我开始游戏。这是屏幕上的一些按钮:button_1,button_2,button_3。例如,我正在触摸button_1:在应用程序之后,第一次触摸根本就开始没有发生任何事情。如果我再次触摸Button_1,它可以正常工作 ->触地得分(按钮映像为10px),然后tountup(按钮图像上升到10px和按钮代码运行)。但是,如果我触摸另一个按钮,例如按钮_2,只会发生button_1的达阵(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;
}
}

inputhandlerer:

@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;
        }
}

在创建()方法中,我得到了:

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);

我重复 - 在桌面上每个按钮都可以正常工作,但在电话上却没有。有什么问题?

也许在桌面上可以正常工作,因为您一次只能单击一次,而在电话上也应该管理指针,如果您不想使用MultitItouch。P>

在您的Inputhandlerer类上,您必须发起:

private int button1pointer = -1;
private boolean button1isPressed = false;

然后,您必须管理托运/UP方法上的指针:

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
for (int i = 0; i < 2; i++) {   //here you can choose how many touches you can manage
    if (Gdx.input.isTouched(i)) {
       screenX = (int) touchPos.x;
       screenY = (int) touchPos.y;
       if (playButton.isTouchDown(screenX, screenY) && (button1pointer != i) && (!button1isPressed)) {
          button1pointer = i;
          button1isPressed = true;
       }
        return true;
    }
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    if (!(Gdx.input.isTouched(pointer)) && (button1pointer == pointer) && (button1isPressed)) {
       button1pointer = -1;
       button1isPressed = false;
       start();
       return true;
    }
}

最新更新