Be a Supporter!

HowdoI: Save n Load?

  • 278 Views
  • 7 Replies
New Topic Respond to this Topic
fleetatks
fleetatks
  • Member since: Apr. 10, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
HowdoI: Save n Load? 2009-07-30 17:13:29 Reply

Before 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 :)

Anamethatworks
Anamethatworks
  • Member since: May. 17, 2009
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to HowdoI: Save n Load? 2009-07-30 21:25:32 Reply

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

knugen
knugen
  • Member since: Feb. 7, 2005
  • Offline.
Forum Stats
Member
Level 42
Programmer
Response to HowdoI: Save n Load? 2009-07-30 22:34:24 Reply

1. Save
2. Flush
3. Load

Anamethatworks
Anamethatworks
  • Member since: May. 17, 2009
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to HowdoI: Save n Load? 2009-07-31 10:39:47 Reply

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)

mychii
mychii
  • Member since: Aug. 1, 2009
  • Offline.
Forum Stats
Member
Level 02
Game Developer
Response to HowdoI: Save n Load? 2009-08-02 00:57:17 Reply

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!"

fleetatks
fleetatks
  • Member since: Apr. 10, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to HowdoI: Save n Load? 2009-08-03 21:52:40 Reply

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);
};
}
Kammce
Kammce
  • Member since: Oct. 3, 2008
  • Offline.
Forum Stats
Member
Level 13
Programmer
Response to HowdoI: Save n Load? 2009-08-04 00:03:08 Reply

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:

Trick To SharedObject

fleetatks
fleetatks
  • Member since: Apr. 10, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to HowdoI: Save n Load? 2009-08-04 03:50:28 Reply

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. :)