Android – center texture libgdx

I try to center the 256px x 256px image in libgdx. When I run the code I am using it, the image will be rendered in the upper right corner of the window. For the height and width of the camera, I use GDx. Graphics. Getheight(); And GDx. Graphis. Getwidth() I set the position of the camera to divide the width of the camera by 2 and divide its height by 2... Should this put it in the middle of the screen? When I paint a texture, I set its position to the width and height of the camera divided by 2 - so it's centered... So I think. Why don't the image be drawn in the center of the screen? What's wrong with me?

thank you!

resolvent:

It sounds like your camera is OK. If you set the texture position, you set the position of the lower left corner of the texture. It is not centered. Therefore, if you set it as the coordinate of the center of the screen, its extension will cover the space on the right and top of the point. To center it, you need to subtract half the texture width from X and half the texture height from y. these things:

image.setPosition(Gdx.graphics.getWidth()/2 - image.getWidth()/2, 
Gdx.graphics.getHeight()/2 - image.getHeight()/2);

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>