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

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

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

Disable mouse event for a specific child of a movieclip

I've a MovieClip that contains lot of children.
One of them is a big (and useless) shadow the graphic designer put there to make my life harder (and also the user's one, probably)
:)
Now I'm facing a little issue: the shadow is catching the MouseEvents attached to the main MovieClip (its parent), and this is very bad because it's very distant from the rest of the graphic. I'm now trying to avoid it.
Obviously I've already tried to set the shadow mc's properties mouseEnabled and mouseChildren to false, but it doesn't work.
I've found a previous thread (here), facing the same situation. But the solution accepted looks like it's not working for me.
What I am missing?
If mouseEnabled and mouseChildren is not working for you then use e.target.name property.
But first you will have to give a name to that shadow MovieClip (say shadowMC).
If you added it dynamically then use,
yourDynamicMC.name = "shadowMC"
If added manually on the stage then give instance name as "shadowMC",
Then, inside your code where you have MouseEvent function for parent MovieClip add the following lines
if(e.target.name != "shadowMC")
{
//Then proceed
}
Reading your question, I am assuming you already tried to set the parent MovieClip's mouseChildren to true with mouseEnabled to false and then set the children's mouseEnabled to true (except for the shadow). This solution should work in my opinion so I am guessing the event might be caught by one parent of your movieclip (you do not give much information about this).
Try to add a listener to the stage to see which object is receiving your MouseEvents:
import flash.utils.getQualifiedClassName;
stage.addEventListener(MouseEvent.CLICK, onClick);
private function onClick(event:MouseEvent):void
{
trace(event.target.name, getQualifiedClassName(event.target));
}
Try changing the hit area of your MovieClip. see the MovieClip doc here, this will override you MovieClip zone.

actionscript3: removeChild and addChild

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.

Children of Children of MovieClip not showing up when MovieClip is added to stage?

This problem seems very simple to me, but I've been unable to fix it, or find an answer anywhere.
This is in a class constructor for a class called block, block_maker is the object that called the constructor, an instance of level.
this.bitmap = new Bitmap(this.bitmap_data);
this.addChild(this.bitmap);
this.block_maker.stage_foreground.addChild(this);
In level, stage_foreground is added to the stage, but nothing appears. trace(stage_foreground.numChildren); shows the correct count of children, and var temp = (this.stage_foreground.getChildAt(0)); trace(temp.numChildren); correctly but the children OF the children don't actually show up, the stage just stays blank.
When I change the above code to
this.bitmap = new Bitmap(this.bitmap_data);
this.block_maker.stage_foreground.addChild(this.bitmap);
the blocks appear on the stage, as children of level_instance.stage_foreground, but with this method, the bitmaps aren't appropriately positioned, as they have no position data. I can simply give this.bitmap x and y positions, and it works, but I am curious as to why it won't work when just adding the bitmap as a child to the block and then adding that as a child to stage_foreground.
I've tried replacing this.bitmap with a number of other object classes, such as a temporary MovieClip I made, or a Shape, but nothing shows up, so I know it has nothing to do with it being a Bitmap.
As you stated
This is in a class constructor for a class called block, block_maker
is the object that called the constructor, an instance of level.
this.bitmap = new Bitmap(this.bitmap_data);
this.addChild(this.bitmap);
this.block_maker.stage_foreground.addChild(this);
The level class needs to to extend a display object for it to show up.
In other words
"this"
has to be a display object or extend it in some form.
The reason this doesn't work.
this.block_maker.stage_foreground.addChild(this);
and this does
this.block_maker.stage_foreground.addChild(this.bitmap);
Is because "this" is not a display object but "this.bitmap" is.
The DisplayObject class is the base class for all objects that can be placed on the display list.
First of all, even though it's not neccesary, I'd suggest to make a classname start with a capital letter, and vars always start with a lower-case letter. It's a common 'rule' in AS3 that's mainly to avoid confusion.
The problem most likely lies within you use of this; what you're doing is adding this into this. It seems to me it's not a proper way of coding.
Also, since all of the other attempts didn't work; have you tried to make the var you want ot add a public static var?

AS3:how to remove textfield from stage?

I've a problem with actionscript 3.
On the Stage i've a texfield named "text0", when i attempt to remove it throught AS3 writing removeChild(text0), flash give me an error(Error #2025). I always used this syntax for remove MovieClip symbols, why it seems not work for textfields?
Or you can try:
text0.parent.removeChild(text0);
You need to call removeChild on the parent of the object that is being removed.
If that isn't clear, maybe post a bit more detailed code.
The error signals that you are trying to remove a DisplayObject with removeChild that apparently is not a child of the DisplayObjectContainer this code is executed from.
One way to solve this problem is to check if the object you are trying to remove is actually a child of the container using contains. You are doing this for some of the objects you are removing (submitButton and saveinfoButton), but not for some others.
Try wrapping the removeChild calls for txt to check whether these DisplayObjects are in the container.
It seems you are trying to remove your textfield from a container that don't have your textfield. Try tracing the parent to make sure that you are doing fine and you are targeting the right parent.
trace(text0.parent);
You can also post some code so we can help you with something particular and not just giving you theories.
You need to remove the textField from its parent. If it is in a movieClip called myMovieClip, and your code is on the main timeline, do myMovieClip.removeChild
If the textField is on the main stage, then on the main timeline write this.removeChild
EDIT
If the code and textField are at the same level, then this.removeChild