2017-10-16 2 views
1

저는 러너 게임을 만들고 있어요. 지상 교실.Libgdx 2 층 floor 애니메이션

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

}

  • 접지 질감 = 1024f

  • GAME_MARGIN가 128x128

  • GAME_WIDTH = 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; 

}