how to get stage in the center in libgdx?

早过忘川 提交于 2019-12-23 12:34:57

问题


My image is of size 720x480 and stage is of same size. The boolean parameter, whether to stretch the stage or not is kept false, so that aspect ratio is maintained, now the problem i am getting that a black area is shown on either side of the screen I want my scene to be centered. The link below refers a file, which shows the problem more clearly, that image is aligned to the bottom left i want it to be centered so that the black area is divided equally on top and the bottom.

what is the approach to solve this?

http://badlogicgames.com/forum/viewtopic.php?f=11&t=3398


回答1:


You have to specify X and Y coordinate as the following way

X = (Gdx.graphics.getWidth() - sprite.getRegionWidth()) / 2.0f;
Y = (Gdx.graphics.getHeight() - sprite.getRegionHeight()) / 2.0f, sprite.getRegionWidth()/2.0f;

You have to calaculate center of the image. Using this way you can put your image in center of the screen. Also here I user Sprite to display image.



来源:https://stackoverflow.com/questions/9385470/how-to-get-stage-in-the-center-in-libgdx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!