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

Removing Movie Clip using RemoveChild Help

New Here ,
Aug 16, 2012 Aug 16, 2012

Copy link to clipboard

Copied

Hi, im new to this forum, and new to actionscript 3.


I'm wondering, how can you remove the movieclip so that it doesnt stack when you click another button.


sh1.png


Btn 1 opens the movieclip by addchild


s2.png


Btn 2 opens the 2nd movieclip but stacks in front , and the first movieclip isnt removed


s3.png

AS linkage of my movieclip so that i can bring it to stage.


My code



thumbnail1.addEventListener(MouseEvent.CLICK ,myThumb1);


function myThumb1(evt:MouseEvent):void

{

var myMC:hover1 = new hover1();

container.addChild(myMC);


if(contains(myMC))

{

container.removeChild(myMC)

}

}


thumbnail2.addEventListener(MouseEvent.CLICK ,myThumb2);

function myThumb2(evt:MouseEvent):void

{

var myMC2:hover2 = new hover2();

container.addChild(myMC2);

}

TOPICS
ActionScript

Views

979

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 , Aug 16, 2012 Aug 16, 2012

Here is your code rewriten using the second approach I described...

thumbnail1.addEventListener(MouseEvent.CLICK ,myThumb1);

function myThumb1(evt:MouseEvent):void
{

     while(container.numChildren > 0){
         container.removeChildAt(0);
     }

     var myMC:hover1 = new hover1();
     container.addChild(myMC);
}

thumbnail2.addEventListener(MouseEvent.CLICK ,myThumb2);

function myThumb2(evt:MouseEvent):void
{
     while(container.numChildren > 0){
          container.removeChildAt(0);
     }


     var myMC2

...

Votes

Translate

Translate
LEGEND ,
Aug 16, 2012 Aug 16, 2012

Copy link to clipboard

Copied

By declaring the vars inside the function, you remove any ability to target them directly by name outside of the function.  So one way to deal with this would be to declare myMC1 and myMC2 outside of any functions so that every function can target them.

An alternative approach would be to just empty the container out before adding any children to it using the removeChildAt() method (you need to check that there are children within it to remove beforehand)

For the code you show, your myThumb1 function isn't going to do much for you since it is adding myMC1 and removing it immediately after that

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 ,
Aug 16, 2012 Aug 16, 2012

Copy link to clipboard

Copied

Here is your code rewriten using the second approach I described...

thumbnail1.addEventListener(MouseEvent.CLICK ,myThumb1);

function myThumb1(evt:MouseEvent):void
{

     while(container.numChildren > 0){
         container.removeChildAt(0);
     }

     var myMC:hover1 = new hover1();
     container.addChild(myMC);
}

thumbnail2.addEventListener(MouseEvent.CLICK ,myThumb2);

function myThumb2(evt:MouseEvent):void
{
     while(container.numChildren > 0){
          container.removeChildAt(0);
     }


     var myMC2:hover2 = new hover2();
     container.addChild(myMC2);
}

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
New Here ,
Aug 16, 2012 Aug 16, 2012

Copy link to clipboard

Copied

Thanks for the fast reply !
Yeah , i kinda got that i am adding the MC1 and then deleting it after .

Your code has helped. Thanks for taking the time to solve it for me

I'm still kinda wondering tho, My lecturer had used the code with an if statement and contains()
do you know how to apply the both of them? 

I

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 ,
Aug 16, 2012 Aug 16, 2012

Copy link to clipboard

Copied

For the code you showed, I would guess you wanted to check if it contains the other other mc (myMC2), not the one you just added because the one you just added is definitely there.  Also, you probably want to specify it for the container since that is where you place the instances, as in...

if(container.contains(myMC2))

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
New Here ,
Aug 16, 2012 Aug 16, 2012

Copy link to clipboard

Copied

Yea, i was using contains to check .
Well originally im going to be making some thumbnails, each thumbnail storing a movieclip . Kinda like a image gallery except im using movie clips .

I was thinking of using contains to check if (myMC1) is still at stage after clicking on thumbnail2,3, or etc , if it is still on stage, removeChild(myMC1) , so that if any other thumbnails were to be clicked on, thumbnail1's movieclip on stage will be removed. That's where im stuck and confused on. Can i ask for your guidance? Thanks and sorry once again for taking up your time .

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 ,
Aug 16, 2012 Aug 16, 2012

Copy link to clipboard

Copied

I think I already provided the guidance you need, you just need to reread what I answered already and think it thru... if you are adding myMC1, which mc do you need to remove (not myMC1, right?)?

If you are having trouble with your assignment I suggest you either work with one of your classmates or your instructor to help resolve your understanding.

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
New Here ,
Aug 16, 2012 Aug 16, 2012

Copy link to clipboard

Copied

LATEST

Why Yes, your right . I think i kind of understand this now. Will reread thru more.

Thanks for the help you provided. and may God bless you

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