一、button回调

1. Lambda 表达式,C++11 Lambda 赋予了Cocos2d-x 3.0创建回调函数的灵活性。

auto itemNor	=	Sprite::create("CloseNormal.png");
auto menuItem = MenuItemSprite::create(itemNor,nullptr,nullptr,[](Ref* sender)
{
log("show this msg.");
});
auto menu = Menu::create(menuItem,nullptr);
this->addChild(menu);

2.宏定义bind方式创建回调.

auto itemNor	=	Sprite::create("CloseNormal.png");
auto menuItem = MenuItemSprite::create(itemNor,nullptr,nullptr,CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
auto menu = Menu::create(menuItem,nullptr);
this->addChild(menu); void HelloWorld::menuCloseCallback(Ref* pSender)
{
log("show this msg.");
}

3.MenuToggleItem回事件回调

auto toggleSpNor	=	Label::createWithSystemFont("OPEN_BAME","WRYH",65);
auto toggleSpSel = Label::createWithSystemFont("CLOSE_BAME","WRYH",65);
auto toggleSpDis = Label::createWithSystemFont("DISABLE_BAME","WRYH",65);
auto toggleItemNor = MenuItemLabel::create(toggleSpNor);
auto toggleItemSel = MenuItemLabel::create(toggleSpSel);
auto toggleItemDis = MenuItemLabel::create(toggleSpDis); auto toggleItem = MenuItemToggle::createWithCallback(CC_CALLBACK_0(HelloWorld::toggleCallBack,this),toggleItemNor,toggleItemSel,nullptr); auto toggleMenu = Menu::create(toggleItem,nullptr);
this->addChild(toggleMenu); void HelloWorld::toggleCallBack()
{
log("Do something when toggle did touched..");
}

二、定时器回调

/*周期定时调用*/
this->schedule(SEL_SCHEDULE(&HelloWorld::gameStep));
/*倒计是定时调用一次*/
this->scheduleOnce(SEL_SCHEDULE(&HelloWorld::gameStep),3.0f);\
/*周期定时调用update需重写update方法*/
this->scheduleUpdate(); void HelloWorld::gameStep(float dt)
{
log("on timer...");
}

三、触屏事件回调

auto touchEvt		    =	cocos2d::EventListenerTouchOneByOne::create();
touchEvt->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan,this);
touchEvt->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved,this);
touchEvt->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded,this);
touchEvt->onTouchCancelled = CC_CALLBACK_2(HelloWorld::onTouchCancelled,this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchEvt,this); bool HelloWorld::onTouchBegan(cocos2d::Touch* touch,cocos2d::Event* evt)
{
log("Touch began..");
return true;
}
void HelloWorld::onTouchMoved(cocos2d::Touch* touch,cocos2d::Event* evt)
{
log("Touch moved..");
}
void HelloWorld::onTouchEnded(cocos2d::Touch* touch,cocos2d::Event* evt)
{
log("Touch leave..");
Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(this);
}
void HelloWorld::onTouchCancelled(cocos2d::Touch* touch,cocos2d::Event* evt)
{
log("Something was happend , touch event is cut..");
}

四、动作回调

auto callBack		=	CallFunc::create(CC_CALLBACK_0(HelloWorld::actionCallBack,this));
this->runAction(callBack); void HelloWorld::actionCallBack()
{
log("Do something when action did finished..");
}

五、自己定义事件回调

auto callBack		=	[](EventCustom* evt)
{
log("catch an custom event!!");
};
cocos2d::EventListenerCustom* customEvt = EventListenerCustom::create("ME_CUSTOM_EVENT_TEST",callBack);
//注冊自己定义事件(处理优先级为12)
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(customEvt,12); //抛出自己定义事件
Director::getInstance()->getEventDispatcher()->dispatchCustomEvent("ME_CUSTOM_EVENT_TEST");

最新文章

  1. java良好的编码习惯
  2. Hbase+ Phoenix搭建教程
  3. jquery幻灯片
  4. Junit4常用注解
  5. C#枚举硬件设备(升级版)
  6. jsp转发action的问题找不到acton
  7. 蒋金楠How ASP.NET MVC Works?[持续更新中…]
  8. iOS9适配系列教程
  9. CSS空白符处理!
  10. 剑指offer试题(PHP篇一)
  11. springdata 多对多配置
  12. ECS集群管理docker
  13. c语言第一次作业--分支 顺序结构
  14. Pandas学习笔记(三)
  15. Nginx 用分片提示缓存效率
  16. 使用js冒泡实现点击空白处关闭弹窗
  17. GitHub学习一-本地电脑与GitHub绑定
  18. JaveWeb 公司项目(6)----- 通过ToolTip给控件添加动态注释
  19. python之初级篇2
  20. Android SQLiteOpenHelper Sqlite数据库升级onUpgrade

热门文章

  1. 正则表达式 re模块 collections模块
  2. CTL_CODE说明
  3. Atos cannot get symbols from dSYM of archived application
  4. Linux下安装mantis配置指南【转】
  5. LINUX环境下的GUN MAKE学习笔记(二)
  6. Selenium2+python自动化42-判断元素(expected_conditions)【转载】
  7. 在.NET4.5项目中添加HttpClient引用的办法
  8. Mysql优化之——启用查询缓存
  9. AC日记——绿豆蛙的归宿 codevs 2488
  10. AC日记——[SHOI2008]小约翰的游戏John bzoj 1022