Libgdx 自动滚动相机



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

为了得到这个,我使用了一个精灵作为背景,很多精灵作为正方形。然后是摄像头来移动屏幕。

在渲染方法中,我有:

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

这是构造函数上的代码。这就是我在班上所拥有的一切

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. 创建具有屏幕大小的精灵并将其保持在底部。
  2. 在屏幕顶部生成随机水平位置的正方形,并更改所有正方形(精灵(的y位置
  3. 无需移动相机。 将其保持在屏幕中间。

最新更新