LibGdx How to repeat background?(LibGdx 如何重复背景?)
问题描述
几天前,我想出了如何在 LibGdx 中进行一些滚动.现在我正在尝试做一些相关的事情.我想重复背景.我的滚动跟随一艘船(是一个 s[ace 船游戏).在背景中有一张作为纹理加载的空间照片.当船到达背景的尽头时,它继续前进,没有背景了.我已经阅读了有关 wrap 的内容,但我并不真正了解它是如何工作的.我做到了:
Few days ago I figured out how to do some scrolling in LibGdx. Now I'm triying to do something related. I want to repeat the background. My scrolling follows a ship (Is an s[ace ship game). In the background there is a space photo loaded as a Texture. When the ship reach the end of the backgorund, It keeps going and there's no background anymore. I have read about wrap but I don't really understand How It works. I did that:
px=new Pixmap(Gdx.files.internal("fondo.jpg"));
background=new Texture(px);
background.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
然后,在我的渲染方法中
And then, in my render method
spriteBatch.begin();
spriteBatch.draw(background,0,0,500,50);
drawShip();
spriteBatch.end();
当然不行,它只会画一次背景.我不知道如何使这种包装方法起作用.有什么帮助吗?
Of course It doesn't work, It only draws the background once. I don't know how make this wrap method work. Any help?
我想通了.这不是一个很好的代码,但它可以工作.
I figured It out. It's not a nice code but It works.
首先我声明两个具有相同图像的纹理
First I declare two Textures with the same image
bck1=new Texture(Gdx.files.internal("fondo.jpg"));
bck2=new Texture(Gdx.files.internal("fondo.jpg"));
我还声明了两个这样的变量来指定每个bck位置的X值
Also I declare two variables like this to specify the X value of the position of each bck
int posXBck1=0,posXBck2=0;
然后我在 Render() 中使用它
Then I use that in Render()
public void calculoPosicionFondos(){
posXBck2=posXBck1+ANCHODEFONDO;
if(cam.position.x>=posXBck2+cam.viewportWidth/2){
posXBck1=posXBck2;
}
}
地点:
ANCHODEFONDO 是我背景的宽度
ANCHODEFONDO is the width of my background
Cam 是 OtrhoCam.
Cam is an OtrhoCam.
所以我说如果凸轮在 bck2 中(这意味着你再也看不到 bck1)它会改变位置,给出 bck2 的 bck1 位置,并在下一个渲染循环中重新计算 bck2
So I said that if the cam is in bck2 (wich means that you can't see bck1 anymore) It change positions, giving bck1 de position of bck2 and, in the next render loop, recalculating bck2
然后在您的渲染模式下绘制两个 bck.
Then just paint both bck in your render mode.
推荐答案
就像 Teitus 说的,不要多次加载你的纹理,永远!无论如何,你在正确的轨道上使用包装器:
Like Teitus said, do not load your texture multiple times, ever! Anyway, you where on the right track with the wrapper:
texture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
现在您可以只对源位置使用 draw 方法.源位置是您选择在纹理上绘制的区域.
Now you can just use the draw method with the source location. The source location is the area you choose to draw on the texture.
batch.draw(texture, x, y, srcX, srcY, srcWidth, srcHeight)
要从右到左滚动你的纹理,你所要做的就是逐渐增加 srcX.所以在更新/渲染方法中创建一个递增的 int.
To scroll your texture from right to left all you have to do is increase srcX incrementally. So create a int that increments in the update/render method.
int sourceX = 0;
//render() method
//Increment the variable where to draw from on the image.
sourceX += 10;
//Simply draw it using that variable in the srcX.
batch.draw(YourTexture, 0, 0, sourceX, 0, screenWidth, screenHeight);
因为您正在包裹纹理,所以它会包裹/循环并无限期地滚动.如果游戏运行很长时间,sourceX int 可能存在问题,因为 int 只能容纳 2147483647.这需要一段时间,但您可以通过在每次数字超过总图像宽度时减去图像宽度来修复它.
Because you are wrapping the texture it will wrap/loop and scroll indefinitely. There might be a issue with the sourceX int if the game runs for a very long time because a int can only hold 2147483647. It takes a while but you can fix it by subtracting the image width each time the number goes over the total image width.
这篇关于LibGdx 如何重复背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:LibGdx 如何重复背景?
基础教程推荐
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01