A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: External SWF loaded, now how do I access it's MC's?

  1. #1
    Junior Member
    Join Date
    Jan 2003
    Posts
    22

    External SWF loaded, now how do I access it's MC's?

    I have successfully loaded a external swf with something similar to:
    Code:
    var request:URLRequest = new URLRequest("content.swf");
    var loader:Loader = new Loader();
    loader.load(request);
    addChild(loader);
    this external SWF file has a movieclip (that diplays just fine in both files) named "testMovieclip_mc".

    So from the parent movie shouldn't I simply be able to communicate with this one by something like?
    Code:
    trace(testMovieclip_mc.x);
    For some reason that doesn't work.

    I've tried every kind of syntax I could think of like:
    Code:
    trace(loader.testMovieclip_mc.x);
    trace(loader.content.testMovieclip_mc.x);
    and tons more and nothing works.
    I've also searched the entire net after a solution..

    Does anyone know?

  2. #2
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Try this:

    Code:
    var swfToLoad:String = "your url here";
    var holderMC:MovieClip();
    var hLoad:Loader = new Loader();
    function hSpotLoader():void {
    			var hURLReq:URLRequest=new URLRequest(swfToLoad);
    			hLoad.contentLoaderInfo.addEventListener(Event.INIT,swfLoadCompleteHandler);
    			hLoad.load(hURLReq);
    			hLoad.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,swfError);
    			trace("swf Loader");
    		}
    		//function to handle error if there's a problem loading the swf
    		function swfError(evt:IOErrorEvent):void {
    			trace("Could not load swf.");
    		}
    		//function to load when the swf is completely loaded.
    		function swfLoadCompleteHandler(evt:Event):void {
    			holderMC=MovieClip(hLoad.content);
    			this.addChild(holderMC);
    		}
    Then you should be able to reference anything like this:

    Code:
    holderMC.mcNameHere_mc.x = 8675309;
    Hope this helps. Let me know.

  3. #3
    Junior Member
    Join Date
    Jan 2003
    Posts
    22
    I'm affraid that did not work:
    Code:
    var holderMC:MovieClip();
    1046: Type was not found or was not a compile-time constant: .

  4. #4
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Did you import the MovieClip class?

    Is this on the main timeline or in a class?

    if in a class try this:

    import flash.display.MovieClip;

    and try dataTyping the holderMC like this:

    var holderMC:MovieClip = new MovieClip();

    Attach all your code as well, I may be able to be of more assistance that way.

    Sorry for the delay.

  5. #5
    Junior Member
    Join Date
    Jan 2003
    Posts
    22
    It's just the main-timeline,
    if I type it like this:
    var holderMC:MovieClip = new MovieClip();
    it doesn't complain any more but it doesn't load anything either.

    Do I really need to use your loading code?
    Is there something about the simple loader (that works!) that I posted at first (see below) that means I can't access the MC's inside it?
    Code:
    var request:URLRequest = new URLRequest("content.swf");
    var loader:Loader = new Loader();
    loader.load(request);
    addChild(loader);
    At some point I'm going to use an advance loader such as the one you have (which has error checking and stuff), but right now I'm just trying to understand what syntax to use to access the movieclip inside it.

    Using "hLoad.content" like you and tutorials suggest (or "loader.content" in my case) for some reason just reports back null, but that external file DOES have a movieclip inside it.

  6. #6
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Hmmm..., you'd have to use the loader.content to access everything, but, if you post your source, fla and file to load, I'll take a look at it.

  7. #7
    Junior Member
    Join Date
    Jan 2003
    Posts
    22
    Wow thanks!
    I'm suprised that it should need that because I thought it was a really simple thing to do, but there could be a whole concept that I'm not understanding here, so thanks alot for taking a look at the flash file.

    I've orginised it so it's really the basics.
    Attached Files Attached Files

  8. #8
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    This will do it:

    Code:
    var request:URLRequest
    var loader:Loader = new Loader();
    function loadIt():void {
    	request = new URLRequest("LoadingExternal-External.swf");
    	loader.load(request);
    	loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
    	
    	addChild(loader);
    }
    loadIt();
    
    
    var holderClip:MovieClip;
    function loadComplete(evt:Event):void {
    	trace("done");
    	loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadComplete);
    	holderClip = MovieClip(loader.content);
    	addChild(holderClip);
    	trace(holderClip.ExternalMC_mc.x);
    }
    
    //Now I want to access that movieclip from that external MC, here's some random things I've tried:
    //trace(ExternalMC_mc.x); //Doesn't work.
    //trace(loader.ExternalMC_mc.x); //Doesn't work.
    //trace(loader.content.ExternalMC_mc.x); //Doesn't work.

  9. #9
    Junior Member
    Join Date
    Jan 2003
    Posts
    22
    Awesome awesome awesome! thanks dude.

    I've tried it and it works! atm I don't really understand why ".content" works in this case and not in my old code, but I'm going to get to the bottom of it!

  10. #10
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    It has to do with AS3's new way of thinking things so to speak. It's kind of like saying this;

    AS3 - Okay I'm loading stuff under this variable, now what part of it should I use?

    Then you:

    You - Use the content, or what's been loaded, I don't care about the other stuff loaded there.

    Make sense?

  11. #11
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Also, glad it works for you.

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