在libgdx中绘制背景图像,而不将精灵与相机一起移动

时间:2014-10-26 16:50:50

标签: java libgdx game-engine

我有一张背景图片,我希望在屏幕上保持相同的位置。因此,当我用我的飞船前进并用太空船移动相机时,相同的背景图像将留在屏幕上。有没有办法在屏幕上绘制背景图像而无需将其与相机一起移动?例如,使用特殊的图层或其他东西,所以不需要附加的矩阵平移来移动BG图像吗?

1 个答案:

答案 0 :(得分:1)

libGDX相机类中有camera.unproject(Vector2 vector)个函数。要将背景图像放在屏幕上的某个位置,您只需在每次绘图时取消投影,并从未投影的坐标绘制到精灵批处理。 E.g:

Vector2 position  = new Vector2(10,10); //Can be anything, the x and y of where the background should be
camera.unproject(position); //Unproject the co-ordinates to the position of the camera, this will be stored back in the position vector

batch.draw(background, position.x, position.y); //You can then draw to your sprite batch from the unprojected co-ordinates