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中用上述方法来注册、分发监听事件


你可能感兴趣的:(技术,Cocos2dx,c++)