Cocos2d-x退出回调

cocos2d-x on exit callback?

本文关键字:回调 退出 Cocos2d-x      更新时间:2023-10-16

我需要在应用程序退出之前运行一些清理和删除代码。

我已经写了onExit函数,我将在调用Director::end之前调用它。但是我需要这个回调在用户通过关闭窗口退出应用程序时运行。

据我所知,cocos2d::Application和cocos2d::ApplicationProtocol都没有定义任何一种可以被覆盖的on exit或on quit虚拟方法。

你可以在AppController中实现(void)applicationWillTerminate:(UIApplication *)application。Mm 如果它尚未在您的cocos项目中实现(在我的cocos2d_tests中)。xcodeproj它已经是),并从那里调用onExit函数。

类似:

- (void)applicationWillTerminate:(UIApplication *)application {
    // call onExit() here, or forward the call to call onExit() :)
}

注意:这只对iOS有效。对于Android,据我所知不等同于applicationWillTerminate

我知道这个问题很老了,但我还是想回答。

我已经创建了一个GameController类,AppDelegate有一个指向这个类的指针。这个GameController类将至少处理第一个场景或在游戏中变化的场景。例如,这是我的appdelegate。h:
#ifndef  _APP_DELEGATE_H_
#define  _APP_DELEGATE_H_
#include "cocos2d.h"
class GameController;
/**
@brief    The cocos2d Application.
Private inheritance here hides part of interface from Director.
*/
class  AppDelegate : private cocos2d::Application
{
public:
    AppDelegate();
    virtual ~AppDelegate();
    virtual void initGLContextAttrs() override;
    /**
    @brief    Implement Director and cocos2d::Scene* init code here.
    @return true    Initialize success, app continue.
    @return false   Initialize failed, app terminate.
    */
    virtual bool applicationDidFinishLaunching() override;
    /**
    @brief  Called when the application moves to the background
    @param  the pointer of the application
    */
    virtual void applicationDidEnterBackground() override;
    /**
    @brief  Called when the application reenters the foreground
    @param  the pointer of the application
    */
    virtual void applicationWillEnterForeground() override;
private:
    GameController* m_gameController;
};
#endif // _APP_DELEGATE_H_

类GameController有一个公共函数Destroy实例,我在其中清理所有其他场景,类等。所以在AppDelegate的析构函数中我调用了那个函数:

AppDelegate.cpp:

#include "AppDelegate.h"
#include "GameController.h"
#include "extensions/cocos-ext.h"
USING_NS_CC;
AppDelegate::AppDelegate() : m_gameController(nullptr)
{
}
AppDelegate::~AppDelegate()
{
    m_gameController->DestroyInstance();
}
...
...
...

在初始化AppDelegate.cpp中的所有内容之后,在返回true之前,我调用GameController::GetInstance()静态函数

bool AppDelegate::applicationDidFinishLaunching() {
...
...
...
m_gameController = GameController::GetInstance();
if (!m_gameController)
    {
        log("n[AppDelegate] [Fatal] - GameController is nullptr.n");
        return false;
    }
return true;
}

然后GameController将(顾名顾义)控制游戏,在游戏结束时(或Win32, Mac osx或linux上的窗口关闭),你可以在AppDelegate Destructor调用的GameController::DestroyInstance()中进行清理。

这是我的gamcontroller .h:

的一部分
#ifndef _GAME_CONTROLLER_H_
#define _GAME_CONTROLLER_H_
#include <atomic>
class GameController
{
public:
    ~GameController();
    static GameController* GetInstance();
    void DestroyInstance();
...
...
...

这是我的gameconcontroller。cpp的一部分:

#include "GameController.h"
static GameController* s_gameController = nullptr;
GameController::GameController()
{
}
GameController::~GameController()
{
}
GameController * GameController::GetInstance()
{
    if (!s_gameController)
    {
        s_gameController = new (std::nothrow) GameController;
        if (s_gameController)
        {
            if (!s_gameController->Init())
            {
                delete s_gameController;
                s_gameController = nullptr;
            }
        }
    }
    return s_gameController;
}
void GameController::DestroyInstance()
{
    if (s_gameController)
    {
        delete s_gameController;
        s_gameController = nullptr;
    }
}
...
...
...
D