2017-12-24 18 views
1

私はlibgdx上で無限のスクロールバックグラウンドを実行しようとしています。画像はX軸でスクロールしていますが、Y軸では画像が下図のように半分に分割されています。どのように画面の下部に地面を表示させることができますか? enter image description herelibgdxの無限スクロールの背景

は、ここであなたがcam.setToOrtho()ビューポートの幅と高さに渡す必要があります私のコード

package com.tashtoons.game.States; 

import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.tashtoons.game.BuckyRun; 
import static com.badlogic.gdx.graphics.Texture.TextureWrap.Repeat; 



public class PlaySate extends State { 
    private Texture bg; 
    float sourceX = 0; 

    public PlaySate(GameStateManager gsm) { 
     super(gsm); 
     bg=new Texture("bg.png"); 
     cam.setToOrtho(false,BuckyRun.WIDTH/2,BuckyRun.HEIGHT/2); 
     bg.setWrap(Repeat,Repeat); 
    } 

    @Override 
    protected void handleInput() { 

    } 

    @Override 
    public void update(float dt) { 

    } 

    @Override 
    public void render(SpriteBatch sb) { 
     sourceX += 10; 
     sb.setProjectionMatrix(cam.combined); 
     sb.begin(); 
     sourceX = (sourceX + Gdx.graphics.getDeltaTime()/3/4) % bg.getWidth(); 
     sb.draw(bg, 0, 0, (int) sourceX, 0,BuckyRun.WIDTH, BuckyRun.HEIGHT); 
     sb.end(); 

    } 

    @Override 
    public void dispose() { 

    } 

    } 

答えて

1

です。

public class PlaySate extends State { 
    private Texture bg; 
    float sourceX = 0; 

    public PlaySate(GameStateManager gsm) { 
     super(gsm); 
     bg=new Texture("bg.png"); 
     cam.setToOrtho(false,BuckyRun.WIDTH,BuckyRun.HEIGHT); // edited this 
     bg.setWrap(Repeat,Repeat); 
    } 


    @Override 
    public void render(SpriteBatch sb) { 
     sourceX += 10; 
     sb.setProjectionMatrix(cam.combined); 
     sb.begin(); 

     // this line doesn't make much sense, you've already incremented sourceX, 
     // "Gdx.graphics.getDeltaTime()/3/4" especially confuses me 
     // why sourceX =% bg.getWidth(); isn't enough? 
     sourceX = (sourceX + Gdx.graphics.getDeltaTime()/3/4) % bg.getWidth(); 

     // you probably want to draw texture in full screen 
     sb.draw(bg, 
       // position and size of texture 
       0, 0, WIDTH, HEIGHT, 
       // srcX, srcY, srcWidth, srcHeight 
       (int) sourceX, 0, bg.getWidth(), bg.getHeight(), 
       // flipX, flipY 
       false, false); 
     sb.end(); 

    } 
} 

を更新する代わりに以下の2行の

sourceX += 10; 
... 
sourceX = (sourceX + Gdx.graphics.getDeltaTime()/3/4) % bg.getWidth(); 
... 

そこがすべき はまた、あなたはおそらく、テクスチャ(コードで左のコメント)を描画するためのコードを修正したいです1つだけです:

sourceX = (sourceX + Gdx.graphics.getDeltaTime() * velocity) % bg.getWidth(); 
+0

ありがとう – tashtoons

+0

"Gdx.graphics.getDeltaTime()/ 3/4"の行について。なぜ3/4で割りますか?インクリメンスsourceXは次のようになります。 'sourceX =(sourceX + Gdx.graphics.getDeltaTime()* velocity)%bg.getWidth();' – Arctic45