2015-06-30 178 views
1

我想在逆時針方向旋轉不停環像這裏是我的代碼旋轉圖像連續

public class SpriteSheet extends ApplicationAdapter { 
    Stage stage; 

    @Override 
    public void create() { 
     stage=new Stage(new ScreenViewport()); 
     Group group=new Group(); 

     Image background =new Image(new Texture(Gdx.files.internal("background.png"))); 
     Image button=new Image(new Texture(Gdx.files.internal("btn.png"))); 
     Image ring=new Image(new Texture(Gdx.files.internal("ring2.png"))); 

     background.setName("background"); 
     button.setName("button"); 
     ring.setName("ring"); 

     group.addActor(background); 
     group.addActor(button); 
     group.addActor(ring); 

     stage.addActor(group); 

     background.setPosition(Gdx.graphics.getWidth()/2-background.getWidth()/2,Gdx.graphics.getHeight()/2-background.getHeight()/2); 
     button.setPosition(Gdx.graphics.getWidth()/2-button.getWidth()/2,Gdx.graphics.getHeight()/2-button.getHeight()/2); 

     ring.setPosition(255,105); 

     ring.setOrigin(255f,105f); 
     ring.rotateBy(2f); // I need continuous rotation here 
    } 

    @Override 
    public void render() { 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
     stage.act(Gdx.graphics.getDeltaTime()); 
     stage.draw(); 
    } 
} 
+0

只需使用一個循環 –

+0

你有什麼問題?你現在擁有哪個結果? –

+0

ring.rotateBy(2f);既沒有旋轉也沒有給出恆定的旋轉 – dilz

回答

4

我猜Actions是你在找什麼。
Action可以添加到Actor s(和子類),它們將在act(delta)方法中執行Actor的方法。
在你的情況下,你可以使用Actions.rotateBy(float rotationAmount, float duration),讓它永遠重複使用Actions.repeat(RepeatAction.FOREVER, rotateAction)

所以你的最終代碼應該是這樣的:

ring.addAction(Actions.repeat(RepeatAction.FOREVER, Actions.rotateBy(rotation, duration))); 

哪裏rotation是旋轉量(我猜度,但我不知道),並duration是應採取由旋轉的時間給定量(以秒爲單位)。

+0

感謝男人,這真的很有幫助,但我想要在汽車的輪圈中心點旋轉 – dilz

+1

因此,您需要將「Actor」的原點設置爲中心。原點是相對於你的'演員'的左下角,所以它的中心應該是P(寬/ 2,高/ 2)。原點定義了「Actor」旋轉和縮放的點,但不影響它的位置。 – Springrbua

+0

非常感謝爲我工作 – Richi