[Cocos2dx]如何为自定义精灵设置图像?

时间:2013-10-19 15:10:37

标签: android c++ cocos2d-x

我正在使用Cocos2dx 2.1.4来开发游戏,并希望创建一个自定义的精灵类。但是,我不知道如何为它设置图像。像CCSprite::create("xxx.png")initWithFile("xx.png")这样的事情。

怎么做?我是否需要在自定义的精灵类中覆盖initWithFile

1 个答案:

答案 0 :(得分:1)

您应该覆盖要使用的CCSprite 创建方法和 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
}