ActionScript to Load External SWF

ActionScript to Load External SWF
Let's add the ActionScript that will load the external ecard swf into our FlashR intro swf. We already have an Actions layer at the top of the Timeline. We created this Actions layer when we built the "Click on Stamp" button on Frame 80.

1. Right-click on Frame 136 and Insert a Keyframe. This will tell Flash to read our ActionScript on this frame. Open the Actions panel (Window – Actions) and copy/paste the following code.

var ecard:String = LoaderInfo(this.root.loaderInfo).parameters.ecard;

var myrequest:URLRequest = new URLRequest(ecard);
var myloader:Loader = new Loader();
myloader.load(myrequest);

function swfLoad(myevent:Event):void
{
stage.addChild(myloader);
var myswf:MovieClip = myevent.target.content;
myswf.x = 0;
myswf.y = 0;
}

myloader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoad);

  1. The first line of code creates a string variable to hold the name of the external swf. Notice that we have given the name of "ecard" to the parameter. We will be using this again later.

  2. Next, we create a variable (named myrequest) and a variable (named myloader) for the URLRequest.

  3. Then we write the function (swfLoad) that will load our external swf. This function uses the addChild to add the external swf to the display list for our intro swf and sets the X and Y positions to 0.0.

  4. Finally, we use an EventListenser to tell us when the external swf has been completely loaded. When this is so, we call the swfLoad function.

    Remember that we published our intro fla to tell Flash to generate the swf and HTML files for this project? Now, we will add a little code to the HTML file. This will tell Flash the name of the external file we wish to load into the intro swf. In the example, I decided to call the external swf "ecard.swf" and the name of my intro swf is "dCipolloDesigns_Ecard18.swf". You may wish to use different names. If so, make the appropriate changes to the following HTML code.

  5. Open your HTML file into a text editor.

  6. Look for the AC_FL_RunContent JavaScript. It should be about line 300 or so.

  7. Now, we will add the name of the external swf (ecard.swf) to this script. We will add it to the scr and movie parameters in the JavaScript. In the example, the name of my intro swf is "dCipolloDesigns_Ecard18.swf". Therefore, I will add the "ecard=ecard.swf" to the end of this. Remember that I said we would see the parameter "ecard" again from above. Notice that we have removed the ".swf" extension from the end of the intro swf (dCipolloDesigns_Ecard18). We must do this for the JavaScript.

    dCipolloDesigns_Ecard18?ecard=ecard.swf

    We need to add this to the src and movie parameters for this script.

  8. Next we will work on the movie param and the object tag. For the movie parameter, we will add the same code. Notice that this time we do not remove the ".swf" at the end of the name for our intro swf.

    dCipolloDesigns_Ecard18.swf?ecard=ecard.swf

  9. The last place that we need to add this code is for the embed tag and the src parameter. Again we keep the ".swf".

    dCipolloDesigns_Ecard18.swf?ecard=ecard.swf

  10. The last thing we need to do is add a stop(); code on Frame 150. Right-click on Frame 150 and Insert a Keyframe. Open the Actions panel and type "stop();". Now you can test your movie.

    Note. If you test your project within the Flash work environment, you will get an error. So we need to test our finished project by using the HTML file.

← Back

Copyright 2018 Adobe Systems Incorporated. All rights reserved. Adobe product screen shot(s) reprinted with permission from Adobe Systems Incorporated. Adobe, Photoshop, Photoshop Album, Photoshop Elements, Illustrator, InDesign, GoLive, Acrobat, Cue, Premiere Pro, Premiere Elements, Bridge, After Effects, InCopy, Dreamweaver, Flash, ActionScript, Fireworks, Contribute, Captivate, Flash Catalyst and Flash Paper is/are either [a] registered trademark[s] or a trademark[s] of Adobe Systems Incorporated in the United States and/or other countries.





RSS
Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map








Content copyright © 2023 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact Diane Cipollo for details.