Resources /
Flash / Multi-pages and Events
Handling events in multi-pages Flash application
In this tutorial we will look at a Flash application that loads another swf file.
Here, the main Flash file is MainContainer, which is loading another SWF called MoveObject into it.
This is what they look like:
| Main Container |
|
MoveObject |
 |
|
 |
Loading external swf file
To load MoveObject, we put this code in MainContainer:
var loader:Loader = new Loader();
...
var urlReq:URLRequest = new URLRequest("MoveObject.swf");
loader.load(urlReq);
addChild(loader);
That will load MoveObject.swf into MainContainer and play it.

Dispatching an event to the loader
Now how do we close it after it finishes playing? We need to dispatch an event from MoveObject to tell MainContainer that it has finished.
The event type is a string and we can put anything in there. We'll use "pageFinish" here.
if (loaderInfo.loader) // Only if this is loaded from another swf
loaderInfo.loader.dispatchEvent(new Event("pageFinish", true));
Listening to the event
Now in the MainContainer, we need to listen to the event and do something when the loaded swf has finished playing (eg. close it).
The event type listened must be the same as the dispatched one (of course), in this case it is "pageFinish".
loader.addEventListener("pageFinish", OnPageFinish); // Listen for the "pageFinish" event
...
private function OnPageFinish(event: Event): void
{
// The page has finished playing, close it.
removeChild(loader);
}
Sample project
Download the sample code and SWFs here.