Cocos2d-x 3.17 版本
// 创建按钮:类型 - 1
{
Sprite *spr1 = Sprite::createWithSpriteFrameName(FRAME_MM_PLAY);
Sprite *spr2 = Sprite::createWithSpriteFrameName(FRAME_MM_PLAY);
spr2->setColor( Color3B(200, 200, 200) );
auto *playButton = MenuItemSprite::create(spr1, spr2, CC_CALLBACK_1(CBirdMainMenu::playBtnPress, this));
playButton->setScale(1.0f);
playButton->setEnabled(true);
auto playMenu = Menu::create(playButton, nullptr);
}
// 创建按钮:类型 - 2
Button *infoButton
{
infoButton = Button::create(FRAME_MM_INFO,FRAME_MM_INFO,FRAME_MM_INFO,Widget::TextureResType::PLIST);
infoButton->setZoomScale(0.2f);
infoButton->setPressedActionEnabled(true);
infoButton->addTouchEventListener([&](Ref* sender, cocos2d::ui::Widget::TouchEventType type){
switch (type)
{
case ui::Widget::TouchEventType::BEGAN:
break;
case ui::Widget::TouchEventType::ENDED:
this->infoButtonPress();
break;
default:
break;
}
});
This->addChild(infoButton, 2);
}
在 Type-2 中,单击时如何更改按钮的颜色。我对所有状态都使用了单个图像。我不喜欢使用单独的图像。是否可以更改 Type2 中选定精灵的颜色?在 Type1 中,对于 MenuItemSprite ,我们可以轻松地为所选图像设置颜色……在 Type-2 中,如果我在 Button 上调用 setColor ,那么它就会崩溃。
infoButton->setColor(Color3B(200, 200, 200)); //Crashed on this
不知道按下时如何更改按钮的颜色。