写继承cocos2d继承精灵类
由于想要自己写一个新的精灵类,然后不太会写和如何继承精灵类,查阅网上资料得出:
一,看下cocos2d精灵类
其中重要的几个创建类:
static Sprite* create()
static Sprite* create(conststd::string& filename)
static Sprite* create(conststd::string& filename, constRect& rect)
static Sprite* createWithTexture(Texture2D *texture)
static Sprite* createWithTexture(Texture2D *texture, constRect& rect, bool rotated=false)
static Sprite* createWithSpriteFrame(SpriteFrame *spriteFrame)
static Sprite* createWithSpriteFrameName(conststd::string& spriteFrameName)
仿写:
class MySprite : public Sprite
{
static MySprite * create(const char *filename);
virtual Sprite* newInstance();
}
Sprite* MySprite::newInstance()
{
return new MySprite();
}
MySprite * MySprite ::create(const char *filename)
{
MySprite *sprite = newInstance();
if (sprite && sprite->initWithFile(filename))
{
sprite->autorelease(); return sprite;
}
CC_SAFE_DELETE(sprite);
return nullptr;
}
# 总结
继承精灵类可以仿造精灵类写就可以了,其他重要的类也是如此。 仿写完如果不想要不停调用创建函数可以继承父类就行,一样的写法。
如果想要加新的特性就只需要加一系列的Get()和Set()函数和属性就可以了。