1

在 cocos2d-x 中,如何显示作为该层子级的所有精灵的边界框?

这是我的出发点:

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

    }
}
4

2 回答 2

1
    //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))));

    }
于 2012-10-02T23:15:25.220 回答
-1

当您自己创建该层中的所有精灵时,例如,您可以创建简单的节点,该节点将绘制其父级的边界框。然后只需将此节点的实例添加到您想要查看的任何节点/精灵。

另一种方法是在附加数组中添加您想要查看的所有精灵/节点,并在此数组中绘制每个对象的边界框。

于 2012-10-02T21:07:17.490 回答