Cocos2d-x退出回调
cocos2d-x on exit callback?
我需要在应用程序退出之前运行一些清理和删除代码。
我已经写了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
- 架构决策:返回std::future还是提供回调
- 正在为Xtensa simcall函数编写回调函数
- 如何在C++中使用非静态成员函数作为回调函数
- FLTK:按下哪个按钮 - 将数字传递给按钮的回调 (lambda)
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 用于在回调中调用解析器的设计模式
- 如何使用C++对象的成员函数作为 C 样式回调?
- Java从C++回调到C++回调
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 从不同的 cpp 调用回调函数会导致bad_function_call
- pcap_handler回调仅在使用 NPCAP v0.9991 时包含空数据包
- 不带轮询的 SDL2 事件回调
- C++存储带有可变参数的回调
- 如何使用 Node-addon-API 实现 node-nan 回调
- 处理影响跨不同线程共享对象的定时回调的最佳方法是什么?
- 访问类C++ C 样式回调
- 处理类内的回调时,必须调用对非静态成员函数的引用
- 如果 C 函数仍然可以间接执行(通过回调函数),那么将它声明为静态函数是否是一种不好的做法?
- 在 Linux 中退出时如何注册进程的回调
- Cocos2d-x退出回调