Reputation: 363
I'm using Cocos2d-x and I'm just looking for efficient way to remove sprites under some rectangular region.
For example if I have a lot of random located sprites on the scene and I want to remove all of them if they belong to rectangular (x1, y1),(x2, y2), then what I need to do?
I see that there are two ways to remove sprite from the scene:
this->removeChildByTag(tag);
or
sprite->removeFromParent();
So from these methods it seems that we need somehow to find which sprites are located inside the area and after that delete them.
But what is the most efficient way to do it?
Thanks!
Upvotes: 1
Views: 155
Reputation: 5675
The only way to do this using default cocos functionality would be to iterate over each child detect overlapping and remove nodes that match criteria. removeFromParent()
as well as removeChildByTag()
will invoke parent->removeChild(this);
. And removeChild()
uses std::find
in Vector
of child nodes. With complexity O(n). So first step of optimization would be to use detachChild
that utilizes index of child.
But if that is not fast enough I would recommend using special data structure to quickly search for overlapping like interval tree.
Upvotes: 1