居中纹理LibGDX [英] Centering a texture LibGDX

查看:137
本文介绍了居中纹理LibGDX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在LibGDX居中一个256PX点¯x256PX形象。当我运行code,我用它呈现在窗口右上角的形象。对于摄像头的高度和宽度,我用 Gdx.graphics.getHeight(); Gdx.graphcis.getWidth(); 。我设置了摄像头位置,摄像头的宽度由两个和高度除以二分...这应该把它放在屏幕右侧中间?当我画的质感,我设置它的位置,摄像头的宽度和高度除以二 - 所以它centered..or所以我觉得。为什么不将图像在屏幕中心画,是有什么我不理解?

I am trying to center a 256px X 256px image in LibGDX. When i run the code I'm using it renders the image in the upper right hand corner of the window. For the camera's height and width I use Gdx.graphics.getHeight(); and Gdx.graphcis.getWidth(); . I set the cameras position to the camera's width divided by the two and its height divided by two... this should put it in the middle of the screen right? when I draw the texture, I set it's position to the camera's width and height divided by two -- so it's centered..or so I think. Why doesn't the image draw in the center of the screen, is there something I'm not understanding?

谢谢!

推荐答案

听起来好像你的相机是确定的。
如果设置了纹理的位置,设置纹理的左下角的位置。它不居中。因此,如果将其设置为在屏幕的中心坐标,其延伸将覆盖空间到右侧,该点的顶部。要居中,你需要减去一半的纹理的宽度从X,并协调从Y纹理高度的一半。沿着这些路线的内容:

It sounds as if your camera is ok. If you set the textures position, you set the position of the lower left corner of that texture. It is not centered. Therefore if you set it to the coordinates of the center of the screen, its extends will cover the space to the right and the top of that point. To center it, you need to subtract half of the textures width from the x, and half of the textures height from the y coordinate. Something along these lines:

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

这篇关于居中纹理LibGDX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆