2016-09-27 58 views
0

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

主要游戏类别:

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()

谢谢!提前:)

+0

Background.wrap或需要设置类似的东西。 – Madmenyo

+0

这是一个我已经询问并回答了如何在LibGDX中实现滚动视差背景的问题。 http://stackoverflow.com/q/24587722/627005 –

回答

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 
0

一件事就是打电话batch.end(),应该始终batch.begin()后调用。

我实现了用演员垂直/水平视差的背景,但是这应该为你的方法工作:

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