【问题标题】:Libgdx Character MovementLibgdx 角色移动
【发布时间】:2016-05-21 20:27:43
【问题描述】:

我最近进入了 Libgdx。我的 ontouch 只工作了一半;我知道当我运行以下命令时正在检测到触摸:

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
player.b2body.applyLinearImpulse(new Vector2(5.1f, 0), player.b2body.getWorldCenter(), true);

        return true;
    }

我的角色确实向右跑,从而证明我的触摸设置正确并且正在被检测到。

我遇到的问题是让角色向左或向右运行,具体取决于用户在屏幕上触摸的位置。如果用户向右按,角色需要向右移动,如果按向左,它应该向左移动,在 x 轴上。我为此写的内容如下:

@Override
public boolean touchDown(int X, int Y, int pointer, int button) {

 //   player.b2body.applyLinearImpulse(new Vector2(5.1f, 0), player.b2body.getWorldCenter(), true);
    if(  player.getX()> X ){
        player.b2body.applyLinearImpulse(new Vector2(5.1f, 0), player.b2body.getWorldCenter(), true);
    }

    else if(  player.getX()< X ){
        player.b2body.applyLinearImpulse(new Vector2(-5.1f, 0), player.b2body.getWorldCenter(), true);
    }
    return true;
}

但是使用我的代码,我的角色不会移动,我想我正在错误地检查玩家的位置。谁能告诉我什么/我做错了什么?

【问题讨论】:

    标签: java android libgdx ontouchlistener ontouch


    【解决方案1】:

    触摸位置以屏幕坐标给出。您的玩家存在于您创建的任何世界坐标中。您需要使用Camera.unproject 使用您的相机将屏幕坐标转换为世界坐标。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 2013-11-16
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多