Libgdx精灵旋转导致位置偏移

3

你好,我正在使用Libgdx框架为Android创建一个游戏。我想把一个精灵放在屏幕顶部靠近X轴中心的位置。虽然我可以做到这一点,但是由于精灵需要旋转90度(为了在精灵表上节省空间),它会导致某种偏移,而我不明白为什么会这样。我认为这是因为原点旋转的缘故,但是我已经尝试将原点设置为精灵的中心,但仍未按计划工作。请帮助我理解为什么旋转会导致偏移,以及如何解决这个问题,谢谢。

    regions [1] = new TextureRegion(menu, 395, 0, 115, 320);
    logo=new Sprite(regions[1]);// the sprite
    logo.setPosition(W/2-logo.getWidth()/2,H*0.95f);  //draw right in the middle of X and 95% up Y
    logo.rotate(90);                                  //rotate the sprite 90 degrees
    logo.setOrigin(logo.getWidth()/2,logo.getHeight()/2);  //rotate around the centre of the sprite

    logo.draw(spriteBatch); //in render method draw the sprite

如果我将logo.setPosition(W/2-logo.getWidth()/2,H*0.95f);更改为logo.setPosition(W/2-logo.getWidth()/2,0);,它会将精灵定位在y轴的中心位置而不是底部,原因不明。 - user2002077
1个回答

2

在旋转精灵之后,您需要设置其原点:

logo.rotate(90);                                  //rotate the sprite 90 degrees
logo.setOrigin(logo.getWidth()/2,logo.getHeight()/2);  //rotate around the centre of the sprite

你应该先设置原点,然后再旋转:

logo.setOrigin(logo.getWidth()/2,logo.getHeight()/2); 
logo.rotate(90);     

谢谢。这很有帮助,但出现了一个问题,Y轴似乎仍然存在偏移。如果我将Y轴设置为0位置,它会出现在0的上方约200像素。您知道可能是什么原因吗? - user2002077
我已经将精灵定位到了我想要的位置。虽然我仍然不明白Y偏移量是从哪里来的,但至少在所有设备上都可以使用这个logo.setPosition(W/2-logo.getWidth()/2,H*0.95f-logo.getHeight()*0.65f)。 - user2002077

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接