简体   繁体   中英

how to get stage in the center in libgdx?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM