Cocos2d-X菜单回调引发异常

时间:2016-05-08 14:54:38

标签: c++ callback cocos2d-x cocos2d-x-3.0

我目前正在将我的软件从Cocos2d-X 2.2.6迁移到Cocos2d-X 3.10,我无法使一些MenuItem回调工作。

我的代码如下:

bool MainMenuLayer::init()
{
    bool result;

    result = Layer::init();

    mainMenu = Menu::create();
    mainMenu->setPosition(0, 0);
    this->addChild(mainMenu);

    Sprite *equalSprite, *equalDownSprite;
    MenuItemSprite *equalButton;
    LabelBMFont *equalButtonLabel;
    MenuItemLabel *equalButtonMenuLabel;
    CCPoint equalButtonMenuLabelPosition;
    CCPoint equalButtonPosition;

    equalSprite = Sprite::createWithSpriteFrameName("botao_menu_igual.png");
    equalDownSprite = Sprite::createWithSpriteFrameName("botao_menu_igual_down.png");

    equalButton = MenuItemSprite::create(equalSprite, equalDownSprite, CC_CALLBACK_1(MainMenuLayer::buttonPressedCallback, this));

    equalButton->setEnabled(gameContext->getEqualGameEnabled());
    equalButton->setTag(EQUAL_BUTTON_TAG);
    equalButtonPosition = Point(screenSize.width / 4, screenSize.height / 2);
    equalButton->setPosition(mainMenu->convertToNodeSpace(equalButtonPosition));

    mainMenu->addChild(equalButton);

    equalButtonLabel = LabelBMFont::create(localization->getLocalizedString("EqualButtonLabel"), fontHelper->getStdFont().c_str());
    equalButtonMenuLabel = MenuItemLabel::create(equalButtonLabel, CC_CALLBACK_1(MainMenuLayer::buttonPressedCallback, this));

    equalButtonMenuLabel->setTag(EQUAL_BUTTON_TAG);
    equalButtonMenuLabelPosition.x = equalButtonPosition.x;
    equalButtonMenuLabelPosition.y = equalButtonPosition.y - equalButton->boundingBox().size.height / 2 - equalButtonMenuLabel->boundingBox().size.height;
    equalButtonMenuLabel->setPosition(mainMenu->convertToNodeSpace(equalButtonMenuLabelPosition));

    mainMenu->addChild(equalButtonMenuLabel);

此处还有一个头文件的一部分,我在其中定义了mainMenu对象:

using namespace cocos2d;

class MainMenuLayer : public Layer
{

private:

    Size screenSize;

    Menu *mainMenu;

    bool init();

    void buttonPressedCallback(Ref *sender);

public:

    MainMenuLayer();

    ~MainMenuLayer();

    static MainMenuLayer *create();

    static CCScene *scene();

};

问题在于,当我运行游戏并点击上面创建的按钮时,我会在文件type_traits文件和return (((*_STD forward<_Ty1>(_Arg1)).*_Pmf)(_STD forward<_Types2>(_Args2)...));行中看到异常(见下文)。

    struct _Invoker_pmf_pointer
    {    // INVOKE a pointer to member function on a [smart] pointer
    template<class _Decayed,
        class _Ty1,
        class... _Types2>
        static auto _Call(_Decayed _Pmf, _Ty1&& _Arg1, _Types2&&... _Args2)
        -> decltype(((*_STD forward<_Ty1>(_Arg1)).*_Pmf)(
            _STD forward<_Types2>(_Args2)...))
        {    // INVOKE a pointer to member function on a [smart] pointer
        return (((*_STD forward<_Ty1>(_Arg1)).*_Pmf)(
            _STD forward<_Types2>(_Args2)...));
        }
    };

我创建这个回调错了?还有其他人遇到过这个吗?

1 个答案:

答案 0 :(得分:0)

我弄清楚错误是什么。问题是我在前一个场景上注册了一个触摸监听器,当场景结束时我没有删除它。

因此,当我在更改场景之前添加了移除触摸侦听器代码时,一切都运行良好。

void SplashScreenMenuLayer::onTouchesEnded(Touch* touch, Event* event)
{
    Director::getInstance()->getEventDispatcher()->removeEventListener(touchListener);
    Director::getInstance()->replaceScene(MainMenuLayer::scene());
}