2017-10-16 86 views
1

我正在建立亞軍遊戲。 地面類。Libgdx 2d樓動畫

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

+0

你是指地面紋理之間的差距?我們能看到這個問題的一個形象嗎? –

+0

更新已添加圖像.. –

回答

0

WOW簡單的解決方案不敢相信我沒看出來。

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; 

}