きょこみのーと

技術に関係ないほうのブログ

Cocos2d-xの選択したMenuItemだけ色をつけて強調する

Tips系です。

Cocos2d-x3.0で検証しました。

こんな感じに、Menu内で最後に選択したMenuItemだけ強調したときに使います。

f:id:kyokomi:20140503205441p:plain

ソースコード(MenuItemUtil.h)

使い方

#include "MenuItemUtil.h"

// ... 省略 ...

void initHogeMenu() {
    cocos2d::Vector<MenuItem*> menuItemArray;
    for (int i = 0; i < 2; i++) {
        auto menuItemLabel = MenuItemLabel::create(Label::createWithSystemFont("hogehoge", "Arial", 32), [](Ref *ref) {
            // ★★★ こんな感じでメニュー押したときのコールバックにいれる ★★★
            MenuItemUtil::touchItemRefreshColor(ref, Color3B::GREEN);
            
            // 〜 メニューおした時の処理 〜
        });
        menuItemArray.pushBack(menuItemLabel);
    }
    auto menu = Menu::createWithArray(menuItemArray);
    menu->alignItemsHorizontallyWithPadding(5);
    this->addChild(menu);
}