View Full Version : Checking if a Sprite/Movieclip has any children
Striker9099
10-16-2009, 04:00 PM
Hey there, I was wondering how do you check whether a certain mc or sprite has any children inside it... does the following do the trick?
[as]
if (mc.length > 0)
{
trace ("mc has children");
} else
{
trace ("mc doesn't have any children")
}
senocular
10-16-2009, 04:06 PM
check the numChildren property
http://livedocs.adobe.com/flex/3/langref/flash/display/DisplayObjectContainer.html#numChildren
Striker9099
10-16-2009, 04:18 PM
check the numChildren property
http://livedocs.adobe.com/flex/3/langref/flash/display/DisplayObjectContainer.html#numChildren
aha so I guess the following should do it
if (mc.length > 0)
{
trace ("mc has children");
} else
{
trace ("mc doesn't have any children")
}
P.S: the reason I want to check if there are any children already inside the movieclip is because I only want ONE child in there... and if there's any other children inside I want to remove them and THEN add the new child...
TomMalufe
10-16-2009, 04:25 PM
It's more like this:
if(mc.numChildren > 0)
{
trace("mc has children, so think twice about marrying her.");
}
else
{
trace("mc does not have any kids... your good to go!");
}
There is also the contains() method which will return true or false if mc.contains(somechildMC);
senocular
10-16-2009, 04:26 PM
Note that contains() also accounds for grandchildren, not direct children. For direct children, assuming you have a reference to the child, you can check child.parent == mc
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.