Cocos2d-X 菜单回调抛出异常
Cocos2d-X Menu Callback Throws Exception
我目前正在将我的软件从 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)...));
}
};
我创建这个回调有误吗?有没有其他人遇到过这个?
我知道错误是什么了。问题是我在之前的场景中注册了一个触摸监听器,当场景结束时我没有删除它。
所以当我在更改场景之前添加删除触摸侦听器代码时,一切正常。
void SplashScreenMenuLayer::onTouchesEnded(Touch* touch, Event* event)
{
Director::getInstance()->getEventDispatcher()->removeEventListener(touchListener);
Director::getInstance()->replaceScene(MainMenuLayer::scene());
}
我目前正在将我的软件从 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)...));
}
};
我创建这个回调有误吗?有没有其他人遇到过这个?
我知道错误是什么了。问题是我在之前的场景中注册了一个触摸监听器,当场景结束时我没有删除它。
所以当我在更改场景之前添加删除触摸侦听器代码时,一切正常。
void SplashScreenMenuLayer::onTouchesEnded(Touch* touch, Event* event)
{
Director::getInstance()->getEventDispatcher()->removeEventListener(touchListener);
Director::getInstance()->replaceScene(MainMenuLayer::scene());
}