我对视差滚动没有更多的话要说,因为PFG已经说得很清楚了。在测试文件夹下的存储库中确实有一个示例,以及网上的几个解释。我喜欢
this one。
背景的问题真的很容易解决。这和其他相关问题可以通过使用模块化代数来解决。我不会详细解释,因为一旦展示出来就非常容易理解。
想象一下,你想在屏幕上显示一个指南针。你有一个代表基本点的1024x16纹理。基本上你只有一条带子。撇开关于实际方向等方面的考虑,你需要渲染它。
例如,您的视口为300x400,并且您想要屏幕上的200px纹理(使其更有趣)。您可以使用单个区域完美地呈现它,直到达到位置(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();