Loading .swf, addChild, and removeChild

Hello all,
I'm trying load an swf when you click a button, and load another in it's place when you click a different button.   I'm using addChild to insert the swf onto the stage.  Problem is that every time I click a new button, the new swf is loaded on top of the previous one, and I need the previous one to disappear.  I tried using the removeChild method before the new swf is loaded to no effect.
Here's the function I'm using for the loader with my removeChild attempt in bold:
function newPage():void {
    trace("load activated");
    trace(""+target+".swf");
    var req:URLRequest=new URLRequest(""+target+""+".swf");
    var loader:Loader = new Loader();
   if (loaderState==true){
        top_mc.removeChild(loader);
    loader.load(req);
    loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, preLoad);
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fileLoaded);
    function preLoad(event:ProgressEvent):void {
        var percent:Number=Math.round(event.bytesLoaded/event.bytesTotal*100);
        top_mc.preload_txt.text=String(""+percent+"");
    function fileLoaded(event:Event):void {
        trace("file loaded");
        top_mc.addChild(loader);
        loaderState=true;
The error I get is as follows:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display::DisplayObjectContainer/removeChild()
    at main_fla::MainTimeline/newPage()
    at main_fla::MainTimeline/workLoaderEnter()
    at main_fla::MainTimeline/clickF()
Perhaps there is a way to give the addChild an instance name so that I can remove it later?
Any ideas?
Thanks!

Part of the problem is your trying to remove the loader just after you define it...  it is not a child of anything at that point.  Try the following instead of what you have.   Another problem you can avoid is to not nest functions...
var loader:Loader
var loaderState:Boolean = false;
function newPage():void {
if (loaderState){
        top_mc.removeChild(loader);
    var req:URLRequest=new URLRequest(""+target+""+".swf");
    loader = new Loader();
    loader.load(req);
    loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, preLoad);
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fileLoaded);
function preLoad(event:ProgressEvent):void {
    var percent:Number=Math.round(event.bytesLoaded/event.bytesTotal*100);
    top_mc.preload_txt.text=String(""+percent+"");
function fileLoaded(event:Event):void {
    top_mc.addChild(loader);
    loaderState=true;

Similar Messages

  • AddChild and removeChild with MouseEvent

    Hello, I am a newby at AS3. I need help with addChild and removeChild.
    I want to make a two buttons one addChild and other removeChild . Friend helped me a little bit, but I didnt uderstand the script ;]
    Here is the example script which my friend gave to me. In other words I want to make a button that attached hair and the other that removes.
    Should I make a separete custom class for Hair? And if do, can someone help? :]
    ActionScript Code:
    public function changeHairs (newHair: Hair): Hair
    Face.removeChild (oldHairs);
    oldHairs = newHair;
    Face.addChild (newHair);
    newHair return;
    Or modify this code. I know how to change text, but dont know how to change objects
    ActionScript Code:
    package  {
        import flash.display.MovieClip;
        import Button;
        import flash.text.TextField;
        import flash.events.MouseEvent;
        public class Faces2 extends MovieClip
            public var arr:Array = new Array("a1","a2","a3");
            trace("aab");
            public var txt:TextField = new TextField()
            public var btnBack:Button = new Button();
            public var btnFwd:Button = new Button();
            public var arrPos:Number = 0;
            public function Faces2()
                setupInterface();
                setupData(arrPos);
            public function setupInterface():void
                btnBack.x = 100; btnBack.y = 10;
                btnFwd.x = 200; btnFwd.y = 10;
                btnBack.addEventListener(MouseEvent.CLICK, back);
                btnFwd.addEventListener(MouseEvent.CLICK, fwd);
                addChild(btnBack);
                addChild(btnFwd);
                txt.textColor = 0xFF0000;
                txt.width = 300;
                txt.border = true;
                txt.x = 100; txt.y = 100;
                addChild(txt)
            public function fwd(evt:MouseEvent):void
                arrPos ++;
                if(arrPos >= arr.length)
                    arrPos = 0;
                setupData(arrPos);
            public function back(evt:MouseEvent):void
                arrPos --;
                if(arrPos < 0)
                    arrPos = arr.length - 1;
                setupData(arrPos);
            public function setupData(pos:Number):void
                txt.text = arr[pos];
    P.S. sorry for my bad english

    did you create your hair movieclip and assign it a class?

  • AddChild and removeChild (AS 3)

    Hi
    If I load an external swf (located in the same dir as the one
    loading it) with addChild. It looks something like this
    var url:String = "patfinder_extra1.swf";
    var urlReq:URLRequest = new URLRequest(url);
    ldr.load(urlReq);
    addChild(ldr);
    Now, I want to be able to close/remove this new child within
    itself, image a little close button with the label "I'm a child and
    will close my self if i click here!".
    Now, this removeChild is a mystery and the new restrictions
    in paths in AS 3 really makes it hard. To simply use
    this.removeChild() returns errors.
    Any tips of how to close this thingy?
    :)

    you can use the code below to have the child movieclip remove
    itself from its parent's display list, but that won't clear it for
    garbage collection.
    i would have the child call a function in the parent that
    removes it and nulls all references to its loader so it can be
    garage collected.

  • Action script 3 question: addChild and removeChild on same button??

    I'm trying to make a button that when u click once, the movieclip will be removed and second time back on stage and so forth....
    somehow i get this error
    ==================================
    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display:DisplayObjectContainer/removeChild()
    at sample_fla::MainTimeline/btnClick()
    ==================================
    Anyone can help me with this, since I'm doing an interactive web for client and the deadline is near..... thanks in advance.
    these are my code:
    stop();
    var swf:MovieClip;
    var loader:Loader= new Loader();
    var defaultswf:URLRequest= new URLRequest("./newSwfImport.swf");
    //default swf when loaded
    loader.load(defaultswf);
    //add to stage
    allSwfLoadHere.addChild(loader);
    var boolean:Boolean = true ;
    function btnClick(event:MouseEvent):void {
    if(boolean == true){
    allSwfLoadHere.removeChild(loader);
    boolean == false
    if(boolean ==false){
    allSwfLoadHere.addChild(loader);
    boolean == true
    newSwfImport.addEventListener(MouseEvent.CLICK,btnClick);

    you have, at least, two errors:
    1.  you failed to use an if-else and
    2.  when trying to re-assign your boolean, you tested.  ie, you used == when you should use =.  so, your boolean remains true.  then when you execute the 2nd time, you try and remove something that's already been removed.
    use:
    var swf:MovieClip;
    var loader:Loader= new Loader();
    var defaultswf:URLRequest= new URLRequest("./newSwfImport.swf");
    //default swf when loaded
    loader.load(defaultswf);
    //add to stage
    allSwfLoadHere.addChild(loader);
    var boolean:Boolean = true ;
    function btnClick(event:MouseEvent):void {
    if(boolean){
    allSwfLoadHere.removeChild(loader);
    } else {
    allSwfLoadHere.addChild(loader);
    boolean=!boolean;
    newSwfImport.addEventListener(MouseEvent.CLICK,btnClick);

  • Explanation of addChild and removeChild?

    Hi
    I'm having trouble getting my head around using removeChild.
    For example, I have a function that is triggered by a button click.  This function loads an external swf.  I then set a global variable swfLoaded to true, with the idea of having an if condition that executes removeChild(loader) if swfLoaded is true.
    When trying to execute removeChild(loader), I get an error telling me that it must be the child of the caller.
    I've seen the code below, which loads a default swf, but I don't want a default swf - I just want one loaded if the button is clicked, and then if another button is clicked, the original one to be removed before the next swf is loaded.
    Can someone please explain to me why removeChild(loader) only works if addChild(loader) was originally executed outside of the function (like below)?
    How do I make this code work without loading a default swf, just loading one once someone clicks a button, and then removing it?
    var Xpos:Number = 110;
    var Ypos:Number = 180;
    var swf:MovieClip;
    var loader:Loader = new Loader();
    var defaultSWF:URLRequest = new URLRequest("swfs/eyesClosed.swf");
    loader.load(defaultSWF);
    loader.x = Xpos;
    loader.y = Ypos;
    addChild(loader);
    // Btns Universal function
    function btnClick(event:MouseEvent):void {
    removeChild(loader);
    var newSWFRequest:URLRequest = new URLRequest("swfs/" + event.target.name + ".swf");
    loader.load(newSWFRequest);
    loader.x = Xpos;
    loader.y = Ypos;
    addChild(loader);
    // Btn listeners
    eyesClosed.addEventListener(MouseEvent.CLICK, btnClick);
    stingray.addEventListener(MouseEvent.CLICK, btnClick);
    demon.addEventListener(MouseEvent.CLICK, btnClick);
    strongman.addEventListener(MouseEvent.CLICK, btnClick);
    Cheers
    Shaun

    Oh, I just did not pay enough attention to your code.
    The way you wrote it - you don' have to remove it every time. A better approach is to add it only once and in subsequent calls to unload it:
    var loader:Loader = new Loader();
    // add it once
    addChild(loader);
    function btnClick(event:MouseEvent):void {
         // don't need this
         //removeChild(loader);
         // unload previously loaded content
         loader.unload();
         var newSWFRequest:URLRequest = new URLRequest("swfs/" + event.target.name + ".swf");
         loader.load(newSWFRequest);
         loader.x = Xpos;
         loader.y = Ypos;
         // don't need this either
         // addChild(loader);
    Also, in Flash 10 it is better to use loader.unloadAndStop()

  • Setting External Loaded SWF dimension

    hi guys...,
    i'll be straight to point,well i'm now working on a project using action  script 3 now what i'm trying to make is a Main SWF that load whatever  other swf into it the tricky thing is that i used 1 xml document read  the external swf source and it's setting(such as it's x,y position and  it's width and height) and i'm having problem setting the loaded swf width and  height btw it's an desktop application and not a website application.,
    here is my code:
    //variable list
    var swfList:XMLList; //hold all the zone list from the xml
    var totalZone:uint; //total of zone there is in the xml
    var myURLLoader:URLLoader = new URLLoader();
    var swf:Movie Clip;//hold the loaded swf
    var swfLoader:Loader = new Loader();//loader instance used to load the external swf
    var myCounter:uint = 0;
    //load the xml file
    myURLLoader.load(new URLRequest('myXMLFile.xml'));
    myURLLoader.addEventListener(Event.COMPLETE, processXML, false, 0, true);
    function processXML(e:Event):void
        removeEventListener(Event.COMPLETE, processXML);
        XML.ignoreWhitespace= true;
        var myXML:XML = new XML(e.target.data);
        swfList = myXML.SWF;
        totalSWF = myXML.SWF.length();
        loadSWF();
    function loadSWF():void
        swfLoader.contentLoaderInfo.addEventListener(Event.INIT, swfSetting);
        swfLoader.load(new URLRequest(swfList[myCounter].@source));
    function swfSetting(e:Event):void
        //making new instance of sprite to hold the new loaded swf
        swf = new MovieClip();
        //casting the loader content into a movieclip
        swf = e.target.content;
        addChild(swf);
        swfLoader.unload();
        swf.x = swfList[myCounter].@left;
        swf.y = swfList[myCounter].@top;
        swf.width= swfList[myCounter].@width;
        swf.height= swfList[myCounter].@height;
        addChild(swf);
        if(myCounter < totalSWF)
            myCounter++;
            trace('myCounter: ' + myCounter );
            loadSWF();
    and here is what the result ( it make the width and height of the loaded swf to 0):
    swfLoader.contentLoaderInfo.width : 320
    swfLoader.contentLoaderInfo.height : 240
    module: MyVideo/flvplayer.swf
    x:0
    x container:0
    y:0
    y container:0
    xml width:550
    width container:0
    xml height:400
    height container:0
    myCounter: 1
    swfLoader.contentLoaderInfo.width : 550
    swfLoader.contentLoaderInfo.height : 400
    module: AnalogueClock.swf
    x:50
    x container:50
    y:0
    y container:0
    xml width:250
    width container:250
    xml height:200
    height container:200
    myCounter: 2
    swfLoader.contentLoaderInfo.width : 800
    swfLoader.contentLoaderInfo.height : 30
    module: MyNewsticker/newsticker.swf
    x:0
    x container:0
    y:0
    y container:0
    xml width:300
    width container:0
    xml height:50
    height container:0
    most of my loaded swf beside the analouge clock is full action script code and in case of the analouge clock it is a swf that has a movie clip on it's stage (the other are fully created from action script 3.0)
    please do help me..,
    cause i'm already really desperate and going crazy by this problem..,

    sorry i copied the code from the other swf i used to do try and error test..,
    basically the container is the same as swf varibale
    here the code so you would'nt get confused:
    //variable list
    var swfList:XMLList; //hold all the zone list from the xml
    var totalZone:uint; //total of zone there is in the xml
    var myURLLoader:URLLoader = new URLLoader();
    var swf:Movie Clip;//hold the loaded swf
    var swfLoader:Loader = new Loader();//loader instance used to load the external swf
    var myCounter:uint = 0;
    //load the xml file
    myURLLoader.load(new URLRequest('myXMLFile.xml'));
    myURLLoader.addEventListener(Event.COMPLETE, processXML, false, 0, true);
    function processXML(e:Event):void
        removeEventListener(Event.COMPLETE, processXML);
        XML.ignoreWhitespace= true;
        var myXML:XML = new XML(e.target.data);
        swfList = myXML.SWF;
        totalSWF = myXML.SWF.length();
        loadSWF();
    function loadSWF():void
        swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfSetting);
        swfLoader.load(new URLRequest(swfList[myCounter].@source));
    function swfSetting(e:Event):void
        //making new instance of sprite to hold the new loaded swf
        swf = new MovieClip();
        //casting the loader content into a movieclip
        swf = e.target.content;
        addChild(swf);
        swfLoader.unload();
        swf.x = swfList[myCounter].@left;
        swf.y = swfList[myCounter].@top;
        swf.width= swfList[myCounter].@width;
        swf.height= swfList[myCounter].@height;
        addChild(swf);
        if(myCounter < totalSWF)
            myCounter++;
            trace('myCounter: ' + myCounter );
            loadSWF();
    well in my code i did cast the loader into a movie clip and then set it's width and heigt,right???
    wouldn't it just give me the same result??
    if it's not can u describe what u mean in more detail??
    some example about accessing the loader content would be really appreciated

  • Loaded SWF file CANNOT be made into button DIRECTLY?

    I'm experiencing a problem which may have no solution, but I
    would like to know why I cannot do what I want to do.
    PROBLEM:
    I am attempting to load in an external SWF file and once it
    is loaded, I wish to directly make the new Sprite/MovieClip into a
    button. However, the newly loaded Sprite/MovieClip seems to be
    resistant to be made into a button DIRECTLY. I would like to know
    why this is.
    WORKAROUNDS:
    Here are a couple of workarounds, but again I wish to know
    why I cannot do what I want to do.
    1) I place the new Sprite/MovieClip into an empty Sprite on
    stage and make the once empty sprite into a button.
    2) Construct the external SWF in such a way so i can refer to
    a MovieClip within the SWF file and make that MovieClip into a
    button.
    CODE EXAMPLE:
    I am loading a SWF file thru a Loader and then taking the
    loaded SWF file and adding it to an existing sprite on the stage. I
    then attempt to make a button out of the newly loaded swf file, but
    it fails to be clickable.
    The following is some quick code to illustrate my problem. I
    already have workarounds in place, so please let me know WHY the
    following code is impossible. thanks!
    ==============================================================================

    There should be no problem with what you are trying to do. If
    you want someone to look at what you've done, you have to be
    careful to show that and not possibly errant code because that is
    the substance of your post and what you requested be looked at--we
    cannot guess what you may have done wrong in setting things up or
    for code that you do not show.
    I tried your code before posting my response and it worked
    fine except for your error with the COMPLETE aspect. I did not
    utilize any objects for loading things into, so I suggest you look
    there.

  • Loaded SWF & hitTest

    Hi,
    I have a problem with loaded SWF's and hitTests that use X
    & Y. We're using content that originally was loaded into a
    Shell SWF at 0,0. All hitTests worked fine.
    Now we're creating a new project where this content loads in
    a window area that is not at 0,0. Because hitTest using X & Y
    uses the Global scope these hitTests now do not work.
    Is there a solution to making the hitTest use the scope of
    itself? I've tried _lockroot but this didn't solve it. Or is there
    another solution?
    Regards,
    Scott

    Scott,
    > Now we're creating a new project where this content
    loads in
    > a window area that is not at 0,0. Because hitTest using
    X & Y
    > uses the Global scope these hitTests now do not work.
    The MovieClip.hitTest() method doesn't really depend on
    scope -- unless
    I'm misunderstanding your use of the word -- but you might be
    facing an
    issue that could be addressed with the
    MovieClip.localToGlobal() or
    globalToLocal() methods.
    > Is there a solution to making the hitTest use the scope
    of itself?
    > I've tried _lockroot but this didn't solve it.
    The MovieClip._lockroot property, when set to true,
    instructs a loaded
    movie clip to interpret any references to _root (in its own
    code, that is)
    as if it *hadn't* been loaded into another SWF. It doesn't
    really affect
    the placement (x and y values) of visual objects on the
    stage.
    David Stiller
    Adobe Community Expert
    Dev blog,
    http://www.quip.net/blog/
    "Luck is the residue of good design."

  • Loaded swf does not recognize stage of parent correctly

    I have an issue that is probably easily solved, but my attempts to solve it have been frivolous. 
    I am creating a “lightbox” to display artwork, but I have come across a slight hiccup.
    I am executing the lightbox functionality from a swf file that is loaded by a parent swf.  For testing purposes, when the lightbox button is clicked, the image should fill the entire stage.  Even though both the loaded swf and the parent swf recognize the same stage dimensions at all times(validated through tracing statements) and the lightbox image is positioned to the top/left corner of the stage, the image instead aligns to the top/left corner of the loaded swf.  And if the stage is resized after the lightbox image is displayed, the container MC (which is located in the parent swf and centers all the site’s content) re-aligns to the top/left corner of the stage . 
    To better illustrate these points, click the link below to see some screenshots with captions:
    http://newsite.katech.com/assets/images/lightbox_screenshots.jpg
    Also, here is the code applicable code from the loaded swf:
    stop();
    picture.visible = false;
    picture.x = 0;
    picture.y = 0;
    picture.width = stage.stageWidth;
    picture.height = stage.stageHeight;
    lightboxBtn.buttonMode = true;
    trace("embedded swf says the stage is " + stage.stageWidth + " X " + stage.stageHeight);
    function onStageResize(event:Event):void
                    trace("embedded swf says the stage is " + stage.stageWidth + " X " + stage.stageHeight);
                    picture.x = 0;
                    picture.y = 0;
                    picture.width = stage.stageWidth;
                    picture.height = stage.stageHeight;
    stage.addEventListener(Event.RESIZE, onStageResize);
    function showImage(event:MouseEvent):void
                    picture.visible = true;
    lightboxBtn.addEventListener(MouseEvent.CLICK, showImage);
    And for the applicable code from the parent swf:
    stop();
    trace("MAIN swf says the stage is " + stage.stageWidth + " X " + stage.stageHeight);
    //aligns site content to center of stage
    function onStageResize(event:Event):void
                    trace("MAIN swf says the stage is " + stage.stageWidth + " X " + stage.stageHeight);
                    container.x = (stage.stageWidth/2) - (container.width/2);
                    container.y = (stage.stageHeight/2) - (container.height/2);
                    if (container.x < 0)
                                    container.x = 0;
                    if (container.y < 0)
                                    container.y = 0;
    stage.addEventListener(Event.RESIZE, onStageResize);
    How can I get the loaded swf to display the lightbox image correctly in respect to the parent swf?  What am I doing wrong?

    I have still yet to resolve this issue on my own, if anyone could point me in the right direction, it would be greatly appreciated.
    Also, when I updated my previous post something "screwy" happened with the edit, so I went back and corrected the mistakes in case it made things unclear.
    Thanks.

  • Loading SWFs and setting z-index

    I have a project that involves 10 SWFs files. I know how to load them using one container. The problem I have is that when a new SWF loads, it covers the main navigation's drop menus from main container. The navigation bar on main container is a movie clip labeled nav_btns.
    Can someone help me set the z-index of main container and other SWFs as they load?
    Here is my code, which works when loading SWFs
    import flash.display.MovieClip;
    import flash.display.Loader;
    import flash.net.URLRequest;
    import flash.events.Event;
    var Xpos:Number=10;
    var Ypos:Number=115;
    var swf:MovieClip;
    var loader:Loader=new Loader();
    var loader1:Loader= new Loader();
    var defaultSWF:URLRequest = new URLRequest("homePage.swf");
    loader.load(defaultSWF);
    loader.x=Xpos;
    loader.y=Ypos;
    addChild(loader);
    function buttonClick(event:MouseEvent):void {
    removeChild(loader);
    var newSWFRequest:URLRequest= new URLRequest("Movies/"+ event.target.name+".swf");
    loader.load(newSWFRequest);
    loader.x=Xpos;
    loader.y=Ypos;
    setChildIndex(this, -1);
    addChild(loader);
    nav_btns.homePage.addEventListener(MouseEvent.CLICK, buttonClick);
    nav_btns.homePage.txt.text="HOME";

    add (or re-add) the menu to the display list after you add the loader.

  • Can't Access MovieClips and Functions in Loaded SWF

    Hi everybody,
    I'm trying to simply access anything inside this loaded SWF and all I get is 'null'.
    I have code in the parent SWF that needs to tell the child SWF movieclip what to do but nothing works. This was a piee of cake in AS2 and I can't seem to get anything to crossover in AS3.
    Here's my code:
    import com.greensock.TweenMax;
    import flash.display.MovieClip;
    var myLoader:Loader = new Loader();
    var url:URLRequest = new URLRequest("SWCS_S3_500x250_exp_panel_2.swf"); // in this case both SWFs are in the same folder
    myLoader.load(url);  // load the SWF file
    panel2.addChild(myLoader);   // add that instance to the display list, adding it to the Stage at 0,0
    panel2 = myLoader.content as MovieClip;
    photo_about.alpha = 0;
    photo_downloads.alpha = 0;
    //panel2.content_about.alpha = 0;
    //panel2.content_downloads.alpha = 0;
    function closeSection():void
              panel2.controlsMC.forcePause();
              TweenMax.to(photo_about, .5, {alpha:0});
              TweenMax.to(photo_downloads, .5, {alpha:0});
              TweenMax.to(photo_home, .5, {alpha:0});
              TweenMax.to(panel2.content_about, .5, {alpha:0});
              TweenMax.to(panel2.content_downloads, .5, {alpha:0});
              TweenMax.to(panel2.controlsMC, .5, {autoAlpha:0});
              TweenMax.to(panel2.content_home, .5, {alpha:0});
    panel2 traces null and all the clips loaded within pull up undefined property errors.
    Can somebody please tell me what I'm doing wrong? I'd be stoked to know what I'm missing and feel like this should be a piece of cake.
    Thanks for any help!

    You are not adding the content to the stage at 0,0, you are adding it to panel2 at 0,0.  THen you take panel2 and assign it to be something that likely doesn't exist by the time you assign it.  You need to wait until the loader completes loading before you attempt to do anything with its content, otherwise it has no content (null).  Similarly, you cannot control anything in the loaded swf until it has finished loading.  So assign an event listener to the contentLoaderInfo property of the Loader to determine when loading is complete, and have the event handler function deal with starting the interaction with it.
    var myLoader:Loader = new Loader();
    var url:URLRequest = new URLRequest("SWCS_S3_500x250_exp_panel_2.swf");
    myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, processLoadedSWF);
    myLoader.load(url);
    function processLoadedSWF(evt:Event):void {
        // deal with the loaded swf here
        // but you don't want to be assigning it to the panel2 object that contains the loader
    As far as controlling anything inside the swf you load, is it an AS1/2 or an AS3 swf?

  • Loading SWF into level and setting height/width/location?

    Hi folks, I am an old AS2 guy who gave up on Flash heavy coding when it switched to AS3. Always thought it was less intuitive, longer, and a brand new language and I got tired of trying to keep up when most of my clients did not want to pay for a full flash site. But I got this client...
    Here's the problem, I can load a swf into my main stage or into an MC , but I cannot find out anywhere how to set parameters or load into a level so it doesn't replace the current material or fix to a size that doesn't block my other material.
    I've gotten this far:
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    loadGallery();
    function loadGallery():void
    var l:Loader = new Loader();
    var req:URLRequest = new URLRequest("shortstack.swf");
    l.load(req);
    l.contentLoaderInfo.addEventListener(Event.INIT, galleryLoaded);
    function galleryLoaded(e:Event):void
    addChild(e.target.loader);
    What i want to do is load shortstack.swf into level 5, say, and fix it to the bottom left corner of the stage, and set its height to 400px and width to 980. 
    I have a an MC on the stage at those exact dimensions called placeholderMC, but I cannot figure out a way to target it, and the tutorials I've read have to do with loading on CLICK (I want it to load on enterFrame), and none of them tell me how to set parameters, as I could in old AS2.
    FWIW, AS3 may be more powerful, but AS2 was easier to understand.
    TIA,
    Jeff

    http://www.adobe.com/devnet/flash/quickstart/display_list_programming_as3.html

  • 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.

  • FAQ: SWF's and loading external files in Flash Catalyst

    How do I make the images in my Flash Catalyst project external to the published SWF?
    You can choose whether images are embedded in your Flash Catalyst SWF, or loaded dynamically; this can help to keep your SWF's small. Simply right-click an image and chose "Externalize Image".
    Answered by: Adam Cath.
    See entire discussion.
    How do I make the SWF I imported to Flash Catalyst load external files?
    When importing a SWF, Flash Catalyst only pulls the SWF asset into your project. Many SWF's, such as slideshows and media players, rely on external data files, such as configuration files or media assets. If you would like to use such a SWF in Catalyst, then you will have to place these external resources into your Catalyst project as well as importing the SWF.
    Let's assume you have a SWF source folder that contains your SWF file and all of the external files it needs. You can open the SWF file from this folder and it works fine. But, if you import the SWF into Catalyst, the Catalyst project doesn't display it correctly because it can't load the external files. Here's what you do:
    1. Open your swf source folder, and copy all of the files/folders there except the SWF you imported.
    2. In Catalyst, Run/Preview your project (Ctl/Cmd-Enter)
    2. Note the file path to Main.html in your browser
    3. In your OS, open the folder that contains /bin-debug/Main.html from above
    4. Open the "src" folder
    5. Paste all of the files from step 1 into the src folder.
    6. Catalyst won't pick up that your project changed, so tweak it a bit (add a Rectangle), and save it.
    7. Run your project. If it doesn't work, try closing and reopening your project and running again.
    For an example, check out: Using an XML-driven SWF in Catalyst to create a slideshow.
    See entire discussion.
    Answered by: Bear Travis

    You can make you swf a custom component with 2 states.
    The first state will be empty and rename it "OFF" the second state will have your swf file and name it ON.
    Then go back to your artboard/project, add your 2 buttons ON & OFF.
    For the ON button add interaction "play transition to custom component ON"
    For the OFF button add interaction "play transition to custom component OFF"
    Here is an example swf_control

  • Last and greatest: conditional statements for click referring to loaded swf

    Hey forum,
    I am so close to being done with this site update it is exciting! I will be sure to throw it up here for you to see, since without this forum I would have been hurting!
    The last big hurdle I have, is the following, and I hope someone can just throw me a bit of guidance on this.
    I am looking for a way to write a function, that on click of my "next" button in the main timeline, will refer to my loaded swf MovieClip(imageLoader.content) telling it to go to the next frame label, so really a compound if/then situation – just referring to the loaded file throws me.
    So, my next button is "pNext," and what I am looking for is something along the lines of:
    if (loaded movie) is on frames 12-144, then go here (on click),
    if (loaded movie) is on frames 144-155, then go here (on click)
    etc. and so forth.
    Thanks so much! Below is my code that works on one next click, so you can see the instances etc.
    b
    pNext.addEventListener (MouseEvent.CLICK, nextClick);
    function nextClick (e:MouseEvent):void{
        MovieClip(imageLoader.content).gotoAndPlay ("aboutS1");

    If I'm reading your request right, what you can try is to create an array of the frame labels in the order they appear and use the currentLabel property to determine where you are and indicate where to go...
    var labels:Array = new Array("one","two","three","four","five"...etc...);
    pNext.addEventListener (MouseEvent.CLICK, nextClick);
    function nextClick (e:MouseEvent):void{
        var mc:MovieClip = MovieClip(imageLoader.content);
        var nextLabel:String = labels[labels.indexOf(String(mc.currentLabel))+1];
        mc.gotoAndPlay (nextLabel);

Maybe you are looking for