[Wood Cocos2d-x 036] I don't need to know how many frames of animation you have to create helper classes

Source: Internet
Author: User

I don't need to know how many frames you have to create an animation helper class.

I am in a bad mood today, so I may be more nagging than usual.

(Xiao RuO: If you are in a bad mood, you need to get tired of us !)

 

OK. I am not writing a tutorial today. I just want to share my simple experiences with you. Presumably everyone thinks that it is a bit cumbersome to create a ccanimation Cocos2d-x, yes, we usually create a helper class, so that we can say that the repetitive work is omitted.

(Xiao RuO: I am not in the mood to get started quickly .)

 

 

What do you mean? Flowers? No, it's your heart ~

Reprinted please note, original address: http://blog.csdn.net/musicvs/article/details/8567220

 

Body:

 

 

For example, we usually see the following helper classes:

CCAnimation* AnimationUtil::createAnimationWithSingleName( const char* singleName, int iFrameNum ) {CCSpriteFrameCache* spriteFrameCache = CCSpriteFrameCache::sharedSpriteFrameCache();CCSpriteFrame* frame = NULL;CCArray* frameArray = CCArray::create();for(int i = 1; i <= iFrameNum; i++){frame = spriteFrameCache->spriteFrameByName(CCString::createWithFormat("%s%d.png", singleName, i)->getCString());frameArray->addObject(frame);} return CCAnimation::createWithSpriteFrames(frameArray);}

Of course, my default action images are all packaged with texturepacker, which is not covered in this article.

(Xiao RuO: I don't understand. Will I still tell you ?)

 

Here are the image resources we use:

 

This is the configuration file corresponding to the image. You can double-click it to see it:

 

(Xiao RuO: Ah! Look at your sister paper! Double-click it. You can show it to me! It's a picture, okay !)

Since the hero1.plist file is too long, I will not post it out. You can get an Action Resource by yourself.

(Xiao RuO: how bad your mood is today ...)

 

Then try using this function:

CCAnimation* heroAnim = AnimationUtil::sharedAnimationUtil()->createAnimationWithSingleName("hero1_atk", 6);heroAnim->setLoops(-1);heroAnim->setDelayPerUnit(0.1f);sprite->runAction(CCAnimate::create(heroAnim));

 

It is quite simple. You have specified the image resource name and the number of frames of the image resource. Then, createanimationwithsinglename creates the ccanimation object. This method can also be found in Baidu.

This is a good method. You only need to use one sentence to create an animation object. This tutorial is complete.

(Xiao RuO: I always feel something is wrong .... No! Didn't you tell us a better way ?!)

 

 

However, there is a more convenient method:

CCAnimation* AnimationUtil::createAnimationWithSingleName( const char* singleName ) {CCSpriteFrameCache* spriteFrameCache = CCSpriteFrameCache::sharedSpriteFrameCache();int index = 1;CCSpriteFrame* frame = NULL;CCArray* frameArray = CCArray::create();do {frame = spriteFrameCache->spriteFrameByName(CCString::createWithFormat("%s%d.png", singleName, index)->getCString());if(frame == NULL) {break;}frameArray->addObject(frame);index++;} while (true);return CCAnimation::createWithSpriteFrames(frameArray);}

 

The only difference from the previous method is:

1. The for loop is replaced by the do while loop.

2. No need to specify the number of frames

(Xiao RuO: the only difference is actually two points... Unique... Is this the only one! The only one should be one !)

 

 

This function will load the image frame according to the specified resource name until it encounters a null value. Although this is a bit opportunistic, it can save a lot of trouble: specifies the number of frames of an animation.

After we pack the image with texturepacker, it is impossible to load an animation every time. Open the pilist file and check the number of frames in the animation? I thought it was so troublesome, so I came up with this speculative method.

For reference only ~!

Well, I don't really want to post this article. I also know that it is not well written (in a bad mood, sorry), but I am afraid that I will forget it again.

(Xiao RuO: Even if you say so, I will not forgive you !)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.