Rotate image continuously

Tags: , ,



I want to rotate the ring image constantly in anticlockwise direction here is my code

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

Answer

I guess the Actions are what you are looking for.
An Action can be added to Actors (and subclasses) and they will be performed inside the act(delta) method, of the Actor.
In your case you could use the Actions.rotateBy(float rotationAmount, float duration) and let it repeat forever by using Actions.repeat(RepeatAction.FOREVER, rotateAction).

So your final code should look like this:

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

Where rotation is the rotation amount (i guess in degrees, but i am not sure) and duration is the time it should take to rotate by the given amount (given in seconds).



Source: stackoverflow