2016-09-27 93 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(); 
}