-----------------解決方法(直接上代碼):
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中用上述方法來注冊、分發監聽事件