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

remove child function

Contributor ,
May 05, 2009 May 05, 2009

Copy link to clipboard

Copied

I have the following function to add movie clips to the stage...

I assume it is the correct way to add movieclips to the stage becuase it works.

function attachMovieClips(){
for (var i=0; i<list_array.length; i++)
{var mySelectFile:selectFile = new selectFile();
mySelectFile.name = "mySelectFile"+i;
mySelectFile.y = (mySelectFile.height + 10)*i;
addChild(mySelectFile);}}

attachMovieClips();

i need a function to remove all the attached movie clips...

I assume i need to use removeChild however everything i've tried hasn't worked...

i should think this is an easy problem if you understand AS3, sadly i don't, i'm not sure why i need to create a class to add a mc to the stage in the first place?... please can someone help... kind regards Jonathan

TOPICS
ActionScript

Views

597

Translate

Translate

Report

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 , May 05, 2009 May 05, 2009

There are many ways to structure this, for example, you could add your new movieclips to an array, and then later run around the array to remove them. But staying with the way you've done it, you could do this:

function removeMovieClips(){
for (var i=0; i<list_array.length; i++) {

var mySelectFileMC:DisplayObject = getChildByName("mySelectFile"+i);

removeChild(mySelectFileMC);

}

}

removeMovieClips();

Votes

Translate

Translate
LEGEND ,
May 05, 2009 May 05, 2009

Copy link to clipboard

Copied

There are many ways to structure this, for example, you could add your new movieclips to an array, and then later run around the array to remove them. But staying with the way you've done it, you could do this:

function removeMovieClips(){
for (var i=0; i<list_array.length; i++) {

var mySelectFileMC:DisplayObject = getChildByName("mySelectFile"+i);

removeChild(mySelectFileMC);

}

}

removeMovieClips();

Votes

Translate

Translate

Report

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
Contributor ,
May 05, 2009 May 05, 2009

Copy link to clipboard

Copied

thanks Colin that works great..

you mentioned there were lots of ways to remove the movie clips....

is this the way you would attach and remove movie clips in a function?

Votes

Translate

Translate

Report

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 ,
May 05, 2009 May 05, 2009

Copy link to clipboard

Copied

As you make things you could add them to an array. On these lines:

var removeLaterArray:Array = [];

then in your routine where you make things, you add:

removeLaterArray.push(mySelectFile);

and it any other functions that make things that you also want to remove at the same time, you add those to the array:

removeLaterArray.push(someOtherMovieClip);

At the time you want to remove them all, you would run around the array, and have the item's parent remove that item. Like this:

for(var i = 0;i<removeLaterArray.length;i++){

    var anItem:DisplayObject = removeLaterArray;

    anItem.parent.removeChild(anItem);

}

and perhaps:

removeLaterArray  = null;

to get rid of any lingering references to the objects.

There can be other issues though, like if each of your custom objects sets up its own listeners, ideally you would want to remove those listeners. And so perhaps your selectFile Class might have a dispose method, that removes its listeners and also removes itself:

public function dispose(){

    removeanylistenersImade();

    this.parent.removeChild(this);

}

I'm sure there are other alternate ways too. Overall I wouldn't worry too much about it, once you have a way that works for the situation you have.

Votes

Translate

Translate

Report

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
Contributor ,
May 05, 2009 May 05, 2009

Copy link to clipboard

Copied

LATEST

Thanks Colin,

I'm still trying to get my head around this AS3, but your help has been much appreciated...

kind regards J.

Votes

Translate

Translate

Report

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