Problem About Spritesheet Animation

Problem About Spritesheet Animation

Hi everyone,

I wrote that code for spritesheet animation. But it gives error about pushBack() function. Where is the problem about this code ?

SpriteBatchNode* spriteBatchNode = SpriteBatchNode::create("Character.png");
	SpriteFrameCache* frameCache = SpriteFrameCache::getInstance();
	auto sprite = Sprite::createWithSpriteFrameName("wlk1.png");
	//spriteBatchNode->setAnchorPoint(Vec2(0.0, 0.0));
	spriteBatchNode->setPosition(Point(visibleSize.width / 2 + origin.x, 100));

	for (int i = 1; i <= 8; i++)
		std::string frameName = cocos2d::StringUtils::format("wlk%d.png", i);
		SpriteFrame* frame = frameCache->getSpriteFrameByName(frameName.c_str());

	Animation* animation = Animation::createWithSpriteFrames(frames, 0.1f);
	auto animate = Animate::create(animation);

Can you tell us the error?

You are using the Cocos Vector class?

Just use an std::vector


Is your SpriteFrame that you put in the vector valid? Also, I think an std::vector is a better choice long term.

1 Like

I tried std::vector, but it gives another error.

Have you looked at our docs on animations or cpp-tests?

I looked cpp-tests but i couldn’t find anything about spritesheet animations :frowning:

It looks like the Vector added 6 objects, can you make sure the 7th and on are valid names in your sprite sheet plist?

You should be pulling from SpriteCache?

    auto spritecache = SpriteFrameCache::getInstance();
    Vector animFrames;
    char str[100];
    for(int i = 1; i <= count; i++)
        sprintf(str, format, i);

Thank you soo much. My 7th sprite name was wrong :smiley: The animation works fine but now there is another problem. As you can see i put the character half of width and 100px for height. But it doesn’t seen on 100px height, it seen on 0px height. if i write 200px for the height, it seen on 100px :confused:

1 Like

How did you create the sprite sheet?

With TexturePackerGUI

If you don’t see the whole sprite something is wrong, load the plist into cache and pull from that.

No,I see. But it looks 100px below where it should be.

The Sprite is likely using the default CENTER anchor point of Vec2(0.5,0.5). Make it Vec2(0.5,0).
sprite->setAnchorPoint(Vec2(0.5f, 0));

1 Like

And all my problems solved. Thank you soo much :slight_smile:

Sure thing, be sure to help others in the cocos community (or world for that matter) when you can …

1 Like