2014-03-12 218 views
0

我想旋转我的精灵,当我推动去左。现在我的角色正在空转并跑到右边。但我很难向左旋转。旋转精灵触摸libgdx

这是我的代码块。如果任何人都可以帮助我,那会很棒。

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(); 

    } 

} 

在此先感谢您,并告诉我您是否需要更多信息。

回答

1

我假设你的currentIdleFrameTextureTextureRegion,而不是SpriteSpriteBatch中的一个与Texture的绘制方法支持flipXflipY。使用这个你可以翻转他,例如,如果你正在向左走,但你的Texture正面朝右。这也支持rotation,这应该是度数的旋转。

Verry重要注意事项:您创建new Texture每个渲染循环。不要这样做。请将所有Texture加载到Texture[] frames中,然后根据stateTime绘制正确的一个。也请看Animations班,这会帮助你。

希望我能帮到

+0

谢谢你关于在那个循环上创建新按钮的提示。我试图找出为什么我的应用程序在26秒后即将死亡。那么......现在我知道为什么 – Doctor06

+0

尝试在create(),show()或constructor中创建对象,因为它们只在实际需要时被调用一次。在渲染中创建'new objects'可能会有点沉重,所以只有在绝对必要时才使用它。 – Springrbua

0

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

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