cocos2dx的代码风受其原生引擎cocos2d-iphone的影响,沿袭了oc的代码风。但3.0版在此基础上又引入了C的编程风。 命名空间与类名 cocos2d-x有一个包含其他所有头文件的“cocos2d.h”。通常在需要使用引擎类库的头文件中包含了这个文件,所以我们能使用引擎的
cocos2dx的代码风格受其原生引擎cocos2d-iphone的影响,沿袭了oc的代码风格。但3.0版在此基础上又引入了C++的编程风格。
- 命名空间与类名
cocos2d-x有一个包含其他所有头文件的“cocos2d.h”。通常在需要使用引擎类库的头文件中包含了这个文件,所以我们能使用引擎的全部功能。
cocos2d-x的类都在cocos2d命名空间下,我们常使用USING_NS_CC宏来引用cocos2d命名空间。
cocos2d-x类的命名用驼峰式,类库缩写的采用大写,再加上类名。如CCAction。3.0中,这种编码风格已被废止了,不需加上CC前缀。
- 构造函数与初始化
cocos2d-x所有的对象都创建在堆上,然后通过指针引用。创建对象通常有两种方法;A、用new创建一个未初始化的对象,再调用init方法来初始化;B、用静态工厂方法直接创建对象。
oc中没有构造方法,cocos2d-x也采用了oc的步骤。-x的类构造器通常没有参数,创建对象所需的参数通过init系列方法传递给对象。如创建精灵:
auto butterFly = new Sprite(); butterFly.initWithFile("HelloWorld.png");也可以用类自带的工厂方法来创建对象。从2.x的版本开始,工厂方法的名称统一为create。在名称冲突的情况下,也可采用以create为前缀的其他函数名:
auto sprite = Sprite::create("HelloWorld.png");两种方式都可以创建对象,但从内存管理的角度讲,推荐后面一种方式。
从cocos2d-x提供的游戏元素派生的新类,需重载init方法,在此方法中为子类添加内容。在子类头文件中需要确保初始化方法声明为虚函数
virtual bool init();然后在cpp中实现这个init,如:
bool HelloWorld::init(){ ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } return true;}
- 选择器
在oc中,选择器类似C++的类函数指针。-x提供了一系列类似oc中创建选择器的宏,用来创建函数指针。这些宏只有一个参数selector,表示被指向的类方法。
typedef void (Object::*SEL_SCHEDULE)(float);typedef void (Object::*SEL_CallFunc)();typedef void (Object::*SEL_CallFuncN)(Node*);typedef void (Object::*SEL_CallFuncND)(Node*, void*);typedef void (Object::*SEL_CallFuncO)(Object*);typedef void (Object::*SEL_MenuHandler)(Object*);typedef int (Object::*SEL_Compare)(Object*);#define schedule_selector(_SELECTOR) static_cast(&_SELECTOR)#define callfunc_selector(_SELECTOR) static_cast(&_SELECTOR)#define callfuncN_selector(_SELECTOR) static_cast(&_SELECTOR)#define callfuncND_selector(_SELECTOR) static_cast(&_SELECTOR)#define callfuncO_selector(_SELECTOR) static_cast(&_SELECTOR)#define menu_selector(_SELECTOR) static_cast(&_SELECTOR)#define event_selector(_SELECTOR) static_cast(&_SELECTOR)#define compare_selector(_SELECTOR) static_cast(&_SELECTOR)在3.0版中用C++11的特性定义了新的回调
// new callbacks based on C++11#define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_ARGS__)#define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, st<span style="color:transparent">本文来源gaodai#ma#com搞*!代#%^码$网!</span>d::placeholders::_1, ##__VA_ARGS__)#define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)#define CC_CALLBACK_3(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3 ##__VA_ARGS__)新版的HelloWorld中已没再使用menu_selector的宏,而是替换为CC_CALLBACK_1。
auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
- 属性
C++的类成员只有方法与字段,没有属性和事件。为了实现oc中提供的属性功能,不得不写大量的set和get方法。cocos2d-x提供了一系列的宏,帮助开发者简化操作。
/** CC_PROPERTY is used to declare a protected variable. We can use getter to read the variable, and use the setter to change the variable. @param varType the type of variable. @param varName variable name. @param funName "get + funName" will be the name of the getter. "set + funName" will be the name of the setter. @warning The getter and setter are public virtual functions, you should rewrite them first. The variables and methods declared after CC_PROPERTY are all public. If you need protected or private, please declare. */#define CC_PROPERTY(varType, varName, funName)\protected: varType varName;\public: virtual varType get##funName(void);\public: virtual void set##funName(varType var);#define CC_PROPERTY_PASS_BY_REF(varType, varName, funName)\protected: varType varName;\public: virtual const varType& get##funName(void) const;\public: virtual void set##funName(const varType& var);CC_PROPERTY的set和get方法是没有实现的,需要使用者重载。可以用CC_PROPERTY来传值,也可以用CC_PROPERTY_PASS_BY_REF来传引用。
对应的CC_SYNTHESIZE,则是已经有初始化的实现的宏。
/** CC_SYNTHESIZE is used to declare a protected variable. We can use getter to read the variable, and use the setter to change the variable. @param varType the type of variable. @param varName variable name. @param funName "get + funName" will be the name of the getter. "set + funName" will be the name of the setter. @warning The getter and setter are public inline functions. The variables and methods declared after CC_SYNTHESIZE are all public. If you need protected or private, please declare. */#define CC_SYNTHESIZE(varType, varName, funName)\protected: varType varName;\public: virtual varType get##funName(void) const { return varName; }\public: virtual void set##funName(varType var){ varName = var; }#define CC_SYNTHESIZE_PASS_BY_REF(varType, varName, funName)\protected: varType varName;\public: virtual const varType& get##funName(void) const { return varName; }\public: virtual void set##funName(const varType& var){ varName = var; }#define CC_SYNTHESIZE_RETAIN(varType, varName, funName) \private: varType varName; \public: virtual varType get##funName(void) const { return varName; } \public: virtual void set##funName(varType var) \{ \ if (varName != var) \ { \ CC_SAFE_RETAIN(var); \ CC_SAFE_RELEASE(varName); \ varName = var; \ } \}同时,这两种宏都有READ_ONLY的方式。
/** CC_PROPERTY_READONLY is used to declare a protected variable. We can use getter to read the variable. @param varType the type of variable. @param varName variable name. @param funName "get + funName" will be the name of the getter. @warning The getter is a public virtual function, you should rewrite it first. The variables and methods declared after CC_PROPERTY_READONLY are all public. If you need protected or private, please declare. */#define CC_PROPERTY_READONLY(varType, varName, funName)\protected: varType varName;\public: virtual varType get##funName(void) const;#define CC_PROPERTY_READONLY_PASS_BY_REF(varType, varName, funName)\protected: varType varName;\public: virtual const varType& get##funName(void) const;/** CC_SYNTHESIZE_READONLY is used to declare a protected variable. We can use getter to read the variable. @param varType the type of variable. @param varName variable name. @param funName "get + funName" will be the name of the getter. @warning The getter is a public inline function. The variables and methods declared after CC_SYNTHESIZE_READONLY are all public. If you need protected or private, please declare. */#define CC_SYNTHESIZE_READONLY(varType, varName, funName)\protected: varType varName;\public: virtual varType get##funName(void) const { return varName; }#define CC_SYNTHESIZE_READONLY_PASS_BY_REF(varType, varName, funName)\protected: varType varName;\public: virtual const varType& get##funName(void) const { return varName; }READ_ONLY就是只有get而没有set而已。
- 单例
单例被广泛使用,如Director控制器,就是一个单例对象。在2.x版中单例方法一般命名为shared+Xxx,3.0版中很多都改为getInstance方法。如:
auto director = Director::getInstance(); auto glView = EGLView::getInstance();