所以,我正在为Android游戏创建欢乐,因此在我的Touching类(实现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上移动,直到我的手指离开屏幕(touchUP正在调用),然后它站立,它不会在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应该更新。您可能在那里只运行了一次,然后继续移动播放器,直到注册了发行版。

08-18 17:21
查看更多