A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: [RESOLVED] How to load external swf into movieclip?

Hybrid View

  1. #1
    Senior Member
    Join Date
    Mar 2007
    Posts
    133

    [RESOLVED] How to load external swf into movieclip?

    Hey guys,

    I've decided to take the jump to AS3 now but im having a lot of issues... Im trying to load an external swf into a movieclip.

    In AS2 it would be:

    PHP Code:
    var mcLoader:MovieClipLoader = new MovieClipLoader();
    var 
    myListener:Object = new Object ();
    mcLoader.addListener(myListener);
    mcLoader.loadClip("mainLoader.swf","externalLoader_mc"); 
    In AS3 atm i have (taken from tutorial):
    PHP Code:
    var myLoader:Loader = new Loader();
    var 
    myRequest:URLRequest = new URLRequest("mainLoader.swf");
    myLoader.load(myRequest);

    myLoader.contentLoaderInfo.addEventListener(Event.OPEN,showPreloader);
    myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,showProgress);
    myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,showLoadResult); 
    How can i load the URLRequest into an actual movieclip as i have done in AS2? As far as i know the load function only accepts one variable. ATM the external swf im loading is just going into thin air and i can't get anything to happen (i have the functions created to deal with the events btw).

    Any help would be greatly appreciated. Thx.


    Edit: Just fixed it by adding externalLoader_mc.addChild(myLoader);
    Last edited by chur; 11-13-2007 at 08:17 PM.

  2. #2
    Junior Member
    Join Date
    Aug 2009
    Posts
    13
    Hi, did you ever get this issue resolved. I just posted a very similar thread. I'm trying to learn how to load a swf into a movie clip using AS3.

    Thanks,
    Brian
    www.brianmcurran.com

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Hey, Im not sure if you have figured this out yet but its actuallty really simple to do in AS3.

    First you have to create a loader.
    Code:
    var whatevername_loader:Loader = new Loader();
    After, that, you have to add an Event listener with what listener you want, for this example, Ill use MouseEvent.CLICK for clicking a button to load a external swf.

    Code:
    instanceName.addEventListener(MouseEvent.CLICK, gotoSomething);
    Next, you must create a funtion that loads the swf when the button is clicked.

    Code:
    function gotoSomething(Event:MouseEvent):void {
    	var request:URLRequest = new URLRequest("flash.swf");
    	whatevername_loader.load(request);
    	addChild(whatevername_loader);
    	removeChild(container_mc);
    }
    Break down of code: >
    var request:URLRequest = new URLRequest("flash.swf"); = the request to load the external swf to the movie stage.

    whatevername_loader.load(request); = accessing AS's ability to load the swf to the stage.

    addChild(whatevername_loader); = adds the loader to the stage (in this case, would add the movie.

    removeChild(container_mc); = this is only used if you want to complete unload a movie and add another one in. If you dont want to completely add in another movie, you can remove this. Or in other cases, you could remove just parts of the current movie and load other parts in.

    Hopefully this all makes sense.

    ----------------------

    P.S. You only need to specify one loader in the AS3.

    For example, here is a portoflio site im working on for myself.

    Code:
    var page_loader:Loader = new Loader();
    
    container_mc.contact_mc.addEventListener(MouseEvent.CLICK, goToContact);
    function goToContact(Event:MouseEvent):void {
    	var request:URLRequest = new URLRequest("contact.swf");
    	page_loader.load(request);
    	addChild(page_loader);
    	removeChild(container_mc);
    }
    
    container_mc.flash_mc.addEventListener(MouseEvent.CLICK, goToFlash);
    function goToFlash(Event:MouseEvent):void {
    	var request:URLRequest = new URLRequest("flash.swf");
    	page_loader.load(request);
    	addChild(page_loader);
    	removeChild(container_mc);
    }
    
    container_mc.home_mc.addEventListener(MouseEvent.CLICK, goToHome);
    function goToHome(Event:MouseEvent):void {
    	var request:URLRequest = new URLRequest("home.swf");
    	page_loader.load(request);
    	addChild(page_loader);
    	removeChild(container_mc);
    }
    ------------------

    Hopefully I helped you out a little : )

  4. #4
    Member
    Join Date
    Oct 2009
    Location
    Toronto, Canada
    Posts
    30
    really helpful. thanks. i had the same enquiry

  5. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Quote Originally Posted by Yasky View Post
    really helpful. thanks. i had the same enquiry
    No problem at all, glad i could help.

    Im also just getting started in AS3 but I like it so far!

  6. #6
    Junior Member
    Join Date
    Apr 2009
    Posts
    4
    Hi! Thanks for the code. Ive used it on my file and it works, but there is a problem that I do not know how to solve.

    The external swf is loaded into the scene but it becomes the top layer, overlapping all my other MCs on the scene.

    My question is how I can load the swf into a Movie Clip, not into the main scene. With the swf loaded in the Movie Clip, I can place it whereever and whichever layer I want to.

    Thanks!

  7. #7
    Member
    Join Date
    Oct 2009
    Location
    Toronto, Canada
    Posts
    30
    @atanism, I guess you can do the same bloc of code. Just put it in the movieclip's timeline instead of the main timeline [or class, depending on how you are coding up your SWF]
    Temet Nosce - Oracle

  8. #8
    Junior Member
    Join Date
    Apr 2009
    Posts
    4
    @Yasky
    Thanks, ive tried that before I posted the question but it didnt work.
    But its okay cos I have already gotten the solution somewhere else. Thanks!

  9. #9
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    hi guys!

    I've tried to use GrimesD solution, but I keep getting this output error:

    TypeError: Error #1010: A term is undefined and has no properties.
    at _deletemetest_fla::MainTimeline/_deletemetest_fla::frame1()
    And no compile error for details. This same error occured on my original file (layout-2.fla) that was adjusted to my menu and container.. so I created a brand new plain file to duplicate exactly what the original answer did.

    Code:
    var page_loader:Loader = new Loader();
    
    container_mc.contact_mc.addEventListener(MouseEvent.CLICK, goToContact);
    function goToContact(Event:MouseEvent):void {
    	var request:URLRequest = new URLRequest("deletemetoo.swf");
    	page_loader.load(request);
    	addChild(page_loader);
    	removeChild(container_mc);
    }
    
    container_mc.flash_mc.addEventListener(MouseEvent.CLICK, goToFlash);
    function goToFlash(Event:MouseEvent):void {
    	var request:URLRequest = new URLRequest("deletemetoo2.swf");
    	page_loader.load(request);
    	addChild(page_loader);
    	removeChild(container_mc);
    }
    I have three objects on my stage: A MC named contact_mc and a MC named flash_mc. Then I have a box in the middle named container_mc.

    The two external swf's just contain text each to identify which is which.

    Thanks in advance!

  10. #10
    Filmmaker J-Luv's Avatar
    Join Date
    Jan 2002
    Location
    With her.
    Posts
    1,497
    Quote Originally Posted by atanism View Post
    @Yasky
    Thanks, ive tried that before I posted the question but it didnt work.
    But its okay cos I have already gotten the solution somewhere else. Thanks!
    so how do you load a swf into a movieclip instead of the main scene?
    Never take life too seriously. Nobody makes it out alive anyways. Film Portfolio


  11. #11
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Okay, to answer my own question, the reason the movie was gibing the 1010 error was because my button was within a seperate MC called LeftMenu, so that was where the listener needed to listen for the click.

    So rather than

    container_mc.flash_mc.addEventListener(MouseEvent. CLICK, goToFlash);

    it was

    LeftMenu.flash_mc.addEventListener(MouseEvent.CLIC K, goToFlash);

    (for my setup)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center