Lbgdx:重复和滚动背景图像

时间:2016-09-27 13:38:07

标签: android libgdx game-engine

您好我正在开发游戏,我需要滚动和重复背景。以下是我的代码:

主要游戏类:

public class MyGdxGame extends ApplicationAdapter {

SpriteBatch batch;
BitmapFont font;
float bgX =0f;
Texture background,background2;

创建:

public void create () {
    batch = new SpriteBatch();
    background = new Texture("background.png");
    background2 = new Texture("background.png");

渲染:

public void render () {

    batch.begin();
    bgX -=1;
    batch.draw(background,bgX,0,background.getWidth(),Gdx.graphics.getHeight());
    batch.draw(background2,background.getWidth() + bgX,0,background.getWidth(),Gdx.graphics.getHeight());


    if(bgX <- background.getWidth())
    {
        bgX = 0;
    }

On Dispose:

public void dispose () {
    batch.dispose();
    background.dispose();

然而,我得到了我想要的结果,但是在分钟之后,滚动变得非常慢。

还有其他更好的选择吗?

我需要,背景从右到左重复滚动,背景高度等于Gdx.graphics.getHeight()

谢谢!事先:)

2 个答案:

答案 0 :(得分:0)

您可以通过在libgdx中使用“translate”方法来实现此目的。这将为您提供稳定和恒定的输出。请试试这个例子。

 public class myGame extends ApplicationAdapter{
    public static Sprite sprite,sprite2;
    public static texture;
    public spriteBatch batch;
        public myGame () {

        }

        @Override
        public void create() {
            texture = new Texture(Gdx.files.internal("background.png"));

            texture2 = new Texture(Gdx.files.internal("background.png"));
            sprite = new Sprite(texture, 0, 0, texture.getWidth(), texture.getHeight());
            sprite2 = new Sprite(texture2, 0, 0, texture.getWidth(), texture.getHeight());
            sprite.setPosition(0, 0);
            sprite2.setPosition(1280, 0);
            batch=new spriteBatch(sprite);



        }
     public void bckgroundMovment()
    {
sprite.translate(-1.5f, 0);

// here 1280 is the screen width
                sprite2.translate(-1.5f, 0);
                if (sprite.getX() < -1280) {
                    sprite.setPosition(1280, 0);
                }
                if (sprite2.getX() < -1280) {
                    sprite2.setPosition(1280, 0);
                }
    }
        @Override
        public void render(float delta) {
        bckgroundMovment()

    batch.begin()
    sprite.draw(batch);
    sprite2.draw(batch);
    batch.end();


        }

        @Override
        public void draw(SpriteBatch batch, float deltaTime) {
            settingUp(Gdx.graphics.getDeltaTime());

            sprite.draw(batch);
            sprite2.draw(batch);
        }

    }

// it will defenitly work ..good luck

答案 1 :(得分:0)

我在代码中没有注意到的一件事,或者您可能忽略了添加的内容,就是调用batch.end()batch.begin()之后应始终调用它。

我使用Actors实现了垂直/水平视差背景,但这应该适用于您的方法:

public void render () {
  batch.begin();

  float left = bgX - 1;
  float backgroundWidth = background.getWidth();
  if (left <= -backgroundWidth) { // right side is now off the screen to the left
     bgX += backgroundWidth; // smoothly reset position
  }

  bgX -= 1; // continue scrolling

  batch.draw(background, bgX, 0, backgroundWidth, Gdx.graphics.getHeight());
  batch.draw(background2, backgroundWidth + bgX, 0, backgroundWidth, Gdx.graphics.getHeight());

  batch.end();
}