# Adding Clips Dynamically from Library

**URL:** <https://forum.kirupa.com/t/adding-clips-dynamically-from-library/263987>\
**Category:** Uncategorized\
**Created:** [June 21, 2008, 2:36pm UTC](https://forum.kirupa.com/t/adding-clips-dynamically-from-library/263987 "2008-06-21T14:36:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bferster](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@bferster](https://forum.kirupa.com/u/bferster)\
**Post date:** [June 21, 2008, 2:36pm UTC](https://forum.kirupa.com/t/adding-clips-dynamically-from-library/263987/1 "2008-06-21T14:36:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 21, 2008, 4:37pm UTC](https://forum.kirupa.com/t/adding-clips-dynamically-from-library/263987/2 "2008-06-21T16:37:28Z")

</div>

[flash.utils.getDefinitionByName](http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#getDefinitionByName())

---

<div class="post-metadata">

**Author:** ![bferster](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@bferster](https://forum.kirupa.com/u/bferster)\
**Post date:** [June 21, 2008, 5:29pm UTC](https://forum.kirupa.com/t/adding-clips-dynamically-from-library/263987/3 "2008-06-21T17:29:36Z")

</div>

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

> [@TheCanadian;2346825](#):
>
> [flash.utils.getDefinitionByName](http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#getDefinitionByName())

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 21, 2008, 5:37pm UTC](https://forum.kirupa.com/t/adding-clips-dynamically-from-library/263987/4 "2008-06-21T17:37:58Z")

</div>

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![bferster](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@bferster](https://forum.kirupa.com/u/bferster)\
**Post date:** [June 21, 2008, 5:46pm UTC](https://forum.kirupa.com/t/adding-clips-dynamically-from-library/263987/5 "2008-06-21T17:46:01Z")

</div>

Worked like a champ. Thanks for your help!  
Bill

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 21, 2008, 5:56pm UTC](https://forum.kirupa.com/t/adding-clips-dynamically-from-library/263987/6 "2008-06-21T17:56:49Z")

</div>

Glad to help :mountie:
