ActionScript 3.0 Argument Error:2025 (Remove Child Problem)
-
30-09-2020 - |
Question
I am encountering the following error message whenever I compile my project in Adobe Flash CS4:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at stageRotation/spawnParticle()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
the code that generates the error is shown below:
for (var i:int = 0; i < particleArrayForward.length; i++ ) {
if (particleArrayForward[i] != null) {
trace("particleArrayForward[" + i + "]:" + particleArrayForward[i]);
this.removeChild(particleArrayForward[i]);
}
}
Any input appreciated. Thanks. :)
Solution
removeChild
throws this error when the passed argument is not a child of the parent that called the method. Are particles added as child to another sub-container within this
object?
Make sure it is indeed a child of the caller:
for (var i:int = 0; i < particleArrayForward.length; i++ ) {
if (particleArrayForward[i] != null && particleArrayForward[i].parent == this) {
trace("particleArrayForward[" + i + "]:" + particleArrayForward[i]);
this.removeChild(particleArrayForward[i]);
}
}
If the particles are not direct children of this
object, you can remove them using:
for (var i:int = 0; i < particleArrayForward.length; i++ ) {
if (particleArrayForward[i] != null && particleArrayForward[i].parent != null) {
trace("particle at " + i + " " + particleArrayForward[i]);
trace("parent is " + particleArrayForward[i].parent);
particleArrayForward[i].parent.removeChild(particleArrayForward[i]);
}
}
OTHER TIPS
You are removing a child from display object by looping through all the particles in the array. However I can not see where you then remove the reference to the child in the array itself. So if you loop through particleArrayFoward again you will be trying to remove a display object that was already removed which I am going to assume is happening?
for (var i:int = 0; i < particleArrayForward.length; i++ ) {
if (particleArrayForward[i] != null) {
trace("particleArrayForward[" + i + "]:" + particleArrayForward[i]);
this.removeChild(particleArrayForward[i]);
particleArrayForward[i]=null;//this will fix it but now the length of array will never shrink
}
}
so better yet:
for (var i:int = 0; i < particleArrayForward.length; i++ ) {
if (particleArrayForward[i] != null) {
trace("particleArrayForward[" + i + "]:" + particleArrayForward[i]);
this.removeChild(particleArrayForward[i]);
}
}
particleArrayForward = new Array();
//or particleArrayForward.length = 0;
otherwise if you are not looping through that array again then somewhere you are adding a child to the array that is not a child of the display object of which you are trying to remove from.