all groups > flash actionscript > july 2007 >
You're in the

flash actionscript

group:

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


AS3 : How to target sub-MC of loaded movie clip? CPruittCCB
7/10/2007 7:17:55 PM
flash actionscript:
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, 'public _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
Re: AS3 : How to target sub-MC of loaded movie clip? SymTsb
7/10/2007 9:05:37 PM
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);
Re: AS3 : How to target sub-MC of loaded movie clip? CPruittCCB
7/11/2007 1:26:43 PM
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.
Re: AS3 : How to target sub-MC of loaded movie clip? kglad
7/11/2007 2:36:48 PM
you can reference the main timeline of your loaded swf via the content property
of your loader. 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:





ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,f,false,0,true);
function f(evt:Event){

MovieClip(ldr.content).btn.addEventListener(MouseEvent.CLICK,btnClickF,false,0,
true);
}
function btnClickF(evt:MouseEvent){
trace("button clicked");
}
Re: AS3 : How to target sub-MC of loaded movie clip? CPruittCCB
7/12/2007 4:34:13 PM
[q][i]Originally posted by: [b][b]kglad[/b][/b][/i]
you then need to cast that object as a MovieClip or the flash compiler has
concerns.[/q]

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.
Re: AS3 : How to target sub-MC of loaded movie clip? kglad
7/12/2007 4:41:47 PM
AddThis Social Bookmark Button