Flash as 3.0 Novice Learning to fly Tutorial: Create an instance dynamically with reflection

Source: Internet
Author: User
Tags addchild reflection

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.