2016-08-18 20 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方法,但如果有另一种解决方案来获得滚动背景的好节目,告诉我他们 –