cocos2dx3.13 lua注册、分发监听真机切到后台事件

 -----------------解决方法(直接上代码):

1.AppDelegate.cpp:

void AppDelegate::applicationDidEnterBackground()

{

    Director::getInstance()->stopAnimation();


    SimpleAudioEngine::getInstance()->pauseBackgroundMusic();

    Director::getInstance()->getEventDispatcher()->dispatchCustomEvent("APP_ENTER_BACKGROUND_EVENT");

}

void AppDelegate::applicationWillEnterForeground()

{

    Director::getInstance()->startAnimation();


    SimpleAudioEngine::getInstance()->resumeBackgroundMusic();

    Director::getInstance()->getEventDispatcher()->dispatchCustomEvent("APP_ENTER_FOREGROUND_EVENT");

}

2.监听事件

    local listenerCustom=cc.EventListenerCustom:create("APP_ENTER_FOREGROUND_EVENT",function ()
        print("切换到前台")
    end)  
    local customEventDispatch=cc.Director:getInstance():getEventDispatcher()
    customEventDispatch:addEventListenerWithFixedPriority(listenerCustom, 1)

 -----------------说明:

1.正常的按照c++的方式应该是:

     cc.Director:getInstance():getEventDispatcher():addCustomEventListener("APP_ENTER_FOREGROUND_EVENT", function ()
         print("========= 前台")
     end)

但lua-binding对c++的该方法不支持回调方法。该lua-binding方法在lua_cocos2dx_auto.cpp中的

int lua_cocos2dx_EventDispatcher_addCustomEventListener(lua_State* tolua_S)方法,其中

        do {

// Lambda binding for lua is not supported.

assert(false);

} while(0)

直观的告诉你,不支持回调方法。

2.cocos2dx-quick3.3中可以使用:

     app:addEventListener("APP_ENTER_FOREGROUND_EVENT", function(event)
         print("========= 前台")
     end)

但quick3.3和cocos2dx3.13中的AppBase实现方式不一样,导致app:addEventListener找不到。

3.所以3.13中用上述方法来注册、分发监听事件


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值