Trouble in loading external swf file and embedded font dynamically

I have developed a flex application doing something like this: it loads A.swf from somewhere and show it on the stage
sometimes A uses fonts not installed on users' machine, so at this time my app will load B.swf with needed fonts embedded in it from another place
I have working it out that fonts in B.swf can be detected and shown in my app stage, but when I use SWFLoader to show A.swf, it still fails in showing the fonts properly...
Some of my codes pasted here:
// load fonts
private function onFontBytesLoadComplete(e:Event):void {
var data:ByteArray = e.target.data as ByteArray;
swfLoader.addEventListener(Event.COMPLETE, onSWFLoadComplete);
swfLoader.source = data;
private function onSWFLoadComplete(e:Event):void {
loadSM = SystemManager(swfLoader.content);
loadSM.addEventListener(FlexEvent.APPLICATION_COMPLETE, onFontLoadComplete);
private function onFontLoadComplete(e:FlexEvent):void {
var clazz:Class = loadSM.loaderInfo.applicationDomain.getDefinition(fontName) as Class;
var fun:Function = clazz.getFont as Function;
var fontClazz:Class = fun();
var font:Font = new fontClazz() as Font;
trace(getQualifiedClassName(new fontClazz()));
Font.registerFont(fontClazz);
if(loadFinishedHandler != null) {
loadFinishedHandler(fontName);
Anyone has idea about this? 3x~

Hm, yes, I remember there being some challenges in this regards, with Modules loading fonts at runtime.
If you take a look at the section "Using run-time style sheets with modules," they describe the basic approach where everything has to be loaded and registered in the primary application domain.
I think it should work fine the way you have it, provided you are registering both the font and the loaded SWF in the primary application domain. If not, you may need to push the font registration down into the application domain being used by the loaded SWF.
Something like this perhaps:
childApplicatonDomain.getDefinition('flash.text.Font').registerFont(fontClass);
Where childApplicatonDomain is the application domain of the loaded SWF.

Similar Messages

  • Loading external SWF fila and accessing variables/functions

    Hello everybody!
    My first post here. Actually I do search google for answers all the time, but now - it's tough one.
    public function loadScreen(swf:File):void {
         _content = swf;
         var swfBytes:ByteArray = new ByteArray();
         var file:File = _content;  
         var loadStream:FileStream = new FileStream();
              loadStream.open( file, FileMode.READ );
              loadStream.readBytes( swfBytes );
              loadStream.close();
         var loader:Loader = new Loader();
         var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
              loaderContext.allowLoadBytesCodeExecution = true;
              loader.loadBytes( swfBytes, loaderContext);
              loader.contentLoaderInfo.addEventListener(Event.INIT, accessSWF);
              loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
    public function errorHandler(e:Event):void {
         trace("PROBLEM LOADING BYTES");
    public function accessSWF(e:Event):void {
         var moduleLoader:LoaderInfo = LoaderInfo(e.target);
         moduleLoader.content.addEventListener(Event.INIT, readyHandler);
    public function readyHandler(e:Event):void {
         _initOptions = new NativeWindowInitOptions();
         with(_initOptions) {
              resizable = true;
              transparent = false;
         var boundsWin:Rectangle = new Rectangle(100, 100, 423, 430);
         _window = new NativeWindow(_initOptions);
         with(_window) {
              activate();
              bounds = boundsWin;
              stage.scaleMode = "noScale";
    The problem is that application loads the external swf with loadBytes, but it's not executing the script. Actually, I'm not able to use the loaded swf. I'm not able even to add it to Stage. But message in output still shows, that the swf file is loaded "[SWF] file.swf/[[DYNAMIC]]/1 - 2746 bytes after decompression"
    There are many solutions for this problem on the internet, but they're old (written in 2008 most of all) and not working either, because those solutions work only in older AIR versions
    Any help?

    Well I got a bit further:
    public function loadScreen(swf:File):void {
         var loadStream:FileStream = new FileStream();
         with(loadStream) {
              openAsync( swf, FileMode.READ );
              addEventListener(ProgressEvent.PROGRESS, loadProgress);
              addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
              addEventListener(Event.COMPLETE, loadComplete);
    public function loadProgress(e:ProgressEvent):void {
         trace("Loading " + e.target.bytesAvailable);
    public function errorHandler(e:IOErrorEvent):void {
         trace("Error ");
    public function loadComplete(e:Event):void {
         var swfBytes:ByteArray = new ByteArray();
         FileStream(e.target).readBytes( swfBytes );
         var loaderContext:LoaderContext = new LoaderContext();
         with(loaderContext) {
              allowLoadBytesCodeExecution = true;
         var loader:Loader = new Loader();
         with(loader) {
              loadBytes( swfBytes, loaderContext);
              contentLoaderInfo.addEventListener(Event.INIT, completedSWF);
    public function completedSWF(e:Event):void {
         var moduleLoader:LoaderInfo = LoaderInfo(e.target);
         moduleLoader.addEventListener(Event.COMPLETE, initiatedFinally);
    public function initiatedFinally(e:Event):void {
         _initOptions = new NativeWindowInitOptions();
         with(_initOptions) {
              resizable = true;
              transparent = false;
         var boundsWin:Rectangle = new Rectangle(100, 100, 423, 430);
         _window = new NativeWindow(_initOptions);
         with(_window) {
              activate();
              bounds = boundsWin;
              stage.scaleMode = "noScale";
         var loader:Loader = e.target.loader;
         loader.addEventListener(Event.ADDED_TO_STAGE, irAdded);
         _window.stage.addChild(loader);
    public function irAdded(e:Event):void {
         trace(e.target.content.vars); // Object Dictionary
    At this point ( function irAdded ) I'm able to retrieve external SWF's variables. But stil, it's not visible on the stage. And yet - there's another problem - rendering the same script , every second time application unloads the external swf movie.

  • Trouble with loading external mp3 files

    I apologize for the length of the post. I hope this is clear
    of what I am trying to explain.
    My problem deals with getting "undefined" when I press a
    button to load an external mp3 files. The following is what my XML
    childNode looks like:
    <option phonetic="Soundless, or A" name="aleph"
    cursive="alephCursive.swf" block="alephBlock.swf"
    audio="char1.mp3"/>
    Every attribute loads the way I've programmed them, except
    for the last one "audio".
    The following is what my actionscript looks like loading in
    these attributes:
    Everything is happening inside the onLoad event for my XML
    file.
    var xmlContent = this.firstChild.firstChild;
    audioContent = new Array();
    for (var i = 0;
    i<this.firstChild.firstChild.childNodes.length; i++) {
    nextEntry = xmlContent.childNodes
    audioContent = [nextEntry.attributes.phonetic,
    nextEntry.attributes.name, nextEntry.attributes.cursive,
    extEntry.attributes.block, nextEntry.attributes.audio];
    The code above simply adds each attribute to the audioContent
    array. Each element traces out correctly.
    The following code is how I am using these attributes:
    for (var i = 0; i<audioLength; i++) {
    audioContainer.attachMovie("container4", "audioItem"+i, i);
    // Create a shortcut reference to the present movie clip
    thisClip = audioContainer["audioItem"+i];
    thisClip.myTxt1.htmlText = audioContent[0];
    thisClip.myTxt2.htmlText = audioContent
    [1];
    thisClip.cursive_mc =
    loadThis(assetPath+audioContent[2], thisClip.cursive_mc);
    thisClip.block_mc = loadThis(assetPath+audioContent
    [3], thisClip.block_mc);
    thisClip.speaker_mc.attachMovie("speaker", "speaker_mc",
    this.getNextHighestDepth());
    thisClip.speaker_mc.speaker_mc.onRollOver = function() {
    this.nextFrame();
    thisClip.speaker_mc.speaker_mc.onRollOut = function() {
    this.prevFrame();
    thisClip.speaker_mc.speaker_mc.onRelease = function() {
    mySnd.loadSound(audioPath+audioContent[4],true);
    Loading the text works inside the htmlText. Using a function
    loadThis to load external swf files works corerctly and even
    attaching a linked movieclip from my library called "speaker" works
    correctly.
    The problem comes from pressing the speaker movieclip that
    the output traces "undefined" instead of the external mp3 file.
    I appreciate any help. Thanks.

    As Ned said the problem might be with the file structure on the server. Other problems may be with spelling. You computer may not be case sensitive to the names of the .swf files that you are loading, but your server might. So, on your computer when you are testing, second.swf is the same as Second.swf, or even second.SWF. But, on the server each of these variations is seen as a different file. Another problem is that when you are loading files from the same computer in testing, those files are close and so they load very quickly from your computer. When the files have to travel from the server, there will likely be a measurable period of time before the file is ready to be shown. This is particularly problematic with video that is embedded into an .swf. The whole file will need to be downloaded and uncompressed before it is available to play. If you stream the video as an external file, you can shorten the wait considerably.

  • Loading external .swf files issue...

    Hi,
    I'm working on a simple example of loading external swf files with some ActionScript.
    I've placed an instance of List Component and gave it an instance name of loadList. Using Component Inspector, I assigned data for 4 external files as below:
    Next, I've added a UILoader component (instance name - loadWindow). The code that is supposed to load the content into UILoader is this:
    loadList.addEventListener(Event.CHANGE, selectItem);
    function selectItem(e:Event):void
        loadWindow.source = e.target.selectedItem.data;
    When I run it in a FlashPlayer, it only loads the first 2 swf files....  (I checked the files and all files are fine).
    I hope to get some light on the issue so any help will be appreciated.

    Try tracing e.target.selectedItem.data to make sure it's what you expect.
    Also, have you tried using the load() method instead of the source property?

  • Loading external swf files in a swf - Error#1009

    I have been having trouble using a swf file within a swf file.   The external swf files all work as expected.  However when used within another swf file I get the standard TypeError #1009 for some of my swfs.   The external swf files are mainly actionscript 3 files.  A typical swf file will have an object (or objects) in the library which has an export for ActionScipt property enabled and an addEventListener(Event.ENTERFRAME, somefunction).   About four years ago when I had to do some work with AS2 I seemed to get around a similar issue with the _lockroot method but this is no longer part of AS3. I have searched various forums and note that I am not the only one with this issue but did not find any relevant solutions.   If anyone could give me any ideas on how to get around this it would be appreciated.
    Regards
    Norman

    Hi,
    I have added a download to rapidshare and its link is:
    Download link: http://rapidshare.com/files/380202712/ExtSwfs.zip
    The main file Training01.fla has been set up with links to download several swf's.   The links that do not work properly are those labelled :  Module6 and Module 8.   The first Module 6 connects to a function that is a variation of how I was trying to load external swfs using Application Domain, while Module 8 is a link to a swf that uses and external class.  Module 8 is supposed to load with rotating stars, while Module 6 loads the same file as the link called Module 1.   As soon as I use the two links Module 6 or Module 8 the whole lot ceases to work properly.  I did also follow through the examples on the Adobe site.  The examples for Module 6 and Module 8 are tutorials I have used from the Internet as part of my training from the site FlashMyMind.com but they replicate the type of work we have been doing with respect to using ActionScript3.
    Re my background, at this stage I am self-taught using tutorials on the web and whatever books I can lay my hands on here.  There is no structured training available where I live as there is insufficient demand and no books available on Flash or ActionScript at the local book stores or libraries so the books are ones I have ordered via the Internet.  The one I am currently using apart from Adobe Classroom in a Book Flash CS4 is O'Reilly's Learning ActionScript 3.0 a Beginners Guide.
    The other problem I have been trying to solve and still researching is why one loses sound in the flash player when playing flv files, if the user has selected or restarted a video (or videos) many times eg 25 to 30 times as we create video training files and serve them to the user within the flash environment using Flash Player 9 and IE6.
    Regards
    Norman

  • Loading external swf file in a new window

    Hi,
    I've got a few .swf files with video tutorials I've created using Captivate 4.
    I wanted to link them so I've created a main screen with menu with buttons in Flash.
    I've added AS to load the external swf files but they load in the same window. How can I load these .swf files in seperate window so that you could get back to the main window after you've finished watching?
    Here's my code:
    stop();
    var myLoader:Loader = new Loader();
    button1_btn.addEventListener(MouseEvent.CLICK, movie1_1);
    function movie1_1(e:MouseEvent):void
        var myURL:URLRequest = new URLRequest("01_01_Welcome.swf");
        myLoader.load(myURL);
        addChild(myLoader);
    I'm also attaching screenshot with menu in flash

    I've deleted the line and now the code lookes like that:
    stop();
    var myLoader:Loader = new Loader();
    button1_btn.addEventListener(MouseEvent.CLICK, movie1_1);
    function movie1_1(e:MouseEvent):void
        var myURL:URLRequest = new URLRequest("01_01_Welcome.swf");
        addChild(myLoader);
    Unfortunately, it doesn't load the .swf file when you click on the button...

  • How to load External swf file in AIR

    I try this code its working in computer but when I publish this file in .apk or .ipa , my external swf file is not opening
    (I include my external file as well)
    places_btn.addEventListener(MouseEvent.MOUSE_DOWN, newvid);
    function newvid(event:MouseEvent) {
         trace("you clicked me");
         var loadit = new Loader();
         addChild(loadit);
         loadit.load(new URLRequest("scene02.swf"));

    thx boss its working great
    I have 3 scenes; load scene code is working great
    1. How can I unload the scene?
    2. Can I unload all scenes or one by one?
    can I unload some think like this
    Load scene 2 , unload scene 1 and scene 3
    Load scene 3, unload other all scene
    i try this
    load2.addEventListener(MouseEvent.MOUSE_DOWN, newvid);
    function newvid(event:MouseEvent) {
              trace("you clicked me");
              var loadit = new Loader();
              addChild(loadit);
        var _lc:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);
        loadit.load(new URLRequest("scene02.swf"), _lc);
              loadit.unload();
    load3.addEventListener(MouseEvent.MOUSE_DOWN, newvid1);
    function newvid1(event:MouseEvent) {
              trace("you clicked me");
              var loadit = new Loader();
              addChild(loadit);
        var _lc:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);
        loadit.load(new URLRequest("scene03.swf"), _lc);
              loadit.unload("scene01.swf scene02.swf");

  • Loading external swf file

    Hi all,
    I am trying to load an external SWF file into my website.. but it is not working. Here is my code
    public var CD:Loader=new Loader();
    protected function application1_creationCompleteHandler(event:FlexEvent):void
    CD.load(new URLRequest("http://localhost/assets/slide.swf"));
    addChild(this.CD);
    CD.x=2;
    CD.y=170; 
    // TODO Auto-generated method stub
    Can anyone tell me what is going wrong..
    The url is correct and its working in my browser when i type 'http://localhost/assets/slide.swf' in the browser... So problem is with flex..

    I am pretty sure that Flash's security measures prevents the coder from loading swfs that are not local to the swf that is trying to load them.  So the swf that is trying to load another swf would have to be on the same system or same networked server.  From what I have experienced its best to reference the location of the files from the swf that is doing the loading.  I am not sure if you are still referencing your swf as "http://localhost/assets/slide.swf" but that might be a problem as well.
    so
    mainfolder
    movie.swf
    assets
    slide.swf
    so in movie.swf
    //Start: Put in desired function
    var swfLoader:Loader = new Loader();
    swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
    swfLoader.load(new URLRequest("assets/slide.swf"));
    //End
    Also add this function
    public function loadComplete(e:Event):void
         stage.addChild(e.target.content);
    This might help explain it
    http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Loader.html
    the first set of bullet points highlight the security options
    ps. I got this working on my end, copy and pasted the code, of course i had to recreate the file system structure and slide.swf

  • Load external SWF file into another SWF

    Hello all.
    I've tried loading a .swf file externally into another .swf file on my  local drive, and it works. Now, when I try to Simulate Download, the  preloader in the 2nd file doesn't get played and the images in there  take a while to get displayed.
    I need to load a .swf file from a server into the parent .swf file(I'm  working on using a "thin flash file" for added security for the company I  work for - blank file which loads the content from another file on  their server).
    So far, I have:
    try
        var myLoader:Loader = new Loader();
        addChild(myLoader);
        var url:URLRequest = new URLRequest("newBook.swf");
        myLoader.load(url);
    catch (e:Error)
        trace("ruhroh");
    When I replace the "newBook.swf" with the URL of another .swf, there's just a blank page.
    I'm not too sure how to proceed, and I hope you will be able to help me out. I'd greatly appreciate the help.
    Many thanks,
    -Nazgul

    What happens is...when I set the speed to DSL(32.6kbps), the screen is blank and the preloader doesn't show up. It just goes directly to the Main screen. And then, the pages inside don't get loaded properly.
    This only happens when I try to Simulate Download.
    But that's not really the problem. I need to load a .swf file from a server into another .swf file, and just adding the link to the child .swf isn't working.
    Anyway, thanks for your help
    -Nazgul

  • Using a container to load several swf files and play them

    I need some help. I want to use several swf files and have them be called upon in a container file and play them in sequence. It's a presentation that needs to play thru but still have the ability to stop, click on items, open a popup and then continue on in the presentation. I am building all the individual "chapters" and their "sub-chapters" as swf files, with the hope that I can load them in order. I am relatively new to AS3. Help?

    If you will be loading swf files into a container then you will use the Loader class to accomplish that, so give that a looking over in the help documents and see what you can do.  If you have a problem getting it to work, post your code and describe what you have done.

  • How can I get a preloader to launch a main (external) swf file, and have the main file remove the preloader once it's fully loaded?

    Using Flash MX, ActionScript 2:
    I’ve been struggling for several weeks, trying to get a preloader to work on a large (~80 MB) photo portfolio file (using the method where the preloader is on frame 1 of the file, and when loading is completed, it jumps to the content).  I’ve done countless tutorials, and none seem to work on my presentation.  I even tried downloading a trial version of CS4 so I could export every one of the file’s library movie clips and images so that they load on Frame 2 (a feature MX doesn’t have).  It takes about 20 seconds for the .exe to load from a CD, and no matter which preloader construction I use, it always seems to appear in the last few seconds before the movie loads completely.  During the initial wait time, there’s no indication that anything is happening:  no hourglass on the mouse, and not even a blank screen. This is only true for the first time that the file is loaded from the CD; on subsequent loads, the file must already be in RAM, because it loads within a few seconds. 
    I decided that I’m going to try and make the file which the user clicks on (one named Windows_Portfolio.exe or another called Mac_Portfolio.hqx) the preloader so that it would be tiny, and would load instantly.  I want that file to launch the external huge portfolio file (renamed files.swf), keep track of its loading progress, and cycle through a slideshow (10 thumbnail images that transition into each other over 100 frames) proportionally, based upon the percentage of files.swf that had been loaded.
    I assume that there should be a loadMovie() or a loadMovieNum() command on the preloader’s timeline to launch files.swf, and that the initial code of files.swf should have some sort of this._parent._visible=false or other way of deleting the preloader on level0.  Can anyone explain the steps to me, or direct me to a good Flash MX tutorial that explains how to launch another external swf from a preloader .exe, keep track of its load progress, and delete the preloader .exe once the external swf had been loaded?
    Also, I’ve read that, using this construction on a CD, every file has to be in the same place, and that I can’t nest files.swf in a folder named “files,” and reference it with files/files.swf.  Is this true?
    Thanks for any assistance you can offer.

    If you know JavaScript (ECMA Script) then you might be able to get it done applying a for loop and a random function
    to the for loop if you know how to get a page-loaded request into JavaScript.
    But an easier solution, if you know ActionScript 3.0, and if your SWF's don't need to be recoded and can load into a "container" SWF would be to create a master FLA file that loads your SWF's in a random fashion using Math.random() as a multiplier, though you won't see much variation if you only have 2-3 SWF's to load--if you had more than that then you would begin to benefit from the random function's capabilities.  In ActionScript 3.0 use the load() method along with addChild() to load the SWF and add it to the stage.  You will be able to search for specifics on how to code the FLA properly by using Flash's Adobe Help tool and also using an internet search.
    -markerline
    P.S. Once you have the master FLA coded you can Publish it to an SWF and an HTML and if the container/master SWF needs to be in a page with other content you can simply copy and paste the object and embed tags for the container SWF from the published HTML into any place on your master HTML that you need it to reside.  You can then use CSS to control placement of the SWF object if it is in a div tag.

  • Is it possible to Load External SWF files into Flex Mobile projects?

    Hi Guys,
    I'm trying to load an external asset in this cse (a SWF file) into my flex project (Apple IOS IPAD), don't get any luck. Can any one suggest a solution ?

    In Apple's words, "No interpreted code may be downloaded or used in an Application except for code that is interpreted and run by Apple's Documented APIs and built-in interpreter(s)".
    Hence external swfs cannot be loaded in iOS.

  • Loading External SWF Files: Making Things Simple...

    Hello everyone! This is my first post and I'm learning AS3 =)
    I didn't know about this forum but now I hope to have the time to come hear a lot, to help and be helped!
    Well, I'm trying to import some external files to my main flash file with buttons. Yes, this is a newbie question, but I'm learning... I click button 1 and content 1 is loaded, click button 2 and content 2 is loaded, and so on. So, I got two ways for doing it:
    **various buttons with listeners to run the function "loadCity"
    var box:MovieClip = new MovieClip();
    addChild(box);
    box.x=20;
    box.y=20;
    function loadCity (Event:MouseEvent):void {
    var currentCity=Event.target.name;
    var swfRequest:URLRequest=new URLRequest(currentCity+".swf");
    var swfLoader:Loader = new Loader();
    swfLoader.load(swfRequest);
    box.addChild(swfLoader); }
    The problem with this first option is that the content to the box is not renew with each click but it stacks one over each other, so I can't use it =/
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    var box:MovieClip = new MovieClip();
    addChild(box);
    box.x=20;
    box.y=20;
    var cityLoader:Loader = new Loader();
    var cityURL:URLRequest=new URLRequest("montreal.swf");
    cityLoader.load(cityURL);
    box.addChild(cityLoader);
    **various buttons with listeners to run it's own functions, like "montreal.addEventListener(MouseEvent.CLICK, loadMontreal);"
    var box:MovieClip = new MovieClip();
    addChild(box);
    box.x=20;
    box.y=20;
    function loadCity (Event:MouseEvent):void {
    var currentCity=Event.target.name;
    var swfRequest:URLRequest=new URLRequest(currentCity+".swf");
    var swfLoader:Loader = new Loader();
    swfLoader.load(swfRequest);
    box.addChild(swfLoader); }
    The problem with this first option is that the content to the box is not renew with each click but it stacks one over each other, so I can't use it =/

    Thanks Andrei!
    The "while statement" did the trick! =D
    Just one little problem. This first thread was opened involuntarily and I don't know how to delete it. The other topic, above this one, is more complete and has more details in what I'm trying to do. I still want to use tweens. =/ Could you check my other topic?
    Well, thanks again Andrei and I'm leaving the finished code here, just in case anyone else needs to do the same thing. =)
    montreal.addEventListener(MouseEvent.MOUSE_UP, loadCity);
    dublin.addEventListener(MouseEvent.MOUSE_UP, loadCity);
    sydney.addEventListener(MouseEvent.MOUSE_UP, loadCity);
    var box:MovieClip = new MovieClip();
    addChild(box);
    box.x=20;
    box.y=20;
    function loadCity (event:MouseEvent):void {
        var currentCity = event.target.name;
        // remove all the children of the box
        while (box.numChildren > 0) {box.removeChildAt(0);}
        var swfRequest:URLRequest=new URLRequest(currentCity+".swf");
        var swfLoader:Loader = new Loader();
        swfLoader.load(swfRequest);
        box.addChild(swfLoader);}

  • Loading External SWF Files: Making Things Simple... Still need help here =)

    Hello everyone! First of all, sorry for my english... I'm Brazilian and english is not my first language.
    This is my first post meant to be my first post and I'm learning AS3. Didn't know about this forum but now I hope to have the time to visit it a lot to help and be helped! =)
    Well, I'm trying to import some external files to my main flash file with buttons. Yes, this is a newbie question, but I'm learning... I click button 1 and content 1 is loaded, click button 2 and content 2 is loaded, and so on. So, I got two ways for doing it:
    edit: The code below is working fine now...
    montreal.addEventListener(MouseEvent.MOUSE_UP, loadCity);
    dublin.addEventListener(MouseEvent.MOUSE_UP, loadCity);
    sydney.addEventListener(MouseEvent.MOUSE_UP, loadCity);
    var box:MovieClip = new MovieClip();
    addChild(box);
    box.x=20;
    box.y=20;
    function loadCity (event:MouseEvent):void {
        var currentCity = event.target.name;
        // remove all the children of the box
        while (box.numChildren > 0) {box.removeChildAt(0);}
        var swfRequest:URLRequest=new URLRequest(currentCity+".swf");
        var swfLoader:Loader = new Loader();
        swfLoader.load(swfRequest);
        box.addChild(swfLoader);}
    The problem with this first option is that whenever I click a button, it don't renew the content of the "box"... instead, the content loaded is stacked over the last one and I don't know how to clean it... The user Andrei did the trick with the "while statement" so the above code is working but I don't know how to use tween with it...
    So here comes the second option:
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    montreal.buttonMode=true;
    dublin.buttonMode=true;
    sydney.buttonMode=true;
    rio.buttonMode=true;
    paris.buttonMode=true;
    london.buttonMode=true;
    home.buttonMode=true;
    var box:MovieClip = new MovieClip();
    addChild(box);
    box.x=20;
    box.y=20;
    var cityLoader:Loader = new Loader();
    var cityURL:URLRequest=new URLRequest("montreal.swf"); //I use this to show a city when the site is opened
    cityLoader.load(cityURL);
    box.addChild(cityLoader);
    **Here I add the listeners to button, each one calling its own function like:
    montreal.addEventListener(MouseEvent.CLICK, loadMontreal);
    function cityTweens():void {
        var cityIn:Tween = new Tween(box, "y", Strong.easeOut, -350, 20, 1, true);}
    function loadMontreal(event:MouseEvent):void {
        var cityURL:URLRequest=new URLRequest("montreal.swf");
        cityLoader.load(cityURL);
        cityTweens();}
    function loadDublin(event:MouseEvent):void {
        var cityURL:URLRequest=new URLRequest("dublin.swf");
        cityLoader.load(cityURL);
        cityTweens();}
    function loadSydney(event:MouseEvent):void {
        var cityURL:URLRequest=new URLRequest("sydney.swf");
        cityLoader.load(cityURL);
        cityTweens();}
    function loadRio(event:MouseEvent):void {
        var cityURL:URLRequest=new URLRequest("rio.swf");
        cityLoader.load(cityURL);
        cityTweens();}
    function loadParis(event:MouseEvent):void {
        var cityURL:URLRequest=new URLRequest("paris.swf");
        cityLoader.load(cityURL);
        cityTweens();}
    function loadLondon(event:MouseEvent):void {
        var cityURL:URLRequest=new URLRequest("london.swf");
        cityLoader.load(cityURL);
        cityTweens();}
    function loadHome(event:MouseEvent):void {
        var cityURL:URLRequest=new URLRequest("home.swf");
        cityLoader.load(cityURL);
        cityTweens();}
    Well, the second option "is" working but I have some problems with the Tween... I have to wait the tween to finish before clicking another button or the tween will bug... I don't know how to disable the buttons until the tween is finished =/
    Now, I used to have one tween for each function but now I got it inside a function and just call it inside each function. Simple for you but I was like WOW when I had the idea and put it to practice! But I'm still repeating a lot of codes here with the "cityLoader.load(cityURL);" but when I try to put it inside the function "cityTweens" it will just open the Montreal City, maybe because I'm calling it as soon as the site opens...
    I'm almost sure I can make things simple, like mixing the idea of the first code (currentCity+".swf") so I don't need to call a function for each button. I just don't know how to do it...
    Could anyone help me? I'll also be VERY happy if you point me to any tip like good practices that I'm not following.
    Thanks in advance!
    Message was edited by: newToAS3

    As I said in your previous post, removing the child would not do the trick. Technically, you need to remove it after the tween is finished.
    In any case there are several issues with how you approach the whole thing. First of all, if you target your application to be on Internet - swf loading will not happen instantly - thus you, perhaps, need to consider waiting for swfs to be loaded before you doing anything with them. But if you just do it on local machine - you don't really need for them to show up. On the other hand, even with local environment it makes sense to take into account asynchronous/unpredicatable nature of external file loading.
    Also, your code can be more comact and the same functionality delegated to a single function. The code below demonstrates one of the ways. This is not the bes way though because it forces to load objects again although they were loaded already. But this is another story. I did not check code in action, of course (this is just an idea) - it is up to your to work out bugs:
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    import fl.transitions.TweenEvent;
    import flash.display.DisplayObject;
    import flash.display.Loader;
    import flash.events.Event;
    montreal.buttonMode = true;
    dublin.buttonMode = true;
    sydney.buttonMode = true;
    rio.buttonMode = true;
    paris.buttonMode = true;
    london.buttonMode = true;
    home.buttonMode = true;
    var box:MovieClip = new MovieClip();
    addChild(box);
    box.x=20;
    box.y=20;
    var cityLoader:Loader = new Loader();
    // this line will allow to wait untill content is loaded
    cityLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
    var cityURL:URLRequest = new URLRequest("montreal.swf"); //I use this to show a city when the site is opened
    cityLoader.load(cityURL);
    // don't need the following line
    // box.addChild(cityLoader);
    // make it a single listener
    montreal.addEventListener(MouseEvent.CLICK, loadSWF);
    dublin.addEventListener(MouseEvent.CLICK, loadSWF);
    sydney.addEventListener(MouseEvent.CLICK, loadSWF);
    rio.addEventListener(MouseEvent.CLICK, loadSWF);
    paris.addEventListener(MouseEvent.CLICK, loadSWF);
    london.addEventListener(MouseEvent.CLICK, loadSWF);
    home.addEventListener(MouseEvent.CLICK, loadSWF);
    // the single function handles all the loading
    function loadSWF(e:MouseEvent):void {
         switch(e.currentTarget) {
              case montreal:
                  cityURL = new URLRequest("montreal.swf");
              break;
              case dublin:
                  cityURL = new URLRequest("dublin.swf");
              break;
              case sydney:
                  cityURL = new URLRequest("sydney.swf");
              break;
              case rio:
                  cityURL = new URLRequest("rio.swf");
              break;
              case paris:
                  cityURL = new URLRequest("paris.swf");
              break;
              case london:
                  cityURL = new URLRequest("london.swf");
              break;
              case home:
                  cityURL = new URLRequest("home.swf");
              break;
         cityLoader.load(cityURL);
    function onLoadComplete(e:Event):void {
         loadedContent = e.target.content;
         // assuming that previous content will be moved out -
         // place new content behind previous one
         box.addChildAt(e.target.content, 0);
         // now you can start tweening
         // if there are more than one child
         if (box.numChildren > 1) {
              cityTweens();
    function cityTweens():void {
         // tween the topmost object
          var cityIn:Tween = new Tween(box.getChildAt(box.numChildren - 1), "y", Strong.easeOut, -350, 20, 1, true);
         // wait until motion finished
         cityIn.addEventListener(TweenEvent.MOTION_FINISH, tweenFinished);
    function tweenFinished(e:TweenEvent):void {
         // make Tween instance eligible for arbage collection
         e.target.removeEventListener(TweenEvent.MOTION_FINISH, tweenFinished);
         // now you can remove tweened object from the box
         box.removeChild(e.target.obj);
         e.target.obj = null;
    Edited some code.

  • AS3 loading external swf files with buttons from inside a movieclip

    In my main .swf I have labels on the timeline, navs for those btns are on the first frame of the AS layer and each button sends the playhead to a different frame.
    On one frame called fr1 there is an mc called mc_1 that appears on stage when the playhead stops there. Inside mc_1 are a set of navigation buttons that need to call external .swfs.
    Do I add event listeners on the last frame of mc_1 for each button?
    how do I call the loaders for the swfs on the buttons since the swfs will load on the main timeline NOT the mc_1 timeline?
    So label on main timeline called fr1 will load external1 swf , on fr10 external 2.swf will load and so on.
    any help?

    The code for that button's event listener needs to be placed wherever it has direct access to the button.  Not necessarily on the same timeline, but in a place where both the button and the code are present at the same time.
    The code could be placed at the frame in the main timeline where the movieclip with the button(s) is, targeting the buttons via the movieclip (mc.button.addEvemtListener...) , unless the buttons are somewhere down the timeline of that movieclip.
    If they are down the timeline of that movieclip, then you would need to have the event listener also inside the movieclip, down its timeline where the buttons are finally settled in... being sure to assign instance names to the buttons at every keyframe.

Maybe you are looking for