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 "//");
Ii. CopyLoaderLoadedSWF
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.
Method1--BytearrayDeep 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.
Method2--ApplicationdomainDynamic class replication during 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:
- The above two replication methods require local file-level security because they both need to load SWF files;
- When the second category (applicationdomain.pdf is used, the document class attribute must be added to the stage when "pait.swf" is generated. The added document class can be a specific external as class or only one 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.
- Applicationdomain has more advanced usage. If you are interested in advanced as, you can study it.
The author's address and a simple copy of the external flash code will be released below
VaR byteloader: urlloader = new urlloader ();
Byteloader. dataformat = urlloaderdataformat. Binary;
Byteloader. addeventlistener (event. Complete, loadcompletehandler );
Byteloader. Load (New URLRequest ("a2.swf "));
Function loadcompletehandler (EVT: Event): void
{
For (var I: Int = 0; I <10; I ++ ){
VaR Loader: loader = new loader ();
Loader. x = I * 50;
Loader. loadbytes (EVT. currenttarget. data );
Addchild (loader );
}
}
Reprinted from: http://hi.baidu.com/iscriptdada/item/30199d5e1d1df22194eb05ed#0