/*方法一*/
texture = CCTextureCache :: sharedTextureCache ()-> addImage ( "player.png" );
float width = texture -> getContentSize (). width / 4 ;
float height = texture -> getContentSize (). height ;
CCSprite *sprite = CCSprite :: createWithTexture ( texture , CCRectMake ( 0 , 0 , width, height));
sprite-> setPosition ( ccp (size.width/ 2 , size . height / 2 ));
this -> addChild (sprite);
/*方法二*/
/*
从缓存中获取动画
*/
/* 说明:
原理:实在已有的大图种分别占位,而缓存中大图是不会显示的,
分别添加时,就是在原来的大图中寻找图片的位置对应放置
1.player.png 中又 4 张图片;
2. 使用
CCSprite *pSprite = CCSprite::create("player.png");
CCTexture2D *pText2d = CCTextureCache::sharedTextureCache()->addImage("player.png");
添加的精灵都是大图精灵,一个大图包含所有动作的图片;
3. 创建时通过计算的出大图中单个小精灵的尺寸获取对应元素;
4. 创建 CCAnimation *animation = CCAnimation::create();// 创建动画
用于存储动画帧
5. 使用循环分别截取大图种包含的分动作
6. 使用 CCAnimate::create(animation) 转换为真正的动作
7. 使用 CCRepeatForever::create() 让动作无限重复
也可使用 animation->setLoops(-1);// 无限循环
8. 让精灵执行动作
pSprite->runAction();
*/
CCSprite *pSprite = CCSprite :: create ( "player.png" );
CCTexture2D *pText2d = CCTextureCache :: sharedTextureCache ()-> addImage ( "player.png" );
// 获取大图宽高
float w = pSprite-> getContentSize (). width / 4 ;
float h = pSprite-> getContentSize (). height ;
CCAnimation *animation = CCAnimation :: create (); // 创建动画
animation-> setDelayPerUnit ( 0.2f ); // 设置动画每一帧的间隔
// animation->setLoops(-1);// 无限循环
for ( int i = 0 ; i< 4 ; i++) {
animation-> addSpriteFrameWithTexture (pText2d, CCRectMake (i*w, 0 , 100 , h));
pSprite = CCSprite :: createWithTexture (pText2d, CCRectMake ( 0 , 0 , 100 , 99 ));
pSprite-> runAction ( CCRepeatForever :: create ( CCAnimate :: create (animation)));
}
pSprite-> setPosition ( ccp (size.width/ 4 , size . height / 2 ));
this -> addChild (pSprite);
/* 说明:
原理:实在已有的大图种分别占位,而缓存中大图是不会显示的,
分别添加时,就是在原来的大图中寻找图片的位置对应放置
1.player.png 中又 4 张图片;
2. 使用
CCSprite *pSprite = CCSprite::create("player.png");
CCTexture2D *pText2d = CCTextureCache::sharedTextureCache()->addImage("player.png");
添加的精灵都是大图精灵,一个大图包含所有动作的图片;
3. 创建时通过计算的出大图中单个小精灵的尺寸获取对应元素;
4. 创建 CCAnimation *animation = CCAnimation::create();// 创建动画
用于存储动画帧
5. 使用循环分别截取大图种包含的分动作
6. 使用 CCAnimate::create(animation) 转换为真正的动作
7. 使用 CCRepeatForever::create() 让动作无限重复
也可使用 animation->setLoops(-1);// 无限循环
8. 让精灵执行动作
pSprite->runAction();
*/
CCSprite *pSprite = CCSprite :: create ( "player.png" );
CCTexture2D *pText2d = CCTextureCache :: sharedTextureCache ()-> addImage ( "player.png" );
// 获取大图宽高
float w = pSprite-> getContentSize (). width / 4 ;
float h = pSprite-> getContentSize (). height ;
CCAnimation *animation = CCAnimation :: create (); // 创建动画
animation-> setDelayPerUnit ( 0.2f ); // 设置动画每一帧的间隔
// animation->setLoops(-1);// 无限循环
for ( int i = 0 ; i< 4 ; i++) {
animation-> addSpriteFrameWithTexture (pText2d, CCRectMake (i*w, 0 , 100 , h));
pSprite = CCSprite :: createWithTexture (pText2d, CCRectMake ( 0 , 0 , 100 , 99 ));
pSprite-> runAction ( CCRepeatForever :: create ( CCAnimate :: create (animation)));
}
pSprite-> setPosition ( ccp (size.width/ 4 , size . height / 2 ));
this -> addChild (pSprite);
/*方法三*/
/*
获取每个单帧的动画图片
有 5 张飞机的连续动作的图片 :player1,player2,player3,player4,player5
分别创建帧,然后添加到 CCAnimation 中
*/
CCSprite *pSprite = CCSprite :: create ( "player1.png" );
// 获取大图宽高
float w = pSprite-> getContentSize (). width ;
float h = pSprite-> getContentSize (). height ;
CCAnimation *animation = CCAnimation :: create (); // 创建动画
animation-> setDelayPerUnit ( 0.2f ); // 设置动画每一帧的间隔
// animation->setLoops(-1);// 无限循环
for ( int i = 1 ; i< 6 ; i++) {
CCString *names = CCString :: createWithFormat ( "player%d.png" ,i);
CCSpriteFrame *frame = CCSpriteFrame :: create (names-> getCString (), CCRectMake ( 0 , 0 , w, h));
animation-> addSpriteFrame (frame);
pSprite-> runAction ( CCRepeatForever :: create ( CCAnimate :: create (animation)));
}
pSprite-> setPosition ( ccp (size.width/ 4 , size . height / 2 ));
this -> addChild (pSprite);
有 5 张飞机的连续动作的图片 :player1,player2,player3,player4,player5
分别创建帧,然后添加到 CCAnimation 中
*/
CCSprite *pSprite = CCSprite :: create ( "player1.png" );
// 获取大图宽高
float w = pSprite-> getContentSize (). width ;
float h = pSprite-> getContentSize (). height ;
CCAnimation *animation = CCAnimation :: create (); // 创建动画
animation-> setDelayPerUnit ( 0.2f ); // 设置动画每一帧的间隔
// animation->setLoops(-1);// 无限循环
for ( int i = 1 ; i< 6 ; i++) {
CCString *names = CCString :: createWithFormat ( "player%d.png" ,i);
CCSpriteFrame *frame = CCSpriteFrame :: create (names-> getCString (), CCRectMake ( 0 , 0 , w, h));
animation-> addSpriteFrame (frame);
pSprite-> runAction ( CCRepeatForever :: create ( CCAnimate :: create (animation)));
}
pSprite-> setPosition ( ccp (size.width/ 4 , size . height / 2 ));
this -> addChild (pSprite);
/*方法四*/
/*
如何读取 plist 文件中的图片 FlagZombie_default.png 单帧图片动画
1. 先将 FlagZombie_default.png 文件的
大图 FlagZombie_default.png 添加到精灵;
2. 将 FlagZombie_default.plist 文件添加到 CCSpriteFrameCache
3. 定义一个数组用于存储动画精灵帧
4. 定义动画帧对象接收单帧动画
5. 设置 CCAnimation
6. 将 CCAnimation 封装到 CCAnimate
7. 让 pSprite 执行动画
*/
// 创建大图精灵
CCSprite *pSprite = CCSprite :: create ( "FlagZombie_default.png" );
// 将大图对应的 plist 文件读取到 CCSpriteFrameCache 中
CCSpriteFrameCache *cache = CCSpriteFrameCache :: sharedSpriteFrameCache ();
cache-> addSpriteFramesWithFile ( "FlagZombie_default.plist" );
char temp[ 100 ];
// 定义一个数组存储 CCAnimationFrame
CCArray *arr = CCArray :: create ();
// 循环添加动画帧
for ( int i = 1 ; i <= 12 ;i++) {
// 拼接获取 plist 文件中的单个图片帧
sprintf (temp, "FlagZombie%d.png" ,i);
CCSpriteFrame *frame = cache-> spriteFrameByName (temp);
如何读取 plist 文件中的图片 FlagZombie_default.png 单帧图片动画
1. 先将 FlagZombie_default.png 文件的
大图 FlagZombie_default.png 添加到精灵;
2. 将 FlagZombie_default.plist 文件添加到 CCSpriteFrameCache
3. 定义一个数组用于存储动画精灵帧
4. 定义动画帧对象接收单帧动画
5. 设置 CCAnimation
6. 将 CCAnimation 封装到 CCAnimate
7. 让 pSprite 执行动画
*/
// 创建大图精灵
CCSprite *pSprite = CCSprite :: create ( "FlagZombie_default.png" );
// 将大图对应的 plist 文件读取到 CCSpriteFrameCache 中
CCSpriteFrameCache *cache = CCSpriteFrameCache :: sharedSpriteFrameCache ();
cache-> addSpriteFramesWithFile ( "FlagZombie_default.plist" );
char temp[ 100 ];
// 定义一个数组存储 CCAnimationFrame
CCArray *arr = CCArray :: create ();
// 循环添加动画帧
for ( int i = 1 ; i <= 12 ;i++) {
// 拼接获取 plist 文件中的单个图片帧
sprintf (temp, "FlagZombie%d.png" ,i);
CCSpriteFrame *frame = cache-> spriteFrameByName (temp);
/*****************************重要步骤*************************************/
// 创建动画帧
CCAnimationFrame *animationframe = new CCAnimationFrame ();
animationframe -> initWithSpriteFrame (frame, 1 , NULL );
animationframe-> autorelease ();
// 将动画帧存入
arr-> addObject (animationframe);
}
// 将动画帧存入静态动画库中及放置的间隔时间
CCAnimation *animation = CCAnimation :: create (arr, 0.2 );
animation-> setLoops (- 1 ); // 设置无限循环
// animation->setDelayPerUnit(0.2f);// 设置每帧动画放置间隔的时间
pSprite-> runAction ( CCAnimate :: create (animation));
pSprite-> setPosition ( ccp (size.width/ 2 , size . height / 2 ));
this -> addChild (pSprite);
// 创建动画帧
CCAnimationFrame *animationframe = new CCAnimationFrame ();
animationframe -> initWithSpriteFrame (frame, 1 , NULL );
animationframe-> autorelease ();
// 将动画帧存入
arr-> addObject (animationframe);
}
// 将动画帧存入静态动画库中及放置的间隔时间
CCAnimation *animation = CCAnimation :: create (arr, 0.2 );
animation-> setLoops (- 1 ); // 设置无限循环
// animation->setDelayPerUnit(0.2f);// 设置每帧动画放置间隔的时间
pSprite-> runAction ( CCAnimate :: create (animation));
pSprite-> setPosition ( ccp (size.width/ 2 , size . height / 2 ));
this -> addChild (pSprite);
/*方法五*/
/*
如何读取 plist 文件中的图片 FlagZombie_default.png 单帧图片动画
1. 先将 FlagZombie_default.png 文件的
大图 FlagZombie_default.png 添加到精灵;
2. 将 FlagZombie_default.plist 文件添加到 CCSpriteFrameCache
3. 定义一个数组用于存储动画精灵帧
4. 定义动画帧对象接收单帧动画
5. 设置 CCAnimation
6. 将 CCAnimation 封装到 CCAnimate
7. 让 pSprite 执行动画
*/
如何读取 plist 文件中的图片 FlagZombie_default.png 单帧图片动画
1. 先将 FlagZombie_default.png 文件的
大图 FlagZombie_default.png 添加到精灵;
2. 将 FlagZombie_default.plist 文件添加到 CCSpriteFrameCache
3. 定义一个数组用于存储动画精灵帧
4. 定义动画帧对象接收单帧动画
5. 设置 CCAnimation
6. 将 CCAnimation 封装到 CCAnimate
7. 让 pSprite 执行动画
*/
//
创建大图精灵
CCSprite *pSprite = CCSprite :: create ( "FlagZombie_default.png" );
// 将大图对应的 plist 文件读取到 CCSpriteFrameCache 中
CCSpriteFrameCache *cache = CCSpriteFrameCache :: sharedSpriteFrameCache ();
cache-> addSpriteFramesWithFile ( "FlagZombie_default.plist" );
CCArray *array = CCArray :: create ();
// 循环添加动画帧
for ( int i = 1 ; i <= 12 ;i++) {
// 拼接获取 plist 文件中的单个图片帧
CCString *name = CCString :: createWithFormat ( "FlagZombie%d.png" ,i);
CCSpriteFrame *frame = cache-> spriteFrameByName (name-> getCString ());
CCLog ( "%.2f" ,frame-> getRect (). size . height );
CCSprite *pSprite = CCSprite :: create ( "FlagZombie_default.png" );
// 将大图对应的 plist 文件读取到 CCSpriteFrameCache 中
CCSpriteFrameCache *cache = CCSpriteFrameCache :: sharedSpriteFrameCache ();
cache-> addSpriteFramesWithFile ( "FlagZombie_default.plist" );
CCArray *array = CCArray :: create ();
// 循环添加动画帧
for ( int i = 1 ; i <= 12 ;i++) {
// 拼接获取 plist 文件中的单个图片帧
CCString *name = CCString :: createWithFormat ( "FlagZombie%d.png" ,i);
CCSpriteFrame *frame = cache-> spriteFrameByName (name-> getCString ());
CCLog ( "%.2f" ,frame-> getRect (). size . height );
/*****************************重要步骤*************************************/
CCAnimationFrame *animationframe = new CCAnimationFrame ();
animationframe -> initWithSpriteFrame (frame, 1 , NULL );
animationframe-> autorelease ();
array-> addObject (animationframe);
}
CCAnimation *animation = CCAnimation :: create (array, 0.2f );
pSprite-> setPosition ( ccp (size.width/ 4 , size . height / 2 ));
this -> addChild (pSprite);
pSprite-> runAction ( CCRepeatForever :: create ( CCAnimate :: create (animation)));
CCAnimationFrame *animationframe = new CCAnimationFrame ();
animationframe -> initWithSpriteFrame (frame, 1 , NULL );
animationframe-> autorelease ();
array-> addObject (animationframe);
}
CCAnimation *animation = CCAnimation :: create (array, 0.2f );
pSprite-> setPosition ( ccp (size.width/ 4 , size . height / 2 ));
this -> addChild (pSprite);
pSprite-> runAction ( CCRepeatForever :: create ( CCAnimate :: create (animation)));