How do you extract a Sprite back out of a ByteArray?

I should probably post this into some AS forums (and will do so later), but I had to get this out there quickly and it’s bugging the hell out of me:

In short, how do you extract a Sprite from a ByteArray? (or any kind of display object for that matter).

I can write the object just fine, and read it back into a variable, but I’ll be blown if I can then convert that back into what it was originally.

Here are my attempts so far:

var test:Sprite = new Sprite();;,0,64,64);;

//addChild(test); // to test, works fine

var b:ByteArray = new ByteArray();

trace(b.length);    //    0 bytes


trace(b.length);    //    754 bytes, so our Sprite is definitely in there

//    Reset the pointer
b.position = 0;

trace(b.position);    //    0 as I’d expect

var newTest:Sprite;
//newTest = b.readObject() as Sprite;    //    Ends up being null
//newTest = Sprite(b.readObject());    // Type Coercion failed error
//addChild(newTest);    //    and kaboom, constant “Parameter child must be non-null.” errors :(

var take2:Object = b.readObject();
trace(take2);    // ok take2 contains an object, but how can I get the Sprite out of it?

trace(b.position);    //    754, so it has definitely read it all

Ummm someone, please help? :)

Posted on February 28th 2009 at 11:47 am by .
View more posts in ActionScript3. Follow responses via the RSS 2.0 feed.

6 Responses

Leave a comment
  • February 28th 2009 at 10:01 pm

    I don’t think it’s possible to write display objects to ByteArrays and then extract them as duplicates. If you’re just trying to copy what’s been drawn to the Graphics object, you might want to check out the copyFrom() function introduced on Graphics in Flash Player 10. I haven’t used it myself, but it appears to duplicate programmatic drawing commands.

  • March 1st 2009 at 5:03 am

    I think there is no direct way to do this other than writing your own de/serialization process for this (which might be quite involved). But I’d like to see being proven wrong with that!

  • March 12th 2009 at 2:49 pm

    add to the your utils class:

    public static function clone(source:Object):* {
    var copier:ByteArray = new ByteArray();
    copier.position = 0;

  • March 12th 2009 at 3:21 pm

    Xitri – Yes this will work for standard Objects, but not for Sprites that have graphics data written to them. There’s also no way to cast the returned object back into a Sprite again (try it and see, you’ll get a type coercion failure).

  • March 12th 2009 at 3:37 pm

    Indeed, it’s almost like trying to clone a human … the clone will get the body but it will not get your soul. It’s like a zombie. And a display object cloned with ByteArray is exactly that, a zombie. 😉

  • cyberprodigy
    April 16th 2013 at 9:02 am

    You can do this by utilizing Loader object.

    Look at this working snippet as an example

    function onLdrComplete(e:Event):void {
    trace(“PatternRenderer::getLoaderFromBytes ” + ldr.content)
    var ldr:Loader = new Loader();
    var loaderContext:LoaderContext = new LoaderContext();
    loaderContext.allowCodeImport = true;
    ldr.loadBytes(clone(byteArray), loaderContext);
    ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onLdrComplete);

Make yourself heard