Reputation: 64
function addEffect()
{
var thisEffect = new MyEffect; //Simple movieClip
addChild(thisEffect);
effectArray[0] = thisEffect;
}
the above works fine. And later I remove it... The Below works fine also.
function removeEffect()
{
if(effectArray[0] != null)
{
removeChild(effectArray[0]);
}
}
However, after I use the functions again, somtimes the next turn, sometimes two turns later I get: Error #2025: The supplied DisplayObject must be a child of the caller. Oddly, I'm using the same technique to add and remove other movieclips, and it is working fine for everything else. I am not referencing the effectArray or anything inside it, outside of these two functions, which are both inside Main.as
Upvotes: 0
Views: 44
Reputation: 3190
That's quite normal. When you add myEffect to array, only a reference to myEffect is stored in the array. And when you try to reach that object by array, everything works correct, and reference to myEffect in array points to object correctly each time.
But what is not going right each time is whether there is an object where the reference points or not. If it has deleted, it cannot be child of the target object no more, so you'll get that error.
What you do in removeEffect function is to check if the reference is there, not the object. And reference is always there if you do not remove it from your array. After you remove the object, remove the reference also. And everything will work correct.
function removeEffect()
{
if(effectArray[0] != null)
{
removeChild(effectArray[0]);
effectArray[0] = null;
}
}
Upvotes: 2