nameistaken1
nameistaken1

Reputation: 110

AS3 Serialization

What I'm trying to do:

-Have objects in a toolbar, drag and dropable onto a movieclip (they then become a child of the movieclip). Once this is done, I want to be able serialize this object, so I can save it to a file. Then, I can reload this file, and continue draging/dropping things onto/off of this movieclip.

How I'm doing it:

public class Serialization {
    public static function serializeToString(value:Object):String{
        if(value==null){
            trace("null isn't a legal serialization candidate");
        }
        var bytes:ByteArray = new ByteArray();
        bytes.writeObject(value);
        bytes.position = 0;
        var be:Base64Encoder = new Base64Encoder();
        be.encode(bytes.readUTFBytes(bytes.length));
        return be.drain();
    }

    public static function readObjectFromStringBytes(value:String):Object{
        var dec:Base64Decoder=new Base64Decoder();
        dec.decode(value);
        var result:ByteArray=dec.drain();
        result.position=0;
        return result.readObject();
    }      
}

This is where call the function/write it to the file:

        var fr:FileReference = new FileReference;
        fr.addEventListener(Event.COMPLETE, success);
        var txtString:String = new String();
        txtString = save.Serialization.serializeToString(pagePic);
        trace(txtString);
        fr.save(txtString, "test.txt");

Unfortunately, txtString appears to be blank. Am I approaching this wrong?

Side notes: This is being developed for a mobile platform.

Upvotes: 0

Views: 2527

Answers (1)

nameistaken1
nameistaken1

Reputation: 110

Unfortunately MovieClips, Sounds, and other resources cannot be serialized. My solution is to create a custom class that will store all my properties and reassign them upon loading, or just write to/parse a text file when saving/loading.

Upvotes: 1

Related Questions