A recent study of how to automatically run a cocos2d-x test project requires a custom global pointer to be added to the testCpp project to point to the currently running test example. Where should we put this global pointer? After studying it, we found that it is best to put it in AppDelegate. In fact, this design was transplanted from iOS and will be very familiar with kids shoes that have been developed for iOS. The Code is as follows:
Class AppDelegate: private cocos2d: Application
{
Public:
AppDelegate ();
Virtual ~ AppDelegate ();
Virtual bool applicationDidFinishLaunching ();
Virtual void applicationDidEnterBackground ();
Virtual void applicationWillEnterForeground ();
BaseTest * getCurrentTest ();
Void setCurrentTest (BaseTest * curTest );
Private:
BaseTest * _ curTest;
};
Of course, this global pointer cannot be directly exposed and accessed using getter/setter.
So how can we call it elsewhere in the program? It is also very simple:
Void BaseTest: onEnter ()
{
Layer: onEnter ();
AppDelegate * app = (AppDelegate *) Application: getInstance ();
App-> setCurrentTest (this );
....
}
GetInstance () is a cocos2d: static function of the Application class, which can be used directly.
Here we use the C ++ Singleton mode.