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.