00:00
00:00
Newgrounds Background Image Theme

refcherry just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

As: Basic Instant Replay

6,795 Views | 13 Replies
New Topic Respond to this Topic

As: Basic Instant Replay 2005-08-29 11:29:37


AS: Main

Basic Instant Replay in Flash Games

Instant Replays in flash games are quite rare but are easy to create. All you need to know is how to create and edit arrays, variable creation and editting, and basic if statements.

Arrays you need to create

1) Array for your character's X
2) Array for your character's Y

Here is the code I used to make this simple replay system: Replay Demo

//PLACE ALL THIS CODE INSIDE A SINGLE FRAMED MOVIE CLIP
//++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++
//CREATED BY IAN BROWN ON AUGUST 28, 2005
//THIS SCRIPT WAS MADE FOR 30 FPS.
//++++++++++++++++++++++++++++++++++++++
//CHANGE THESE VARIABLES
fps = 30;
//FRAMES PER SECOND
secondsToRecord = 2;
//HOW MANY SECONDS TO RECORD
//++++++++++++++++++++++++++++++++++++++
playbackArrayX = new Array();
//this is used to see how to move the players x
playbackArrayY = new Array();
//this is used to see how to move the players y
i = 0;
//sets the variable i to 0
j = 0;
//sets the variable j to 0
recording = false;
//sets variable recording to false
moved = false;
//setes variable moved to false
onEnterFrame = function () {
if (Key.isDown(Key.SPACE) && !recording) {
// Pressing Space will start the "recording" process
recording = true;
}
if (Key.isDown(Key.SHIFT) && recording) {
// Pressing Shift starts the "replay" process
playbackArrayX[i] = "EOF";
// creates an End Of File marker
recording = false;
moved = true;
_x = playbackArrayX[0];
// returns the characters _x to the starting position of the array
_y = playbackArrayY[0];
// returns the characters _y to the starting position of the array
}
if (recording) {
// if recording do this
if (Key.isDown(Key.LEFT)) {
// if Left Arrow is pressed
_x -= 1;
} else if (Key.isDown(Key.RIGHT)) {
// if Right Arrow is pressed
_x += 1;
}
if (Key.isDown(Key.UP)) {
// if Up Arrow is pressed
_y -= 1;
} else if (Key.isDown(Key.DOWN)) {
// if Down Arrow is pressed
_y += 1;
}
playbackArrayX[i] = _x;
playbackArrayY[i] = _y;
i++;
// adds 1 to the variable i
if (playbackArrayX.length>fps*secondsToRecord
) {
// if more than 2 seconds of actions are recorded remove the first element of the arrays
playbackArrayX.shift();
playbackArrayY.shift();
i--;
}
} else if (!recording && moved) {
// If no longer recording and the character has actually been moved
_x = playbackArrayX[j];
_y = playbackArrayY[j];
j++;
// adds 1 to variable j
if (playbackArrayX[j] == "EOF") {
// if the End Of File marker appears replay has ended and start over
_x = playbackArrayX[0];
_y = playbackArrayY[0];
j = 0;
}
}
};

AS: Arrays
AS: Variables

Response to As: Basic Instant Replay 2005-08-29 11:34:56


At 8/29/05 11:29 AM, Cojones893 wrote: Basic Instant Replay in Flash Games

Nice, very much so.


Sup, bitches :)

BBS Signature

Response to As: Basic Instant Replay 2005-08-29 11:35:39


very nifty script, appreciated :)

I do it differntly, but it's the same concept

Response to As: Basic Instant Replay 2005-08-29 11:36:17


Good job, could be a cool effect in games like Dad 'n Me =P

The code seems to be unnecessarily long though, but I never bothered to look it through.


BBS Signature

Response to As: Basic Instant Replay 2005-08-29 11:37:28


At 8/29/05 11:35 AM, Inglor wrote: I do it differntly, but it's the same concept

How do you do it? It's better to have more than one of the same code, so that people can see how you do it properly. If they don't understand the 1st code they can see the 2nd, etc :P


Sup, bitches :)

BBS Signature

Response to As: Basic Instant Replay 2005-08-29 11:42:37


At 8/29/05 11:36 AM, Rantzien wrote: The code seems to be unnecessarily long though, but I never bothered to look it through.

This is a shortened version, just without quotes and with commas...

fps=30, secondsToRecord=2;
playbackArrayX=Array(), playbackArrayY=Array();
i=0, j=0;
recording=false, moved=false;
onEnterFrame = function () {
if (Key.isDown(Key.SPACE) && !recording) {
recording = true;
}
if (Key.isDown(Key.SHIFT) && recording) {
playbackArrayX[i] = "EOF";
recording=false, moved=true;
_x=playbackArrayX[0], _y=playbackArrayY[0];
}
if (recording) {
if (Key.isDown(Key.LEFT)) {
_x -= 1;
} else if (Key.isDown(Key.RIGHT)) {
_x += 1;
}
if (Key.isDown(Key.UP)) {
_y -= 1;
} else if (Key.isDown(Key.DOWN)) {
_y += 1;
}
playbackArrayX[i]=_x, playbackArrayY[i]=_y;
i++;
if (playbackArrayX.length>fps*secondsToRecord
) {
playbackArrayX.shift(), playbackArrayY.shift();
i--;
}
} else if (!recording && moved) {
_x=playbackArrayX[j], _y=playbackArrayY[j];
j++;
if (playbackArrayX[j] == "EOF") {
_x=playbackArrayX[0], _y=playbackArrayY[0];
j = 0;
}
}
};

40 Lines

Probably best to use the longer one, so you can read through the comments and see what everything is doing.


Sup, bitches :)

BBS Signature

Response to As: Basic Instant Replay 2005-08-29 11:45:58


At 8/29/05 11:42 AM, -liam- wrote: This is a shortened version, just without quotes and with commas...

Yeah, I noticed there was a lot of comments in the code right after I wrote it.


BBS Signature

Response to As: Basic Instant Replay 2005-08-29 13:51:53


The other reason the code is longer is because I have a feature to toggle it on then replay whenever you want. In a game you would pretty much run the recording code all the time and replay it when something big happens.

Response to As: Basic Instant Replay 2006-02-22 14:18:10


I recently coded something similar in order to make a "ghost" system on the time-trials for my racing game. Just thought i'd mention a use for this kind of thing


- Matt, Rustyarcade.com

Response to As: Basic Instant Replay 2007-02-18 13:51:33


what's instant replay?


Click the Squid -> 🦑

Response to As: Basic Instant Replay 2008-07-07 05:14:44


thats awesome but I can't think of something I make that would need it


lawl

BBS Signature

Response to As: Basic Instant Replay 2010-06-05 22:46:12


At 6/5/10 09:15 PM, BeefyBoy12 wrote: Is it possible to make it move backwards? As in, reverse replay?
for (var i = array.length-1;i > -1;i--){ trace( array[i] ); }

Response to As: Basic Instant Replay 2010-06-05 22:48:45


Also flash has a built-in function which is simply

array.reverse();

Response to As: Basic Instant Replay 2010-06-06 01:05:57


This just deals with recording coordinates in space. There can be a lot of things going on at any point in a game.

I am assuming that I could record _currentframe as an array to ensure a character is playing the right animations during the replay. But what about other situations, take duplicated or dynamically created (and destroyed) clips for instance. Lets say my character is shooting bullets or fireballs. It can quickly become a mess.

Wouldnt you also need an array for every property per sprite involved? And wouldnt that quickly become a performance issue?