Search code examples
cocos2d-iphonecocos2d-x

drawing bounding box of sprite


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))));
    
        }