removeChild() on parent will remove its child? - actionscript-3

I have a construction as below:
parent
+ child
+ child
+ child
+ sub-parent
+ sub-child
+ sub-child
For now, I use multiply of "removeChild()", to remove elements from scene one by one. Like this:
removeChild(sub-child);
removeChild(sub-child);
removeChild(sub-parent);
//and so on
It's okay, but I found out that if I remove a parent all its children will disappear from scene too. (For now I do not know for sure ...)
How do you remove elements from a scene in the correct way ? As I do it now (one by one), or I could remove just a parent and my code will be a little bit shorter. Or is it the same thing?

Removing a display object from the stage will also remove all of that object's children. Think of it as a container that holds those children object. If you remove the container, you also remove the objects inside of it.
However, if you still have references to those children objects, or have event listeners attached to them, they won't be garbage collected (they will stick around in memory executing any code associated with them). So you still need to make sure you clean up everything when you remove the parent object.

The "display list" is a tree structure that looks like this:
When you add children to a container (DisplayObjectContainer), those children will remain as children of that container unless they are specifically removed from it.
When a container or DisplayObject is attached to the stage, they will render. If the object is a container, all of its descendants (children, children of children and so on) will also render.
If you remove an object from the stage, it and all of its descendants will stop rendering but their existing parent/child relationships remain in tact. This means if you add a container with children to the stage again, all of those children will begin to render again as well.
So to more accurately answer your question: removing a container does not actually remove its children. The children will not have a connection to the stage and will not render, but they are still children of the container.
With all that said, there is not often a reason that you would need to remove each individual child from a container. So long as the children do not have event listeners attached or are not referenced by the main application in any other way, they will all be eligible for garbage collection when their connection to the stage is severed. Removing the topmost possible container from the stage is perfectly normal.

You can remove the "sub-parent" and its child's will be removed as well from the scene (stage).

Related

Container in Construct 2 doesn't make all objects visible

I thought that the action you apply to one object of a container is automatically applied to every object in the container, but this doesn't seem to be the case in my project:
By making 1_br_ok_e visible, I would axpect the other two objects in the container to be made visible too, but nothing happens, only 1_br_ok_e becomes visible.
What am I missing?
No this isn't how containers work.
I am quoting from the Scirra documentation (https://www.scirra.com/manual/150/containers):
Placing objects in a container has the following effects:
If one object in a container is created, every other object in its container is also automatically created.
If one object in a container is destroyed, every other associated object in its container is also destroyed.
If a condition picks one object in a container, every other associated object in its container is also picked.
And that's it. Nothing else should be expected from containers.
In your case, destroying the object (and consecutively the whole container) could be an option if you never need them to reappear.
To support Scirra on this, I want to add that maybe in your case this could be a positive feature, but in most cases it's not a desirable functionality. Imagine creating a tank container where you have the tank's body and its turret. Rotating the turret would make the whole tank rotate, since the body would share the same action. You would have no way to make this work.

parent-child depth will change in actionscript

Please have a look at this project, in this project I have two symbol, one of them create and add another symbol as child.
Child symbol is drawn over parent as I expected. Parent symbol has two frames. When parent goes to second frame, child symbol drawn under parent. I mean depth order will change.
Can somebody help me!!?? Sorry for poor language.
Download link:
https://drive.google.com/file/d/0B-KCX3wxRH-cOUk5YU1OUzNFN3M/view?usp=sharing
Yellow shape from the frame 1 move to the new layer.
Hide Layer 3 and go to the second frame.
Select and remove the yellow bitmap image.
Show Layer 3.
Another solution:
Add next code after gotoAndStop(2);
swapChildren(btselect,getChildAt(numChildren - 1));
You misunderstand completely how the Flash rendering works so here's a couple of pointers:
"Child symbol is drawn over parent as I expected", nope, that doesn't happen. A parent has content which can be pictures, other symbols ect ... and they are all inside the parent so no child are drawn over the parent, they have a display hierarchy that's all.
"child symbol drawn under parent." well once again nope, a child is not drawn under it's parent, it is inside the parent and under other objects.
When a MovieClip with multiple frame displays the content of a frame it adds those content to it's display list (inside itself). After that if you add another child in it it will be on top. Then you move to another frame let's say frame 2. Once again the MovieClip adds the content of frame 2 inside itself, but since the child you added in the previous frame is already there all the new content is added on top of it.

Cocos2d-x: should all the added children be removed MANUALLY when deallocing a CCNode object

The case is that I have a CCLayer created with cocosBuilder, and there are children added either programmatically or initially using the builder.
I am not sure if the children would be released correctly if I do not remove all of them manually.
You do not need to remove them manually. Each node removes its children (empties the children array) when it deallocates.
But I think we should remove them if we are adding them to the main game layer very frequently . As with time there will be lots of them waiting to be removed,which may increase memory usage.

Show 'expand' control on JTree nodes after children are removed?

I have a DefaultTreeModel containing a subclass of DefaultMutableTreeNode. I have only overridden isLeaf() to always return true because I lazily load the children when the node is expanded. Then, when the node is collapsed, I remove the children (firing the proper treeNodesRemoved event) because I have unsubscribed from updates from the server.
The problem is that after the user collapses a node and I remove the children, the stupid little expand circle disappears (but clicking that area still works to expand the node). How can I always show the expand control when the children have been removed?
Related: Add 'expand' button to JTree node that has no children?. Is adding a fake child the only way?
The way I did it is I add a fake child and expansion listener when children are removed. When I get notification that the node with fake child is going to be expanded I replace the fake child with actual lazily loaded children.
This way the node always has children and expand control is always presented

AS3 Do I need remove childs, if I remove the parent itseft?

In flash AS3 Do I need remove childs, if I remove the parent itseft?
If I remove childs first, then remove the parent object afterall
OR
If I just remove parent object
Will flash take same memory?
Well, if the children contains no reference variable or all children reference variable are set to null then removing the parent and setting the parent reference to null, will remove the children from memory also.But you have to manually remove listener attached to them. Using weak event listeners is pref-able in most situations as they are removed automatically when the objects or movie-clips are deleted by garbage collector, so u do not have to remove them manually.
here is little example help u understand
var obj = new MainContainer();
obj.addEventListener(MouseEvent.CLICK, MainContainer_Clicked)
addChild(Obj)
Now removing the Obj using removeChild(obj) will remove it from stage but not from memory. you have to set obj=null. And this goes same for MainContainer Children if it has. Now you have to manually remove the event listener attached to obj in this way
obj.removeEventListener(MouseEvent.CLICK, MainContainer_Clicked)
OR use weak event listener if you do not want it to remove yourself like this
obj.addEventListener(MouseEvent.CLICK, MainContainer_Clicked,false,0,true)
Read more about weak event listener here
http://gskinner.com/blog/archives/2006/07/as3_weakly_refe.html