libgdx矩形的平滑运动

时间:2014-03-20 06:27:16

标签: android libgdx game-physics

我想在触摸时平滑地移动两个物体。

这是我的代码:

for(int i = 0; i <96; i++){
    Asstest.rect_pipe_down.y--);
}

这应该将矩形96像素向下移动(SMOOTH)

但它没有平滑就关闭......

我错了什么?

如果你触摸,管道应该关闭,但不要硬,平滑,如果它们关闭。 但是通过以下代码,他们只是努力接近......

以下是完整的代码:

    if(Gdx.input.isTouched()){
        Assets.rect_pipe_down.y = 512 - 320/2;
        Assets.rect_pipe_up.y = -320 + 320/2;
        for (int i = 0; i < 96; i++){
            smoothTime = TimeUtils.millis();
            if(TimeUtils.millis() - smoothTime > 10) {
                Assets.rect_pipe_down.y--;
                Assets.rect_pipe_up.y++;
                batch.begin();
                    batch.draw(Assets.region_pipe_down, Assets.rect_pipe_down.x, Assets.rect_pipe_down.y);
                    batch.draw(Assets.region_pipe_up, Assets.rect_pipe_up.x, Assets.rect_pipe_up.y);
                batch.end();
            }
        }  
        closed = true;
    }

1 个答案:

答案 0 :(得分:0)

您无法在一次render()调用中多次渲染,一次调用仅用于绘制一帧。在您当前的代码中,后面的图像只会覆盖以前的图像。

你可以做的是有一个变量,它存在于帧之间,它存储管道当前是否正在关闭,速度的常量和一些条件告诉它们何时可以停止 - 也许当它们距离每个都有一定的距离时其他,不知道你想要什么。无论如何,这就是我在我的例子中使用的。

然后在render()方法中,在绘制任何内容之前,您可以这样做:

if (closing) {
    Assets.rect_pipe_down.y -= CLOSE_SPEED * delta;
    Assets.rect_pipe_up.y += CLOSE_SPEED * delta;
    if (Assets.rect_pipe_down.y - Assets.rect_pipe_up.y < TARGET_DIST) {
        Assets.rect_pipe_down.y = Assets.rect_pipe_up.y + TARGET_DIST;
        closing = false;
    }
}

此处,closing是您希望它们开始关闭时设置为true的变量,其他变量是常量。如果你想确保它们最终在一个独立于帧率的特定高度上,你可以添加一些变量/常量。

相关问题