LibGDX-InputProcessor-如果用户将手指从表示在X上移动的按钮拖动到表示在Y上移动的按键



所以,我正在为android游戏创建一个joystic,所以在我的触摸类(实现InputProcessor)中,它看起来像一个:

public class Touching implements InputProcessor{
    int y = 0;
    int x = 0;
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        x = Gdx.input.getX();
        y = Gdx.input.getY();
    if (x > 122 && x < 202 && y > 620 && y < 700 )
    {
        Core.player1.anmov(2); //button 1
    }
    if ( x > 122 && x < 202 && y > 520 && y < 600)
    {
        Core.player1.anmov(1); //button 2
    }
    if (x > 20 && x < 100 && y > 620 && y < 700)
    {
        Core.player1.anmov(3); //button 3
    }
    if (x > 222 && x < 302 && y > 620 && y < 700)
    {
        Core.player1.anmov(4); // button 4
    }
        return true;
    }
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {

        x = Gdx.input.getX();
        y = Gdx.input.getY();
        if (x > 122 && x < 202 && y > 620 && y < 700)
        {
            Core.player1.anmov(7); // button 1
        }
        if ( x > 122 && x < 202 && y > 520 && y < 600)
        {
            Core.player1.anmov(8); // button 2
        }
        if (x > 20 && x < 100 && y > 620 && y < 700)
        {
            Core.player1.anmov(5); // button 3
        }
        if (x > 222 && x < 302 && y > 620 && y < 700)
        {
            Core.player1.anmov(6); // button 4
        }
        return false;
    }
    public boolean touchDragged(int screenX, int screenY, int pointer) {
x = Gdx.input.getX();
y = Gdx.input.getY();
        return true;
    }

现在,如果我触摸代表在X上移动的按钮,拖动到代表在Y上移动的按键,它仍然在X上运动,直到我的手指离开屏幕(触摸UP在呼叫),然后,它站着,它不在Y上运动。

有人能帮我吗?我认为这是一个非常原始的问题,但我找不到解决方案。

我可能会在播放器类更新方法本身中选择这样的东西

//Declare velocity on top so you just have to change this to fine tune speed.
float velocity = 10;
if (Gdx.input.isTouched()) //isTouched should register as long as a screen is touched, you can use pointers for multiple fingers.
{
    if (touch within button) //just use Gdx.input.getX / Gdx.input.getY
    {
        position.X += velocity * Gdx.graphics.getDeltaTime();
    {
     //do for each button
}

我自己从未使用过拖动屏幕按钮,但理论上这应该有效,因为只要触摸长屏幕,isTouched就会注册。Gdx.input.getX/Y应该更新。你可能有一些东西只跑了一次,然后继续移动你的玩家,直到注册发布。

最新更新