我正在使用 Cocos2dx 2.1.4 开发游戏,想创建一个自定义的 Sprite 类。但是,我不知道如何为它设置图像。像 CCSprite::create("xxx.png")
或 initWithFile("xx.png")
这样的东西。
怎么做?是否需要覆盖自定义 Sprite 类中的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
}
关于android - [Cocos2dx]如何为自定义 Sprite 设置图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19467522/