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:
The above two replication methods require local file-level security because they both need to load swf files;
When you use the second category (applicationdomaincategory, you must add the document class attribute to the stage when generating category t.swf. 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.
ApplicationDomain has more advanced usage. If you are interested in advanced AS, you can study it.
Copy external swf