Adding Clips Dynamically from Library

I am trying to dynmically add sprites from the Library in AS3, but don’t know how to do this.

Assuming that ‘Bubble’ is an artwork clip in my Libary, I can do this:

var mc:Sprite=new Bubble();

but want to do something like this, where the sprite is derrived from a string:

var str=“Bubble”;
var mc:Sprite=new str;

Obviously this won’t work. Any thoughts?

Thanks,

Bill

flash.utils.getDefinitionByName

It looks like it should work, but I get a null when I run this:

var tmc:MovieClip=getDefinitionByName(“Bubble”) as MovieClip;

Bubble does exist in the Library. Any thoughts?

Thanks!

Bill

var tmc:Bubble = new (getDefinitionByName("Bubble") as Class)();

That’s it all mangled into one line.

If you want to use the Bubble reference a lot in your code, you should store it in a variable:

var bubbleRef:Class = getDefinitionByName("Bubble") as Class;
var tmc:Bubble = new bubbleRef();

Note that you need to use the fully qualified class name. So if your Bubble class is in the package foo.bar you would use the string “foo.bar.Bubble” to aquire the reference.

Worked like a champ. Thanks for your help!
Bill

Glad to help :mountie: