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