Understand LibGDX Coordinate system and drawing sprites

不想你离开。 提交于 2019-12-01 16:57:57

If you want to draw a sprite in center of screen, do this in your create method

logosprite.setposition(scrw/2-logosprite.getwidth()/2,scrh/2-logosprite.getheight/2);

here scrw is your viewport's width,

and scrh is your viewport's height,

this way your sprite will be in center of screen

sprite.setsize is used for setting size of the sprite and sprite.setscale is used when we scale a large/small texture so that its quality remains good in all devices(hdpi.mdpi,xhdpi,ldpi)..

no need to worry if you are using spine it works smoothly in libgdx..

You can use just this code if possible

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

To center the sprite into the middle of the screen Where "image" is the Texture you have loaded/declared initially.

As for why it is coming in a odd position is due to the fact that you are using a camera. Which changes the view a lot just go through the documentations of libgdx about camera here

In my case, I needed to set position of camera and then call update() method. Then never forget camera's (0,0) is its center. Everything is being placed that way. My camera code:

   private void cameralariUpdateEt() {
        cameraGame.position.set(cameraGame.viewportWidth * 0.5f,
                cameraGame.viewportHeight * 0.5f, 0);
        cameraGame.update();

        cameraScore.position.set(cameraScore.viewportWidth * 0.5f,
                cameraScore.viewportHeight * 0.5f, 0);
        cameraScore.update();
    }

Call this method from inside render();

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