android - 如何移动角色 - 尝试应用 touchDown 和 touchUp

标签 android libgdx

使用示例 with the bucket 我正在尝试而不是使用

 if(Gdx.input.isTouched()) {
         Vector3 touchPos = new Vector3();
         touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);

使用 touchUp 和 touchDown。

所以,为了使用它们,我定义:

 Vector2    position = new Vector2();
 Vector2    velocity = new Vector2();

然后:

public boolean touchDown(int x, int y, int pointer, int button) {

    if (x < 800 / 2 && y > 480 / 2) {
        //here?? for left movement
    }
    if (x > 800 / 2 && y > 480 / 2) {
        //here?? for right movement
    }
    return true;

}

一般来说,我知道我有一个位置和一个速度。我必须更新与速度相关的位置,但我不知道该怎么做。

最佳答案

您必须使用增量时间和速度矢量每帧更新对象的位置。

像这样的东西(在渲染中):

position.set(position.x+velocity.x*delta, position.y+velocity.y*delta);

和:

public boolean touchDown(int x, int y, int pointer, int button) {

    if (x < 800 / 2 && y > 480 / 2) {
        //here?? for left movement
        velocity.x = -10;
    }
    if (x > 800 / 2 && y > 480 / 2) {
        //here?? for right movement
        velocity.x = 10;
    }
    return true;
}

public boolean touchUp(int x, int y, int pointer, int button) {
    velocity.x = 0;
}

关于android - 如何移动角色 - 尝试应用 touchDown 和 touchUp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17220633/

相关文章:

java - XML 布局存在一些问题(android 开发)

javascript - 视口(viewport)缩放反弹

android - 应用程序在 Android Market 中丢失

model - 如何在 libgdx 中先缩放然后旋转模型实例

android - LibGDX Box2D 绳索疯狂地拉伸(stretch)

android - 恢复 Kivy 应用程序,即使最小化(未关闭)到最近的 Activity

Android检查用户之前登录,否则开始登录 Activity

java - 折线轮廓构造/绘制粗折线

java - 从 libgdx 项目加载 Activity

java - LibGDX 仅渲染(如果相机看到它)