Skip to main content
Participant
June 22, 2013
Answered

How to remove a child from view in actionscript 3.0?

  • June 22, 2013
  • 1 reply
  • 1394 views

I am working with a movieclip labeled tut_mc and inside it i have a button named tut_x_btn. what i want to happen when the X button clicked is to remove the tut_mc from the main timeline. I do not know much about the child commands in Flash. i know that i will have to access the maintimeline with MovieClip(root) command to remove the movieclip. and also how would you do the opposite when a button in clicked adding a child that exists in the library?

This topic has been closed for replies.
Correct answer Ned Murphy

One more proper way to attack this is to have the code for the button in the main timeline, targeting it thru the movieclip...

tut_mc.tut_x_btn.addEventListener(MouseEvent.CLICK, removeTut);

function removeTut(evt:MouseEvent):void {

      removeChild(tut_mc);

}

I would do the same approach for adding a child from the library.

That approach avoids doing what you suggested doing which is not really proper oop implementation.... child objects should not direct the activities of their parent objects -  they should not have to rely on their existence, which target the parent/root does.

If you wanted to go the way you suggested, you could use MovieClip(root).removeChild(tut_mc);  (I'm not sure if you also need to target MovieClip(root).tut_mc for the argument though... been awhile.)  But using root references can be chancy when you start involving separate files.  Sometimes it is better to use parent targets instead.

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
June 22, 2013

One more proper way to attack this is to have the code for the button in the main timeline, targeting it thru the movieclip...

tut_mc.tut_x_btn.addEventListener(MouseEvent.CLICK, removeTut);

function removeTut(evt:MouseEvent):void {

      removeChild(tut_mc);

}

I would do the same approach for adding a child from the library.

That approach avoids doing what you suggested doing which is not really proper oop implementation.... child objects should not direct the activities of their parent objects -  they should not have to rely on their existence, which target the parent/root does.

If you wanted to go the way you suggested, you could use MovieClip(root).removeChild(tut_mc);  (I'm not sure if you also need to target MovieClip(root).tut_mc for the argument though... been awhile.)  But using root references can be chancy when you start involving separate files.  Sometimes it is better to use parent targets instead.

Participant
June 22, 2013

i tried the code in multiple locations, but it is giving me an error when i go to test it its saying access of unidentified property tut_mc when i try to restructure its suggesting i should use MovieClip(root).removeChild(child:DisplayObject):DisplayObject

i am not sure what it is suggesting. i am sure its asking me to fill in the proper areas with the tut_mc but i am not sure about that.

User Unknow
Legend
June 22, 2013

tut_mc.tut_x_btn.addEventListener(MouseEvent.CLICK, removeTut);

function removeTut(evt:MouseEvent):void {

      tut_mc.tut_x_btn.removeEventListener(MouseEvent.CLICK, removeTut);

      MovieClip(tut_mc.parent).removeChild(tut_mc);