3Animation动画的创建,CCSpeed,CCFollow


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

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 方法二(对一的改进)

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

3Animation动画的创建,CCSpeed,CCFollow

6 CCSpeed CCFollow

3Animation动画的创建,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);

    }

}

运行结果:

3Animation动画的创建,CCSpeed,CCFollow

吃豆之后:

3Animation动画的创建,CCSpeed,CCFollow

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

3Animation动画的创建,CCSpeed,CCFollow

3Animation动画的创建,CCSpeed,CCFollow

 

 

 

 

 

 

 

 

 

 

 

 

 

 

上一篇:Linux下进程内存管理之malloc和sbrk


下一篇:还在自建代码仓库?阿里云的这款企业级代码管理工具免费、还香!