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

One Button to addChild & removeChild

Community Beginner ,
Dec 21, 2017 Dec 21, 2017

Copy link to clipboard

Copied

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

Views

1.3K

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

Community Expert , Dec 21, 2017 Dec 21, 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

Votes

Translate

Translate
Community Expert ,
Dec 21, 2017 Dec 21, 2017

Copy link to clipboard

Copied

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

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
Community Expert ,
Dec 21, 2017 Dec 21, 2017

Copy link to clipboard

Copied

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

}

}

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
Community Beginner ,
Dec 21, 2017 Dec 21, 2017

Copy link to clipboard

Copied

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.

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
Community Expert ,
Dec 21, 2017 Dec 21, 2017

Copy link to clipboard

Copied

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

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
Community Beginner ,
Dec 21, 2017 Dec 21, 2017

Copy link to clipboard

Copied

Dear JC,

I've move the infoCarving_btn button to other layer along with the scrip to the top & voilà! it work!

Thank you very much.

I have another question, will cc you & hope you dont mind.

Thanks again JC & kglad​ both of your code is working.

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
Community Expert ,
Dec 22, 2017 Dec 22, 2017

Copy link to clipboard

Copied

LATEST

Nice!

You're welcome!

And you can send your questions. No problem. I'll be happy to help.

Regards,

JC

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