我对 Java、libGDX 和 android 开发还很陌生,我已经浏览了许多教程视频,现在决定自己冒险。我知道,有风险。
无论如何,我希望有一个极其简单的游戏,并且在这个游戏中我希望背景无限滚动。就像《Flappy Bird》或《Doodle Jump》中的那样。
目前,我有一个 5760 x 1080 的大图像,我水平滚动它,如下所示:
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.translate(10, 0);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();
}
我相信您会知道,这会以每次渲染 10 像素的速率沿背景移动相机。问题是,图像的大小有限,所以我希望它本质上从头开始重新启动图像。
我已经查过如何做到这一点,但对这一切感到完全困惑,所以我想尝试问一个具体的问题。
除了创建我的纹理和 spriteBatch 之外,实际上没有其他代码,正如我所说,到目前为止这是一件非常简单的事情。
如何让背景图像重新加载并永久继续?
最佳答案
LibGDX 有这样的类:
public class ParallaxLayer {
public TextureRegion region ;
public Vector2 parallaxRatio;
public Vector2 startPosition;
public Vector2 padding ;
public ParallaxLayer(TextureRegion region,Vector2 parallaxRatio,Vector2 padding){
this(region, parallaxRatio, new Vector2(0,0),padding);
}
/**
* @param region the TextureRegion to draw , this can be any width/height
* @param parallaxRatio the relative speed of x,y {@link ParallaxBackground#ParallaxBackground(ParallaxLayer[], float, float, Vector2)}
* @param startPosition the init position of x,y
* @param padding the padding of the region at x,y
*/
public ParallaxLayer(TextureRegion region, Vector2 parallaxRatio, Vector2 startPosition, Vector2 padding){
this.region = region;
this.parallaxRatio = parallaxRatio;
this.startPosition = startPosition;
this.padding = padding;
}
}
并且:
public class ParallaxBackground {
private ParallaxLayer[] layers;
private OrthographicCamera camera;
private SpriteBatch batch;
private Vector2 speed = new Vector2();
/**
* @param layers The background layers
* @param width The screenWith
* @param height The screenHeight
* @param speed A Vector2 attribute to point out the x and y speed
*/
public ParallaxBackground(ParallaxLayer[] layers,float width,float height,Vector2 speed){
this.layers = layers;
this.speed.set(speed);
camera = new OrthographicCamera(width, height);
batch = new SpriteBatch();
}
public void render(float delta){
this.camera.position.add(speed.x*delta,speed.y*delta, 0);
for(ParallaxLayer layer:layers){
batch.setProjectionMatrix(camera.projection);
batch.begin();
float currentX = - camera.position.x*layer.parallaxRatio.x % ( layer.region.getRegionWidth() + layer.padding.x) ;
if( speed.x < 0 )currentX += -( layer.region.getRegionWidth() + layer.padding.x);
do{
float currentY = - camera.position.y*layer.parallaxRatio.y % ( layer.region.getRegionHeight() + layer.padding.y) ;
if( speed.y < 0 )currentY += - (layer.region.getRegionHeight()+layer.padding.y);
do{
batch.draw(layer.region,
-this.camera.viewportWidth/2+currentX + layer.startPosition.x ,
-this.camera.viewportHeight/2 + currentY +layer.startPosition.y);
currentY += ( layer.region.getRegionHeight() + layer.padding.y );
}while( currentY < camera.viewportHeight);
currentX += ( layer.region.getRegionWidth()+ layer.padding.x);
}while( currentX < camera.viewportWidth);
batch.end();
}
}
}
您可以将这些类复制/粘贴到您的项目中。然后,要使用,请执行以下操作:
ParallaxBackground background = new ParallaxBackground(new ParallaxLayer[]{
new ParallaxLayer(textureRegion, new Vector2(1, 1), new Vector2(0, 0)),
}, 1080, 720, new Vector2(50, 0));
然后在 render()
方法中执行 background.render(delta)
。
您可以向 ParallaxBackground
构造函数添加更多层以满足您的需求。
关于android - libGDX 中重复滚动背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39453896/