Cocos 2dx - CCDrawNode boundingbox.size总是{0,0}

时间:2014-06-23 06:32:41

标签: c++ cocos2d-x

我是cocos 2dx的新手。

我们如何找到CCDrawNode的边界框?它始终显示"在poly"之外。

我的代码是:

  void HelloWorld :: drawAlpha(){  
     CCPoint points[25] = {ccp(435,190), ccp(360, 200), ccp(315, 135), ccp(289,100), ccp(206,100),ccp(160,150),ccp(145,215),ccp(155,290),ccp(168,340),ccp(225,370),ccp(285,370),ccp(320,340),ccp(350,260),ccp(445,280),ccp(420,390),ccp(340,470),ccp(219,470),ccp(120,450),ccp(40,334),ccp(30,200),ccp(65,90),ccp(150,5),ccp(202,5),ccp(346,5),ccp(430,90)};

       CCDrawNode* polygon = CCDrawNode :: create();
        polygon -> drawPolygon(points, 25, ccc4f(1,0,0,0.5), 6.0, ccc4f(0,0,1,1));
        polygon -> setPosition(ccp( 300, 300));
        CCLOG("Width : %f", polygon -> boundingBox().size.width);
        this -> addChild(polygon);
    }

void HelloWorld:: ccTouchesBegan(CCSet* pTouches, CCEvent* event){
    touchPosition = (CCTouch*) pTouches -> anyObject();
    touchLocation = touchPosition -> getLocation();


    if(polygon -> boundingBox().containsPoint(touchLocation)){
        CCLOG("inside poly");
    }
    else{
        CCLOG("outside poly");
    }
}

1 个答案:

答案 0 :(得分:0)

除非您手动设置,否则它将始终返回空白大小。

由于Node是一个简单的容器,因此它不会自动计算边界框。你必须从点数组中找到min x,min y和max x和max y,然后你可以很容易地计算出边界矩形。