【问题标题】:Implementing multiple inputs in libgdx在 libgdx 中实现多个输入
【发布时间】:2016-02-06 13:03:55
【问题描述】:

我正在研究 libgdx 中的控件。我有 3 个按钮:向上、向右和向左。如果我单独单击按钮,一切都会正常工作。

同时点击 2 个按钮时,例如向右和向上按钮,玩家没有流畅的跳跃。它被击中并且无法正确渲染。

我的部分代码:

//Up Button
buttonup.addListener(new InputListener() {
    @Override
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {

        gamehero.heroBody.setLinearVelocity(0, 4f);

        return true;
    }
});

//Right Button
buttonright.addListener(new InputListener() {

    @Override
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){

        gamehero.heroBody.setLinearVelocity(2f, 0);
        return true;
    }

    @Override
    public void touchUp(InputEvent event, float x, float y, int pointer, int button) {

        gamehero.heroBody.setLinearVelocity(0, 0);
    }
});

//Left Button
buttonleft.addListener(new InputListener() {

    @Override
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {

        if gamehero.heroBody.setLinearVelocity(-2f, 0);
            return true;
    }

    @Override
    public void touchUp(InputEvent event, float x, float y, int pointer, int button) {

        gamehero.heroBody.setLinearVelocity(0, 0);

    }
});

我浏览过很多帖子,尤其是this,但这对我没有帮助。我觉得必须用指针来做一些事情,但我不知道如何将它与按钮一起使用。如果我同时触摸 2 个按钮,有什么方法可以让播放器呈现流畅的渲染?

【问题讨论】:

    标签: button input libgdx


    【解决方案1】:

    每个按钮应该只影响您的一个坐标方向的运动:x 代表左/右,y 代表上。因此,与其将另一个方向的速度设置为 0,不如将其保持不变(即将其设置为可能存储在 gamehero 中的当前值)。

    例如您的向上按钮应设置如下:

    gamehero.heroBody.setLinearVelocity(gamehero.velocity.x, 4f);
    

    【讨论】:

      猜你喜欢
      • 2014-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-02
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      相关资源
      最近更新 更多