2017-10-16 3 views
1

私はRunner Gameをビルドしています。 グランドクラス。Libgdx 2階アニメーション

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(); 
} 

}

  • グラウンドテクスチャ変化= 128×128

  • GAME_WIDTH = 1024f

  • GAME_MARGIN = 250F

  • 速度です。

スピードとFPSに従って地面が動いている間。 (Speed * delta) 問題:地面のテクスチャの間に常にギャップがあります。特定の動きの後に 関数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; 

} 
関連する問題