Cocos2d-X遊戲開發(六)

Cocos2d-X遊戲開發

  1. 菜單

菜單Menu是專門用來承載菜單按鈕的Layer圖層,圖層中的子節點只能夠是菜單項MenuItem或其子類。通常先創建菜單項MenuItem,然後使用一個或多個菜單項生成菜單Menu,最後把Menu加入當前Layer圖層。

如果直接在層中添加MenuItem也可以正常顯示,但是無法響應回調函數,因爲Menu是繼承至Layer,也就繼承了觸摸的相關事件,而MenuItem只是從Node繼承而來,並不響應觸摸,因此無法調用回調函數。

  1. 菜單項的子類可以分成三類,總共六個:
  2. 文字菜單項:MenuItemLabelMenuItemFont
  3. MenuItemLabel標籤菜單項:

    //讀取菜單數據

    Dictionary *strings = Dictionary::createWithContentsOfFile("menu.xml");

    constchar *startmenu = ((String*)strings->objectForKey("startmenu"))->getCString();

    //添加一個遊戲菜單

    autolabStartMenu = Label::createWithTTF(startmenu, "fonts/simhei.ttf", 24 * 2);

    //添加標籤菜單,添加回設函數

    autostartItem = MenuItemLabel::create(labStartMenu, this, menu_selector(StartLayer::menuNewGameCallback));

    //設定標記與位置

    startItem->setTag(1);

    startItem->setPosition(Vec2(origin.x + visibleSize.width / 2,

        origin.y + visibleSize.height / 2));

    //創建菜單對象

    Menu* pMenu = Menu::create(startItem, NULL);

    //設定位置

    pMenu->setPosition(Vec2::ZERO);

    //添加菜單到圖層

    this->addChild(pMenu, 1);

結果:

 

  1. MenuItemFont字體菜單項:

    //讀取菜單數據

    Dictionary *strings = Dictionary::createWithContentsOfFile("menu.xml");

    constchar *startmenu = ((String*)strings->objectForKey("startmenu"))->getCString();

    //設置字體大小

    MenuItemFont::setFontSize(64);

    //設置字體

    MenuItemFont::setFontName("simhei");

    //添加一個遊戲菜單項,設定菜單字體大小

    autostartMenuItem = MenuItemFont::create(startmenu, [&](Ref* sender)

    {

        //添加回調

        log("菜單項回調!"); }

    );

    //設置菜單項大小

    startMenuItem->setPosition(Point(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2));

    //創建菜單對象

    Menu* pMenu = Menu::create(startMenuItem, NULL);

    //設定位置

    pMenu->setPosition(Vec2::ZERO);

    //添加菜單到圖層

    this->addChild(pMenu, 1);

結果:

 

 

  1. 圖片菜單項:MenuItemSpriteMenuItemImage
  2. MenuItemSprite精靈菜單項

    autostartNormal = Sprite::create("start.png");

    autostartSelected = Sprite::create("startSelect.png");

    autostartSpriteItem = MenuItemSprite::create(startNormal, startSelected, CC_CALLBACK_1(StartLayer::menuNewGameCallback, this));

    startSpriteItem->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));

    //創建菜單對象

    Menu* pMenu = Menu::create(startSpriteItem, NULL);

    //設定位置

    pMenu->setPosition(Vec2::ZERO);

    //添加菜單到圖層

    this->addChild(pMenu, 1);

結果:

  1. MenuItemImage圖像菜單項

    autostartNormal = MenuItemImage::create("start.png", "startSelect.png", CC_CALLBACK_1(StartLayer::menuNewGameCallback, this));

    startNormal->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));

    //創建菜單對象

    Menu* pMenu = Menu::create(startNormal, NULL);

    //設定位置

    pMenu->setPosition(Vec2::ZERO);

    //添加菜單到圖層

    this->addChild(pMenu, 1);

  1. 觸發器菜單項:MenuItemToggle
  2. MenuItemToggle觸發器菜單項

    //讀取菜單數據

    Dictionary *strings = Dictionary::createWithContentsOfFile("menu.xml");

    constchar *startmenu = ((String*)strings->objectForKey("startmenu"))->getCString();

    constchar *gamequit = ((String*)strings->objectForKey("gamequit"))->getCString();

    //設置字體大小

    MenuItemFont::setFontSize(64);

    //設置字體

    MenuItemFont::setFontName("simhei");

    //創建觸發器按鈕,添加內容與回調

    autotoggleItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(StartLayer::menuNewGameCallback, this), MenuItemFont::create(startmenu), MenuItemFont::create(gamequit), NULL);

    toggleItem->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));

    //創建菜單對象

    Menu* pMenu = Menu::create(toggleItem, NULL);

    //設定位置

    pMenu->setPosition(Vec2::ZERO);

    //添加菜單到圖層

    this->addChild(pMenu, 1);

  1. 回調函數:

/*菜單回調方法*/

voidStartLayer::menuNewGameCallback(Ref* pSender)

{

    intvalue = dynamic_cast<MenuItemToggle*>(pSender)->getSelectedIndex();

    log("%d", value);

}

  1. 結果:

發佈了39 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章