Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /share/CACHEDEV1_DATA/Web/www/libraries/UBBcode/text_parser.class.php on line 228
Cloning external MovieClips in ActionScript 3

Comments Blog About Development Research Sites

Cloning external MovieClips in ActionScript 3

Jun 23, 2009
To take a detour past unfamiliar territory I will this time write a little about an often overlooked language: ActionScript 3. It shares many aspects with JAVA, yet it is only optionally type-save like PHP. Combined with a determination in the latest version of this language to go OOP all the way the result is both interesting and in some cases horifying, but more on that later.

A problem I came acros a few days ago is that AS3 does not support the direct cloning of MovieClips. There used to be a method for doing so in AS2, but that is now a thing of the past. Thus, anyone who would want to load in an external SWF file, then copy it or even rename it is in trouble.

The solution to this is not directly evident for one searching in the wrong direction: instead of focusing on the object in the SWF file, focus on the class of that object. This means that before you export an object as SWF file you must assign it a class, but that is a trivial matter. Consider an external object loader and the associated completion handler:

Code (php) (nieuw venster):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private function completeHandler (event:Event):void {
  var
definition:Class        =
    event.target.applicationDomain.getDefinition("Asset");
  var
instance:DisplayObject  = new definition();
  timeline.addChild(instance);
}


private function
loadExternalObject (objectId:String):void {
  var
loadHandler:Loader      = new Loader();
  var
urlRequest:URLRequest   = new URLRequest(objectId);
  dispatcher.addEventListener(Event.COMPLETE, completeHandler);
  loadHandler.load(urlRequest);
}


The first line of the completion handler is what it's all about: instead of retrieving the event.target.content (which defines the top-most object in that SWF file) we retrieve the class definition given therein of that object. Using that definition we can instantiate a new displayObject (be it a MovieClip or otherwise) and store the definition for later if we want to create more such objects. What is more important, the newly created object is not connected to a timeline yet, so you could possibly assign it a new name and whatnot!

Credits for this idea go to Daryl Teo who describes it (with pretty pictures!) here in his blog.

FragFrog out!

New comment

Your name:
Comment: