Skip to content
Aug 10 2009 / alecmce

Clone Loaded Swf

To create a clone of a loaded swf:

/**
 * clone a Loader by duplicating its loaded bytes; this can be performed only once
 * the loader has completed loading, otherwise you'll run into problems!
 *
 * @param source The original loader, which remains unchanged
 * @return A new Loader that is a clone of source
 */
function cloneLoader(source:Loader):Loader
{
	var clone:Loader = new Loader();
	clone.loadBytes(source.contentLoaderInfo.bytes);
	return clone;
}

This post is as much a quick reminder for me as for everyone else, but it came up today and it’s the sort of thing I do rarely enough to forget exactly how. Since I try as far as humanly possible not to contain code and assets in the same swf (at least for big projects), this is useful because it avoids the need to do something like this:

/**
 * clone a display object, only if it has an ActionScript linkage ID
 * otherwise you'll end up with an empty display object of some
 * description.
 *
 * @param source The source display object
 * @return A new display object that is a clone of source
 */
function cloneClip(source:DisplayObject):DisplayObject
{
	var classToCreate:Class = Object(source).constructor;
	return new classToCreate();
}

which requires that the DisplayObject has an AS3 linkage ID, and therefore that code and assets are not separated.

  • Anonymous

    Nice one Alec!
    this gives more freedom to designers so they don’t need to set Linkage Ids but just create the Clips.

  • http://blog.vizio360.co.uk simone

    Nice one Alec!
    this gives more freedom to designers so they don’t need to set Linkage Ids but just create the Clips.

  • http://www.jacksondunstan.com/ Jackson Dunstan

    Nice examples. I should note though that this doesn’t really clone the object, but rather instantiates a new one. So if you had, say, set the alpha on the object you are cloning, the newly-created object would not have its alpha set as well. Still, this is quite useful when you want to make a lot of instances, particularly of a MovieClip.

  • http://www.jacksondunstan.com Jackson Dunstan

    Nice examples. I should note though that this doesn’t really clone the object, but rather instantiates a new one. So if you had, say, set the alpha on the object you are cloning, the newly-created object would not have its alpha set as well. Still, this is quite useful when you want to make a lot of instances, particularly of a MovieClip.

  • http://alecmce.com Anonymous

    You’re right. Either can be expanded into a more traditional ‘clone’ method, but I tend to prefer not to. (Much as cloning Dolly didn’t immediately end up with a sheep of the same size!)

    One word of warning: if you’re loading a third-party swf with this method, you ought to exercise extreme caution! By copying the bytes into a new loader, you remove security restrictions and the resultant Loader’s content will be in your application’s security context.

    If you know that a third-party application is loading in your content, you could use this as an exploit to manipulate the code of the loading application.

  • http://alecmce.com alec

    You’re right. Either can be expanded into a more traditional ‘clone’ method, but I tend to prefer not to. (Much as cloning Dolly didn’t immediately end up with a sheep of the same size!)

    One word of warning: if you’re loading a third-party swf with this method, you ought to exercise extreme caution! By copying the bytes into a new loader, you remove security restrictions and the resultant Loader’s content will be in your application’s security context.

    If you know that a third-party application is loading in your content, you could use this as an exploit to manipulate the code of the loading application.

  • Daniel

    This helped me!
    I was trying to use bulkloader, but I was having a hard time accessing the loader, so I tried it with the casa lib’s swfLoader and was able to to clone the object easily.
    Thanks!!

  • Daniel

    This helped me!
    I was trying to use bulkloader, but I was having a hard time accessing the loader, so I tried it with the casa lib's swfLoader and was able to to clone the object easily.
    Thanks!!