LibGDX问题旋转精灵

时间:2014-03-03 15:48:01

标签: java android rotation libgdx sprite

好的,所以我真的很困惑我之前已经旋转了精灵并且没有任何问题,例如当船在海洋中移动时旋转船,但由于某种原因,这次我有一个非常大的问题。所以我在资源文件中创建一个纹理,但不是静态纹理。我使用以下内容加载纹理:

class Assets{
    Texture img;
    public Assets(){
        img = new Texture(Gdx.files.internal("images/PNG.png")

然后我通过调用:

来调用主类中的资产
Assets assets = new Assets()

然后我有一个专门为这个主要角色设计动画的课程,因为他的动画与其他角色差别很大。

class Animations{
    Guy MYGUY;
    Texture firstTexture;
    ArrayList<Texture> running;
    Sprite CurrentSprite;
    public Animations(Texture standingStill, Guy myGuy){
        MYGUY = myGuy;
        firstTexture = standingStill;
        running = new ArrayList<Texture>();
        running.add(firstTexture);
        CurrentSprite = new Sprite(firstTexture);

    public void update (int direction, int state){
         CurrentSprite.setPosition(MYGUY.X, MYGUY.Y)
        // I have a switch here, but it does nothing yet because I haven't added in different actions for the character.
        //However I do have a switch for direction, because that is important right now
        switch(state){
        case Guy.LEFT:
            CurrentSprite.set rotation(180);
        //yes there are more, but even rotating 180 won't work correctly
        }

然后我有一个渲染器类来绘制所有内容,我在一个名为MyGuy的世界的对象中有对象myLand,我用它绘制:

myLand.GUY.animation.CurrentSprite(batch);

所以我的问题出现在旋转上,每当它旋转180度时,似乎总是围绕坐标(0,0)而不是精灵的中心旋转。所以它通常会在我向左移动的位置结束,但是如果我尝试向左移动它会向后移动一倍的距离,但相机的位置保持不变,而这个人通常会从左侧或右侧消失。屏幕。

3 个答案:

答案 0 :(得分:6)

尝试使用rotate(...)方法代替setRotation(...).

setOrigin(widthSprite\2, heightSprite\2)

该动作旋转精灵本身。

答案 1 :(得分:0)

不要旋转精灵,只需用这一行翻转它:

CurrentSprite.flip(true, false);

第一个布尔值是X flip(这是你想要向左移动时设置为true),第二个是Y flip。

答案 2 :(得分:0)

尝试

sprite.setOriginCenter();

这应该有帮助