Andrew Ribeiro
Andrew Ribeiro

Reputation: 676

Save/Load MovieClip Air Android

In AS3 AIR for Android Application I'm developing, the user can build some drawing using shapes and graphics properties. It must to possibility to user save its drawing in its File.applicationStorageDirectory and then, load its drawing again, with properties of draw intact. Look at the following code:

This is class that represent the drawer:

public class Background extends Sprite {
    public var shape:Shape;
    public function Background(){
        this.shape = new Shape();
        shape.graphics.beginFill(0x000000);
        shape.graphics.drawRect(0, 0, 400, 400);
        shape.graphics.endFill();
        this.addChild(shape);
    }
}

Outside, into main: public var bg:Background;

So, when I load some swf externally, I can save/load it without any problem, cause I have its ByteArrays into loader class.

But how can I save a DisplayObject built at runtime? Something like this:

bg = new Background();

try{
            bytes = new ByteArray();
            file = File.applicationStorageDirectory.resolvePath("test/some.swf");
            fileStream = new FileStream();
            fileStream.open(file, FileMode.WRITE);
            fileStream.writeBytes(bytes) // -----HERE, I DO NOT HAVE bg bytes,
            fileStream.close();

        } catch (e:IOError){
            fileStream.close();
        }

I have tried using writeObject() method, it saves normally, but when I try to load it, throws a exception: Unknow file format.

How can I do that? How can I extract bg bytes, or save it any way? Obviously, I wanna retrieve it again, with all its properties intact.

Upvotes: 0

Views: 226

Answers (1)

null
null

Reputation: 5255

Serialisation of DisplayObjects was not possible for a long time and failed silently.

You can either do the serialisation yourself, by recording what the user does, then save that record of the actions instead of the result. (The command pattern comes to mind.)

Or, this: http://www.bytearray.org/?p=4893

Also, take a look at this question: How to read data from shape/graphics object

Upvotes: 1

Related Questions