I’m using the following function to add some images from the library to the stage.
function AddImage(image_name:String):void {
if(image_count == 4) return;
// change the following line so it uses "image_name"
var defaultImage:added_1 = new added_1(100, 100);
var tmpImage:Bitmap = new Bitmap(defaultImage);
tmpImage.x = 124.5 + (108.5 * image_count);
tmpImage.y = 1511.9;
addChild(tmpImage);
image_count++;
}
What I’d like to be able to do is pass the image name as a string parameter to the function but can’t seem to figure out how to do this.
Can someone help me out?
What you want to do is get the Class Definition via using getDefinitionByName so that you can create an instance, the following code is how you do that :
The changes are the import :
import flash.utils.getDefinitionByName;and these two lines :
Note —
Also wanted to mention that in certain cases you might run into an issue if you are compiling with Flex as opposed to the Flash IDE, where you get the following error :
The way to handle that situations is by declaring a variable in your class variable declarations for the compiler, so it recognizes that symbol.
So if your two images class names were image_1 and image_2, in your class declarations you would do something like :
If you have a ton of images, that might be a pain, but that’s the only way I’ve found to get the compiler to recognize them. :/ haha