Reputation: 4212
I am very much new to ActionScript.
What I am trying to do is adding bitmap to sprite object like this -
testSprite.addChild(myBitmap);
This gets called many times and each time myBitmap may have different bitmapData. It works fine up-till here. Then I try to store myBitmap in a global array each time its added to sprite.
BitmapArray.push(myBitmap);
In another function, I want to remove all these bitmaps, which I am doing it this way -
for each(var currentBitmap:Bitmap in BitmapArray)
{
testSprite.removeChild(currentBitmap);
}
However, I am getting this error -
The supplied DisplayObject must be a child of the caller
Can anybody tell me what wrong I am doing here ?
Upvotes: 0
Views: 443
Reputation: 6751
Your array is not likely in sync with the display list of your testSprite.
If you are adding a bitmap to the display list of testSprite and pushing it into the BitmapArray, you must also remove it from the BitmapArray when you are removing it from the display list of testSprite.
It is true that you can check if it's contained by the parent when removing , and it's also true that you specify to only push the bitmap into the array if it doesn't already exist in the array.
However, it's really best to follow the simple practice of adding it to the array when adding to the display list, and removing it from the array when it's removed from the display list.
For example, what if there are other things that you are doing with these objects that 'assume' it's on the display list ? Are you going to add code in those areas to check if it's REALLY on the display list or not, or if there is a duplicate in the array ?
That is a recipe for headaches. So while the other answers do indeed solve the immediate problem, it's really just a recipe for for bad practice and far larger headaches.
Upvotes: 0
Reputation: 604
The error refers to the fact that the particular currentBitmap
you're trying to remove from testSprite
is not contained in testSprite
at that moment.
Without seeing more of the code, this could be caused by a number of things:
testSprite
, but subsequently added to a different container, removing it from testSprite
in the processBitmapArray
, meaning that in the for each
loop there's more than one attempt to remove it (and only the first will succeed).One quick fix is to check that the currentBitmap
is a child of testSprite
before each iteration in the loop like so:
for each(var currentBitmap:Bitmap in BitmapArray)
{
if (testSprite.contains(currentBitmap))
{
testSprite.removeChild(currentBitmap);
}
}
So that should circumvent the issue - but it might make any subsequent work easier if you try to solve the problem at the source, if you're expecting that all of the members of BitmapArray
are children of testSprite
.
Upvotes: 2
Reputation: 2101
As you said, you may add myBitmap to BitmapArray more than once. So when you remove child in the loop, assume myBitmap appear at index 0 and index 1. After You remove myBitmap at index 0 and try to remove it in index 1, it will give you an error because myBitmap isn't a child of testSprite any more.
testSprite = [myBitmap, myBitmap].
So there two solutions
First
When you add item to BitmapArray, check if the item is existed in the BitmapArray with indexOf function.
BitmapArray.indexOf(item)
Second
When you remove child from testSprite, check if testSprite contains the child
if (testSprite.contains(currentBitmap)) {
testSprite.removeChild(currentBitmap);
}
Upvotes: 0