As3 copying a visual object)

Source: Internet
Author: User
Tags addchild

As3 copying a visual object

 

I. Copy the video clips on the stage

Method 1 -- Reflection Method:

VaR classref: class = getdefinitionbyname (getqualifiedclassname (t_mc) as class;

VaR clone_mc: displayobject = new classref ();

Addchild (clone_mc );

Method 2 -- constructor method:

VaR classref: class = t_mc.constructor;

VaR clone_mc: movieclip = new classref ();

Addchild (clone_mc );

Method 3 -- the compiler automatically generates the class method:

This method is a variant of method 1, which is simpler, more crude, and more effective than method 1. I will explain this method for you as follows:

First, create a video clip in the library, draw a picture at will, and then add a single line comment "//" to the first frame;

Next, drag the video clip you just created to the stage and create an example named "t_mc ";

Now, the point is: Write the following statement on the main time line, press Ctrl + enter, and observe the output information in the output panel carefully.

Trace (getqualifiedclassname (t_mc ));

The output result is "_ FLA: timeline_1 ". Out of a clear understanding of the function getqualifiedclassname, you immediately thought that timeline_1 should be the class automatically created by the compiler for video editing in our library. We can use this class to produce multiple identical video clips! Is that true? Let's verify:

VaR clone_mc: displayobject = new timeline_1 ();

Addchild (clone_mc );

CTRL + enter, you are surprised to find that we have succeeded! After the experiment, many friends may be excited to write a learning summary. Don't worry, guys. Our experiment is not complete yet-now, we use the same method to create a video clip in the library again. Then, drag it to the stage to create an instance named "d_mc ". Next, we will change "t_mc" in the trace (getqualifiedclassname (t_mc); statement to "d_mc ". Test the video. The output result is "flash. display: movieclip ".

Suddenly! Silly, you cannot use new movieclip to copy "d_mc!

How can this problem be solved? I will give you a tip: Try to get each of the two video clips in the library a loud name (name), and then let's take a look at the output class name.

Note the following before copying a video clip:

You must create a video clip in the library and drag the video clip in the library to the stage to create an instance;
The video clip frame must contain code (the comment is also a line, the simplest is to add a single line comment symbol "//");

2. Copy the SWF loaded by loader

Copying external SWF is a relatively advanced operation, with many steps and complicated processes. It takes a lot of time to elaborate on the principle details. Therefore, here I am lazy and I will explain it in detail. I just use code to explain the problem, and I hope to play a role in attracting others who can understand it.

Method 1 -- bytearray deep Replication

VaR byteloader: urlloader = new urlloader ();

Byteloader. dataformat = urlloaderdataformat. Binary;

Byteloader. addeventlistener (event. Complete, loadcompletehandler );

VaR Loader: loader = new loader ();

Loader. Y = 200;

Addchild (loader );

Loader. contentloaderinfo. addeventlistener (event. init, loadinithandler );

Function loadinithandler (EVT: Event): void

{

VaR cloneloder: loader = new loader ();

Addchild (cloneloder );

Cloneloder. loadbytes (byteloader. data );

}

Byteloader. Load (New URLRequest ("t.swf "));

Function loadcompletehandler (EVT: Event): void

{

Loader. loadbytes (byteloader. data );

}

Simple principle: Use urlloader to load external SWF to bytearray in binary data format, and then use loadbytes of the loader class to load bytearray in urlloader.

Method 2 -- Dynamic class replication during applicationdomain Runtime

VaR Loader: loader = new loader ();

Loader. Y = 200;

Addchild (loader );

Loader. contentloaderinfo. addeventlistener (event. init, loadinithandler );

VaR context: loadercontext = new loadercontext ();

Context. applicationdomain = applicationdomain. currentdomain;

Loader. Load (New URLRequest ("t.swf"), context );

Function loadinithandler (EVT: Event): void

{

VaR classref: class = loader. contentloaderinfo. applicationdomain. getdefinition ("testclass") as class;

VaR clone_mc: movieclip = new classref ();

Addchild (clone_mc );

}

Note:

1. The above two replication methods require local file-level security because they both need to load SWF files;
2.when using the second category (applicationdomaincategory, when generating category t.swf ", you must add the document class attribute for the stage. The added document class can be a specific external as class or just a name. In the above example, the document name I added is "testclass". The parameter content in getdefinition ("testclass") should be the same as the document class name of the SWF to be copied.
3. More advanced usage of applicationdomain. If you are interested in advanced as, you can study it.

 

Source: http://hi.baidu.com/javabbs/blog/item/dcb9a1456269182dcefca32d.html

 

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.