【发布时间】:2018-09-19 07:01:00
【问题描述】:
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
不知道按下时如何改变按钮的颜色。
【问题讨论】:
-
我认为这没有任何问题。假设 FRAME_MM_X 是图像名称的 char 数组,并且图像存储在纹理图集中,因为您使用的是
Widget::TextureResType::PLIST。顺便说一句,什么是崩溃? -
@Yucel_K 崩溃只发生在我们调用 infoButton->setColor...否则不会崩溃。
-
您是否尝试过在初始化按钮后立即更改颜色?在我的 cocos2dx 中,一切正常。我怀疑可能
infoButton指针在调用setColor之前变得无效。你遇到了什么崩溃? -
@Yucel_K 这是屏幕截图..感谢您的收看。 app.box.com/s/hefysv8c8nccpcz41piv0z5e6fkceiwc
-
你能告诉我你在哪里初始化
toolbar指针吗?我相信我知道它在哪里被释放但想确定一下。
标签: ios iphone xcode cocos2d-x cocos2d-x-3.x