【问题标题】:Libgdx Scrolling Background appearance of a lineLibgdx 滚动背景线条的外观
【发布时间】:2016-08-18 06:13:19
【问题描述】:

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

【问题讨论】:

  • 这些图像是在纹理图集中,还是在运行时从单独的图像文件中加载?你用的是什么纹理过滤?
  • 感谢您的评论,我使用 TextureRegion,我编辑了我的问题,您可以更了解我

标签: java android libgdx screen


【解决方案1】:

您可能计算错误,因为右侧有一列黑点。

如果您根据屏幕右侧进行计算,请小心,以免它是宽度。它将是 width-1,因为 x 以零开头。

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

【讨论】:

  • 谢谢,我认为坐标是正确的,我用代码编辑了我的问题
【解决方案2】:

您的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 方法会在达到左边界时产生可能可见的卡顿,因为您没有移动该帧上的背景。无论是否要跳过一个矩形,都应该在每一帧上移动背景。

【讨论】:

  • 感谢您的回答,我尝试了您的 resetBounds 方法,但我仍然有相同的问题,请告诉我是否有其他解决方案可以为滚动背景提供良好的显示效果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多