菜单的几种创建方法

.h

// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);


.cpp

    //创建菜单
    //方法一
    auto label = Label::createWithSystemFont("Jacedy", "Marker Felt", 40);
    auto item1 = MenuItemLabel::create(label, CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    item1->setPosition(Vec2(visibleSize.width*0.9, visibleSize.height*0.8));
     
    //方法二
    auto item2 = MenuItemFont::create("Jacedy", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    item2->setPosition(Vec2(visibleSize.width*0.9, visibleSize.height*0.7));
     
    //方法三
    auto sprite1 = Sprite::create("CloseNormal.png");
    auto sprite2 = Sprite::create("CloseSelected.png");
    auto item3 = MenuItemSprite::create(sprite1, sprite2, CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    item3->setPosition(Vec2(visibleSize.width*0.9, visibleSize.height*0.6));
     
    //方法四
    auto item4 = MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    item4->setPosition(Vec2(visibleSize.width*0.9, visibleSize.width*0.5));
     

// create menu, it's an autorelease object
    auto menu = Menu::create(item1,item2,item3,item4, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);

void HelloWorld::menuCloseCallback(Ref* pSender)
{
    Director::getInstance()->end();
}


你可能感兴趣的:(cocos2d-x)