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;
}