Libgdx 自动滚动相机

Libgdx auto-scrolling camera

我正在开发 libgdx 游戏。我想要做的是创建一个屏幕,其中一些正方形 "falls" 从顶部到底部。所以:
1.我要设置背景图
2. 我必须随机生成一些正方形。
3. 我要把屏幕从底部移到顶部。

为了实现这一点,我使用了 Sprite 作为背景,为正方形使用了很多 Sprite。然后一个摄像头移动屏幕。

在渲染方法中我有:

 @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        Gdx.input.setInputProcessor(stage);

        batch.begin();

        backgroundSprite.draw(batch);

        for(Square square : squareList) {
            //Gdx.app.log("[Match]", square.toString());
            square.updatePosition();
            square.setSize(20, 20);
            square.draw(batch);
        }
        batch.end();
        batch.setProjectionMatrix(fixedCamera.view);

        fixedCamera.position.y +=1;
        System.out.println("Camera position: x:" +fixedCamera.position.x+" y:"+fixedCamera.position.y);
        fixedCamera.update();
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }

这是构造函数中的代码。这就是我在 class

中的全部
final static int WIDTH = Gdx.app.getGraphics().getWidth();
final static int HEIGHT = Gdx.app.getGraphics().getHeight();
skin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json"));
        fixedCamera = new OrthographicCamera(WIDTH, HEIGHT );
        stage = new Stage(new ScreenViewport());
        batch = new SpriteBatch();
        dynBatch = new SpriteBatch();
        backgroundSprite = new Sprite(new Texture(Gdx.files.internal(scenario.getTexturePath())));
        backgroundSprite.setPosition(0,0);
        backgroundSprite.setSize(WIDTH, HEIGHT);
        fixedCamera.position.set(0,0, 0);
        fixedCamera.update();

项目显示正确,但是当相机移动时,所有精灵都消失了... 我该如何解决?有什么方法可以更有效地处理它吗?

您的要求可以通过以下步骤实现:

  1. 创建具有屏幕大小的 Sprite 并将其保持在底部。
  2. 在屏幕顶部生成具有随机水平位置的正方形并更改所有正方形(精灵)的 y 位置
  3. 无需移动相机。保持在屏幕中间。