The Enchanted Cave 2
Delve into a strange cave with a seemingly endless supply of treasure, strategically choos
4.36 / 5.00 33,851 ViewsGhostbusters B.I.P.
COMPLETE edition of the interactive "choose next panel" comic
4.09 / 5.00 12,195 ViewsBefore I embark on a game making adventure I want to know that I can be able to save and load games. I did a quick google search and found some simple code (Actionscript 2.0 for Flash 8) and tried it out. My little test didn't work and I want to know if it is the code, or maybe because it isn't online (just a .swf on my hard drive). Here's the code on the Save and load buttons:
//put on save button
on (release)
{
_root.pos = sharedobject.getLocal("COOKIE NAME");
_root.pos.data.guy = _root.guy._x;
_root.pos.data.guy = _root.guy._y;
trace("Saved")
}
//put on load button
on (release) {
_root.pos = sharedobject.getLocal("COOKIE NAME");
setProperty(_root.guy, _x, _root.pos.data.guyx);
setProperty(_root.guy, _y, _root.pos.data.guyy);
trace("loaded")
}
I should be able to just click save, move my little "guy" and click load and he'll "teleport" to the saved spot right? All the happens is the trace "saved" and "loaded" appears when I click my buttons :(
If you know an easier way to save and load please let me know :)
I think I may know the problem...
you're not allowed to use spaces in it..
so "COOKIE NAME" should be "COOKIENAME" or any other name without spaces
you don't really need flush. its true that it doesn't alter the sharedobject until you either flush or close the swf, but it does remember you are going to though.. so flush is rather pointless.
however, you still can't use spaces, otherwise it returns as undefined.
here's and example fla
(click and hold to drag the red thing, store to store location, read to reset it to its saved location)
as you can see it's almost the same, but the name used is "save". if you were to change it to "sa ve" it wouldn't work (returns undefined)
Hi, just want to make it clear, does this mean we're saving in our local computer instead of saving the game in the server (using shared objects or text file)?
"Always do your best!"
Thanks for the help, but it still isn't working. I tried that example flash file you gave me but it said invalid file type (I use flash 8). Maybe you will be able to open mine and see what I might be doing wrong. SaveAndLoad File
And to answer your question, It will save the "game file" to the same spot your computer stores cookies.
And incase you're too lazy to open here are the changes:
//put on save button
on (release)
{
_root.pos = sharedobject.getLocal("save");
_root.pos.data.guy = _root.guy._x;
_root.pos.data.guy = _root.guy._y;
_root.pos.flush(1024);
}
//put on load button
on (release) {
_root.pos = sharedobject.getLocal("save");
setProperty(_root.guy, _x, _root.pos.data.guyx);
setProperty(_root.guy, _y, _root.pos.data.guyy);
_root.guy._x = _root.pos.data.guyx
_root.guy._x = _root.pos.data.guyy
}
//put on "guy"
onClipEvent (enterFrame) {
if(Key.isDown(37)){
_root.guy._x -= 5;
};
if(Key.isDown(38)){
_root.guy._y -= 5;
};
if(Key.isDown(39)){
_root.guy._x += 5;
};
if(Key.isDown(40)){
_root.guy._y += (5);
};
} Here is a link to the best Save N' Load tutorial ever. It also tells you how to allow one swf file to load data made from another.
Here it is:
Woo I got it working, it turns out there were only 2 small errors with my code:
_root.pos = SharedObject.getLocal("save");
_root.pos.data.guyx = _root.guy._x;
_root.pos.data.guyy= _root.guy._y;
Now I can save and teleport my guy like I should've been able to hours ago. :)