//1、精灵文件创建动画(文件在硬盘上)
//生成第一个精灵,动画实际上是该精灵的一种动作(该精灵为动画的第一帧)
CCSprite *sp = CCSprite::create("crop1.png");
sp->setPosition(ccp(100,100));
addChild(sp);
//创建动画动作(在此对象中放入需要创建动画的一切材料)
CCAnimation *animation = CCAnimation::create();
//将每帧图放入动画类中
for (int i = 0; i < 4; i++) {
char str[100];
sprintf(str, "crop%d.png",i+1);
//用文件名创建精灵帧
animation->addSpriteFrameWithFileName(str);
}
//设置帧间隔时间,此参数必须设置,无此参数动画不会播放
animation->setDelayPerUnit(.5f); //设置延迟每个单元
//动画播放完毕后,帧序是否重设为默认第一帧
animation->setRestoreOriginalFrame(true);//如果参数为true动画会重设为第一帧,如果flase停在最后一帧
//设置循环次数,-1代表无限次循环
animation->setLoops(2); //设置动画播放几次
//形成动画效果
CCFiniteTimeAction *animate = CCAnimate::create(animation);
sp->runAction(animate);
</p>//生成一个精灵
CCSprite *sp = CCSprite::create("crop1.png");
sp->setPosition(ccp(170,200));
addChild(sp);
//将每帧图放入动画类中
CCAnimation *animation = CCAnimation::create();
for (int i = 0; i < 4; i++) {
char str[100];
sprintf(str, "crop%d.png",i+1);
//生成一个取悦
CCRect rect = CCRect(0,0,sp->getContentSize().width,sp->getContentSize().height);
//第二个参数指定截取第一个参数指定的图片的一部分(文件名,图像大小)
CCSpriteFrame *frame = CCSpriteFrame::create(str, rect);
animation->addSpriteFrame(frame);
}
//设置帧间隔时间,此参数必须设置,无此参数动画不会播放
animation->setDelayPerUnit(.5f); //设置延迟每个单元
//动画播放完毕后,帧序是否重设为默认第一帧
animation->setRestoreOriginalFrame(true);//如果参数为true动画会重设为第一帧,如果flase停在最后一帧
//设置循环次数,-1代表无限次循环
animation->setLoops(-1); //设置动画播放几次
//形成动画效果
CCFiniteTimeAction *animate = CCAnimate::create(animation);
sp->runAction(animate);
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
//使用资源文件创建精灵帧缓存(一次性把所有文件读入)
CCTexture2D::PVRImagesHavePremultipliedAlpha(true);
//精灵帧缓存(只有一个对象)
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("crop.plist");
//从精灵帧缓存中创建第一个精灵
CCSprite *sp = CCSprite::createWithSpriteFrameName("crop1.png");
sp->setPosition(ccp(170, 200));
addChild(sp);
//将精灵帧缓存中的每个精灵放入数组中
CCArray *animFrames = CCArray::createWithCapacity(4);
char str[100] = {0};
for (int i = 1; i < 5; i++) {
sprintf(str, "crop%i.png",i);
CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(str);
//将得到的精灵帧放入数组
animFrames->addObject(frame);
}
//将该数组放入动画类对象中
CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames,1.0f);
//形成动画效果
animation->setLoops(-1);
sp->runAction(CCAnimate::create(animation));
//使用addSpriteFramesWithFile函数将帧添加到缓存后,不使用时再使用下面的函数释放
//CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFrameByName(crop.plist);
return true;
}

被折叠的 条评论
为什么被折叠?



