cocos2d-x 3.2 Button点击事件里调用移除当前层报错
http://www.th7.cn/program/ios/201408/271227.shtml
诡异的错误,点击关闭按钮,居然进入
void Widget::onTouchEnded(Touch *touch, Event *unusedEvent)
两次,第二次报错。
修改下这个函数:调换一下位置就行了
void Widget::releaseUpEvent() { if (_touchEventListener && _touchEventSelector) { (_touchEventListener->*_touchEventSelector)(this, TOUCH_EVENT_ENDED); } if (_touchEventCallback) { _touchEventCallback(this, TouchEventType::ENDED); } }
ShopPanel类:
#include "ShopPanel.h" #include "ui/CocosGUI.h" #include "Global.h" using namespace CocosDenshion; using namespace cocos2d; using namespace ui; ShopPanel::ShopPanel() { } ShopPanel::~ShopPanel() { } bool ShopPanel::init() { if (!Node::init()) { return false; } auto s = Director::getInstance()->getWinSize(); Layout* la = static_cast<Layout*>(cocostudio::GUIReader::getInstance()->widgetFromJsonFile("ui/shopPanel.json")); this->addChild(la); closeBtn = static_cast<Button*>(Helper::seekWidgetByName(la, "closeBtn")); closeBtn->addTouchEventListener(CC_CALLBACK_2(ShopPanel::closeEvent, this)); return true; } void ShopPanel::open(Node* parent) { if (parent != nullptr && !isopen) { isopen = true; parent->addChild(this); this->setScale(0); ScaleTo *scale1 = ScaleTo::create(0.2f, 1.0f); EaseBackOut *tween = EaseBackOut::create(scale1); this->runAction(tween); } } void ShopPanel::close() { if (isopen){ isopen = false; this->removeFromParent(); } } void ShopPanel::closeEvent(Ref *pSender, Widget::TouchEventType type) { switch (type) { case Widget::TouchEventType::ENDED: this->close(); break; default: break; } } void ShopPanel::onExit() { Node::onExit(); }
调用打开函数:
void FirstScene::openShop(Ref *pSender, Widget::TouchEventType type) { switch (type) { case Widget::TouchEventType::ENDED: ShopPanel* shop = ShopPanel::create(); shop->open(this); break; } }