在之前Cocos2dx-3.4中,或者3.x中,addEventListener使用方式如下:
_pPageView = PageView::create();
_pPageView->setContentSize(Size(960.0f, 640.0f));
Layout *pLayout = nullptr;
for (int i = 1; i < 5; ++i)
{
pLayout = Layout::create();
auto pLayoutSprite = Sprite::createWithSpriteFrameName(StringUtils::format("tips_%d.png", i));
auto tipsText = Sprite::createWithSpriteFrameName(StringUtils::format("tips_text_%d_CN.png", i));
std::string num = StringUtils::format("%d", i);
tipsText->setPosition(480, 160);
pLayoutSprite->setPosition(Vec2(480, 370));
pLayout->addChild(tipsText);
pLayout->addChild(pLayoutSprite);
_pPageView->insertPage(pLayout, i);
}
addChild(_pPageView);
_pPageView->setPassFocusToChild(true);
_pPageView->scrollToPage(_iCurLevelIndex);
// 监听事件
_pPageView->addEventListener([=](Ref *pSender, PageView::EventType event)
{
if (PageView::EventType::TURNING == event)
{
_iCurLevelIndex = _pPageView->getCurPageIndex();
std::string num = StringUtils::format("%d", _iCurLevelIndex + 1);
bottomSprite->removeAllChildren();
auto layoutNum = NumSprite::createNum(num, "Themes/Items/numwhite-hd.png");
layoutNum->setPosition(465, 40);
bottomSprite->addChild(layoutNum, 80);
}
});
而到了Cocos2dx-3.1.5中,需要强制换为ccPageViewCallback,代码如下:
_pPageView->addEventListener((PageView::ccPageViewCallback)([=](Ref *pSender, PageView::EventType event)
{
if (PageView::EventType::TURNING == event)
{
_iCurLevelIndex = _pPageView->getCurPageIndex();
std::string num = StringUtils::format("%d", _iCurLevelIndex + 1);
bottomSprite->removeAllChildren();
auto layoutNum = NumSprite::createNum(num, "Themes/Items/numwhite-hd.png");
layoutNum->setPosition(465, 40);
bottomSprite->addChild(layoutNum, 80);
}
}));