Home Game Development Got a bug with body animation? – C++

Got a bug with body animation? – C++

0
Got a bug with body animation? – C++

[ad_1]

I’m certain there is no such thing as a error within the location of the plist file and png picture, why is that this error nonetheless displaying?
the code as follows:

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

the errors as follows:
11

please take away “/” from “”/protagonist_move/a.plist” => ““protagonist_move/a.plist”

The drawback nonetheless exists after modification

I uploaded the demo file, please check out what’s the drawback.
demo.zip (1.2 MB)

The right sprite body identify is “10020.png” and “10021.png”, you may verify it in aaaa.plist, additionally you don’t want 10020.png and 10021.png, the aaaa.plist will add aaaa.png.

If 10020.png and 10021.png usually are not wanted, how ought to the code be written?

The a.plist ought to containing the sprite body identify, then you need to use the identify to get the body from cache.

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

The following is an announcement that fetches frames from the cache:

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

This sentence is contained within the following code:

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

But the error within the title seems after operating. doesn’t resolve the issue.
11

In your demo.zip, the aaaa.plist incorporates 2 sprite body, which named 10020.png and 10021.png, whenever you add aaaa.plist to SpriteFrameCache, get the SpriteBody by identify 10020.png and 10021.png.
May be you misunderstand the utilization of sprite sheet and sprite body cache.
You don’t want /demo/Resources/protagonist_move/10020.png and 10021.png within the venture when including sprite sheet to SpriteFrameCache, simply aaaa.plist and aaaa.png is sufficient.
All the wanted info is saved in aaaa.plist, you may verify it by textual content editor.

Please look additionally on the examples on cpp_test.
Plz learn additionally the doc. The API und possibly additionally some c++ Tutorials too.
I belive you be a c++ newbie.

1 Like

I hope you discover this recommendation to be useful. Please contact us when you require any further help.

I don’t know the best way to implement sprite framecache animation by simply including aaaa.plist and aaaa.png. The query is after including aaaa.plist and aaaa.png to the sprite framechche, how ought to the following animation be carried out? I would like the complete code, if you realize please present the complete code.

Regarding sprite framechche animation, the tutorial doesn’t emphasize the file path drawback of sprite body acquisition, and the doc doesn’t give an entire implementation technique of sprite framecache animation. It solely explains the best way to receive sprite frames into the cache.

After so many days of making an attempt, I lastly discovered the explanation. When including png photographs to the sprite framecache, the png photographs file path should be deleted. The modified full code is as follows:

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

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here