【问题标题】:Move Background Screen in libgdx game(android)在 libgdx 游戏中移动背景屏幕(android)
【发布时间】:2014-03-01 13:57:56
【问题描述】:

我正在使用适用于 Android 的 libgdx 开发一个简单的游戏,我想在其中移动背景场景,就像 Bird 正在向前移动一样。我尝试了互联网上提供的各种示例和代码来移动或翻译相机,但无济于事。请帮助我并建议一些代码来完成此操作。

这是我的代码中的 sn-p:

x+=0.1;
            trunk_rec.x-=10;
            trunk1_rec.x-=10;
            bird_rec.y-=5;
            if(trunk_rec.x<0)
            {
                trunk_rec.x=trunk1_rec.x=900;
            }

            camera.translate(1,0);
            camera.update();
            game.batch.setProjectionMatrix(camera.combined);

            game.batch.begin();
            game.batch.draw(bg,0, 0,800,500);

            game.batch.draw(trunk,trunk_rec.x,trunk_rec.y);
            game.batch.draw(trunk,trunk1_rec.x,trunk1_rec.y);
            game.batch.draw(bird,bird_rec.x,bird_rec.y);

            game.batch.end();

            if(trunk_rec.x==bird_rec.x)
            {
                score++;
            }

            if(Gdx.input.isTouched())
            {
                bird_rec.y+=30;
            }

            if((trunk_rec.overlaps(bird_rec))|| trunk1_rec.overlaps(bird_rec) || bird_rec.y<10)
            {
                Gdx.app.exit();
                System.out.println("Score is "+score);
            }

我也尝试使用 camera.position 来移动相机。

谢谢!

【问题讨论】:

  • 更好的方法是移动小鸟而不是背景。向我们展示背景/鸟渲染的代码。
  • @noone : 但这是要求!!

标签: android camera screen libgdx


【解决方案1】:

你可以使用 Actors 和 Actions

使用一个以上的地面演员来填充屏幕宽度。

 if(isout){
 isout=false;
 ground1.addAction(Actions.sequence(Actions.moveTo(-ground.getWidth(),ground.getY(),duration),isDone));
 }
 Action isDone = new Action() {

    @Override
    public boolean act(float delta) {
                             isout=true;
                             ground1.setPosition(screenWidth,ground.getY());
        return true;
    }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多