天天看點

3Animation動畫的建立,CCSpeed,CCFollow



動畫,不同于動作,動畫并非屬性的改變。而是對幀的播放。

3Animation動畫的建立,CCSpeed,CCFollow

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次):

3Animation動畫的建立,CCSpeed,CCFollow

6 ccspeed ccfollow

3Animation動畫的建立,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);

運作結果:

3Animation動畫的建立,CCSpeed,CCFollow

吃豆之後:

3Animation動畫的建立,CCSpeed,CCFollow

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

3Animation動畫的建立,CCSpeed,CCFollow
3Animation動畫的建立,CCSpeed,CCFollow
3Animation動畫的建立,CCSpeed,CCFollow