LibGdx如何重复背景? [英] LibGdx How to repeat background?

查看:166
本文介绍了LibGdx如何重复背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前我想出了如何在LibGdx中进行一些滚动。现在我正在努力做一些相关的事情。我想重复背景。我的滚动跟随一艘船(是一个s [王牌船游戏]。在后台,有一张空间照片作为纹理加载。当船到达backgorund的末端时,它继续前进并且不再有背景。我已经阅读过关于包装的内容,但我并不了解它是如何工作的。我做到了:

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 de bck2的位置,并在下一个渲染循环中重新计算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如何重复背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆