使用CCAnimate、CCAnimation、CCTextureCache、CCTexture2D来实现动画效果

来源:互联网 发布:js读取本地txt文件 编辑:程序博客网 时间:2024/05/16 09:04
使用CCTexture2D来创建动画效果,前提资源是有一张合成的大图
下面看具体的做法:
CCSprite* heroSprite = CCSprite::create();
heroSprite -> setAnchorPoint(ccp(0.35,0.3));
heroSprite -> setPosition(ccp(heroSprite -> getContentSize().width/2, m_winSize.height/2));
this -> addCHild(heroSprite,1,0);
CCAnimation* animation = CCAnimation::create();
animation -> setDelayPerUnit(0.3f);//设定时间间隔
animation -> setLoops(-1);//(-1)表示一直执行
CCTextureCache* cache = CCTextrueCache::sharedTextureCache();
CCTexture2D* texture = cache -> addImage("background.png");//这张图片上有要实现运动的各个图片
for(int i = 0; i < 5; i++)
{
  CCRect rect = CCRectMake(85*i,0,85,121);
  animation -> addSpriteFrameWithTexture(texture,rect);
}
CCAnimate* animate = CCAnmiate::create(animation);

heroSprite -> runAction(animate);


当然,如果你的图片是自己合成的,就应该有plist文件,这时候,就不用使用CCTexture2D来实现动画效果了,还有一个更简单的方法

例如:

CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();

cache -> addSpriteFrameWithFile("background.plist");

char temp[20];

CCArray* plistArray = CCArray::createWithCapacity(10);

for(int i = 0; i < 10; i++)

{

  sprintf(temp,"pook%d",i);

  CCSpriteFrame* frame = cache -> spriteFrameByName(temp);

  plistArray -> addObject(frame);

}

CCAnimation* animation = CCAnimation::createWithSpriteFrames(plistArray,0.1);

CCAnimate* animate = CCAnimate::create(animation);

//然后让执行此动画的精灵开始执行animate即可

//heroSprite -> runAction(animte);

0 0
原创粉丝点击