Advance Spriteframe animation.

Hi all, I would like to share a class which I wrote. Its called AdvanceSprite and its basically an easier way to control sprite animations. You can do things like set frame rate for every object, move from any frame to any frame. Loops n number of times etc. I feel its easier than using the CCAction for Sprite Frame animation.

Animation done using Sprite frames by loading Sprite Sheet.

Description:
It extends from CCSprite. Its more advance and easy to use then CCAction.

General Controls:

  1. Pause animation.
  2. Resume animation.
  3. Stop animation.
  4. Callback after complete the animation.

Additional control are

  1. Changing frame rate during running of animation.
  2. while running animation we can move to any frames from current running frame.
  3. Looping Animation.
  4. Reverse Animation with Looping.
  5. Delete by itself after complete animation.

Example:

//Create Object….
AdvanceSprite m_advanceSprite = new AdvanceSprite();

//Add Sprite pList or CCSpriteFrame Array.
m_advanceSprite~~>addFrames;
//Run Animation
m_advanceSprite~~>startAnimation(int startInd, int endInd, int number_Loop, SEL_CallFunc pfnSelectorDelegate, SelectorProtocol *pTarget,int NumberOfFramesPerSecond, bool NeedToRunReverseAnimation, bool NeedToDeleteItself);

//To move to any frame from the current frame
m_advanceSprite->moveTo(frameNo);


Advance_Sprite_Animation.zip (3.4 KB)

Oh, it looks very nice! beautiful coding style, well commented methods.
And you’re doing your magic in AdvanceSprite::update

ye, nice share!

I believe I found a “bug” in this code.

I think:

AdvanceSprite::AdvanceSprite
{
\ if
\ CC_SAFE_DELETE_ARRAY;
}
should\ be:
AdvanceSprite::
AdvanceSprite()
{
if(m_isPlistLoaded)
CC_SAFE_DELETE(m_AnimationFrames);
}

Otherwise it crashes when you attempt to “delete” an AdvanceSprite created with “new”. Also “stopAnimation” was spelled “stopAnimaiton” throughout the class. Other than that, thank you VERY much for this class. I am finding it quite useful.

Thanks for feedback.

How can i add this to the layer for rendering?

Pls Checkout the github. I have added examples how it works. This is link https://github.com/varadharaj/cocos2d-x.

@varadharaj
Bin is creating the cross-platform feature & test framework in cocos2d-x-extension repository this week. It will be ready soon :slight_smile:

Thanks for the class. I noticed a huge increase in performance when switching from CCMutableArray<SpriteFrame*> to std::vector<SpriteFrame*> for m_AnimationFrames.

m_AnimationFrames->getObjectAtIndex(m_CurrentIndex) in updateAnim is the culprit

AdvanceSprite didn’t work for me with the newer version of cocos2d-x without some modification. This article will tell you how to fix it http://www.cocos2d-x.org/boards/6/topics/4700

I took the easy option adding the init() to the CCSprite constructor… for now.

i will check out This is link https://github.com/varadharaj/cocos2d-x.
thank you for your work.

Old topic but I wanted to use this class with the latest cocos2d-2.0-x-2.0.2 so I updated it today and it seems to work fine.

Just include the files into your project and use it like this:

        m_pAdvanceSprite = new AdvanceSprite();
        m_pAdvanceSprite->init();   // Must call init for latest version cocos2d-x

        m_pAdvanceSprite->setPosition(ccp(size.width/2,size.height/2));
        addChild(m_pAdvanceSprite);
        m_pAdvanceSprite->addFrames("mystictemple.plist","mystictemple.png");
        m_pAdvanceSprite->startAnimation(1, MT_NUM_FRAMES, -1, 0, this, 30, true, false);

If there are any suggestions for improvement then let me know while it is fresh on my mind. The header file is well documented on what the meaning of the parameters.

Thanks

Francis

Thanks
it helped

Hey guys,

I’ve noticed that when I use this class (Francis Styck’s version) on PC it works perfectly, but when I try to use it on iOS, the animation frames are out of order. I believe the problem actually lies in the Cocos code, in the function, CCSpriteFrameCache::addSpriteFramesWithDictionary, which uses the CCDICT_FOREACH macro. This macro returns the frames in order on PC, but when I trace this in xcode, the frames are not returned in order.

I’m wondering, have any of you guys noticed this problem?

thanks!

Ok, I believe I see the problem. The macro, CCDICT_FOREACH, does not always return elements in order, and so in addFrames, sometimes the the frames are added to m_AnimationFrames out of order.

In my local copy, I fixed this by adding a “frameNumber” key to the .plist file generated by TexturePacker, and then I made sure the frames are added to m_AnimationFrames in the order of the frameNumber.

Hi, how can i male Callback after complete the animation

I am using Francis Styck’s version

Thanks