Application of Singleton mode in cocos2d-x

Source: Internet
Author: User

I will not go into details about the singleton mode. I will go directly to the topic. We often can use the singleton mode in C ++, but in the cocos2d-x, the use of Singleton mode, whether you have encountered trouble, a variety of "could not resolve ", "error LNK" error. Next I will use an example to briefly introduce how the order example mode is written in the cocos2d-x: Here I write a class Global to store the unique variables that can be accessed globally in the game, this requires that we can access Global in any object at any time to read the unique variable header file stored in it as follows: [cpp] # ifndef _ GLOBAL_H _ # define _ GLOBAL_H _ # include "cocos2d. h "# include" StartLayer. h "class Global {public: StartLayer * startLayer; // stores the globally accessible unique variable static Global * toIns (); // returns the Global Object protected through this method :~ Global () ;};# endif Cpp file: [cpp] # include "Global. h "using namespace cocos2d; static Global * share = NULL; // This line is very important. The previous mistake we made was the C ++ habit. We put the variable declaration and initialization in the header file, cause error Global ::~ Global (void) {startLayer = NULL;} Global * Global: toIns () {if (! Share) {share = new Global (); CCLOG ("first") ;}cclog ("hello"); return share ;}you can also do this: the header file is as follows: [cpp] # ifndef _ GLOBAL_H _ # define _ GLOBAL_H _ # include "cocos2d. h "# include" StartLayer. h "class Global {public: StartLayer * startLayer; static Global * toIns (); static Global * share; // The static variable declaration is here protected :~ Global () ;};# endif CPP file: [cpp] # include "Global. h "using namespace cocos2d; // static Global * share = NULL; Global * Global: share = NULL; // put static variable initialization here, instead of in the header file Global ::~ Global (void) {startLayer = NULL;} Global * Global: toIns () {if (! Share) {share = new Global (); CCLOG ("first") ;}cclog ("hello"); return share ;}use the code in your project, have you learned? For the exception, you can also refer to the Code in the singleton (such as CCDirector) in the cocos2d-x, how they instance the singleton

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.