Lambda是一个比较高端的东西,自己用着感觉不是舒服,所以即使我使用最新的Cocos2dx 3.8版本,还是用原来的方法进行回调的设置。
使用Lambda表达式:
typedef std::function<void(Ref*)> ccWidgetClickCallback;
void Widget::addClickEventListener(const ccWidgetClickCallback &callback)
{
this->_clickEventListener = callback;
}
std::string str = "big.png";
auto pFortuneBtnTmp = Button::create(str, str, str, ui::Widget::TextureResType::PLIST);
pFortuneBtnTmp->addClickEventListener([&](Ref* sender) {
CCLOG("addClickEventListener");
});
不使用Lambda表达式:
std::string str = "big.png";
auto pFortuneBtnTmp = Button::create(str, str, str, ui::Widget::TextureResType::PLIST); pFortuneBtnTmp->addClickEventListener(CC_CALLBACK_1(CSelectStarScene::FortuneClick, this));
void CSelectStarScene::FortuneClick(Ref *pRef)
{
<span style="white-space:pre"> </span>CCLOG("CSelectStarScene::FortuneClick");
}