Got a bug with frame animation?

I’m sure there is no error in the location of the plist file and png image, why is this error still showing?
the code as follows:

SpriteFrameCache::getInstance()->addSpriteFramesWithFile("/protagonist_move/a.plist");
	auto pSprite2 = Sprite::create();
	pSprite2->setPosition(500, 600);
	this->addChild(pSprite2,2);
	Vector<SpriteFrame*>list;
	list.reserve(2);
	list.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("/protagonist_move/10001.png"));
    list.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("/protagonist_move/10002.png"));
	auto pAnimation = Animation::createWithSpriteFrames(list, 0.2f, 1000);
	auto pAnimate = Animate::create(pAnimation);
	pSprite2->runAction(pAnimate);

the errors as follows:
11

please remove “/” from “”/protagonist_move/a.plist" => "“protagonist_move/a.plist”

The problem still exists after modification

I uploaded the demo file, please take a look at what is the problem.
demo.zip (1.2 MB)

The correct sprite frame name is “10020.png” and “10021.png”, you can check it in aaaa.plist, also you don’t need 10020.png and 10021.png, the aaaa.plist will add aaaa.png.

If 10020.png and 10021.png are not needed, how should the code be written?

The a.plist should containing the sprite frame name, then you can use the name to get the frame from cache.

https://docs.cocos.com/cocos2d-x/manual/en/sprites/spriteframe_cache.html

The following is a statement that fetches frames from the cache:

SpriteFrameCache::getInstance()->getSpriteFrameByName("protagonist_move/10020.png")

This sentence is contained in the following code:

	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("protagonist_move/aaaa.plist");
	auto pSprite2 = Sprite::create();
	pSprite2->setPosition(500, 600);
	this->addChild(pSprite2, 2);
	Vector<SpriteFrame*>list;
	list.reserve(2);
	list.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("protagonist_move/10020.png"));
	list.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("protagonist_move/10021.png"));
	auto pAnimation = Animation::createWithSpriteFrames(list, 0.2f, 1000);
	auto pAnimate = Animate::create(pAnimation);
	pSprite2->runAction(pAnimate);

But the error in the title appears after running. doesn’t solve the problem.
11

In your demo.zip, the aaaa.plist contains 2 sprite frame, which named 10020.png and 10021.png, when you add aaaa.plist to SpriteFrameCache, get the SpriteFrame by name 10020.png and 10021.png.
May be you misunderstand the usage of sprite sheet and sprite frame cache.
You don’t need /demo/Resources/protagonist_move/10020.png and 10021.png in the project when adding sprite sheet to SpriteFrameCache, just aaaa.plist and aaaa.png is enough.
All the needed information is stored in aaaa.plist, you can check it by text editor.

Please look also on the examples on cpp_test.
Plz read also the doc. The API und maybe also some c++ Tutorials too.
I belive you be a c++ beginner.

1 Like

I hope you find this advice to be helpful. Please contact us if you require any additional assistance.

I don’t know how to implement sprite framecache animation by just adding aaaa.plist and aaaa.png. The question is after adding aaaa.plist and aaaa.png to the sprite framechche, how should the next animation be implemented? I need the full code, if you know please provide the full code.

Regarding sprite framechche animation, the tutorial does not emphasize the file path problem of sprite frame acquisition, and the document does not give a complete implementation method of sprite framecache animation. It only explains how to obtain sprite frames into the cache.

After so many days of trying, I finally found the reason. When adding png images to the sprite framecache, the png images file path must be deleted. The modified complete code is as follows:

	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("protagonist_move/aaaa.plist");
	auto pSprite2 = Sprite::create();
	pSprite2->setPosition(500, 600);
	this->addChild(pSprite2, 2);
	Vector<SpriteFrame*>list;
	list.reserve(2);
	list.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("10020.png"));
	list.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("10021.png"));
	auto pAnimation = Animation::createWithSpriteFrames(list, 0.2f, 1000);
	auto pAnimate = Animate::create(pAnimation);
	pSprite2->runAction(pAnimate);

It is because the file path is really depends on how you create the sprite sheet, it can have folder structure inside the plist resources.