---
title: "CCAnimation创建动画-沈大海cocos2d-x教程22"
author: "mmdev"
date: "2012-11-30T16:11:00Z"
tags: []
slug: "ccanimation-cocos2d-x-22"
draft: false
lastmod: "2012-12-05T21:57:49Z"
id: 1742034
url: "https://mmdev.iteye.com/blog/1742034"
---

CCSize s = CCDirector::sharedDirector()-&gt;getWinSize();  
//载入动画所需纹理图片  
CCTexture2D \*texture = CCTextureCache::sharedTextureCache()-&gt;addImage("animations/dragon\_animation.png");

// manually add frames to the frame cache// 切图获取每一帧  
CCSpriteFrame \*frame0 = CCSpriteFrame::createWithTexture(texture, CCRectMake(132\*0, 132\*0, 132, 132));  
CCSpriteFrame \*frame1 = CCSpriteFrame::createWithTexture(texture, CCRectMake(132\*1, 132\*0, 132, 132));  
CCSpriteFrame \*frame2 = CCSpriteFrame::createWithTexture(texture, CCRectMake(132\*2, 132\*0, 132, 132));  
CCSpriteFrame \*frame3 = CCSpriteFrame::createWithTexture(texture, CCRectMake(132\*3, 132\*0, 132, 132));  
CCSpriteFrame \*frame4 = CCSpriteFrame::createWithTexture(texture, CCRectMake(132\*0, 132\*1, 132, 132));  
CCSpriteFrame \*frame5 = CCSpriteFrame::createWithTexture(texture, CCRectMake(132\*1, 132\*1, 132, 132));

//  
// Animation using Sprite BatchNode  
//  
CCSprite* sprite = CCSprite::createWithSpriteFrame(frame0);  
sprite-&gt;setPosition( ccp( s.width/2-80, s.height/2) );  
addChild(sprite);

CCArray* animFrames = CCArray::createWithCapacity(6);  
animFrames-&gt;addObject(frame0);  
animFrames-&gt;addObject(frame1);  
animFrames-&gt;addObject(frame2);  
animFrames-&gt;addObject(frame3);  
animFrames-&gt;addObject(frame4);  
animFrames-&gt;addObject(frame5);

CCAnimation \*animation = CCAnimation::createWithSpriteFrames(animFrames, 0.2f);//这里定义了每一帧，和动画的间隔时间  
CCAnimate \*animate = CCAnimate::create(animation);//这里创建了动画的Action  
CCActionInterval* seq = (CCActionInterval\*)(CCSequence::create( animate,  
CCFlipX::create(true),  
animate-&gt;copy()-&gt;autorelease(),  
CCFlipX::create(false),  
NULL) );

sprite-&gt;runAction(CCRepeatForever::create( seq ) );

/.//////////////////////////////////////////////////////////////////////////////////////////////////////////貌似很简单，可以在应用中用该方法加入各种特效

但真正游戏中的角色，NPC，需要做碰撞的内容最好还是用SpriteBatchNode 来管理每个Sprite 通过Schedule自定义编程的方式来实现运动和变化。