Skip to main content
iezqandarzulqarnaen
Known Participant
December 21, 2017
Answered

One Button to addChild & removeChild

  • December 21, 2017
  • 6 replies
  • 1766 views

Hi all,

Can anyone show me the proper code to addChild (from lib) & removeChild using only one button.

Seem that my code not working

button_btn.addEventListener(MouseEvent.CLICK, button_mc_onPress);

function button_mc_onPress(e: MouseEvent): void

{

    var popup:popup_class = new popup_class();

    addChild(popup);

    button_btn.removeEventListener(MouseEvent.CLICK, button_mc_onPress);

}

button_btn.addEventListener(MouseEvent.CLICK, btn_delete_onPress);

function btn_delete_onPress(e:MouseEvent) : void

{

    removeChild(popup);

    button_btn.removeEventListener(MouseEvent.CLICK, btn_delete_onPress);

}

& got error "1120: Access of undefined property popup."

I know it's because the "popup" is not yet exist, then i add :

var box_popup:MovieClip;

on top of the codes, the error msg is gone, when i click "button_btn" the "popup" is appear but when I click again to remove the called "popup", its still there.

Thank you.

cc: kglad

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer JoãoCésar17023019

Hi again!

It's nice to know that our code helped you.

About your FLA, to get it working:

- Inside of your 'cnc_stone' object, there is a layer with code called 'flash0.ai'. Move it to the top of the stack. Somehow, because of code execution order, I guess, the code in it it's not being called.

- Inside of your 'infoCarving' object, there is a stop() function in the first frame. Remove it.

Now it should work.

Don't hesitate to ask if you have any further questions.

Regards,

JC

6 replies

kglad
Community Expert
Community Expert
December 21, 2017

you can do that a little more efficiently:

var popup:popup_class = new popup_class();

button_btn.addEventListener(MouseEvent.CLICK, button_mc_onPress);

function button_mc_onPress(e: MouseEvent): void

{

if(popup.stage){

removeChild(popup)

} else

    addChild(popup);

}

}

iezqandarzulqarnaen
Known Participant
December 22, 2017

Hi kglad​ & JC,

Thanks for the code, I've apply it to the new AS3 document & worked, but when I apply it into my existed document it's not working.

I have upload the .fla here Dropbox - levelOne_CNC.fla. can you help me look what is wrong with it.

Once you test the .fla click the "CNC Machining Centre" then a info sheet will popup (an addchild) then I want to add another popup (an addChild) beside the existing info sheet by click the red circle with "i" in it. Tried your & JC code but no luck.

Sorry for the trouble.

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
December 22, 2017

Hi again!

It's nice to know that our code helped you.

About your FLA, to get it working:

- Inside of your 'cnc_stone' object, there is a layer with code called 'flash0.ai'. Move it to the top of the stack. Somehow, because of code execution order, I guess, the code in it it's not being called.

- Inside of your 'infoCarving' object, there is a stop() function in the first frame. Remove it.

Now it should work.

Don't hesitate to ask if you have any further questions.

Regards,

JC

JoãoCésar17023019
Community Expert
Community Expert
December 21, 2017

Hi.

Declare your popup variable outside the function definition. Another thing is that you are adding two event handlers to same object that get fired one after the other. So you add your object from Library and remove it immediately after.

One possible solution is this:

import flash.events.MouseEvent;

var popup:popup_class;

function clickHandler(e:MouseEvent): void

{

    if (popup)

    {

        removeChild(popup);

        popup = null;

    }

    else

    {

        popup = new popup_class();

        addChild(popup);

    }

}

button_btn.addEventListener(MouseEvent.CLICK, clickHandler);

Regards,

JC