AppDelegate details, appdelegate
Cocos2d-x in the AppDelegate. cpp file, this is a more important thing in the Cocos2d-x, below we explain in detail: applicationDidFinishLaunching refers to the completion of the program startup
ApplicationDidEnterBackground indicates when the program enters the background (for example, when the player receives a call, the game will run in the background)
ApplicationWillEnterForeground is worth returning to the front-end (for example, the user returns to the game after receiving the call)
See applicationDidFinishLaunching. This method means that the program is started to load, that is, the first thing to load. // initialize director
CCDirector * pDirector = CCDirector: shareddire ();
This statement initializes a CCDirector. This is a mechanism of this engine. The director mechanism, initialized CCDirector, has a director.
Second sentence
PDirector-> setOpenGLView (CCEGLView: Export dopenglview ());
It means to set its GLView attribute to OpenGL for this director.
PDirector-> setDisplayStats (true );
PDirector-> setDisplayStats (false );
The first number represents the number of elements on the current screen.
The second is the time required to draw a frame.
The maximum time required to draw a frame.
The third number is the number of frames per second.
PDirector-> setAnimationInterval (1.0/60); this is set frame rate, also known as lock frame. The frame rate is set to a value, which means the frame rate is set to a maximum of 60 frames per second.
// Create a scene. it's an autorelease object
CCScene * pScene = HelloWorld: scene ();
This sentence uses a very important object CCScene. CCScene refers to the scenario. As we mentioned earlier, this is the scenario. The director is in place, and then let's create another scenario:
CCScene * pScene = HelloWorld: scene ();
// Run
PDirector-> runWithScene (pScene );
It is to give this scene to our director and let him run it.
Finally, there is a return true; this should be noted that most methods in the cocos2dx engine must have the return value bool. The engine will process the code in your method only when the returned value is true. Otherwise, the code will be skipped.
The inheritance characters in c ++ are:
CCLayer indicates a layer. A scenario consists of multiple layers.
Note:
The AppDelegate class inherits cocos2d: Application,
Class CC_DLL Application: public ApplicationProtocol {}
The implementation class of ApplicationProtocol is applied on different platforms. The source code is as follows:
Cocos2d-x-3.0/cocos/2d/platform directory, you can read in detail
Each platform has its own entry function (for example, main)
In main, a window of a platform is created, and then an AppDelegate instance is created,
So throughout the Cocos2d-x, AppDelegate is a singleton mode:
Static AppDelegate s_sharedApplication; (for example, in AppController. mm of Mac platform)
In the Application implementation:
Application * Application: sm_pSharedApplication = 0;
Application: Application ()
{
CC_ASSERT (! Sm_pSharedApplication );
Sm_pSharedApplication = this;
}
Application ::~ Application ()
{
CC_ASSERT (this = sm_pSharedApplication );
Sm_pSharedApplication = 0;
}
// Use this method after 3.0
Application * Application: getInstance ()
{
CC_ASSERT (sm_pSharedApplication );
Return sm_pSharedApplication;
}
In programming, we only need to define this class to process three callback methods. Window messages of different platforms are divided into three types:
Corresponding to several methods in AppDelegate
Generally, in the applicationDidFinishLaunching method:
1. initialize the director
2. Create the first scenario
3. Start this scenario
Then, let's look at the run method in the Application (this method is called by the main method of the system platform)
Int Application: run ()
{
If (applicationDidFinishLaunching ())
{
[[CCDirectorCaller sharedDirectorCaller] startMainLoop];
}
Return 0;
}
Therefore, after this method is executed, the Cocos2d-x begins the main logical loop.
When the android and ios platforms call, a new window will pop up to cover the current window, And applicationDidEnterBackground will be called.
When windows and mac platforms are minimized, applicationDidEnterBackground will be called.
It is generally handled in this method:
Stop all screen actions
Stop all music and sound effects
(Sometimes you need to save the current game status to the disk)
Processing in applicationWillEnterForeground
Continue all screen actions
Continue with music and sound effects
(Recover user data)