droqen
|
|
« on: May 11, 2010, 08:53:52 PM » |
|
I have a very simple question that should be easily answered, but I can't find a thing about it anywhere! Am I looking in all the wrong places?
I want to make a Flash game that can save files on the local computer, and load them up again upon reopening the file.
I just need to store some numerical data for local high scores - that's it. ;_;
In case it's important, I'm using AS3 in FlashDevelop, and using FlashPunk.
help meee (please)
|
|
|
Logged
|
|
|
|
Martin 2BAM
|
|
« Reply #1 on: May 11, 2010, 09:10:19 PM » |
|
For some reason you're avoiding using a SharedObject? Those are like Flash "Cookies" and the operation is completly transparent. There is another way to save a file and load it up, but it's a bit awkward for a game (an OS dialog pops up). Is there a real reason for this? Regards! EDIT: To answer your question, something like this //Saving
var file:FileReference = new FileReference; var data:ByteArray = new ByteArray; data.writeObject(dataToSave); file.save(data);
//Loading
var file:FileReference = new FileReference; file.addEventListener(Event.SELECT, loadSelect); file.browse([]);
private function loadSelect(e:Event):void { var file:FileReference = FileReference(e.target); file.addEventListener(Event.COMPLETE, loaded); file.removeEventListener(Event.SELECT, loadSelect); file.load(); } private function loaded(e:Event):void { var file:FileReference = FileReference(e.target); file.removeEventListener(Event.COMPLETE, loaded); trace("LOADED DATA", file.data); }
But I'd suggest //save var so:SharedObject = SharedObject.getLocal("meh"); so.data["points"] = highscore; so.flush();
//load var so:SharedObject = SharedObject.getLocal("meh"); highscore = so.data["points"];
|
|
« Last Edit: May 11, 2010, 09:18:17 PM by nitram_cero »
|
Logged
|
|
|
|
droqen
|
|
« Reply #2 on: May 11, 2010, 09:16:32 PM » |
|
Thank you so much, you have no idea how much this helped me.
Now that I have a name (somehow I found a different name that FlashDevelop didn't recognize/couldn't import) that works, I can get to making things work.
Yay! Thank you very much, nitram <3
|
|
|
Logged
|
|
|
|
Martin 2BAM
|
|
« Reply #3 on: May 11, 2010, 09:19:44 PM » |
|
Oh, you just wrote a message... well I've just edited mine Hey, no problem! Make cool games
|
|
|
Logged
|
|
|
|
droqen
|
|
« Reply #4 on: May 11, 2010, 10:06:21 PM » |
|
This will be my final message; you were very helpful, and I have now implemented pretty much every modicum of saving that I planned to implement in this game. Your examples were of great assistance :]
(as was google, but I can't thank google personally :3)
Thank you! I will make cool games! And I will do it foreverrrr
|
|
|
Logged
|
|
|
|
st33d
Guest
|
|
« Reply #5 on: May 12, 2010, 02:26:14 PM » |
|
Pro-tip: Did you know that you can also call javascript from your flash movie to save text files? We use this to save xml files of levels we're designing at work. Here's where I got the code from: http://www.galasoft.ch/myjavascript/CExplorer/index.html
|
|
|
Logged
|
|
|
|
Draknek
Level 6
"Alan Hazelden" for short
|
|
« Reply #6 on: May 12, 2010, 03:13:32 PM » |
|
Did you know that you can also call javascript from your flash movie to save text files?
Sure you can do that, but why would you? It won't work in Firefox unless it's downloaded locally, and even then you're relying on the vagaries of the Javascript implementation. If you want to save to an actual file (for e.g. level editing) you should use FileReference.save(), as in Nitram Cero's example #1. If you want to have save data work transparently to the user you should use SharedObject, as in example code #2.
|
|
|
Logged
|
|
|
|
increpare
Guest
|
|
« Reply #7 on: May 12, 2010, 03:16:23 PM » |
|
Woah. I didn't know about file references. That's really neat!
|
|
|
Logged
|
|
|
|
Draknek
Level 6
"Alan Hazelden" for short
|
|
« Reply #8 on: May 12, 2010, 03:26:31 PM » |
|
They are indeed. A couple of minor things it might be worth pointing out about them:
You need to target Flash 10. The FileReference class has been around before then, but it only got the useful direct save-to-disk/load-from-disk functionality in Flash 10.
You need to call the load/save methods from a Flash key event. If you don't, you'll get a security exception (when uploaded, anyway) and your game might appear to freeze up. So if you normally use something like if (Input.keyDown(KEY.S)) { save(); } you'll need to add a Flash event listener to make it work.
|
|
|
Logged
|
|
|
|
st33d
Guest
|
|
« Reply #9 on: May 13, 2010, 01:17:14 AM » |
|
Thanks for the FileReference thing. This should save me a lot of trouble.
I wish they would point out these things when they roll out the new players.
We've had to rely on this Javascript hack since making stuff in AS2, it was the only way we could save files and knock out level editors for the games.
Especially annoying is that I was stuck using Firefox as my editor, because the javascript hack only saves ansi encoded files, so when I used an LZW compression algorithm on the XML I would have to use a Mozilla specific XML file output method.
And yes it does only work locally, but when you need to create a level editor for co-workers to use with anything less than Flash Player 10...
|
|
|
Logged
|
|
|
|
st33d
Guest
|
|
« Reply #10 on: May 13, 2010, 01:36:16 AM » |
|
Oh wait, now I see why this FileReference feature is a bit shit.
It won't let me do my quicksave feature. I usually silently save settings files as the editor runs. And I create a quicksave file before testing a level, it's saved me from quite a few accidents.
The FileReference security won't let me save anything without opening a dialog box. So that means I can save a small file remembering the layout of the editor without bothering the user constantly with dialog boxes. And no I can't opt for a sharedobject - it would lock me into developing from a specific file location on the computer.
Oh well.
|
|
|
Logged
|
|
|
|
CiroContinisio
|
|
« Reply #11 on: May 13, 2010, 03:03:38 AM » |
|
Yes the FileReference always ask the user for confirmation (by opening the Save As... dialog). This is intended for security reasons.
If you can, you can target AIR. It has file saving features. If that's viable for you.
|
|
|
Logged
|
|
|
|
BorisTheBrave
|
|
« Reply #12 on: May 13, 2010, 02:06:02 PM » |
|
File reference is soo annoying. I'd recommend AIR, but I admit I haven't tried it's save features, I just know they are "proper".
|
|
|
Logged
|
|
|
|
|