2016-08-18 47 views
0

我使用Inkscape創建背景,我使用2個背景相同的圖像來顯示移動的背景,但是當我運行遊戲時出現一條線,任何解決方案?Libgdx Scrolling背景外觀一條線

Picture for the problem in a background the line appears and disappears

Picture for the problem in another background ; the repeat of background is clear

爲了澄清更多的,這是我的後臺代碼:

在GameStage類相機設置
public class Background extends Actor { 


     private final TextureRegion textureRegion; 
     private Rectangle textureRegionBounds1; 
     private Rectangle textureRegionBounds2; 
     private int speed = 70; 

     public Background() { 
      textureRegion = new TextureRegion(new Texture(Gdx.files.internal(Constants.BACKGROUND_IMAGE_PATH))); 
      textureRegionBounds1 = new Rectangle(-800/2, 0, 800,480); 
      textureRegionBounds2 = new Rectangle(800/2, 0, 800, 480); 
     } 

     @Override 
     public void act(float delta) { 
      if (leftBoundsReached(delta)) { 
       resetBounds(); 
      } else { 
       updateXBounds(-delta); 
      } 
     } 

     @Override 
     public void draw(Batch batch, float parentAlpha) { 
      super.draw(batch, parentAlpha); 
      batch.draw(textureRegion, textureRegionBounds1.x, textureRegionBounds1.y, 800,480); 
      batch.draw(textureRegion, textureRegionBounds2.x, textureRegionBounds2.y, 800,480); 
     } 

     private boolean leftBoundsReached(float delta) { 
      return (textureRegionBounds2.x - (delta * speed)) <= 0; 
     } 

     private void updateXBounds(float delta) { 
      textureRegionBounds1.x += delta * speed; 
      textureRegionBounds2.x += delta * speed; 
     } 

     private void resetBounds() { 
      textureRegionBounds1 = textureRegionBounds2; 
      textureRegionBounds2 = new Rectangle(800, 0, 800, 480); 
     } 

    } 

... 
     private static final int VIEWPORT_WIDTH = 800; 
     private static final int VIEWPORT_HEIGHT = 480; 
... 
    public GameStage(){ 
      super(new ScalingViewport(Scaling.stretch, VIEWPORT_WIDTH, VIEWPORT_HEIGHT, 
        new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT))); 
      Gdx.input.setInputProcessor(this); 
      // renderer = new Box2DDebugRenderer(); 
      setUpWorld(); 
      setupCamera(); 

     } 
... 
private void setupCamera() { 
     camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT); 
     camera.position.set(camera.viewportWidth/2, camera.viewportHeight/2, 0f); 
     camera.update(); 
    } 
... 
+0

這些圖像是在紋理圖集,還是在運行時從單獨的圖像文件加載?你在用什麼紋理過濾? – Tenfour04

+0

感謝您的評論,我使用TextureRegion,我編輯我的問題,你可以更多地瞭解我 –

回答

0

你可能會計算錯了,因爲你有一列黑色點在右邊。

要小心,如果你正在計算屏幕的權利,所以它不會是寬度。它將是寬度-1,因爲x以零開始。

但這不僅僅是錯誤。但我不能從一張圖片猜測其他錯誤。 如果您提供一些鱈魚,您使用的過濾器或您使用的原始紋理,會更好。

+0

謝謝,我認爲座標是正確的,我編輯我的問題的代碼 –

0

您的resetBounds()方法在兩個矩形區域之間的間距中引入誤差。當leftBoundsReached()返回true時,區域2界限的位置不會是400或800或您正在使用的任何間距的精確倍數。但resetBounds()創建一個新的組都正好位於x界= 800

我建議是這樣的:

private void resetBounds() { 
     Rectangle tmp = textureRegionBounds1; 
     textureRegionBounds1 = textureRegionBounds2; 
     textureRegionBounds2 = tmp; 
     textureRegionBounds2.x = textureRegionBounds1.x + textureRegionBounds1.width; 
    } 

順便說一句,我想你act方法將產生一個可能可見的口吃時達到了左邊界,因爲您不移動該框架上的背景。無論是否需要跳過矩形,都應在每一幀上移動背景。

+0

謝謝你的答案,我嘗試你的resetBounds方法,但如果有另一種解決方案來獲得滾動背景的好節目,告訴我他們 –