Here is the program entry for Cocos2d-x:
class Appdelegate: privatecocos2d::application
{
Public:
Appdelegate ();
virtual ~appdelegate ();
Virtual bool applicationdidfinishlaunching ();
Virtual void applicationdidenterbackground ();
Virtual void applicationwillenterforeground ();
};
Description
This class inherits the cocos2d::application.
class cc_dll application: public Applicationprotocol
.......
In each of the different platforms will be Applicationprotocol implementation class, the source code in:
The Cocos2d-x-3.0/cocos/2d/platform folder. You can read the specific
Each platform will have its own entry function (for example, main)
In main, a form of a platform is created, and then an instance of Appdelegate is created.
So Appdelegate is a singleton pattern throughout the Cocos2d-x:
Static appdelegate s_sharedapplication; (for example, in appcontroller.mm on Mac platforms)
Then in the implementation of the application, for example the following:
Application* application:: Sm_psharedapplication =0;
Application:: Application ()
{
cc_assert(! Sm_psharedapplication);
sm_psharedapplication =this;
}
application:: ~application ()
{
cc_assert( this = =sm_psharedapplication);
sm_psharedapplication =0;
}
After 3.0, use this method
Application* application:: getinstance ()
{
cc_assert(sm_psharedapplication);
returnsm_psharedapplication;
}
We only need to define this class to handle 3 callback methods in the programming, the form message of different platform is roughly divided into 3 kinds:
1. form creation of system platform completed
2. The system-through-platform form is covered and will go into the background execution
3. Restore the System platform form to the foreground
Several methods of appdelegate are respectively corresponding.
Virtual bool applicationdidfinishlaunching ();
Virtual void applicationdidenterbackground ();
Virtual void applicationwillenterforeground ();
Under normal circumstances in the Applicationdidfinishlaunching method:
1. Initialize the Director
2. Create the first scene
3. Start the scene
Then we look at the Run method in application (the method called by the main method of the system platform)
int Application:: Run ()
{
if (applicationdidfinishlaunching())
{
[[ccdirectorcallershareddirectorcaller] startmainloop];
}
return 0;
}
Therefore, after this method is finished running. Cocos2d-x begins the main logic loop.
When the Android,ios platform comes to the phone. A new form will cover the current form, and Applicationdidenterbackground will be called
When the Windows,mac platform is minimized, the applicationdidenterbackground is called
Generally handled in this way:
To stop all the screen moves.
Let all the music and sound come to a halt
(sometimes to save the current game state persisted to disk)
Processing in Applicationwillenterforeground
Let all the screen moves continue
Let the music and sound continue
(Recover user data)
---------------------------------Summary----------------------------
From this point of view, learning Cocos2d-x than Android, iOS is also simple
OnCreate
OnStart
Onresume
OnPause
OnStop
OnDestroy
Android actually got so much to do.
Cocos2d-x 3.0final Terminator Series Tutorial 05-appdelegate Entry class