1

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.

Smashbox
  • 41
  • 3

3 Answers3

0

I think we could only serialize dynamic object, and save it as json. like XML,Array,Object,dictionary can do this. make sure their elements are basic type and dynamic class instances with can be interate.

Last Chance
  • 73
  • 1
  • 7
0

Why not save out some XML or something that you can read in and parse to objects?

Amy Blankenship
  • 6,485
  • 2
  • 22
  • 45
0

Similar questions have been asked before. Check out the answers: IExternalizable (as3), Saving application state AIR.

Community
  • 1
  • 1
Valentin Simonov
  • 1,768
  • 10
  • 14