新手请教!如何建立一个炸弹类继承CCSprite, 新继承类用create() 和 createWithSpriteFrameName()读取图片?
我准备新定义了一个炸弹类,继承CCSprite ,此炸弹还带有几个子精灵(光环,闪光,冲击波等)。
建立这个炸弹类的目的,是用CCArray 存储 10个炸弹, 玩家可以放置多个炸弹,用定时器 检测多个炸弹的冲击波。
但是,自己建立的新类是无法直接使用create() 和 createWithSpriteFrames()函数读取图片的。
我查询了下资料,按照create()方法重写了个函数,就可以直接读取文件了。(见:static gamesprite* gameSpriteWithFile(const char * pszFileName);)
现在想了解,如何重写createWithSpriteFrames()函数,才能读取精灵帧缓存???
感谢。
====create()方法重写gameSpriteWithFile读图片文件。成功。
.h定义
class gamesprite : public cocos2d::CCSprite
{
public:
gamesprite(void);
~gamesprite(void);
static gamesprite* gameSpriteWithFile(const char * pszFileName);
};
.cpp实现
gamesprite* gamesprite::gameSpriteWithFile( const char pszFileName) {
gamesprite sprite = new gamesprite();
if(sprite&&sprite->initWithFile(pszFileName))
{
sprite->autorelease();
return sprite;
}
CC_SAFE_DELETE(sprite);
return NULL;
}
_player1 = gamesprite::gameSpriteWithFile(“Icon-72.png”);
==========
createWithSpriteFileName方法重写 gameWithSpriteFileName,失败,运行报错。
.h
class org_bomb : public cocos2d::CCSprite
{
public:
org_bomb();
~org_bomb();
static org_bomb* gameWithSpriteFrame(CCSpriteFrame* pSpriteFrame);
static org_bomb* gameWithSpriteFrameName(const char pszSpriteFrameName);
bool isvisible;
CCSprite halo;
CCSprite* sparkle;
CCSprite* _shockWave;
};
.CPP
org_bomb* org_bomb::gameWithSpriteFrame(CCSpriteFrame *pSpriteFrame)
{
org_bomb *pobSprite = new org_bomb();
if (pSpriteFrame && pobSprite && pobSprite->initWithSpriteFrame(pSpriteFrame))
{
pobSprite->autorelease();
return pobSprite;
}
CC_SAFE_DELETE(pobSprite);
return NULL;
}
org_bomb* org_bomb::gameWithSpriteFrameName(const char* pszSpriteFrameName){
CCSpriteFrame *pFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(pszSpriteFrameName);
#if COCOS2D_DEBUG > 0
char msg = {0};
sprintf(msg, “Invalid spriteFrameName: %s”, pszSpriteFrameName);
CCAssert(pFrame != NULL, msg);
#endif
return org_bomb::gameWithSpriteFrame(pFrame);
}
报错:Undefined symbols for architecture x86_64:
“org_bomb::org_bomb()”, referenced from:
org_bomb::gameWithSpriteFrame(cocos2d::CCSpriteFrame*) in HelloWorldScene.o
org_bomb::gameWithSpriteFrameName(char const*) in HelloWorldScene.o