首页 > 解决方案 > Cocos2d-x 按钮 - MenuItemSprite 与按钮

问题描述

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

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

标签: iosiphonexcodecocos2d-xcocos2d-x-3.x

解决方案


您正在创建按钮并分配给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 表达式识别。

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

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。这种方式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课程是做什么的,因为这不包含在代码中。如果toolBar是自定义类,我建议您将您的infoButtonfrom CBirdMenutotoolBar改为使用第二种方法来解决您的问题。


推荐阅读