Cocos2d-x animation tools

Source: Internet
Author: User
Forbidden. h file # ifndef_AnimateUtil_H _ # define_AnimateUtil_H _ # includecocos2d. hus

//////////////////////////////////////. Hfile # ifndef _ AnimateUtil_H _ # define _ AnimateUtil_H _ # include cocos2d. h us

Bytes

//////////////////////////////////////. H file

# Ifndef _ AnimateUtil_H _

# Define _ AnimateUtil_H _
# Include "cocos2d. h"
Using namespace cocos2d;
Using namespace std;
Class AnimateUtil // animation tool class
{
Public:
// Create an animation object name based on the file name prefix to determine whether the playback interval is loop playback.
Static Animation * createWithSingleFrameName (const char * name, float delay, int Loops );
// Create an animation object based on the file name prefix, and specify the number of animated images. Name the number of pictures. The playback interval indicates whether the animation is played cyclically.
Static Animation * createWithFrameNameAndNum (const char * name, int num, float delay, int Loops );
};

# Endif

//// //. Cpp File

# Include "AnimateUtil. h"


Animation * AnimateUtil: createWithSingleFrameName (const char * name, float delay, int Loops)
{
/* Load the image to the sprite frame buffer pool */
SpriteFrameCache * frameCache = SpriteFrameCache: getInstance ();
Vector FrameVec;
SpriteFrame * frame = NULL;
Int index = 1; // number of small images
Do
{
// Obtain the SpriteFrame object from the SpriteFrame Buffer Pool
Frame = frameCache-> getSpriteFrameByName (StringUtils: format ("%s0000d.png", name, index ++ ));
// Continuously obtain the spriteFrame object until the obtained value is NULL.
If (frame = NULL)
{
Break;
}
FrameVec. pushBack (frame );
} While (true );
// Use the SpiteFrame list to create an animation object
Animation * animation = Animation: createWithSpriteFrames (frameVec );
Animation-> setLoops (Loops); // you can specify whether to loop.
Animation-> setRestoreOriginalFrame (true );
Animation-> setDelayPerUnit (delay); // sets the animation gap
Return animation;
}


Animation * AnimateUtil: createWithFrameNameAndNum (const char * name, int num, float delay, int Loops)
{
SpriteFrameCache * frameCache = SpriteFrameCache: getInstance ();


SpriteFrame * frame = NULL;
Vector FrameVec;
Int index = 1;
For (int I = 1; I <= num; I ++)
{
Frame = frameCache-> getSpriteFrameByName (StringUtils: format ("%s0000d.png", name, index ++ ));
If (frame = NULL)
{
Break;
}
FrameVec. pushBack (frame );
}
Animation * animation = Animation: createWithSpriteFrames (frameVec );
Animation-> setLoops (Loops );
Animation-> setRestoreOriginalFrame (true );
Animation-> setDelayPerUnit (delay );
Return animation;
}

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.