简体   繁体   English

Libgdx滚动线的背景外观

[英]Libgdx Scrolling Background appearance of a line

I create a background using Inkscape and i use 2 same images of this background to show a moving background but a line appear between them when i run the game, any solutions ? 我使用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 背景的重复很清楚

To clarify more this is my Background code : 为了澄清更多,这是我的背景代码:

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

    }

in GameStage class Camera settings : 在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();
    }
...

You are probably calculating wrong because you got a column of black dots on right. 您可能计算错了,因为您在右边有一列黑点。

Be careful if you are calculating acording to right of the screen so its not will be width. 如果要根据屏幕右侧进行计算,请小心,以免宽度不正确。 It will be width-1 because x starting with a zero. 因为x从零开始,所以它将为width-1。

But this is not only error you have. 但这不仅是您的错误。 But i cant guess other errors from just a picture. 但是我无法仅凭图片猜测其他错误。 It would be better if you provide some cod, filters you apply or original texture that you used. 如果提供一些鳕鱼,您应用的过滤器或使用的原始纹理会更好。

Your resetBounds() method introduces error in the spacing between the two rectangular regions. 您的resetBounds()方法会在两个矩形区域之间的间距中引入错误。 When leftBoundsReached() returns true, the position of your region 2 bounds is not going to be an exact multiple of 400 or 800 or whatever spacing you're using. leftBoundsReached()返回true时,区域2边界的位置将不会是400或800的精确倍数,也不会是您使用的任何间距。 But resetBounds() creates a new set of bounds that are exactly at x = 800. 但是resetBounds()创建了一组刚好在x = 800的边界。

I would suggest something like this: 我建议这样的事情:

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

By the way, I think your act method will produce a possibly visible stutter whenever the left bounds are reached, because you don't move the background on that frame. 顺便说一句,我认为只要达到左边界,您的act方法就会产生可能可见的结结,因为您不会在该帧上移动背景。 The background should be moved on every frame regardless if it's time to skip a rectangle over. 无论是否要跳过矩形,都应在每帧上移动背景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM