- 动画,不同于动作,动画并非属性的改变。而是对帧的播放。
2
方法一
CCSprite * sp = CCSprite::create(“animation/p_2_01.png”); sp->setPosition(ccp(240,160)); //注意:这里的CCRectMake中的参数都是相对自己来说的,前两个参数定义了一个点,80,80表示的是矩形的长,宽。 CCSpriteFrame * frame1 = CCSpriteFrame::create("animation/p_2_01.png",CCRectMake(0,0,80,80)) ; CCSpriteFrame * frame2 = CCSpriteFrame::create("animation/p_2_02.png",CCRectMake(0,0,80,80)) ; CCSpriteFrame * frame3 = CCSpriteFrame::create("animation/p_2_03.png",CCRectMake(0,0,80,80)) ; CCSpriteFrame * frame4 = CCSpriteFrame::create("animation/p_2_04.png",CCRectMake(0,0,80,80)) ; CCSpriteFrame * frame5 = CCSpriteFrame::create("animation/p_2_05.png",CCRectMake(0,0,80,80)) ; CCSpriteFrame * frame6 = CCSpriteFrame::create("animation/p_2_06.png",CCRectMake(0,0,80,80)) ; CCSpriteFrame * frame7 = CCSpriteFrame::create("animation/p_2_07.png",CCRectMake(0,0,80,80)) ; CCSpriteFrame * frame8 = CCSpriteFrame::create("animation/p_2_08.png",CCRectMake(0,0,80,80)) ; CCAnimation * animation = CCAnimation::create(); animation->addSpriteFrame(frame1); animation->addSpriteFrame(frame2); animation->addSpriteFrame(frame3); animation->addSpriteFrame(frame4); animation->addSpriteFrame(frame5); animation->addSpriteFrame(frame6); animation->addSpriteFrame(frame7); animation->addSpriteFrame(frame8); animation->setDelayPerUnit(0.1f); animation->setLoops(kCCRepeatForever); CCAnimate *animate = CCAnimate::create(animation); sp->runAction(animate); addChild(sp); |
3
方法二(对一的改进)
CCSprite * sp = CCSprite::create(“animation/p_2_01.png”); sp->setPosition(ccp(240,160)); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFrameWithFile(“animation/plant.plist”); char bufname[100]; CCArray * array = CCArray::create(); for(int I = 1;i<= 8;i++) { memset(bufname,”p_2_0%d.png”,i); CCSpriteFrame * frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(bufname); array->addObject(frame); } CCAnimation * animation = CCAnimation::createWithSpriteFrames(array,0.2f); animation->setLoops(kCCRepeatForever); CCAnimate * animate = CCAnimate::create(animation); addChild(sp); |
4
更简单的
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(“animation/plant.plist”); char bufname[100]; CCAnimation * animation = CCAnimation::create(); for(int i = 1;i<=8;i++){ memset(bufname,sizeof(bufname),0); sprint(bufname,”p_2_0%d.png”,i); animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(bufname)); } animation->setDelayPerUnit(0.2f); animation->setLoops(kCCRepeatForever); |
5
案例说明:
T14Animation.h |
#ifndef __T14Animation_H__ #define #include #include USING_NS_CC; class { public: static CREATE_FUNC(T14Animation); bool CCSprite *spr; void void }; #endif |
T14Animation.cpp |
#include "T14Animation.h" #include CCScene *T14Animation::scene() { CCScene * T14Animation * scene->addChild(layer); return } bool { TBack::init(); return } //在进入场景的时候做以下操作 void { TBack::onEnter(); //以图片的方式创建一个精灵 spr = //设置精灵的显示位置 spr->setPosition(ccp(winSize.width addChild(spr); } void { TBack::onEnterTransitionDidFinish(); //plist中是图片信息 CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("animation/plant.plist"); //创建动画 CCAnimation * //这个用于存储图片的名字 char for (int { memset(nameBuf, sprintf(nameBuf, animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(nameBuf)); } //设置每次动画执行的时候的延时 animation->setDelayPerUnit(0.1f); //这只循环两次 animation->setLoops(2); CCAnimate * spr->runAction(animate); } |
动画效果(2秒钟内循环执行了20次): |
6 CCSpeed CCFollow
案例:
T15Speed.h |
#ifndef _T15Speed_H__ #define #include #include USING_NS_CC; class { public: static CREATE_FUNC(T15Speed); bool //英雄这个精灵 CCSprite * //食物这个精灵 CCSprite * CCMoveBy * void }; #endif |
T15Speed.cpp |
#include "T15Speed.h" #include CCScene *T15Speed::scene() { CCScene * T15Speed * scene->addChild(layer); return } bool { TBack::init(); hero = hero->setPosition(ccp(100, addChild(hero); by = hero->runAction(by); food = food->setPosition(ccp(200, addChild(food); //因为小人跑动的姿势都是一幅幅的动画效果组成,所以下面通过 //CCSpriteFrameCache的方式加载一系列的图片 CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("animation/run.plist"); //创建动画效果 CCAnimation * char for (int { memset(nameBuf, //取出图片等信息 sprintf(nameBuf, animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(nameBuf)); } //图片切换的时候的时间延迟为0.1f animation->setDelayPerUnit(0.1f); //下面的方式表示永久循环 animation->setLoops(-1); CCAnimate * hero->runAction(animate); //开启定时器 scheduleUpdate(); return } void { //将boundingBox()缩放 CCRect hero->boundingBox().origin.y, hero->boundingBox().size.width hero->boundingBox().size.height); //通过下面的方式实现:当人碰到豌豆了的时候移除豌豆 if (food&&heroRect.intersectsRect(food->boundingBox())) { //通过下面这句实现将food从主窗口中清除 food->removeFromParentAndCleanup(true); food = //通过CCSpeed将原来的速度增加5倍 CCSpeed * hero->runAction(speed); } } |
运行结果: 吃豆之后: |
CCFollow
T16CCFollow.h |
#include "T16CCFollow.h" #include CCScene *T16CCFollow::scene() { CCScene * T16CCFollow * scene->addChild(layer); return } bool { TBack::init(); //背景 CCSprite * //设置锚点 bg->setAnchorPoint(ccp(0, addChild(bg); //创建一个英雄的精灵 hero = hero->setPosition(ccp(240, addChild(hero); //下面的代码是添加动画的代码 CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("animation/run.plist"); CCAnimation * char for (int { memset(nameBuf, sprintf(nameBuf, animation->addSpriteFrame( CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(nameBuf)); } animation->setDelayPerUnit(0.1f); animation->setLoops(-1); CCAnimate * hero->runAction(animate); by = //添加假动作 CCCallFuncN * CCSequence * hero->runAction(seq); //使用CCFollow的方式创建英雄 CCFollow * this->runAction(follow); return } void { CCSprite * CCLog("x CCPoint CCLog("x } |
T16CCFollow.cpp |
#include "T16CCFollow.h" #include CCScene *T16CCFollow::scene() { CCScene * T16CCFollow * scene->addChild(layer); return } bool { TBack::init(); //背景 CCSprite * //设置锚点 bg->setAnchorPoint(ccp(0, addChild(bg); //创建一个英雄的精灵 hero = hero->setPosition(ccp(240, addChild(hero); //下面的代码是添加动画的代码 CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("animation/run.plist"); CCAnimation * char for (int { memset(nameBuf, sprintf(nameBuf, animation->addSpriteFrame( CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(nameBuf)); } animation->setDelayPerUnit(0.1f); animation->setLoops(-1); CCAnimate * hero->runAction(animate); by = //添加假动作 CCCallFuncN * CCSequence * hero->runAction(seq); //使用CCFollow的方式创建英雄 CCFollow * this->runAction(follow); return } void { CCSprite * CCLog("x CCPoint CCLog("x } |
运行结果: |