带有CCDrawNode的CCMenuItem不起作用

时间:2014-11-18 17:47:13

标签: android c++ cocos2d-x cocos2d-x-3.0

我试图创建一个带有CCDrawNode作为孩子的CCMenuItem,但是它不起作用,它被画了但是它在点击时没有做任何事情。

这里是创建按钮的代码:

    CCSprite* normalState = CCSprite::create();
    normalState->addChild(this->createButton( ccp(0,0), ccc4f(0.74, 0.90, 0.96, 1.0)));

    CCSprite* selectedState = CCSprite::create();
    selectedState->addChild(this->createButton( ccp(0,0), ccc4f(0.74, 0.90, 0.16, 1.0)));
    CCMenuItem* playDemo =  CCMenuItemSprite::create(   normalState
                                                        ,selectedState
                                                        ,this
                                                        ,menu_selector(InitView::goToLevelsView));



    playDemo->setPosition(ccp(0, 200 ));

    CCLabelTTF *mText = CCLabelTTF::create("Play!!!", "fonts/soopafre.ttf", SCOREBOARD_FONT_SIZE);
    mText->setPosition(ccp( playDemo->getContentSize().width/2 ,
                            playDemo->getContentSize().height/2));

    playDemo->addChild(mText);


    CCMenu* pMenu2 = CCMenu::create(playDemo);
    this->addChild(pMenu2, 1);

要创建NodeDraw,我有以下代码:

CCDrawNode* InitView::createButton( CCPoint position, ccColor4F color){

    CCDrawNode *circle = CCDrawNode::create();

    float radius = 50;
    float aux = 1;

    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    int menuWidth = visibleSize.width/2;

    CCPoint rectangle[4];
    rectangle[0] = ccp( position.x - menuWidth/2, position.y - radius );    //Left-Bottom
    rectangle[1] = ccp( position.x + menuWidth/2, position.y - radius );    //Right-Bottom
    rectangle[2] = ccp( position.x + menuWidth/2, position.y + radius );    //Right-Top
    rectangle[3] = ccp( position.x - menuWidth/2, position.y + radius );    //Left-Top

    circle->drawPolygon(rectangle, 4, color, 1, color);

    float angle = M_PI/2;
    for (; angle <= M_PI + M_PI/2; angle += 0.01)
    {

        circle->drawSegment(ccp(position.x - menuWidth/2, position.y),
                              ccp(position.x - menuWidth/2 + radius * cos(angle), position.y + radius * sin(angle)),
                              aux,
                              color);
    }

    for (; angle <= 2 * M_PI + M_PI/2; angle += 0.01)
    {

        circle->drawSegment(ccp(position.x+menuWidth/2, position.y),
                              ccp(position.x+menuWidth/2 + radius * cos(angle), position.y + radius * sin(angle)),
                              aux,
                              color);
    }

    return circle;
}

1 个答案:

答案 0 :(得分:0)

我解决了! 接缝它没有得到DrawNode的大小所以使它工作我设置内容大小:

selectedState->setContentSize( CCSize(width, height) );