Cocos2dx game development-Design Mode

Source: Internet
Author: User

I. II Design Model

The second build mode separates the memory space allocation and initialization, and then calls a static method to return this object.

 

When calling sprite: Create (), use new internally to allocate memory space, and then call the init method to initialize some variable settings. Therefore, the second build mode in cocos2dx is to separate the memory space allocated by new from the init initialization content, rather than the traditional C ++ method to initialize variables in the constructor.

Sprite *Sprite: Create (){Sprite * sprite =NewSprite ();If(Sprite & sprite->Init ()){Sprite->Autorelease ();ReturnSprite;} cc_safe_delete (sprite );ReturnNullptr ;}

 

Advantage: Because the constructor does not return values and cannot call virtual functions, an error occurs while loading image resources. Therefore, separate the allocated memory and initialization by using two segments, which is very beneficial to the query of bugs.

 

Ii. Singleton Design Mode

When you need to initialize an object to be set as a singleton in a class, assign values to the member variables of this object, and when in another class, when we need to obtain the value of the member variable in this object, we design it into a singleton.

 

It is a global variable. I personally think it is not necessary to use it. After all, there is a high chance of a small error.

 

 

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.