drawing bounding box of sprite
-
12-12-2019 - |
Question
In cocos2d-x, how can I display the bounding box for all sprites that are children of this layer?
Here's my starting point:
void MyLayer::draw()
{
// super draw
CCLayer::draw();
// Iterate through all nodes of this layer
for ( CCNode* node = ??? )
{
// Make sure the node is a CCSprite
if ( node == CCSprite ??? )
{
CCSprite* sprite = (CCSprite*) node;
ccDrawRect( sprite->boundingBox() ??? );
}
}
}
Solution
//put this line at the top of your cpp file
#define CC_VERIFY_TYPE(__OBJECT__,__CLASS_TYPE__) assert(dynamic_cast<__CLASS_TYPE__>(__OBJECT__))
//these lines in your code
CCObject* child;
CCARRAY_FOREACH(m_pChildren, child)
{
CC_VERIFY_TYPE(child,CCSprite*);
CCSprite* sprite = (CCSprite*) child;
CCSize s = sprite->boundingBox().size;
ccDrawRect(sprite->boundingBox().origin, ccpAdd(sprite->boundingBox().origin, (ccp(s.width,s.height))));
}
OTHER TIPS
As you create all sprites in this layer by your own, you can, for example, create simple node, that will draw bounding box of its parent. Then just add instance of this node to any node/sprite you want to see.
Another way is to add all sprites/nodes you want to see in additional array and draw bounding box of each object in this array.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow