java - Libgdx 2d 地板动画

标签 java android libgdx

我正在构建 runner 游戏。 地面级。

public class Ground extends Actor {

private Texture texture;
private ArrayList<Vector2> groundLocations;
private float speed;
private float cameraLeft, cameraRight;
private int textureWidth;


public Ground() {
    this.cameraRight = Const.GAME_WIDTH + Const.GAME_MARGIN;
    this.cameraLeft = 0 - Const.GAME_MARGIN;
    texture = new Texture(Gdx.files.internal("ui/ground.png"));
    groundLocations = new ArrayList<Vector2>();
    init();
}

public void setSpeed(float speed) {
    this.speed = speed;
}

private void init() {

    textureWidth = texture.getWidth();
    float currentPosition = cameraLeft;
    while (currentPosition < cameraRight) {

        Vector2 newLocation = new Vector2(currentPosition, 0);
        groundLocations.add(newLocation);
        currentPosition += textureWidth;
    }

}

public int getFloorHeight() {
    return texture.getHeight();
}


@Override
public void act(float delta) {
    super.act(delta);
    int size = groundLocations.size();

    for (int i = 0; i < size; i++) {
        Vector2 location = groundLocations.get(i);
        location.x -= delta * speed;
        if (location.x < cameraLeft) {

            location.x = findMax().x + textureWidth;
        }
    }

}

private Vector2 findMax() {
    return Collections.max(groundLocations, new Vector2Comparator());
}


@Override
public void draw(Batch batch, float parentAlpha) {
    super.draw(batch, parentAlpha);
    for (Vector2 location : groundLocations) {
        batch.draw(texture, location.x, location.y);
    }
}




public void dispose() {
    if (texture != null)
        texture.dispose();
}

}

  • 地面纹理为 128x128

  • GAME_WIDTH = 1024f

  • GAME_MARGIN = 250f

  • 速度=变化。

本地面根据速度和 FPS 移动时。 (速度*增量) 问题是:地面纹理之间总是存在间隙。经过一定的运动后 函数 findMax 查找具有最大 X 的纹理 任何帮助将不胜感激。

更新:图像 Image 1

最佳答案

WOW Simply Solution 不敢相信我没有看到它。

   for (int i = 0; i < size; i++) {
    Vector2 location = groundLocations.get(i);
    if (location.x < cameraLeft) {

        location.x = findMax().x + textureWidth;
    }          
    location.x -= delta * speed;

}

关于java - Libgdx 2d 地板动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46776179/

相关文章:

java - Libgdx 中奇怪的缩放

java - Libgdx 1.2.0 Controller 扩展崩溃

java - 用于将 exec-maven-plugin 替换为 maven-antrun-plugin 的脚本

android - react native 构建失败

android - Android Studio 中窗口类型 2002 的权限被拒绝

android - 无法在未调用 Looper.prepare 的线程内创建处理程序

java - Libgdx 双指平移而不是单指?

java - 关于子类中重写的困惑

java - 将日期选择器值放入 java 中的列表中

java - 文本文件io和换行符