我正在使用Cocos2dx 2.1.4 开发游戏,想创建一个自定义的精灵类。但是,我不知道如何为其设置图像。之类的东西CCSprite::create("xxx.png")
或initWithFile("xx.png")
。
怎么做?我需要initWithFile
在自定义精灵类中覆盖吗?
你应该重写你想要使用的 CCSprite 的create方法和onEnter onExit方法,比如:
MySprite* MySprite::create(const char *pszFileName)
{
MySprite *pobSprite = new MySprite();
if (pobSprite && pobSprite->initWithFile(pszFileName))
{
pobSprite->autorelease();
return pobSprite;
}
CC_SAFE_DELETE(pobSprite);
return NULL;
}
void MySprite::onEnter()
{
// Register touch delegate
}
void MySprite::onExit()
{
// Unregister touch delegate
}