1-first write a class for loading external SWF files
Package package
{
Import flash. display. displayobject;
Import flash. display. loader;
Import flash. Events .*;
Import flash.net. URLRequest;
Import MX. Core. uicomponent;
Import MX. Events. flexevent;
Public class ctmobjloader extends uicomponent
{
Private var Loader: loader;
Private var progresshandle: function = NULL;
Public VaR _ bloaded: Boolean = false;
Public Function ctmobjloader (URLRequest: String = NULL, progresshandle: function = NULL)
{
Super ();
If (URLRequest) loadthis (URLRequest, progresshandle );
}
Public Function loadthis (URLRequest: String, progresshandle: function = NULL): void {
Removechild ();
Loader = new loader ();
Progresshandle = progresshandle;
// Deletelisteners (loader. contentloaderinfo );
Configurelisteners (loader. contentloaderinfo );
Addchild (loader );
VaR request: URLRequest = new URLRequest (URLRequest );
Loader. Load (request );
}
Public Function unloadthis (): void {
Try {
Removechild ();
This. Parent. removechild (this );
} Catch (E: Error ){}
// This = NULL;
}
Public Function get content (): displayobject {
If (! _ Bloaded) return NULL;
Return loader. content;
}
Private function removechild (): void {
If (_ bloaded ){
Try {
Deletelisteners (loader. contentloaderinfo );
Loader. Unload ();
Removechild (loader );
Loader = NULL;
_ Bloaded = false;
} Catch (E: Error ){}
}
}
Private function configurelisteners (Dispatcher: ieventdispatcher): void {
Dispatcher. addeventlistener (event. Complete, completehandler );
// Var A: flexevent
Dispatcher. addeventlistener (flexevent. application_complete, tttt );
Dispatcher. addeventlistener (httpstatusevent. http_status, httpstatushandler );
Dispatcher. addeventlistener (ioerrorevent. io_error, ioerrorhandler );
If (progresshandle! = NULL) Dispatcher. addeventlistener (progressevent. Progress,
Progresshandle );
}
Private function tttt (Event: flexevent): void {
Dispatchevent (new event (flexevent. application_complete ));
}
Private function deletelisteners (Dispatcher: ieventdispatcher): void {
If (dispatcher. haseventlistener (event. Complete) Dispatcher. removeeventlistener
(Event. Complete, completehandler );
If (dispatcher. haseventlistener (httpstatusevent. http_status ))
Dispatcher. removeeventlistener (httpstatusevent. http_status, httpstatushandler );
If (dispatcher. haseventlistener (ioerrorevent. io_error ))
Dispatcher. removeeventlistener (ioerrorevent. io_error, ioerrorhandler );
If (dispatcher. haseventlistener (progressevent. Progress ))
If (progresshandle! = NULL) Dispatcher. removeeventlistener (progressevent. Progress,
Progresshandle );
}
Private function completehandler (Event: Event): void {
This. width = loader. content. width;
This. Height = loader. content. height;
_ Bloaded = true;
Dispatchevent (new event (event. Complete ));
// If (cmphandle) cmphandle
}
Private function httpstatushandler (Event: httpstatusevent): void {
Dispatchevent (new event (httpstatusevent. http_status ));
}
Private function ioerrorhandler (Event: ioerrorevent): void {
Dispatchevent (new event (ioerrorevent. io_error ));
}
}
}
This class is used to load external SWF files [flex-flash]. Of course, you can also load image files.
2. Write the main file main. mxml
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute"
Xmlns: NS1 = "manageview. *" fontsize = "12">
<Mx: SCRIPT>
<! [CDATA [
/*
Flex controls the loading of external SWF files [the subfile is Flex]
*/
Import MX. Managers. systemmanager;
Import MX. Controls. Alert;
Import package. ctmobjloader;
Import flash. utils. setTimeout;
[Bindable] private VaR _ ctmobjloader: ctmobjloader;
Private var loadedsm: systemmanager;
Private function loadswf (): void {// file Loading Function
Loadedsm = NULL; // sub-File Manager
If (_ ctmobjloader ){
_Ctmobjloader.loadthis('child.swf '); // child.swf indicates the sub-file name, which is the same as the directory.
} Else {
_ Ctmobjloader = new ctmobjloader(''child.swf '');
_ Ctmobjloader. addeventlistener (ioerrorevent. io_error, onfailhandle); // listener failure event
_ Ctmobjloader. addeventlistener (event. Complete, oncompletehandle); // listen to the loading completion event
This. addchild (_ ctmobjloader); // Add the loaded container to the main program
}
_ Ctmobjloader. setstyle (''horizontalcenter'', 0 );
_ Ctmobjloader. setstyle (''verticalcenter'', 0 );
}
Private function onfailhandle (E: Event): void {
MX. Controls. Alert. Show (''error '');
}
Private function oncompletehandle (E: Event): void {
Loadedsm = systemmanager (_ ctmobjloader. content );
}
Private function ontestcall (E: Event): void {// echo subfile event
Alert. Show (''child event call '');
}
Private function mctestcall (): void {// call the subfile Function
If (loadedsm ){
Try {
Loadedsm.doc ument. testcall (''from parent call '');
} Catch (E: Error ){
Alert. Show (E. Message );
}
} Else {
Alert. Show (''Load file '');
Return;
}
}
Private function testaddchildlistener (): void {// sets a callback event for the subfile. If this parameter is not set, the subfile cannot be received.
File Event Response
If (! _ Ctmobjloader) {alert. Show (''Load file''); return ;}
VaR D: displayobject = _ ctmobjloader. content;
Try {
Loadedsm.doc ument. addeventlistener ("eventgotoparent", ontestcall );
Alert. Show (''set successfully '');
} Catch (E: Error) {alert. Show (E. Message )}
// _ Url = urltxt. text;
}
]>
</MX: SCRIPT>
<Mx: button label = "loading flex sub-Files" Click = "loadswf ();" x = "26" Y = "10"/>
<Mx: button label = "set listener for the loaded file" Click = "testaddchildlistener ();" x = "150" Y = "10"/>
<Mx: button label = "tested file function" Click = "mctestcall ();" x = "342" Y = "10"/>
</MX: Application>
3. Write the child. mxml sub-File
<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" width = "750" Height = "444"
Applicationcomplete = "initapp ()" stylename = "border3" fontsize = "12">
<Mx: SCRIPT>
<! [CDATA [
Import MX. Controls. Alert;
Private function initapp (): void {
}
Public Function testcall (MSG: string): void {
MX. Controls. Alert. Show (MSG );
}
Private function eventgotoparent (): void {
Dispatchevent (new event (''eventgotoparent ''));
}
]>
</MX: SCRIPT>
<Mx: button x = "327" Y = "148" label = "Notify parent of events" Click = "eventgotoparent ()"/>
</MX: Application>
Complete. All files can be directly copied and then compiled and run.