C++ 退出回调时的cocos2d-x?
在应用程序退出之前,我需要运行一些清理和分解代码 我已经编写了我的onExit函数,我将在调用Director::end之前调用它。但我需要在用户关闭窗口退出应用程序时运行此回调 据我所知,cocos2d::Application和cococos2D::ApplicationProtocol都没有定义任何类型的可重写的on exit或on quit虚拟方法 您可以在AppController.mm中实现C++ 退出回调时的cocos2d-x?,c++,cocos2d-x,C++,Cocos2d X,在应用程序退出之前,我需要运行一些清理和分解代码 我已经编写了我的onExit函数,我将在调用Director::end之前调用它。但我需要在用户关闭窗口退出应用程序时运行此回调 据我所知,cocos2d::Application和cococos2D::ApplicationProtocol都没有定义任何类型的可重写的on exit或on quit虚拟方法 您可以在AppController.mm中实现(void)application willterminate:(UIApplication*
(void)application willterminate:(UIApplication*)application
,如果它还没有在您的COCOOS项目中实现(在my CoCoCoCos2D_tests.xcodeproj中已经实现),然后从那里调用您的onExit
函数
比如:
- (void)applicationWillTerminate:(UIApplication *)application {
// call onExit() here, or forward the call to call onExit() :)
}
注意:这仅对iOS有效。就我所知,对于Android来说,没有一个等价于
的应用程序将终止。我知道这个问题很老,但我还是想回答它
我创建了一个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析构函数调用的GameController::DestroyInstance()中进行清理
这是我游戏控制器的一部分。h:
#ifndef _GAME_CONTROLLER_H_
#define _GAME_CONTROLLER_H_
#include <atomic>
class GameController
{
public:
~GameController();
static GameController* GetInstance();
void DestroyInstance();
...
...
...
希望它有帮助:D您可以将清理放入保存状态的类的析构函数中。只要它被销毁,代码就会被执行。替代方法是使用std::atexit
。我看到它是在AppController.mm中定义的。我不太熟悉把Objto-C和C++粘在一起。我猜它使用反射来查看应用程序类是否具有applicationWillTerminate方法;它没有在任何C++中定义。您可以调用应用程序中的任何函数,如:<代码>应用程序::GETStaseAuth[Off]>FooWoE()(代码)>在<代码>应用程序< /代码>中实现<代码>脚注()/代码>。可以在.mm文件中调用C++代码。
#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;
}
}
...
...
...