Forum Topic: As: Basic Instant Replay

(4,471 views • 10 replies)

This topic is 1 page long.

<< < > >>
None

Cojones893

Reply To Post Reply & Quote

Posted at: 8/29/05 11:29 AM

Cojones893 EVIL LEVEL 22

Sign-Up: 03/09/03

Posts: 2,595

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


None

liaaaam

Reply To Post Reply & Quote

Posted at: 8/29/05 11:34 AM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,536

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

Nice, very much so.


None

Inglor

Reply To Post Reply & Quote

Posted at: 8/29/05 11:35 AM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

very nifty script, appreciated :)

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


None

Rantzien

Reply To Post Reply & Quote

Posted at: 8/29/05 11:36 AM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

None

liaaaam

Reply To Post Reply & Quote

Posted at: 8/29/05 11:37 AM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,536

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


None

liaaaam

Reply To Post Reply & Quote

Posted at: 8/29/05 11:42 AM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,536

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.


None

Rantzien

Reply To Post Reply & Quote

Posted at: 8/29/05 11:45 AM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

None

Cojones893

Reply To Post Reply & Quote

Posted at: 8/29/05 01:51 PM

Cojones893 EVIL LEVEL 22

Sign-Up: 03/09/03

Posts: 2,595

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.


None

Rustygames

Reply To Post Reply & Quote

Posted at: 2/22/06 02:18 PM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,662

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


None

squidly

Reply To Post Reply & Quote

Posted at: 2/18/07 01:51 PM

squidly EVIL LEVEL 20

Sign-Up: 02/16/07

Posts: 4,376

what's instant replay?

It's the only way out!
Contraversal::C&C::FAIL::Idiot Pm's

BBS Signature

Goofy

Captainhams

Reply To Post Reply & Quote

Posted at: 7/7/08 05:14 AM

Captainhams DARK LEVEL 08

Sign-Up: 05/04/08

Posts: 124

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

the grass might be greener on the other side - but you still have to mow it

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 11:39 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!