Cocos2dx BASICS (28)-singleton Mode

Source: Internet
Author: User

[Nagging]

The Singleton mode is generally used to create globally unique static instance objects for a class. That is to say, resources will be released from the creation of the game program until the end of the game program, during which the data is stored.

Singleton should be an essential part of most games. For example, the switch control of the entire game music and the loading and storage of data all require a global access point, the Singleton class is required.


[Thank you]

Http://blog.csdn.net/star530/article/details/22610229



[Singleton mode]

There is a very good metaphor from: http://blog.csdn.net/star530/article/details/22610229

If you are a man, but you cannot take care of yourself in your private life: You can't cook, wash, and wear no fashion.

What do you need at this time? Yes, you need a girlfriend! But how do you "use" this girlfriend?

You can choose from the following two options:

(1) "CREATE" A girlfriend (that is, find a girlfriend) when you want to wash the clothes, and then drop the girlfriend after the washing;

When you want to cook, continue to "CREATE" a girlfriend and get rid of it;

Even when you need to meet some ulterior motives, you need to "CREATE" a girlfriend... and then...

(2) find a girlfriend who can stay together forever. If you don't leave, she will not give up!

If you want to use it, you can use it easily, saving you from a series of troubles such as dating!

Although both of the above methods can make you say goodbye to your right hand, the prerequisites for the first method are too exaggerated: if you are a rich guy, like a blogger, haha.

So choose the second one, that is, the singleton mode we call.


1. Singleton mode in cocos2dx

There are actually many Singleton classes in cocos2dx, And I believe everyone has come into contact with them.

For example:Ccdirector,Simpleaudioengine,Ccuserdefault,CcspriteframecacheThe Singleton mode is used, and the singleton object can be obtained through shared *** (), suchCcdirector: shareddirector ().


2. Create a singleton class

2.1 statement. h

// Class Global: cocos2d: ccobject {PRIVATE: static global * m_global; // unique static Instance Object m_globalpublic: static global * shareglobal (); // obtain the Global Access Point m_global // declaration of other attribute functions //......}; //


2.2 define implementation. cpp

//// Initialize the unique instance object m_globalglobal * Global: m_global = NULL; // obtain the Global Access Point global * Global: shareglobal () {// determine whether m_global has been instantiated. If not, set the instance if (m_global = NULL) {m_global = new global ();} return m_global ;} // definition implementation of other functions //...... //



This article is from the "summer wind" blog, please be sure to keep this source http://shahdza.blog.51cto.com/2410787/1548273

Cocos2dx BASICS (28)-singleton Mode

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.