Java libgdx上的无限滚动背景
我试图在libgdx上做一个无限滚动背景。图像在X轴上滚动良好,但在Y轴上,图像被一分为二,如下图所示。如何从屏幕底部开始接地 这是我的密码Java libgdx上的无限滚动背景,java,android,libgdx,infinite-scroll,Java,Android,Libgdx,Infinite Scroll,我试图在libgdx上做一个无限滚动背景。图像在X轴上滚动良好,但在Y轴上,图像被一分为二,如下图所示。如何从屏幕底部开始接地 这是我的密码 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.tashtoo
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() {
}
}
您应该传递到
cam.setToOrTo()
视口宽度和高度。
此外,您可能还希望更正用于绘制纹理的代码(代码中的左注释):
更新
而不是这两行:
sourceX += 10;
...
sourceX = (sourceX + Gdx.graphics.getDeltaTime() / 3/4) % bg.getWidth();
...
应该只有一个:
sourceX = (sourceX + Gdx.graphics.getDeltaTime() * velocity) % bg.getWidth();
非常感谢您使用“Gdx.graphics.getDeltaTime()/3/4”完成这一行。你为什么要除以3/4?递增sourceX应该如下所示:
sourceX=(sourceX+Gdx.graphics.getDeltaTime()*velocity)%bg.getWidth()代码>
sourceX = (sourceX + Gdx.graphics.getDeltaTime() * velocity) % bg.getWidth();