Events
If you are going to use AS3, I would advice you to use events to establish proper communication between Parent and Child.
For each event (user action or something else) you would like to intercept, you must register a so-called event listener function. The syntax is the following:
addEventListener(Type_of_event.Name_of_event, Name_of_function_YOU_define);
The function that handle the event is like the following :
function event_handler(event:Event) {
/* Do something with this event */
}
If you want to know more about events, you may have a look at this page.
Adding arguments to events
The problem with events in AS3 is that, by default, you cannot pass extra arguments to the event handler. But there are at least 2 ways of doing it :
You create your own custom event type. It would be the cleanest way to do it, and maybe the most recommended one if you are working on a relatively big Flash project.
If you only use the flash IDE, working without the document class, and want to write procedural code or want to stick to the IDE and the timeline as much as possible, you should use dynamic instance variables.
I guess for your case, the appropriate solution would be the second one.
Dynamic instance variables
Dynamic instance variables can only be associated to classes declared with the attribute dynamic. Once a class has been defined as dynamic, we can add a new dynamic instance to any instance of that class via a standard variable assignment statement.
var myInstance:DynamicClass= new DynamicClass();
myInstance.foo = "hello World"; // this won't cause any compile time errors
trace(myInstance.foo ); //returns hello World
In AS3, unlike previous versions, only the MovieClip class is defined as dynamic :(
The example
Maybe the part you were most expecting ^_^
For example :
In the child SWF
/*
child.swf
You have 3 movie clips on stage
*/
// Add dynamic instance variables to all 3 clips
mc_1.num = 1;
mc_2.num = 2;
mc_3.num = 3;
In the parent SWF
/*
parent.swf
*/
//Load the child.swf
var movieRequest:URLRequest = new URLRequest("child.swf");
var movieLoader:Loader = new Loader();
movieLoader.load(movieRequest);
addChild(movieLoader);
movieLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, movieComplete);
//Once loaded in attach listeners here
function movieComplete(event:Event):void {
mc_1.addEventListener(MouseEvent.CLICK,onClickListener);
mc_2.addEventListener(MouseEvent.CLICK,onClickListener);
mc_3.addEventListener(MouseEvent.CLICK,onClickListener);
}
function onClickListener(e:MouseEvent):void{
// Retreive the num : dynamically set variable
trace(e.currentTarget.num);
}