Cloning non-primative datatypes in Flash MX

I got it.

It’s asyncronous, but I got it. Still, that’s better than the dangerous recursive versions.

Had an idea about how to clone an object in Flash MX as there was one Class that I remember actually copied an object: LocalConnection. I figured this out almost a year ago when we all tried to pass a connected NetConnection object to other Flash movie’s hoping they could share it (since NetConnection’s don’t come cheap in Flashcom). Ultimately, it was a failure because, just like SharedObject’s, when you save a class to it, it strips it of any known linkage to its derived class (unless you use Branden Hall’s trick [don’t have link handy]).

Anyway, read on for the code…

5 Replies to “Cloning non-primative datatypes in Flash MX”

  1. Handy little code snippet, definitly something to keep in the archives. I have to wonder why Flash doesn’t have built in support for it though – something like ColdFusion’s ‘StructCopy’. I’m almost expecting Flash 7 to support this….

    Either way, great job!

  2. Remember, its easy to close an array(see below), just one huge pain in the rear to copy an object, nice code snippet.

    copy=original.slice()

    or

    copy=original.concat()

  3. I was thinking about this on the way to work this morning.

    Ever tried using Brandon Hall’s wddx_mx.as file? If you serialize and then deserialize an object, you should be able to create a clone of it – without the code being asyncronous.

    Something like…

    tmp_wddx = new WDDX();
    copy = tmp_wddx.deserialize(tmp_wddx.serialize(obj_to_copy));

    Just a thought…

  4. Have you seen the inside of that code?

    This isn’t a slam against Branden, but rather Flash. It uses nested while loops, and I’ll admit I’m a little wary of those, but in our case:

    while loop used on unknown content = potential for disaster

    If you have a deeply nested object, then this could get ugly summoning el Script Error Window.

    However, if your cloning that big of an object, maybe one would need to rethink their design. Good point, though, I’ll have to take a look at it.

Comments are closed.