Copy link to clipboard
Copied
Hi! How do I remove a child from a parent timeline with an externally-loaded swf? Specifically, I have a main.swf that loads movie_1.swf and movie_2.swf. In the movie_1.swf, I have a button to remove movie_2.swf from main.swf.
//--------- main.swf code:
var loader1:Loader = new Loader();
var loader2:Loader = new Loader();
loader1.load(new URLRequest("movie_1.swf"));
loader2.load(new URLRequest("movie_2.swf"));
addChild(loader1);
addChild(loader2);
//------------- movie_1.swf code:
removeMovie2_btn.addEventListener(MouseEvent.CLICK , removeMovie2);
function removeMovie2(event:MouseEvent):void
{
this.parent.parent.removeChild(this.loader2); // this does not work (get error code 2007, "Parameter child must be non-null")
}
// Thank you!
Here's how your code will change:
//--------- main.swf code:
var loader1:Loader = new Loader();
var loader2:Loader = new Loader();
loader1.contentLoaderInfo.addEventListener(Event.COMPLETE, assignMovie1Listener);
loader1.load(new URLRequest("movie_1.swf"));
loader2.load(new URLRequest("movie_2.swf"));
addChild(loader1);
addChild(loader2);
function assignMovie1Listener(evt:Event):void {
MovieClip(evt.currentTarget.content).addEventListener("eventTriggered", removeLoader2);
}
function removeLoader2(evt:
...Copy link to clipboard
Copied
Unless you defined what loader2 is for movie_1.swf, movie_1.swf has no idea what loader2 is.
You are better off keeping the unloading functionality in the main file... if it loaded it, it should be unloading it as well. Create a function in the main to do the unloading and just have movie_1.swf indicate to the main swf that it needs to unload loader2.
The proper way of doing this is to have the main file assign an event listener to movie_1.swf as soon as it finishes loading and then have movie_1.swf dispatch an event for that listener. The listener's event handler function in the main file can then process the unloading.
Doing it this way allows the files to be separately compilable files that have no dependency on one another.
Copy link to clipboard
Copied
Here's how your code will change:
//--------- main.swf code:
var loader1:Loader = new Loader();
var loader2:Loader = new Loader();
loader1.contentLoaderInfo.addEventListener(Event.COMPLETE, assignMovie1Listener);
loader1.load(new URLRequest("movie_1.swf"));
loader2.load(new URLRequest("movie_2.swf"));
addChild(loader1);
addChild(loader2);
function assignMovie1Listener(evt:Event):void {
MovieClip(evt.currentTarget.content).addEventListener("eventTriggered", removeLoader2);
}
function removeLoader2(evt:Event):void {
loader2.unloadAndStop();
removeChild(loader2);
}
//------------- movie_1.swf code:
removeMovie2_btn.addEventListener(MouseEvent.CLICK , removeMovie2);
function removeMovie2(event:MouseEvent):void
{
dispatchEvent(new Event("eventTriggered"));
}
Copy link to clipboard
Copied
As usual, Ned, you're right on "target". Thank you!
Copy link to clipboard
Copied
You're welcome
Find more inspiration, events, and resources on the new Adobe Community
Explore Now