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

[Cocos2dx]How to set image for self-defined sprite?

本文关键字:自定义 精灵 设置 图像 Cocos2dx      更新时间:2023-10-16

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

怎么办?我需要覆盖自定义精灵类中的initWithFile吗?

你应该覆盖CCSprite的创建方法,你应该使用,并在Enter上退出方法,如:

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
}