Libgdx滚动背景线的外观

时间:2016-08-18 06:13:20

标签: java android libgdx screen

我使用Inkscape创建一个背景,我使用这个背景的2个相同的图像来显示移动的背景,但是当我运行游戏时,它们之间会出现一条线,任何解决方案?

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

为了澄清更多这是我的背景代码:

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

    }
GameStage类中的

相机设置:

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

2 个答案:

答案 0 :(得分:0)

你可能计算错了,因为右边有一列黑点。

如果按照屏幕右侧计算,请注意不要宽度。它将是width-1,因为x从零开始。

但这不仅是你的错误。但我不能仅仅从一张图片中猜出其他错误。 如果你提供一些鳕鱼,你应用的过滤器或你使用的原始纹理会更好。

答案 1 :(得分: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方法就会产生一个可见的断续,因为你不会在那个帧上移动背景。无论是否跳过矩形,都应该在每一帧上移动背景。