Import assets from Library

In ActionScript 2.0 it is quite easy to import movie clips from Library via the MovieClip.attachMovie() function. In ActionScript 3.0 it’s a little bit different. The movie clips (or assets in the Library) don’t have a linkage id anymore. Instead they have class names.

So the way to import a movie clip (or another asset) is to first get a reference to that class specified as class name. This is done using the flash.utils.getDefinitionByName() function. Then instantiate the class and finally add the object to the display list.

Here is a simple example:

1
2
3
4
5
6
7
8
9
import flash.utils.getDefinitionByName;
 
var className:String = "TestClip";
var ClassReference:Class = getDefinitionByName(className) as Class;
var clip:MovieClip = new ClassReference();
 
addChild(clip);
clip.x = 100;
clip.y = 100;

One thought on “Import assets from Library

  1. Very cool! I was looking for that for quite a long time, and finally I found it now. Saves me a lot of work.
    Thank you very much!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">