【问题标题】:Cocos2d-x Buttons - MenuItemSprite Vs ButtonCocos2d-x 按钮 - MenuItemSprite 与按钮
【发布时间】: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


【解决方案1】:

您正在创建按钮并分配给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 的类成员。这样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 的问题。但是我仍然不确定 yourtoolBar 类做了什么,因为这不包含在代码中。如果toolBar 是自定义类,我建议您将infoButtonCBirdMenu 移动到toolBar,而不是使用第二种方法来解决您的问题。

【讨论】:

  • 非常感谢您的回答...两种解决方案都有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多