两阶段构造器及静态create()函数
第一阶段:运行C++类构造器
在C++类默认构造器中,成员变量必须设定为默认值,但不应该在它里面编写任何逻辑,因为不会返回值。
Myclass::MyClass(): _data(NULL), _flag(false), _count(0) {
memset(_array, 0, sizeof(_array));
}
第二阶段:调用MyClass:init()函数
bool MyClass::initWithFilename(const std::string& filename) {
// just take loading texture as a sample, this behaviour can fail if the image file doesn't exist.
bool bReturnValue = loadTextureIntoMemory(filename);
return bReturnValue;
}
所以,可以构建这样一段代码:
MyClass* obj = new MyClass;
if (true == obj->initWithFilename("texture.png")) {
// congratulations, go ahead!
}
else {
// error process
}
在Cocos2d-x中,已经包装好这两阶段的构造器,并且在create()中会自动释放引用计数,每一个cocos2d类都有自己的静态create方法。
doSomething()
doWithResource()
onEvenCallback()
onAction表明这是一种回调函数,当引发一定条件时,其它类可以调用这种方法。
getInstance
单例模式类,没有create方法,但是有getInstance方法,对应的析构方法为destroyInstance()。
属性:getter和setter
setProperty()
改变属性的值
getProperty()
因为不能修改属性,所以使用const,例如const CCSize& getSize() const;
isProperty()
类似getProperty,返回类型为bool。
总结
如果属性为“只读”,将不会有 setProperty(type) 方法;
如果属性为一个bool值,将会有 setProperty(bool) 及 isProperty() 方法。 比如: Sprite::isDirty() 和 Sprite::setDirty(bool bDirty) 。
如果属性不是一个bool值,将会有 setProperty(type) 和 getProperty() 方法。比如: void Sprite::setTexture(Texture2D*) 和 Texture2D* CCSprite::getTexture() 。