If you are creating a game and you want your users to be able to save their current location, level, points, settings, etc then using this script is what you need.
If you understand SharedObject.getLocal then this will be easy.
Say for example you have a Dynamic Text box for the players score.
The text box is called score1 and the text box variable is called score. As the player collects a certin item (kills an enemy, etc) his score goes up in the score1.text box. After awhile he may want to quit playing but return later and start off where he last left. So create a button. Next to the button put Save Game. Inside the buttons actionScript you need to add
code:
on (release) {
//this takes the current score and places it in saveScore
saveScore(score1.text);
}
this takes the data from score1.text.
in order for it to actully save and recall the data you need to add to
the keyframe that the save button is placed in and add in actions
code:
//reads any saved data from "test"
//if "test" is changed the data is saved in a different location
localInfo = SharedObject.getLocal("test");
function saveScore() {
localInfo.data.score
}
You now have the players score saved onto their hard drive.
Now if you want the player to be able to recall the data...
Add a load game button to your main page.
In the laod game button add
code:
gotoAndStop("Scene 2", 1);//or whatever you have the game starting at
_root.loadedgame = 1;
the new game button should just have
code:
gotoAndStop("Scene 2", 1);//or whatever you have the game starting at
In Scene 2 frame 1 you should have the following...
code:
if (_root.loadedgame == 1) {
localInfo = SharedObject.getLocal("test");
if (localInfo.data.score == undefined) {
play();
//makes the score 0 as if starting a new game
//with no saved data found
score = 0;
} else {
//makes score the saved data
score = localInfo.data.score;
} else if (!_root.loadedgame == 1) {
//starts a new game, score is 0
score = 0;
}
If you wanted to add more like the current level the player was on all you would need is to have a variable that says the current level, then when the player saves add
saveLevel(level1.text);//make sure the text is called level1
or you could have
code:
localInfo.data.level = 1//or current level, number is changed
//in each different level
then in the same keyframe with the save game button
add
function saveLevel() {
localInfo.data.level
}
//function saveScore() should also be here
then when recalling the data
code:
if (_root.loadedgame == 1) {
localInfo = SharedObject.getLocal("test");
if (localInfo.data.score == undefined || localInfo.data.level == undefined) {
play();
//makes the score 0 as if starting a new game
//with no saved data found
score = 0;
} else {
//makes score the saved data
level = localInfo.data.level
if (level == 1) {
gotoAndPlay("Scene 2", 1);
} else if (level == 2) {
gotoAndStop("Scene 3", 1);
} //and so on
score = localInfo.data.score;
} else if (!_root.loadedgame == 1) {
//starts a new game, score is 0
score = 0;
}
its not to hard to change or add more/ less save data from there...
if you don't understand somthing
or need help, reply to this post...
ill be making a fla for download so everyone can see what I mean if you dont understand. It may take a little to understand, but once you get it you can have many save game options.
I uh, don't get it. So your creating a variable to save the game progress? What if I closed the window, and ran the game again. How would I load my game then?
yes it is like a cookie... it saves the data onto the users hard drive, and yes if you close the game and reload it the data will still be there as long as you call it up
and like I said... to recall data
then when recalling the data
code:
if (_root.loadedgame == 1) {
localInfo = SharedObject.getLocal("test");
//this checks to see if anydata was saved in the first place
if (localInfo.data.score == undefined || localInfo.data.level == undefined) {
//if not...
play();
//makes the score 0 as if starting a new game
//with no saved data found
score = 0;
} else {
//data was saved, recalling and using saved data
//as if the level was saved, recalls the data and sends the user
level = localInfo.data.level
if (level == 1) {
gotoAndPlay("Scene 2", 1);
} else if (level == 2) {
gotoAndStop("Scene 3", 1);
} //and so on
//makes score the saved data
score = localInfo.data.score;
} else if (!_root.loadedgame == 1) {
//starts a new game, score is 0
score = 0;
}
Im building a guide for alot of flash things at my website... www.wtflash.com
One of the places its useful is in a game where you login. There you can store the username from last time in a cookie and then the user will have his name there when he opens the game again.
yes it is and here is an example of how you could load up the data, if you had other variables to save data
code:
this.createTextField("dData", myDepth, 50, 190, 150, 70);
name.selectable = false;
dData.selectable = false;
dData.setNewTextFormat(Arial);
dData.text = localInfo.data.user+","+newline+"Last time you played"+newline+"you won "+localInfo.data.winS+" round(s) and lost "+localInfo.data.losses+"."+newline+newline+"S tart New Game >>>";
name = "Is '"+localInfo.data.user+"' not your name?"
name2 = "Sign in as someone else >>>"
EDIT:
--sorry about the coding extending off the screen.
Also... name and name2 were text box created on the screen not added with coding and next to name 2 was a button that inside stated the localInfo.data.name = undefined so a new name could be created.
Im still working on a save game fla for download, I had created one eariler but it is sloppy and probably would be hard to understand... Im also adding this to my UFG [ultimate flash guide] at my site www.wtflash.com when I get the time
Another related trick is to serialize the state of your game objects using XML, WDDX or any other object serializer and save that string using SharedObjects. That way you can save and load complex objects very easily with just a single call. Have fun!
I've put all the actionscript in, it says there are no syntax errors, everything works fine except it either won't save or load. See the swf attachment. I've used this actionscript:
New Game Button:
code:
on(release){
gotoAndStop("Scene 2", 1);
//or whatever you have the game starting at
}
Load Game Button:
code:
on(release){
gotoAndStop("Scene 2", 1);
//or whatever you have the game starting at
_root.loadedgame = 1;
}
Save Game Button:
code:
on (release) {
//this takes the current score and places it in saveScore
saveScore(score.text);
}
Scene 2, Frame 1 (The one with the Fire! button on it)
code:
//reads any saved data from "test"
//if "test" is changed the data is saved in a different location
localInfo = SharedObject.getLocal ("test");
function saveScore () {
localInfo.data.score;
}
if (_root.loadedgame == 1) {
localInfo = SharedObject.getLocal ("test");
if (localInfo.data.score == undefined) {
//makes the score 0 as if starting a new game
//with no saved data found
score = 0;
} else {
//makes score the saved data
score = localInfo.data.score;
}
} else if (!_root.loadedgame == 1) {
//starts a new game, score is 0
score = 0;
}
here, let me make you a save data fla, using only MX...
edit: btw, you didnt add an attachment
...the thing I can think you may of done wrong is that you didnt give the score text field the -instance name- of "score" you gave it the -variable name- of "score", so it couldnt find the text field, so it wouldnt save the data.
trying to use this code for a save-feature on a leveling calculator i'm developing for players of an existing MMORPG... having some issues understanding where "test" came from and how it's implimented... downloaded the FLA and haven't had nearly enough time to look through it... but tested the resulting SWF and it worked like I want it to... as long as this can be used to save user-input then I'm golden...