动画,不同于动作,动画并非属性的改变。而是对帧的播放。
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
方法二(对一的改进)
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);
ccanimate * animate = ccanimate::create(animation);
4
更简单的
ccspriteframecache::sharedspriteframecache()->addspriteframeswithfile(“animation/plant.plist”);
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);
5
案例说明:
t14animation.h
#ifndef
__t14animation_h__
#define
#include
"cocos2d.h"
"tback.h"
using_ns_cc;
class
t14animation :public
tback
{
public:
static
ccscene *
scene();
create_func(t14animation);
bool
init();
ccsprite *spr;
void
onenter();
onentertransitiondidfinish();
};
#endif
t14animation.cpp
"t14animation.h"
"appmacros.h"
ccscene *t14animation::scene()
scene =
ccscene::create();
t14animation *
layer =
t14animation::create();
scene->addchild(layer);
return
scene;
t14animation::init()
tback::init();
true;
//在进入场景的时候做以下操作
t14animation::onenter()
tback::onenter();
//以图片的方式创建一个精灵
spr =
ccsprite::create("animation/p_2_01.png");
//设置精灵的显示位置
spr->setposition(ccp(winsize.width
/ 2, winsize.height
/ 2));
addchild(spr);
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->setloops(2);
ccanimate *
animate =
ccanimate::create(animation);
spr->runaction(animate);
动画效果(2秒钟内循环执行了20次):
6 ccspeed ccfollow
案例:
t15speed.h
#ifndef _t15speed_h__
_t15ccspeed_h__
t15speed :public
create_func(t15speed);
//英雄这个精灵
ccsprite *
hero;
//食物这个精灵
food;
ccmoveby *
by;
update(float
dt);
t15speed.cpp
"t15speed.h"
ccscene *t15speed::scene()
t15speed *
t15speed::create();
t15speed::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,
addchild(food);
//因为小人跑动的姿势都是一幅幅的动画效果组成,所以下面通过
//ccspriteframecache的方式加载一系列的图片
ccspriteframecache::sharedspriteframecache()->addspriteframeswithfile("animation/run.plist");
//创建动画效果
char
i < 15;i++)
//取出图片等信息
"run%d.png",
//图片切换的时候的时间延迟为0.1f
//下面的方式表示永久循环
animation->setloops(-1);
hero->runaction(animate);
//开启定时器
scheduleupdate();
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);
null;
//通过ccspeed将原来的速度增加5倍
ccspeed *
speed =
ccspeed::create(by,
5);
hero->runaction(speed);
运行结果:
吃豆之后:
ccfollow
t16ccfollow.h
"t16ccfollow.h"
ccscene *t16ccfollow::scene()
t16ccfollow *
t16ccfollow::create();
t16ccfollow::init()
//背景
bg =
ccsprite::create("map.png");
//设置锚点
bg->setanchorpoint(ccp(0,
0));
addchild(bg);
//创建一个英雄的精灵
hero->setposition(ccp(240,
//下面的代码是添加动画的代码
i < 15;
animation->addspriteframe(
ccspriteframecache::sharedspriteframecache()->spriteframebyname(namebuf));
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);
t16ccfollow::funcncallback(ccnode
* node)
spr = (ccsprite
*)node;
cclog("x
= %g, y = %g",
spr->getpositionx(),
spr->getpositiony());
ccpoint
world =
this->converttoworldspace(spr->getposition());
world.x,
world.y);
t16ccfollow.cpp