Game Development Reference
In-Depth Information
Instantiating objects and managing
memory
There is no Automatic Reference Counting ( ARC ) in Cocos2d-x, so Objective-C deve-
lopers who have forgotten memory management might have a problem here. However, the
rule regarding memory management is very simple with C++: if you use new , you must
delete. C++11 makes this even easier by introducing special pointers that are memory-man-
aged (these are std::unique_ptr and std::shared_ptr ).
Cocos2d-x, however, will add a few other options and commands to help with memory
management, similar to the ones we have in Objective-C (without ARC). This is because
Cocos2d-x, unlike C++ and very much like Objective-C, has a root class. The framework is
more than just a C++ port of Cocos2d. It also ports certain notions of Objective-C to C++
in order to recreate its memory-management system.
Cocos2d-x has a Ref class that is the root of every major object in the framework. It al-
lows the framework to have autorelease pools and retain counts, as well other
Objective-C equivalents.
When instantiating Cocos2d-x objects, you have basically two options:
• Using static methods
• The C++ and Cocos2d-x style
Search WWH ::




Custom Search