Alexander Ovchinnikov Alexander Ovchinnikov - 1 year ago 148
C++ Question

Cocos2d-x Remove sprites in specified rectangular area

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:




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?


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download