2014-03-12 66 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參數或調用翻轉。

哦,如果這是你的主循環,停止像你一樣加載紋理。在開始時加載它們,並根據需要交換它們。