I feel like I'm a little out of my depth here - I need to figure out a way to allow users to save their galaxy in an outer space game that I'm building. At the most basic level, I need to write the randomly generated galaxy (which is an instance of a custom class) to disk and then read it from the disk later. I've not used Air's filestream abilities before and I'm having a bit of trouble. My code:
public function saveGame(e:MouseEvent):void
{
trace("save");
var file:File = File.desktopDirectory.resolvePath("test.sv");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeObject(gal);
fileStream.close();
}
public function loadGame(e:MouseEvent):void
{
trace("load");
var file:File = File.desktopDirectory.resolvePath("test.sv");
if (file.exists)
{
var obj:Object;
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
obj = fileStream.readObject();
fileStream.close();
trace(obj);
}
}
The object "gal" is my galaxy, and I've verified that it's a valid instance of my object (it contains children and it contains many variables) but for some reason the file created on the desktop is only 1 byte - obviously too small to contain any info.
When I trace the object in the second function it's null.
Any help on this would be greatly appreciated, as it's very difficult to find this information clearly explained for a beginner such as me.
Thanks for any insight you may have.