3Animation动画的创建,CCSpeed,CCFollow



  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);

}

运行结果:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

时间: 2024-07-28 20:24:56

3Animation动画的创建,CCSpeed,CCFollow的相关文章

使用动画转换创建生动的用户体验

典型的应用程序生存期由一些状态组成:数据输入表单.结果界面.含有各种图像的相簿.含有项目的购物车等等.通常,应用程序执行繁 重的任务使用户在这些不同的界面中切换:新的界面替代原来的表单,突然在屏幕中弹出结果,图形和 GUI 对象时而从屏幕中跳出.一般而言 ,各个当前 UI 屏幕都会在操作完成后关闭,然后新的屏幕将取代它的位置. HTML 应用程序就是一个很好的例子:在某个页面中填写一些数据,单击提交(Submit)按钮,原页面将消除,取而代之的是另一个全新的 用户界面.然后,您将仔细审视这个新界

实例讲解iOS应用UI开发之基础动画的创建_IOS

一.简单介绍 CAPropertyAnimation的子类 属性解析: fromValue:keyPath相应属性的初始值 toValue:keyPath相应属性的结束值 随着动画的进行,在长度为duration的持续时间内,keyPath相应属性的值从fromValue渐渐地变为toValue 如果fillMode=kCAFillModeForwards和removedOnComletion=NO,那么在动画执行完毕后,图层会保持显示动画执行后的状态.但在实质上,图层的属性值还是动画执行前的初

CSS3 Animations创建返回顶部的平滑动画

一般在一个长的网页中,我们都会有一个按钮,让用户能够快速的定位到网页的不同地方,一般都是一个返回顶部的按钮,这个功能我们都是使用的jQuery来完成,但是今天,我们为大家讲解一下不使用jQuery而是使用CSS3的动画,创建返回顶部的平滑动画效果.   返回顶部的动画效果,能够让我们快速的导航到菜单栏,通常,我们使用jQuery代码是这么编写代码的 $( 'body, html' ).animate({ scrollTop : 0 }, 800 ); 所以,如果我们设置scrollTop为500

Cocos2d-x3.2 Animate帧动画

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 //GameScene.h   #include "cocos2d.h"   class GameScene : public cocos2d::Layer { public:     static cocos2d::Scene* createScene();           virtual bool init();           void menuCallback(cocos2d::Ref* pSend

flash8.0形状补间动画详解及实例

形状补间动画是Flash中非常重要的表现手法之一,运用它,你可以变幻出各种奇妙的不可思议的变形效果. 本节从形状补间动画基本概念入手,带你认识形状补间动画在时间帧上的表现,了解补间动画的创建方法,学会应用"形状提示"让图形的形变自然流畅,最后,提供了二个实例练手,帮助你更深地理解形状补间动画. 1.形状补间动画的概念 (1)形状补间动画的概念 在Flash的时间帧面板上,在一个时间点(关键帧)绘制一个形状,然后在另一个时间点(关键帧)更改该形状或绘制另一个形状,Flash 根据二者之间

Silverlight:关键帧动画

本主题介绍 Silverlight 中的关键帧动画.通过关键帧动画,您可以使用两 个以上的目标值制作动画,并控制动画的内插方法. 先决条件 若要了解本概述,应先熟悉 Silverlight 动画. 什么是关键帧动 画? 与 From/To/By 动画类似,关键帧动画以动画形式显示了目标属性的值. 它通过其 Duration 创建其目标值之间的过渡.但是,From/To/By 动画创建两个 值之间的过渡,而单个关键帧动画可以创建任意数量的目标值之间的过渡. 与 From/To/By 动画不同,关键

Silverlight &amp;amp; Blend动画设计系列二:旋转动画(RotateTransform)

Silverlight的基础动画包括偏移.旋转.缩放.倾斜和翻转动画,这些基础动画毫无疑 问是在Silverlight中使用得最多的动画效果,其使用也是非常简单的.相信看过上一篇<偏 移动画(TranslateTransform)>文章的朋友大多数对Silverlight & Blend动画设计已 经产生了莫大的兴趣,本篇将继续介绍Silverlight中的基础动画之旋转动画 (RotateTransform). 所谓旋转动画(RotateTransform)也就是一个元素以一个坐标点

Silverlight &amp;amp; Blend动画设计系列一:偏移动画(TranslateTransform)

用户界面组件.图像元素和多媒体功能可以让我们的界面生动活泼,除此之外, Silverlight还具备动画功能,它可以让应用程序"动起来".实际上,英文中Animation这 个单词的意思是给某物带来生命.在界面中添加动画效果,给人以印象深刻可视化提示,可 以让用户的注意力集中到我们想让他们关注的地方. 动画主要是通过计时器来完成,在Silverlight中开发动画程序通常是使用微软主推的设 计工具Microsoft Expression Blend,Silverlight 中提供了优秀

iOS开发:UIView动画详解

  执行动画所需要的工作由UIView类自动完成,但仍要在希望执行动画时通知视图,为此需要将改变属性的代码包装到一个代码块中. 1.UIView动画具体创建方法 - (void)buttonPressed { // 交换本视图控制器中2个view位置 [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; //UIView开始动画,第一个参数是动画的标识,第二个参数附加的应用程序信息用来传递给动画代理消息 [UIView beginA