1. 动画,不同于动作,动画并非属性的改变。而是对帧的播放。

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 __T14Animation_H__

#include "cocos2d.h"

#include "TBack.h"

USING_NS_CC;

class T14Animation :public TBack

{

public:

static CCScene * scene();

CREATE_FUNC(T14Animation);

bool init();

CCSprite *spr;

void onEnter();

void onEnterTransitionDidFinish();

};

#endif

T14Animation.cpp

#include "T14Animation.h"

#include "AppMacros.h"

CCScene *T14Animation::scene()

{

CCScene * scene = CCScene::create();

T14Animation * layer = T14Animation::create();

scene->addChild(layer);

return scene;

}

bool T14Animation::init()

{

TBack::init();

return true;

}

//在进入场景的时候做以下操作

void T14Animation::onEnter()

{

TBack::onEnter();

//以图片的方式创建一个精灵

spr = CCSprite::create("animation/p_2_01.png");

//设置精灵的显示位置

spr->setPosition(ccp(winSize.width / 2, winSize.height / 2));

addChild(spr);

}

void T14Animation::onEnterTransitionDidFinish()

{

TBack::onEnterTransitionDidFinish();

//plist中是图片信息

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("animation/plant.plist");

//创建动画

CCAnimation * animation = CCAnimation::create();

//这个用于存储图片的名字

char  nameBuf[100];

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

{

memset(nameBuf, 0, sizeof(nameBuf));

sprintf(nameBuf, "p_2_0%d.png", i + 1);

animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(nameBuf));

}

//设置每次动画执行的时候的延时

animation->setDelayPerUnit(0.1f);

//这只循环两次

animation->setLoops(2);

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

spr->runAction(animate);

}

动画效果(2秒钟内循环执行了20次):

6 CCSpeed CCFollow

案例:

T15Speed.h

#ifndef _T15Speed_H__

#define _T15CCSpeed_H__

#include "cocos2d.h"

#include "TBack.h"

USING_NS_CC;

class T15Speed :public TBack

{

public:

static CCScene * scene();

CREATE_FUNC(T15Speed);

bool init();

//英雄这个精灵

CCSprite * hero;

//食物这个精灵

CCSprite * food;

CCMoveBy * by;

void update(float dt);

};

#endif

T15Speed.cpp

#include "T15Speed.h"

#include "AppMacros.h"

CCScene *T15Speed::scene()

{

CCScene * scene = CCScene::create();

T15Speed * layer = T15Speed::create();

scene->addChild(layer);

return scene;

}

bool T15Speed::init()

{

TBack::init();

hero = CCSprite::create("animation/hero.png");

hero->setPosition(ccp(100, 160));

addChild(hero);

by = CCMoveBy::create(10, ccp(300, 0));

hero->runAction(by);

food = CCSprite::create("animation/food.png");

food->setPosition(ccp(200, 160));

addChild(food);

//因为小人跑动的姿势都是一幅幅的动画效果组成,所以下面通过

//CCSpriteFrameCache的方式加载一系列的图片

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("animation/run.plist");

//创建动画效果

CCAnimation * animation = CCAnimation::create();

char nameBuf[100];

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

{

memset(nameBuf, 0, sizeof(nameBuf));

//取出图片等信息

sprintf(nameBuf, "run%d.png", i + 1);

animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(nameBuf));

}

//图片切换的时候的时间延迟为0.1f

animation->setDelayPerUnit(0.1f);

//下面的方式表示永久循环

animation->setLoops(-1);

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

hero->runAction(animate);

//开启定时器

scheduleUpdate();

return true;

}

void T15Speed::update(float dt)

{

//将boundingBox()缩放

CCRect heroRect = CCRect(hero->boundingBox().origin.x + 50,

hero->boundingBox().origin.y,

hero->boundingBox().size.width - 100,

hero->boundingBox().size.height);

//通过下面的方式实现:当人碰到豌豆了的时候移除豌豆

if (food&&heroRect.intersectsRect(food->boundingBox()))

{

//通过下面这句实现将food从主窗口中清除

food->removeFromParentAndCleanup(true);

food = NULL;

//通过CCSpeed将原来的速度增加5倍

CCSpeed * speed = CCSpeed::create(by, 5);

hero->runAction(speed);

}

}

运行结果:

吃豆之后:

CCFollow

T16CCFollow.h

#include "T16CCFollow.h"

#include "AppMacros.h"

CCScene *T16CCFollow::scene()

{

CCScene * scene = CCScene::create();

T16CCFollow * layer = T16CCFollow::create();

scene->addChild(layer);

return scene;

}

bool T16CCFollow::init()

{

TBack::init();

//背景

CCSprite * bg = CCSprite::create("map.png");

//设置锚点

bg->setAnchorPoint(ccp(0, 0));

addChild(bg);

//创建一个英雄的精灵

hero = CCSprite::create("animation/hero.png");

hero->setPosition(ccp(240, 160));

addChild(hero);

//下面的代码是添加动画的代码

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("animation/run.plist");

CCAnimation * animation = CCAnimation::create();

char nameBuf[100];

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

{

memset(nameBuf, 0, sizeof(nameBuf));

sprintf(nameBuf, "run%d.png", i + 1);

animation->addSpriteFrame(

CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(nameBuf));

}

animation->setDelayPerUnit(0.1f);

animation->setLoops(-1);

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

hero->runAction(animate);

by = CCMoveBy::create(10, ccp(400, 0));;

//添加假动作

CCCallFuncN * func = CCCallFuncN::create(this, callfuncN_selector(T16CCFollow::funcNCallBack));

CCSequence * seq = CCSequence::create(by, func, NULL);

hero->runAction(seq);

//使用CCFollow的方式创建英雄

CCFollow * follow = CCFollow::create(hero);

this->runAction(follow);

return true;

}

void T16CCFollow::funcNCallBack(CCNode * node)

{

CCSprite * spr = (CCSprite *)node;

CCLog("x = %g, y = %g", spr->getPositionX(), spr->getPositionY());

CCPoint world = this->convertToWorldSpace(spr->getPosition());

CCLog("x = %g, y = %g", world.x, world.y);

}

T16CCFollow.cpp

#include "T16CCFollow.h"

#include "AppMacros.h"

CCScene *T16CCFollow::scene()

{

CCScene * scene = CCScene::create();

T16CCFollow * layer = T16CCFollow::create();

scene->addChild(layer);

return scene;

}

bool T16CCFollow::init()

{

TBack::init();

//背景

CCSprite * bg = CCSprite::create("map.png");

//设置锚点

bg->setAnchorPoint(ccp(0, 0));

addChild(bg);

//创建一个英雄的精灵

hero = CCSprite::create("animation/hero.png");

hero->setPosition(ccp(240, 160));

addChild(hero);

//下面的代码是添加动画的代码

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("animation/run.plist");

CCAnimation * animation = CCAnimation::create();

char nameBuf[100];

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

{

memset(nameBuf, 0, sizeof(nameBuf));

sprintf(nameBuf, "run%d.png", i + 1);

animation->addSpriteFrame(

CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(nameBuf));

}

animation->setDelayPerUnit(0.1f);

animation->setLoops(-1);

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

hero->runAction(animate);

by = CCMoveBy::create(10, ccp(400, 0));;

//添加假动作

CCCallFuncN * func = CCCallFuncN::create(this, callfuncN_selector(T16CCFollow::funcNCallBack));

CCSequence * seq = CCSequence::create(by, func, NULL);

hero->runAction(seq);

//使用CCFollow的方式创建英雄

CCFollow * follow = CCFollow::create(hero);

this->runAction(follow);

return true;

}

void T16CCFollow::funcNCallBack(CCNode * node)

{

CCSprite * spr = (CCSprite *)node;

CCLog("x = %g, y = %g", spr->getPositionX(), spr->getPositionY());

CCPoint world = this->convertToWorldSpace(spr->getPosition());

CCLog("x = %g, y = %g", world.x, world.y);

}

运行结果:

3Animation动画的创建,CCSpeed,CCFollow相关推荐

  1. 【Flutter】Animation 动画 ( AnimatedBuilder 动画使用流程 | 创建动画控制器 | 创建动画 | 创建动画作用的组件 | 关联动画与组件 | 动画执行 )

    文章目录 ◯.AnimatedBuilder 引入 一.创建动画控制器 二.创建动画 三.创建动画作用的组件 四.创建 AnimatedBuilder 关联动画与组件 五.动画运行 六.完整代码示例 ...

  2. 【Flutter】Animation 动画 ( AnimatedWidget 动画使用流程 | 创建动画控制器 | 创建动画 | 创建 AnimatedWidget 动画组件 | 动画运行 )

    文章目录 ◯.AnimatedWidget 组件引入 一.创建 AnimatedWidget 动画组件 二.创建动画控制器 三.创建动画 四.动画运行 五.完整代码示例 六.相关资源 Animated ...

  3. 【Flutter】Animation 动画 ( Flutter 动画基本流程 | 创建动画控制器 | 创建动画 | 设置值监听器 | 设置状态监听器 | 布局中使用动画值 | 动画运行 )

    文章目录 一.创建动画控制器 二.创建动画 三.设置值监听器 四.设置状态监听器 五.布局中使用动画值 六.动画运行 七.完整代码示例 八.相关资源 Flutter 动画基本流程 : ① 创建动画控制 ...

  4. AnimatorController即动画控制器创建的BUG

    AnimatorController即动画控制器创建的BUG //------------------------------------------------------------------- ...

  5. 【Flutter】Hero 动画 ( Hero 动画使用流程 | 创建 Hero 动画核心组件 | 创建源页面 | 创建目的页面 | 页面跳转 )

    文章目录 ◯.Hero 动画简介 一.创建 Hero 动画核心组件 二.创建源页面 三.创建目的页面 四.页面跳转 五.完整代码示例 六.相关资源 ◯.Hero 动画简介 Hero Widget 动画 ...

  6. Android复习15【动画:创建资源文件夹、创建动画资源文件、组合动画、属性动画、材料设计新特性】

    2020-05-09-[12周-周四] Android动画 https://blog.csdn.net/zhangbijun1230/article/details/80262359 https:// ...

  7. UE4-(蓝图)第四十四课过场动画之创建及动画添加

    一.主序列.关卡序列.子序列之间的不同 区别在于他们的使用方法和组织结构,主序列包含关卡序列的子序列,就好比主序列是一个盒子,装满了包含其他内容的盒子,你可以移动大盒子里面的小盒子,主序列就是一个容器 ...

  8. Unity 2D教程 | 骨骼动画:创建动画

    转载自:2016-02-13 Unity官方平台 本教程主要讲解Unity引擎自带的2D骨骼动画工具,以及2D动画的基本概念.本篇会添加一些动画,如默认状态.跳动.坠落等. 基础动画理论 制作动画要牢 ...

  9. Flare动画进阶——创建可互动的一拳超人动画

    开头 我们直接来看最终的动画实现效果: (使用琦玉老师作为例子,是因为他的画风比较简单,非常适合新手操作!!!) 动画演示完毕,接下来,就是实现过程啦. 听,是引擎的声音.. 动画部分 如果你对于Fl ...

最新文章

  1. python3-pwntools教程_python的pwntools工具的日常使用
  2. NLog在Asp.Net MVC的实战应用
  3. 2021清华本科特奖答辩现场:有人用AI识别甲骨文,有人研究6G,奥运冠军:走下领奖台一切归零...
  4. linux动态链接库---一篇讲尽
  5. python3精要(50)-类
  6. 数据结构与算法 - 递归回溯(迷宫问题)
  7. 南工院C语言试卷答案,南工院11-12-1C期末B试卷 附答案.doc
  8. 【MySQL】数据库事务处理---MySQL
  9. SQL 注入竟然把我们的系统搞挂了
  10. @程序员,你真的会用 Unix 命令?
  11. 历经外企、创业公司、大厂的程序员告诉你:第一份工作有多重要!
  12. 利用socket.io构建一个聊天室
  13. 软件系统介绍文档模板
  14. mysql 客户端命令行_Windows的MySQL命令行客户端
  15. 极客日报第 21 期:360 安全浏览器尝试收费;苹果macOS首次出现在云端
  16. 【学习笔记】SAP资产模块
  17. ios开发学习--按钮(Button)效果源码分享
  18. 谢震业,离“苏神”还有多远?
  19. 中国汽车市场的“底层”骗局
  20. 解析拼手气红包金额划分算法

热门文章

  1. 性能远超AtomicLong,LongAdder原理完全解读
  2. VTK:命名颜色用法实战
  3. wxWidgets:使用事件
  4. wxWidgets:wxCheckBox类用法
  5. wxWidgets:将 PNG 图像文件包含到可执行文件中
  6. boost::type_index模块type_index`(和 `type_info`)能够存储确切的类型,无需剥离 const、volatile 和引用
  7. boost::tokenizer模块相关的测试程序
  8. boost::spirit模块实现一个以逗号分隔的数字列表的生成器的测试程序
  9. boost::smart_ptr模块智能指针测试程序
  10. boost::leaf::exception用法的测试程序