Search code examples
androidcocos2d-xcocos2d-x-3.0

this->getContentSize().width give me zero response but layer is equal to device width and height


one more problem facing that getContentSize() return zero width and height.

i have created circle using Drawnode and than get its content size but return zero.

My working code is

where rad=100;

DrawNode *drawnode = DrawNode::create();
    for ( int i = 0 ; i <100; i ++)
    {
        float rads = i * M_1_PI; // radians
        Circle [i] .x = rad * cosf (rads); //vertex x
        Circle [i] .y = rad * sinf (rads); //vertex y
    }

    drawnode->setPosition(Director::sharedDirector()->getVisibleSize().width/2,Director::sharedDirector()->getVisibleSize().height/2);
    drawnode->drawPolygon(Circle,100,Color4F(0,0,0,0),1,Color4F(1,122,153,1));

    CCSprite *spr = CCSprite::create(image);
    spr->setPosition(ccp(drawnode->getContentSize().width/2,0));
    drawnode->addChild(spr);
    CCLog("Draw node width : %f",this->getContentSize().width);
    float p = (100/spr->getContentSize().width)+0.5;
    spr->setAnchorPoint(ccp(0,p));
    auto rotate = RotateBy::create(3,360);
    spr->runAction(CCRepeatForever::create(rotate));

    this->addChild(drawnode);

please give me solution for better work.

Thanks in advance Rishabh Shah


Solution

  • Since your node is a container here, therefore your have to calculate content size explicitly. Only Node (Sprite) having texture returns the actual content size otherwise you will get a CCPointZero.

    You have to calculate content on the basis on bounding box of DrawNode and you can easily calculate using circle radius, here is a sample.

    drawNode->setContentSize(CCSizeMake(2*Radius, 2*Radius))