Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

removeChild in parent.swf from child.swf (AS3)

New Here ,
Aug 03, 2013 Aug 03, 2013

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!

TOPICS
ActionScript
2.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Aug 04, 2013 Aug 04, 2013

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:

...
Translate
LEGEND ,
Aug 04, 2013 Aug 04, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 04, 2013 Aug 04, 2013

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"));
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 04, 2013 Aug 04, 2013

As usual, Ned, you're right on "target". Thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 04, 2013 Aug 04, 2013
LATEST

You're welcome

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines