【问题标题】:Lbgdx: Repeating and scrolling background imageLbgdx:重复和滚动的背景图像
【发布时间】:2016-09-27 13:38:07
【问题描述】:

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

主游戏类:

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;
    }

处理时:

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

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

还有其他更好的选择吗?

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

谢谢!提前:)

【问题讨论】:

  • Background.wrap 或类似的东西需要设置。
  • 这是一个我问过并回答过的关于如何在 LibGDX 中实现滚动视差背景的问题。 stackoverflow.com/q/24587722/627005

标签: android libgdx game-engine


【解决方案1】:

您可以通过在 libgdx 中使用“翻译”方法来简单地实现这一点。这将为您提供稳定和恒定的输出。请试试这个例子。

 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

【讨论】:

    【解决方案2】:

    我在您的代码中没有注意到或者您可能忽略添加的一件事是调用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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 2013-08-14
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多