Sprite Sheet Animation Error->object != nullptr [RESOLVED!]

Hey guys, I’m trying to create an animation from a spritesheet but my app keeps
crashing when I run it. I get an error saying:

Assert failed: The object should not be nullptr
Assertion failed!
cocos2d\co...\ccvector.h
Line: 271
Expression: object != nullptr

My plist embedded files are named C1_%Facing%_Walk_i=0(i++).png

Here is my helloworld,

// Initialise TileMap Background
	std::string file = "tilemaps/TileMap.tmx";
	auto str = String::createWithContentsOfFile (FileUtils::getInstance()->fullPathForFilename(file.c_str()).c_str());
    _tileMap = TMXTiledMap::createWithXML(str->getCString(),"");
    _background = _tileMap->layerNamed("Background");
    addChild(_tileMap, -1);

// Initialise TileMap Objects
TMXObjectGroup *objects = _tileMap->getObjectGroup("Object-Player");
CCASSERT(NULL != objects, "'Object-Player' object group not found");
auto playerSpawn = objects->getObject("PlayerSpawn");
CCASSERT(!playerSpawn.empty(), "PlayerSpawn object not found");
int x = playerSpawn["x"].asInt();
int y = playerSpawn["y"].asInt();

// Initialise Sprite Cache
SpriteBatchNode* spriteSheet = SpriteBatchNode::create("Sprites/Characters/C1_SpriteSheet.png");
SpriteFrameCache* frameCache = SpriteFrameCache::getInstance();
frameCache->addSpriteFramesWithFile("Sprites/Characters/C1_SpriteSheet.plist");
_player = Sprite::createWithSpriteFrameName("C1_Front_Walk_0.png");
_player->setPosition(x + _tileMap->getTileSize().width / 2, y + _tileMap->getTileSize().height / 2);
_player->setScale(1);
spriteSheet->addChild(_player);
addChild(spriteSheet);

// Initialise Sprite Animation
Vector<SpriteFrame*> animFrames(4);
char fn[100] = {0};
for(int i = 0; i < 4; i++) 
{
	sprintf(fn, "C1_Front_Walk_%d.png", i);
	SpriteFrame* frame = frameCache->getSpriteFrameByName(fn);
	animFrames.pushBack(frame);
}
Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.1);
_player->runAction( RepeatForever::create( Animate::create(animation) ) );

// Create camera lock onto player
setViewPointCenter(_player->getPosition());

// Wait for Player Input for control and movement
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [&](Touch *touch, Event *unused_event)->bool {return true;};
listener->onTouchEnded = CC_CALLBACK_2(LocalMapScene::onTouchEnded, this);
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

return true;

You’ll need to tell us which line is causing that assertion to fire.

I will say the way you initialise str looks very dodgy. What’s wrong with:

string str = FileUtils::getInstance()->getStringFromFile("tilemaps/TileMap.tmx");

thanks for the reply.

// Initialise Sprite Animation
Vector<SpriteFrame*> animFrames(4);
char fn[100] = {0};
for(int i = 0; i < 4; i++) 
{
	sprintf(fn, "C1_Front_Walk_%d.png", i);
	SpriteFrame* frame = frameCache->getSpriteFrameByName(fn);
	animFrames.pushBack(frame);
}

this part here is what causes the error. i believe it is to do with adding the frames to the vector.

my png files are named as
C1_Front_Walk_0.png
C1_Front_Walk_1.png
C1_Front_Walk_2.png
C1_Front_Walk_3.png
C1_Right_Walk_0.png
etc

and they are in a plist file. all the resources are in the same folder
resource/sprites/characters

Unless you’ve added sprites/characters to the search path used by FileUtils, you’ll need to include that in the filename.

Also get rid of that sprintf() and use:

string fn = StringUtils::format("sprites/characters/C1_Front_Walk_%d.png", i);

Also please note that the folder is called Resources, not resources.

1 Like

^ this fixed the problem!!

Thank you so much =)