ui::button 属于Widget 可以使用wedget的触摸,点击
typedefstd::function<void(Ref*,Widget::TouchEventType)> ccWidgetTouchCallback;
typedefstd::function<void(Ref*)> ccWidgetClickCallback;
typedefstd::function<void(Ref*,int)> ccWidgetEventCallback;
这是三个回调事件以 点击 事件举例。
首先定义一个函数
void myClass::testButtonCallBack(Ref * pSender);
button->addClickEventListener(ui::Widget::ccWidgetClickCallback(ViewCell::testButtonCallBack));
touch事件
myTextField->addEventListener(CC_CALLBACK_2(myClass::textFieldEvent, this));
UI Button在以上添加回调事件时,不能使用this指针,会一直提示静态函数里不能使用非静态变量,等等。
还可以使用以下方法添加回调事件
ui button 回调
//创建 UI Button
auto getButton = (ui::Button*)_giftNode->getChildByTag(19);
getButton->addClickEventListener(CC_CALLBACK_1(GiftLayer::getButtonCallBack, this));
//UI Button 回调函数
void GiftLayer::getButtonCallBack(Ref* Pesender)
{
if (this->isGameScene)
{
auto gameScene = (GameScene*)this->getParent();
gameScene->mResume();
}
this->removeFromParent();
}
这种方法就没有了之前的限制,可以很方便的使用。