Runnable Action

Tutorial Objective

Shows how to use a RunnableAction to update a variable as part of a Sequence of Actions.

What you should be familiar with for this tutorial

Code

Full Code available for download here or can be read below.

Main Class (MyGdxGame.java)

Explanation

You can use a RunnableAction to execute a bit of code as part of a sequence of Action.

RunnableAction updateLapCountAction = new RunnableAction();
updateLapCountAction.setRunnable(new Runnable() {
@Override
public void run () {
updateLapsCount(); // Whatever Action you want to execute
}
});
 This action can be added to a SequenceAction, ParallelAction. So to run a RunnableAction after a bunch of other actions you could use:
SequenceAction overallSequence = new SequenceAction();
overallSequence.addAction(topLeftRightParallelAction);
overallSequence.addAction(moveBottomRightAction);
overallSequence.addAction(bottomLeftRightParallelAction);
overallSequence.addAction(leftBottomTopParallelAction);
overallSequence.addAction(updateLapCountAction);

image.addAction(overallSequence);

 Code

Full Code available for download here or can be read below.

public class MyGdxGame extends ApplicationAdapter {
private Stage stage;
private static int lapsCount;
private Label lapsLabel;
private Group lapsLabelContainer;

@Override
public void create () {
lapsCount = 3;
stage = new Stage(new ScreenViewport());
Skin skin = new Skin(Gdx.files.internal("skin/glassy-ui.json"));

Texture texture = new Texture(Gdx.files.absolute("image.jpg"));

int X_left = Gdx.graphics.getWidth() / 3 - texture.getWidth() / 2;
int X_right = Gdx.graphics.getWidth() * 2 / 3 - texture.getWidth() / 2;
int Y_top = Gdx.graphics.getHeight() * 2 / 3 - texture.getHeight() / 2;
int Y_bottom = Gdx.graphics.getHeight() / 3 - texture.getHeight() / 2;

Image image1 = new Image(texture);
image1.setPosition(X_left, Y_top);
image1.setOrigin(image1.getWidth() / 2, image1.getHeight() / 2);
stage.addActor(image1);

ParallelAction topLeftRightParallelAction = new ParallelAction();
topLeftRightParallelAction.addAction(Actions.moveTo(X_right, Y_top, 1, Interpolation.exp5Out));
topLeftRightParallelAction.addAction(Actions.scaleTo(2, 2, 1, Interpolation.exp5Out));

MoveToAction moveBottomRightAction = new MoveToAction();
moveBottomRightAction.setPosition(X_right, Y_bottom);
moveBottomRightAction.setDuration(1);
moveBottomRightAction.setInterpolation(Interpolation.smooth);

RunnableAction updateLapCountAction = new RunnableAction();
updateLapCountAction.setRunnable(new Runnable() {
@Override
public void run () {
updateLapsCount();
}
});

ParallelAction bottomLeftRightParallelAction = new ParallelAction();
bottomLeftRightParallelAction.addAction(Actions.moveTo(X_left, Y_bottom, 1, Interpolation.sineOut));
bottomLeftRightParallelAction.addAction(Actions.scaleTo(1, 1, 1));

ParallelAction leftBottomTopParallelAction = new ParallelAction();
leftBottomTopParallelAction.addAction(Actions.moveTo(X_left, Y_top, 1, Interpolation.swingOut));
leftBottomTopParallelAction.addAction(Actions.rotateBy(90, 1));

SequenceAction overallSequence = new SequenceAction();
overallSequence.addAction(updateLapCountAction);
overallSequence.addAction(topLeftRightParallelAction);
overallSequence.addAction(moveBottomRightAction);
overallSequence.addAction(bottomLeftRightParallelAction);
overallSequence.addAction(leftBottomTopParallelAction);

RepeatAction loopNbrAction = new RepeatAction();
loopNbrAction.setCount(lapsCount);
loopNbrAction.setAction(overallSequence);

lapsLabel = new Label(" Loop :", skin, "big-black");
lapsLabel.setPosition(0, 0);
lapsLabel.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
lapsLabel.setAlignment(Align.center);

lapsLabelContainer = new Group();
lapsLabelContainer.addActor(lapsLabel);
lapsLabelContainer.setOrigin(lapsLabel.getWidth() / 2, lapsLabel.getHeight() / 2);

stage.addActor(lapsLabelContainer);

RunnableAction completedAction = new RunnableAction();
completedAction.setRunnable(new Runnable() {
@Override
public void run () {
finished();
}
});

image1.addAction(Actions.sequence(loopNbrAction, completedAction));

}

private void updateLapsCount () {
lapsCount--;
lapsLabelContainer.setScale(0);
SequenceAction FadingSequenceAction = new SequenceAction();
FadingSequenceAction.addAction(Actions.fadeIn(1));

ParallelAction parallelAction = new ParallelAction();

lapsLabel.setText(" Laps : " + (lapsCount + 1));
FadingSequenceAction.addAction(Actions.fadeOut(2));
parallelAction.addAction(Actions.scaleBy(5, 5, 4));

parallelAction.addAction(FadingSequenceAction);
lapsLabelContainer.addAction(parallelAction);
}

private void finished () {
lapsLabelContainer.setScale(0);
SequenceAction FadingSequenceAction = new SequenceAction();
FadingSequenceAction.addAction(Actions.fadeIn(1));

ParallelAction parallelAction = new ParallelAction();

lapsLabel.setText(" Finished!");
// FadingSequenceAction.addAction(Actions.fadeOut(10));
parallelAction.addAction(Actions.rotateBy(360, 2));
parallelAction.addAction(Actions.scaleBy((float)1.2, (float)1.2, 3, Interpolation.bounce));

parallelAction.addAction(FadingSequenceAction);
lapsLabelContainer.addAction(parallelAction);
}

@Override
public void render () {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();

}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s