Segmentation fault when i get texture from a sprite

I got segmentation fault when i execute this code:

void HelloWorld::loadCharacter(cocos2d::Size winS){
_spriteCache = SpriteFrameCache::getInstance();
for (int i = 1; i < 14; i++)
{
    std::string num = StringUtils::format("%d", i);
    Sprite *sprite = Sprite::create("head"+num+".tiff");
    SpriteFrame *spriteFrame = SpriteFrame::createWithTexture(sprite->getTexture(),sprite->getTextureRect());
    _spriteCache->addSpriteFrame(spriteFrame,"head"+num+".tiff");
    _idlePlayer.pushBack(_spriteCache->getSpriteFrameByName("head"+num+".tiff"));
}
_idleAnimation = Animation::createWithSpriteFrames(_idlePlayer, 0.05f);
_idleAnimation->retain();
auto temp_sprite = Sprite::create();
temp_sprite->setAnchorPoint(Vec2(0.5,0.5));
temp_sprite->setPosition(Vec2(winS.width * 0.2, winS.height * 0.5));
temp_sprite->setScale(0.1);
this->addChild(temp_sprite);
auto walk = Animate::create(_idleAnimation);
temp_sprite->runAction(RepeatForever::create(walk));

}

What is num?

Is a iterator, but this is not the problem.

Its crashes when i call sprite->getTexture() and sprite->getTextureRect()

Here is one way to do it. A loop works great too

https://docs.cocos2d-x.org/cocos2d-x/v4/en/actions/basic.html

I try create a SpriteFrame with a Sprite, passing the texture and rect.

I dont think .tiff format is supported, you should use png or jpg.
First try with single file first.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.