gpt4 book ai didi

android - 在 libGDX 中重复滚动背景?

转载 作者:搜寻专家 更新时间:2023-11-01 09:42:08 24 4
gpt4 key购买 nike

我对 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com