LibGDX – Simple action replay

I have been working on a “2D platformer” and while there are lots of basic features to finish I fancied something a it fun. The first time I saw a replay of gameplay was probably “Striker” or “Speedball 2” on the Amiga 500, I should imagine it was quite difficult back then. Memory not being an issue now I thought it would be simple enough to implement.

Featured image

Action Replay System
frames are the key to animation, so I would capture enough information at each tick and store it in an array of frames. The hero co-ordinates and current texture would be needed along with the current time. I already have a timer class which works as a basic stop watch.


public class Frame {
public float x;
public float y;
public TextureRegion frame;
public float time;
public float delta;
public int frame_rate;

public Frame(float x, float y, float time, float delta, int fps, TextureRegion frame){
this.x = x;
this.y = y;
this.time = time;
this.delta = delta;
this.frame_rate = fps;
this.frame = frame;
}
}

Replay Class
This class contains the array of frames which you add too every tick of your game. I have conditions to adding a new frame like… “if timer.running”, when I hit the goal hitbox the timer stops and frames are no longer added to the array.

I then have a simple if replay is true test then call the tick and draw methods of this class. Tick will push the frame onto the next in the array until it reaches the last frame and draw will output the hero, a tooltip and the current time to the screen.


public class Replay {
public Frame current_frame;
public float playback_frame = 0;
public Array frames = new Array();
BitmapFont font = new BitmapFont();
main game;
public Replay(main game){
font.setScale(.6f);
this.game = game;
}
public void tick(float delta){
if(playback_frame < frames.size){
current_frame = frames.get((int) playback_frame);
}
playback_frame ++;
}
public void draw(float delta, SpriteBatch batch){
if(current_frame != null){
batch.draw(current_frame.frame, current_frame.x-5, current_frame.y-5);
batch.draw(game.media.tooltip, current_frame.x-11, current_frame.y+24);
font.draw(batch, "Time: " + String.format("%.2f", current_frame.time), current_frame.x-10, current_frame.y+42);
}
}
public void add_frame(Frame frame){
frames.add(frame);
}
public void reset(){
playback_frame = 0;
frames.clear();
}
}

This is a quick and dirty implementation, I will re-factor the code later. There are hard coded values and textures passed in from the main class and so on, also currently I do not check the FPS for capture and playback, I assume LibGDX always runs at 60fps.

Advertisements

One thought on “LibGDX – Simple action replay

  1. bomberzocker says:

    FPS are not always locked. You can unlock them by setting foreground fps to 0 in application configuration.

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