Cocos2d-x 3.0final Terminator Series Tutorial 05-appdelegate Entry class

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.