When you create an object or instance by using the new operator, you call the constructor of a specific class directly, and when you write a program, you often need to create an instance of the class dynamically, and you can use reflection to create the instance.
For example, if you know only an instance without knowing the corresponding class for the instance, you can use reflection to create the instance. All classes in AS3 belong to the class type, for example, MovieClip is a class,bitmap and a class.
Cases:
1. Create a new Flash document
2, click the layer first frame, open the Action panel, enter the code:
//动态得到类的构造函数
var classcontructor:Class = MovieClip;
//创建相关类的实例
var mc:MovieClip = new classcontructor();
mc.graphics.beginFill(0xff9900,100);
mc.graphics.drawRect(0,0,100,100);
addChild(mc);
3, the test film, you can see the drawing of the rectangular map.
Instead of using the MovieClip class to create an instance, the above code first saves the MovieClip class in a variable "Classcontructor" with a data type class, and then creates an instance through "Classcontructor". When the value of "Classcontructor" is changed to another type, the instance created is also of another type.
In the same way to export the picture in the library, step above, look at the code:
//保存链接名
var linkname:String = "BigPhoto";
//得到类信息
var cla:Class = getDefinitionByName(linkname);
//创建类的实例
var bitmapdata:BitmapData = new cla(50,50);
var img:Bitmap = new Bitmap(bitmapdata);
addChild(img);
To test the movie, you can see the picture exported from the library. (Test this code, select the "File" Publishing Settings Flahs tab, ActionScript version (A) point set, remove the rigorous mode of the check, point to determine. )
With the above principle, can be based on the link name from the library dynamically exported pictures, in-depth, you can make SWF material library, according to the link name in the library can dynamically export material.