actionscript3: removeChild and addChild - actionscript-3

I add a child to stage, and i need my code trace if this child present in the stage and remove this child from this position to add it in another position in the stage
Please help me

I THINK you're asking how to move a child from one place in the display list to another. If so, you do not need to remove the child first; When you addChild to the new 'location' the child will be removed from the old location (a child can only exist in one place on the display list). That's assuming you don't need the child to disappear for some period of time before reappearing in the new location.
Alternatively you can check if a display object contains another with this.contains(referenceToChild);

A child is on the stage if its parent property is not null:
trace( child.parent !== null ); // True if the child is on the stage.
For positioning you just need to adjust x and y.

Related

removeChild() on parent will remove its child?

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

How to insert/place a movie clip from the library in a specific layer (z-order) in AS3?

I haven't found anything about this and it's really intriguing me. Here's an example:
var example1:example1_mc = new example1_mc();
addChild(example1);
It comes on top of all layers in my scene and I wanted to assign it to the back.
To put an item at the back most z-index of it's parent, do the following:
addChildAt(example1,0);
//the second parameter is the z-index to put the child at
//0 is the back, parent.numChildren-1 is the front most
//so this:
addChild(example1);
//is the same as this:
addChildAt(example1, numChildren-1);
addChild always places the child at the front.
To change the position of an item later, you can either addChild/addChildAt again, or use setChildIndex(example1,0).
So this:
addChildAt(example1,0); //add it to the back
setChildIndex(example1,2); //move it above the next 2 items
is the exact* same as this:
addChildAt(example1,0);
addChildAt(example1,2); //since example1 is already added, this just moves it above the next 2 items in the parent (it doesn't add a copy)
addChild will dispatch an Event.ADDED event on the item you added the child to. setChildIndex will not result in that event being dispatched.

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.

Can I add several sprite children with the same index?

I want 2 sprites on the one index position. The sprites have different coordinates (x attributes), so, each of them are visible when the child position is active.
Clarification, the code:
addChildAt( sprite1, 1 );
addChildAt( sprite2, 1 );
Are both of the sprites visible?
The documentation gives you the answer of what will happen with your snippet.
index:int — The index position to which the child is added. If you specify a currently occupied index position, the child object that exists at that position and all higher positions are moved up one position in the child list.
Two objects can't share the same index because it wouldn't make sense. How would you display two overlapping objects at the same index?
No, you can't. But you can add extra sprite between parent and children to easily control visibility or whatever.
Sample:
parentSprite.addChild(extra)
extra.addChildAt( sprite1, 1 );
extra.addChildAt( sprite2, 2 );

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