【问题标题】:rotating sprite on touch libgdx在触摸 libgdx 上旋转精灵
【发布时间】:2014-03-12 15:26:48
【问题描述】:

当我向左推时,我正在尝试旋转我的精灵。现在我的角色正在空转并向右跑。但我无法向左旋转。

这是我的代码块。如果有人可以帮助我,那就太棒了。

public void draw(SpriteBatch spriteBatch) {
    stateTime += Gdx.graphics.getDeltaTime();
    //continue to keep looping




    if(Gdx.input.isTouched()){
        int xTouch = Gdx.input.getX();
        int yTouch = Gdx.input.getY();
        //System.out.println(Gdx.graphics.getWidth()/4);
        //go left
        if(xTouch < (width/4) && yTouch > height - (height/6)){
            currentRunFrame = runAnimation.getKeyFrame(stateTime, true);
            spriteBatch.draw(currentRunFrame,  runSprite.getX() - 32, runSprite.getY() + 150, 128, 128);
            RealGame.leftButton = new Texture(Gdx.files.internal("leftButtonOver.png"));
            moveLeft();
        }
        if(xTouch > (width/4) && xTouch < (width/4)*2 && yTouch > height - (height/6)){
            currentRunFrame = runAnimation.getKeyFrame(stateTime, true);
            spriteBatch.draw(currentRunFrame,  runSprite.getX() - 32, runSprite.getY() + 150, 128, 128);
            RealGame.rightButton = new Texture(Gdx.files.internal("rightButtonOver.png"));
            moveRight();
        }
        if(xTouch > (width/4) * 2 && xTouch < (width/4) * 3 && yTouch > height - (height/6)){
            RealGame.shootButton = new Texture(Gdx.files.internal("shootButtonOver.png"));
        }
        if(xTouch > (width/4) * 3 && xTouch < (width/4) * 4 && yTouch > height - (height/6)){
            RealGame.jumpButton = new Texture(Gdx.files.internal("jumpButtonOver.png"));
        }

    }
    if(!Gdx.input.isTouched()){

        currentIdleFrame = idleAnimation.getKeyFrame(stateTime, true);
        spriteBatch.draw(currentIdleFrame,  idleSprite.getX() - 32, idleSprite.getY() + 150, 128, 128);
        RealGame.leftButton = new Texture(Gdx.files.internal("leftButton.png"));
        RealGame.rightButton = new Texture(Gdx.files.internal("rightButton.png"));
        RealGame.shootButton = new Texture(Gdx.files.internal("shootButton.png"));
        RealGame.jumpButton = new Texture(Gdx.files.internal("jumpButton.png"));
        moveStop();

    }

}

提前感谢您,如果您需要更多信息,请告诉我。

【问题讨论】:

    标签: libgdx


    【解决方案1】:

    我假设您的currentIdleFrameTextureTextureRegion,而不是Sprite。使用TextureSpriteBatchs 绘制方法之一支持flipXflipY。使用它您可以翻转他,例如,如果您向左走,但您的 Texture 面向右侧。这也支持rotation,应该是以度为单位的旋转。

    非常重要的注意事项:您在每个渲染循环中创建 new Texture。不要这样做。而是将所有Textures 加载到Texture[] frames 中并根据stateTime 绘制正确的。还可以查看Animations 课程,这将对您有所帮助。

    希望能帮到你

    【讨论】:

    • 感谢您提供有关在该循环上创建新按钮的提示。我试图弄清楚为什么我的应用程序在 26 秒后死机。嗯...现在我知道为什么了
    • 尝试在create()show()constructor 中创建对象,因为它们只在实际需要时调用一次。在渲染中创建new objects 可能有点繁重,所以只有在绝对必要时才使用它。
    【解决方案2】:

    使用 SpriteBatch 绘制方法,该方法采用布尔型 flipX 参数或调用 Sprite 上的翻转。

    哦,如果这是您的主循环,请停止像您一样加载纹理。在开始时加载它们,然后根据需要交换它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-04
      • 1970-01-01
      • 2014-09-02
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多