2016-09-27 6 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.begin()後に呼ばれるべきbatch.end()を呼び出すことです。

私は俳優を使って、水平/垂直視差の背景を実装しましたが、これはあなたのアプローチのために働く必要があります。

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(); 
} 
関連する問題