cocos2d-x using_ns_cc

cocos2d-x using_ns_cc

本文关键字:cc ns cocos2d-x using      更新时间:2023-10-16

我需要知道Cocos2D-X在c++中的这句话的功能:

USING_NS_CC

代码示例:

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include "GameOverScene.h"
USING_NS_CC;
HelloWorld::~HelloWorld()
{
    if (_targets)
    {
        _targets->release();
        _targets = NULL;
    }
    if (_projectiles)
    {
        _projectiles->release();
        _projectiles = NULL;
    }
    // cpp don't need to call super dealloc
    // virtual destructor will do this
}

根据cocos2d-x源代码宏相当于using namespace cocos2d

你可以在cocos2d-x仓库的CCPlatformMacros.h中自己检查:

#ifdef __cplusplus
    #define NS_CC_BEGIN                     namespace cocos2d {
    #define NS_CC_END                       }
    #define USING_NS_CC                     using namespace cocos2d
    #define NS_CC                           ::cocos2d
#else
    #define NS_CC_BEGIN 
    #define NS_CC_END 
    #define USING_NS_CC 
    #define NS_CC
#endif 

对于@rene的回答,我查看了作者Siddharth Shekar的"Learning Cocos2d-x Game Development",作者错了。

这是本书那一段的链接,这是该段的图片。

关于USING_NS_CC他写错了什么:

  • 它不"#include CCPlatformMacros.h",它不加载额外的宏

    要使用它,您需要通过添加直接包含CCPlatformMacros.h#include <cocos/CCPlatformMacros.h>或间接地通过添加其他包含它的头,例如#include <cocos2d.h>

  • 它没有将命名空间设置为cocos2d

    它包含了来自cocos2d命名空间的所有符号,这使得你可以使用cocos2d-x符号而不指向正确的cocos2d::

    为你的符号设置一个命名空间cocos2d,你应该使用

    namespace cocos2d
    {
      // some declarations/definitions
    }
    

    或相当于:

    NS_CC_BEGIN
      // some declarations/definitions
    NS_CC_END