/*方法一*/
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)));