视差滚动的背景下,libgdx无限次重复 [英] Scrolling parallax background, infinitely repeated in libgdx

查看:321
本文介绍了视差滚动的背景下,libgdx无限次重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个2D滚屏空间射击类型的游戏,我需要一个能够infintely滚动(是平铺或重复包装)背景。我也想实现视差滚动,所以也许有一个最低的背景星云纹理几乎没有移动,较高的一种含遥远的恒星,几乎没有移动,并含有收星的最高背景下,移动了不少。

I'm making a 2D sidescrolling space shooter-type game, where I need a background that can be scrolled infintely (it is tiled or wrapped repeatedly). I'd also like to implement parallax scrolling, so perhaps have one lowest background nebula texture that barely moves, a higher one containing far-away stars that barely moves and the highest background containing close stars that moves a lot.

我从谷歌看到,我不得不每一层移动50%,低于它上面的一层,但我怎么在libgdx实现这个?我有一个可以在与出被放大一个照相机,而在物理800x480的屏幕可以显示从128×128像素(船)任何的空间为特色的纹理面积巨大缠绕多次其边缘。

I see from google that I'd have each layer move 50% less than the layer above it, but how do I implement this in libgdx? I have a Camera that can be zoomed in and out, and in the physical 800x480 screen could show anything from 128x128 pixels (a ship) to a huge area of space featuring the textures wrapped multiple times on their edges.

我如何时间可持续换一个较小的纹理(比如512×512),就好像它是无限的瓷砖(当相机变焦右出的),然后我怎么层多重纹理像这些,让他们一起在一个合适的结构(有一个在libgdx API?)和移动它们作为玩家的COORDS改变?我看的javadoc和例子,但找不到这样的问题什么,道歉,如果这是显而易见的!

How do I continuosly wrap a smaller texture (say 512x512) as if it were infinitely tiled (for when the camera is zoomed right out), and then how do I layer multiple textures like these, keep them together in a suitable structure (is there one in the libgdx api?) and move them as the player's coords change? I've looked at the javadocs and the examples but can't find anything like this problem, apologies if it's obvious!

推荐答案

我没有多说就视差滚动比PFG已经做了。有确实是下test文件夹和整个网络的若干问题的解释版本库的一个例子。我喜欢这个 。 与背景的事情是很容易解决的。这和其他相关问题,可以通过模块化的代数接近。我不会详谈,因为一旦显示的是很容易理解的。

I have not much more to say regarding to the Parallax Scrolling than PFG already did. There is indeed an example in the repository under the test folder and several explanations around the web. I liked this one. The matter with the background is really easy to solve. This and other related problems can be approached by using modular algebra. I won't go into the details because once shown is very easy to understand.

想象一下,你想显示在屏幕指南针。你有质感1024x16重新presenting东南西北。基本上你已经是一个条带。让拨出约的真正方向和这​​样的考虑,你必须渲染。

Imagine that you want to show a compass in your screen. You have a texture 1024x16 representing the cardinal points. Basically all you have is a strip. Letting aside the considerations about the real orientation and such, you have to render it.

您视是300x400例如,你想在屏幕上的纹理200像素(使它更有趣)。你可以直到你到达的位置(1024-200)= 824。一旦你在这个位置上完美呈现其与一个区域显然没有更多的质感。但由于它是一个指南针,这是显而易见的,一旦你达到它的结束,它必须重新开始。所以,这就是答案。另一个纹理区域会做的伎俩。范围825-1023必须由另一个地区psented重新$ P $。第二个区域将对每个值POS> 824放大器的尺寸(1024-POS);&安培; POS&L​​T; 1024

Your viewport is 300x400 for example, and you want 200px of the texture on screen (to make it more interesting). You can render it perfectly with a single region until you reach the position (1024-200) = 824. Once you're in this position clearly there is no more texture. But since it is a compass, it's obvious that once you reach the end of it, it has to start again. So this is the answer. Another texture region will do the trick. The range 825-1023 has to be represented by another region. The second region will have a size of (1024-pos) for every value pos>824 && pos<1024

这code的目的是作为一个指南针的真实的例子。这是非常肮脏的,因为它的工作原理与相对位置所有的时间,由于范围(0-3.6)之间的转换(0-1024)。

This code is intended to work as real example of a compass. It's very dirty since it works with relative positions all the time due to the conversion between the range (0-3.6) to (0-1024).

spriteBatch.begin();
    if (compassorientation<0)
        compassorientation = (float) (3.6 - compassorientation%3.6);
    else
        compassorientation = (float) (compassorientation %  3.6);
    if ( compassorientation < ((float)(1024-200)/1024*3.6)){
        compass1.setRegion((int)(compassorientation/3.6*1024), 0, 200, 16);
        spriteBatch.draw(compass1, 0, (Gdx.graphics.getHeight()/2) -(-250 + compass1.getTexture().getHeight()* (float)1.2), Gdx.graphics.getWidth(), 32 * (float)1.2);

    }
    else if (compassorientation > ((float)(1024-200)/1024*3.6)) {
        compass1.setRegion((int)(compassorientation/3.6*1024), 0, 1024 - (int)(compassorientation/3.6*1024), 16);
        spriteBatch.draw(compass1, 0, (Gdx.graphics.getHeight()/2) -(-250 + compass1.getTexture().getHeight()* (float)1.2), compass1.getRegionWidth()/200f * Gdx.graphics.getWidth() , 32 * (float)1.2);
        compass2.setRegion(0, 0, 200 - compass1.getRegionWidth(), 16);
        spriteBatch.draw(compass2, compass1.getRegionWidth()/200f * Gdx.graphics.getWidth() , (Gdx.graphics.getHeight()/2) -(-250 + compass1.getTexture().getHeight()* (float)1.2), Gdx.graphics.getWidth() - (compass1.getRegionWidth()/200f * Gdx.graphics.getWidth())  , 32 * (float)1.2);
    } 


    spriteBatch.end();

这篇关于视差滚动的背景下,libgdx无限次重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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