5 Replies Latest reply on Jul 12, 2007 9:41 AM by kglad

    AS3 : How to target sub-MC of loaded movie clip?

    CPruittCCB Level 1
      Hey everyone. I'm new to AS3 and very... very lost (Last time I did much with flash was Flash 5).

      Short version: I'm trying to load an external .swf from a URL and add event handlers to the movie clips (or it's movie clips sub-MCs) on its stage. I can't find a THING online on how to do this.

      Details:

      I've been tasked by my employer to build a simple control interface in flash, with just a few buttons to do things like display some info or images or maybe an associated MP3. I've written little test apps to do all of these individual things & managed to fake my way through.

      But the sticking point is that I'm now trying to place all the UI controls into an external .swf, which I then load into the canvas by a URL. The goal is to be able to load it into a variable named, say, 'private _ui:MovieClip' and then add event handlers to mivie clips within that by name like:

      _ui.someButton1.addEventListener(MouseEvent.MOUSE_UP, handlerFunction1);
      _ui.controlStrip.someButton2.addEventListener(MouseEvent.MOUSE_UP, handlerFunction2);

      The goal is that if we want to swap out the UI for a particular client, we just export a new .swf with all the correctly named MC instances & use the new URL to load the UI.

      I seem to be able to load the .swf OK and trace the movie clips "Main Timeline" like so:

      _ui2.contentLoaderInfo.addEventListener(Event.COMPLETE , uiDidLoad);
      function uiDidLoad(evt:Event){
      trace("UI2 LOADED");
      if(_ui2.content){
      addChild(_ui2);
      trace(_ui2.content); //--> [object MovieClip]
      // Target the "ControlStrip" named instance on the loaded .swf's main timeline
      trace(_ui2.content.controlStrip); //--> ERROR
      }
      }

      Can anyone point me in the right direction here? I've spent hours making no headway on this. I think I've read half of adobe's website so far. :-(

      - Cliff
        • 1. AS3 : How to target sub-MC of loaded movie clip?
          SymTsb Level 2
          loading a movie clip in AS3 is done with the Loader Class.

          Modified example directly from CS3 Help...

          import flash.display.*;
          import flash.net.URLRequest;
          var ldr:Loader = new Loader();
          var url:String = " http://www.unknown.example.com/content.swf";
          var urlReq:URLRequest = new URLRequest(url);
          ldr.load(urlReq);
          addChild(ldr);

          I'd recommend getting your hands on Essential ActionScript 3.0 and the ActionScript 3 Cookbook. I remember this topic coming up in the cookbook. I'm going to check my code at home tonight. I am pretty sure I used a similar tactic when I was updating an image reel I did in Flash 8 (AS2).
          • 2. Re: AS3 : How to target sub-MC of loaded movie clip?
            CPruittCCB Level 1
            SymTsb,
            Thanks. I am not having any problem at all with loading the .swf, I just can't seem to target anything inside of it. It seems like [_ui2.content], because it is a movie clip, should respond to .getChildAt() or getChildByName() but both of those generate errors saying that [_ui2.content] is a DisplayObject, which I guess means it's not a DisplayObjectContainer. THat's confusing to me because I thought the API docs said that MovieClip inherits from DisplayObjectContainer.

            (I'm not looking at my code right now so some of the method/class names I'm using might be slightly wrong here but I double checked them all in my code)

            If you have any further insight on how to target items within the loaded .swf I'd be really grateful. I'll check out the books & see if they offer any insight.

            Thanks.
            • 3. Re: AS3 : How to target sub-MC of loaded movie clip?
              kglad Adobe Community Professional & MVP
              you can reference the main timeline of your loaded swf via the content property of your loader (say ldr) AFTER loading is complete.

              you then need to cast that object as a MovieClip or the flash compiler has concerns. if you have an object on the main timeline of your loaded swf with instance name btn, you can use the following to add a mouse handler to it:



              • 4. Re: AS3 : How to target sub-MC of loaded movie clip?
                CPruittCCB Level 1
                quote:

                Originally posted by: kglad
                you then need to cast that object as a MovieClip or the flash compiler has concerns.


                I discovered this yesterday after a *lot* of trial and error. Everything seems to work fine when using:

                ldr.content as MovieClip;

                I'm kind of surprised that's not documented somewhat clearly, but I suppose it might be something that's obvious if you know AS well enough. I am trying to learn as much as I can as I go but don't my timeline doesn't really permit a while lot of time for learning from the ground up.

                Thanks.