Cocos2d-x 按钮 - MenuItemSprite 与按钮

Cocos2d-x Buttons - MenuItemSprite Vs Button

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

不知道如何在按下时改变按钮的颜色。

您正在创建按钮并分配给 InfoButton 指针。

infoButton = Button::create(FRAME_MM_INFO,FRAME_MM_INFO,FRAME_MM_INFO,Widget::TextureResType::PLIST);

问题是您的 infoButton 是本地指针。

Button *infoButton;
  {
    ...
    ...

从您提供的屏幕截图中,我可以看到它是在 CBirdMenu::SetupMenu() 中本地创建的。

你然后将 info button 作为子对象添加到一个名为 toolBar 的指针所指向的对象中,但是当 CBirdMenu::SetupMenu() 结束时,你的 infoButton 将不再被 lambda 表达式识别。

解决您的问题的一种可能也是最简单的方法是对 lambda 表达式中的 lambda 参数 Ref* sender 使用动态转换。

InfoButton->addTouchEventListener([&](Ref* sender, cocos2d::ui::Widget::TouchEventType type)
{
    cocos2d::ui::Button * infButton = dynamic_cast<cocos2d::ui::Button*>(sender);
    if(infButton)//check if casting done properly
       infButton->setColor(Color3B(0, 200, 0)); //colour set to green.
});

或者,不使用本地指针 infoButton,而是将其存储为 CBirdMenu 的 class 成员。这样 infoButton 永远不会在 cBirdMenu 存在的情况下丢失。

这是一个快速演示。 头文件;

    #include "cocos2d.h"
    #include "ui\CocosGUI.h"
    class HelloWorld : public cocos2d::Layer
    {
    public:
        static cocos2d::Scene* createScene();
        virtual bool init();
        void menuCloseCallback(cocos2d::Ref* pSender);
        CREATE_FUNC(HelloWorld);
    private:
        cocos2d::ui::Button * InfoButton; //member of HelloWorld.
    };

注意私有成员cocos2d::ui::Button * InfoButton; 最后是实例化按钮并分配给 infoButton 指针的源文件。

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
        return false;

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    InfoButton = cocos2d::ui::Button::create("HelloWorld.png", "HelloWorld.png", "HelloWorld.png", ui::Widget::TextureResType::LOCAL);
    InfoButton->setColor(Color3B(255, 0, 0)); //colour is set to red as suppose to.
    InfoButton->setTitleFontSize(InfoButton->getTitleFontSize() * 0.7);
    InfoButton->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
    InfoButton->addTouchEventListener([&](Ref* sender, cocos2d::ui::Widget::TouchEventType type)
    {
        InfoButton->setColor(Color3B(0, 200, 0)); //colour set to green.
    });
    // add the button as a child to this layer
    this->addChild(InfoButton, 2);
    return true;
}

如果您将相同的原则应用于您的代码,这应该可以解决您当前使用 lambda 的问题。但是我仍然不确定你的 toolBar class 做了什么,因为这不包含在代码中。如果 toolBar 是自定义 class,我建议您将 infoButtonCBirdMenu 移至 toolBar,如果您使用第二种方法来解决您的问题.