关于视差滚动,我没有什么比 PFG 已经做过的更多的要说的了。在测试文件夹下的存储库中确实有一个示例,并在网络上提供了一些解释。我喜欢这个。有背景的事情真的很容易解决。这个和其他相关问题可以通过使用模代数来解决。我不会详细介绍,因为一旦显示就很容易理解。
想象一下,您想在屏幕上显示一个指南针。你有一个代表基点的纹理 1024x16。基本上你所拥有的只是一条带子。抛开关于真实方向等的考虑,你必须渲染它。
例如,您的视口为 300x400,并且您希望屏幕上有 200 像素的纹理(使其更有趣)。您可以使用单个区域完美地渲染它,直到到达位置 (1024-200) = 824。一旦您处于该位置,显然就没有更多的纹理了。但既然是指南针,很明显,一旦走到尽头,就必须重新开始。所以这就是答案。另一个纹理区域可以解决问题。范围 825-1023 必须由另一个区域表示。对于每个值 pos>824 && pos<1024,第二个区域的大小将为 (1024-pos)
此代码旨在用作指南针的真实示例。由于范围(0-3.6)到(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();