您好,欢迎访问一九零五行业门户网

cocos2dx3.0的代码风格

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__, std::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();
其它类似信息

推荐信息