View Full Version : Adding Clips Dynamically from Library...
bferster
June 21st, 2008, 10:36 AM
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
TheCanadian
June 21st, 2008, 12:37 PM
flash.utils.getDefinitionByName (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#getDefinitionByName())
bferster
June 21st, 2008, 01:29 PM
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
flash.utils.getDefinitionByName (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#getDefinitionByName())
TheCanadian
June 21st, 2008, 01:37 PM
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.
bferster
June 21st, 2008, 01:46 PM
Worked like a champ. Thanks for your help!
Bill
TheCanadian
June 21st, 2008, 01:56 PM
Glad to help :mountie:
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.