Start the Flash program. Click "New" from the File menu on the main Flash menu bar. Click the file type "Flash File(ActionScript 3.0)" from the New Document dialog box. Click the "OK" button to close the dialog box.
Select the "Actions" option from the menu on the main Flash menu bar to open the Actions editor. Position your mouse cursor on the first line of the Actions editor. Click your mouse button. Type the following code to assign two string variables with the URL of the two Flash movie files(swf files) that you want to load and unload:
var movieAddress1:String = new String("http://www.shareswf.com/media/games/swf/11003.swf");
var movieAddress2:String = new String("http://www.shareswf.com/media/games/swf/27.swf");
Type the code below, starting at the next line in the Actions editor, to create two URLRequest objects that will be sent with a Loader object, named "movieLoader," to retrieve the swf files from the requested website URL and file path:
var movieLoad1:URLRequest = new URLRequest(movieAddress1);
var movieLoad2:URLRequest = new URLRequest(movieAddress2);
var movieLoader:Loader = new Loader();
Type the code below (beginning at the next line in the Actions editor) to create and place two different colored buttons -- red and green -- on the Flash stage that will be used to load one swf and unload the other swf.
var myButton:MovieClip = new MovieClip();
myButton.graphics.lineStyle(4);
myButton.graphics.beginFill(0xFF0000);
myButton.graphics.drawRect(5, 200, 50, 20);
myButton.graphics.endFill();
addChild(myButton);
var myButton1:MovieClip = new MovieClip();
myButton1.graphics.lineStyle(4);
myButton1.graphics.beginFill(0x00FF00);
myButton1.graphics.drawRect(5, 250, 50, 20);
myButton1.graphics.endFill();
addChild(myButton1);
Type the following code on the next line in the Actions editor to add event listeners to the two buttons so that when "myButton" is clicked, the swf file located at URL "http://www.shareswf.com/media/games/swf/11003.swf" will be loaded, and the swf file located at URL "http://www.shareswf.com/media/games/swf/27.swf" will be unloaded. Also, when the "myButton1" button is clicked, the swf file located at URL "http://www.shareswf.com/media/games/swf/27.swf" will be loaded, and the swf file located at URL "http://www.shareswf.com/media/games/swf/11003.swf" will be unloaded.
myButton.addEventListener(MouseEvent.CLICK, button_fn);
function button_fn(e:MouseEvent): void {
movieLoader.load(movieLoad1);
movieLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, movieLoaded_fn);
function movieLoaded_fn(event:Event):void
{
addChild(movieLoader);
movieLoader.x =100;
movieLoader.y = 0;
};
};
myButton1.addEventListener(MouseEvent.CLICK, button1_fn);
function button1_fn(e:MouseEvent): void {
movieLoader.load(movieLoad2);
movieLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, movieLoaded_fn);
function movieLoaded_fn(event:Event):void
{
addChild(movieLoader);
movieLoader.x =100;
movieLoader.y = 0;
};
};
Copy and paste the following code to ensure that your load and unload movie program is syntax-free and runs correctly.
var movieAddress1:String = new String("http://www.shareswf.com/media/games/swf/11003.swf");
var movieAddress2:String = new String("http://www.shareswf.com/media/games/swf/27.swf");
var movieLoad1:URLRequest = new URLRequest(movieAddress1);
var movieLoad2:URLRequest = new URLRequest(movieAddress2);
var movieLoader:Loader = new Loader();
var myButton:MovieClip = new MovieClip();
myButton.graphics.lineStyle(4);
myButton.graphics.beginFill(0xFF0000);
myButton.graphics.drawRect(5, 200, 50, 20);
myButton.graphics.endFill();
addChild(myButton);
var myButton1:MovieClip = new MovieClip();
myButton1.graphics.lineStyle(4);
myButton1.graphics.beginFill(0x00FF00);
myButton1.graphics.drawRect(5, 250, 50, 20);
myButton1.graphics.endFill();
addChild(myButton1);
myButton.addEventListener(MouseEvent.CLICK, button_fn);
function button_fn(e:MouseEvent): void {
movieLoader.load(movieLoad1);
movieLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, movieLoaded_fn);
function movieLoaded_fn(event:Event):void
{
addChild(movieLoader);
movieLoader.x =100;
movieLoader.y = 0;
};
};
myButton1.addEventListener(MouseEvent.CLICK, button1_fn);
function button1_fn(e:MouseEvent): void {
movieLoader.load(movieLoad2);
movieLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, movieLoaded_fn);
function movieLoaded_fn(event:Event):void
{
addChild(movieLoader);
movieLoader.x =100;
movieLoader.y = 0;
};
};
Click the "TestMovie" option in the Control menu on the main Flash menu bar. One green button and one red button appear on the Flash stage. Click the "red button," and observe that a swf file plays. Click the "green button" to play a different swf file, and the old swf movie is not on the screen (it has been unloaded from the flash stage.) Repeat the process to see the last movie again and unload the current swf.