触摸开始时更改menuItemLabel颜色

时间:2015-12-10 19:34:16

标签: c++ cocos2d-iphone cocos2d-x

嘿,我无法在任何地方找到答案。我想在触摸时更改menuItemLabel的字体颜色。默认情况下,当您第一次触摸它时,它会缩放和动画更大,但如何更多地自定义它?这是我的标签和menuitem:

//add options label
optionsLabelSettings = Label::createWithTTF("OPTIONS", "fonts/font1.ttf", 140);
optionsLabelSettings->setColor(Color3B(255,255,255));
auto optionsItem = MenuItemLabel::create(optionsLabelSettings, CC_CALLBACK_1(MainMenuScene::GoToOptionsScene, this));
optionsItem->setPosition( Point (visibleSize.width/2  + origin.x, visibleSize.height /2));

//add menu
auto menu = Menu::create(optionsItem, NULL);
menu->setPosition( Point(0,0));
this->addChild(menu);

2 个答案:

答案 0 :(得分:0)

如果要更改字体颜色,可以在处理程序中插入此类方法:

void MainMenuScene::GoToOptionsScene(parameters)
{
optionsItem->getLabel()->setColor(Color3B::BLACK); // or any other color
… // your method of switching to another scene
}

如果在按下后切换到另一个场景,则表示没问题 但是如果你打算留在当前场景,这种方法不适合,因为你无法恢复字体颜色。在这种情况下,更好的方法是使用MenuItemImage并为正常和选定状态创建图像:

MenuItemImage *optionsItem = MenuItemImage::create(«normalImage.png», «selectedImage.png», CC_CALLBACK_1(MainMenuScene::GoToOptionsScene, this));

或者使用ui :: Button,如果你没有图像:

ui::Button* optionsItem = ui::Button::create();
    optionsItem->setPosition(…);
    optionsItem->setTitleText(«OPTIONS»);
    optionsItem->setTitleFontName("fonts/font1.ttf");
    optionsItem->setTitleFontSize(140);
    optionsItem->setTitleColor(Color3B::WHITE);
    optionsItem->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type){
        switch (type)
        {
            case ui::Widget::TouchEventType::BEGAN:
                this->startPressingGoToOptionsScene();
                break;
            case ui::Widget::TouchEventType::ENDED:
                this->finishPressingGoToOptionsScene();
                break;
            default:
                break;
        }
    });
    this->addChild(optionsItem);

然后在每个处理程序中设置不同的按钮行为:

void MainMenuScene::startPressingGoToOptionsScene(parameters)
{
optionsItem->setTitleColor(Color3B::BLACK);
optionsItem->setTitleText(«…») // change anything else
…
}

void MainMenuScene::finishPressingGoToOptionsScene(parameters)
{
optionsItem->setTitleColor(Color3B::WHITE); // return original font color and other changes
…
}

答案 1 :(得分:0)

customise your MenuItemLabel class & Implement following two methods  :-

class ChangedMenuItemLabel : MenuItemLabel
{
bool ChangedMenuItemLabel::initWithLabel(cocos2d::Node *label, const ccMenuCallback &callback)
{
    if (!MenuItemLabel::initWithLabel(label,callback)) {
        return false;
    }
    return true;
}
void selected(){
this->setColor("your color");
}
void unselected(){
this->setColor("your color");//or nothing
}
}