Cocos2d-x_CCAnimate(动画类)介绍

动画原理:

//
// HelloWorldScene.h
//

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;

class HelloWorld : public cocos2d::CCLayer
{
public:
    virtual bool init();
    static cocos2d::CCScene* scene();

    CREATE_FUNC(HelloWorld);

    void myUpdate(float dt);

private:
    int currentFramIdx = 0;
};

#endif
//
// HelloWorldScene.cpp
//

#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    CCScene *scene = CCScene::create();
    HelloWorld *layer = HelloWorld::create();
    scene->addChild(layer);
    
    return scene;
}

bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    // 添加4个精灵
    CCSprite *pSpr1 = CCSprite::create("crop1.png");
    CCSprite *pSpr2 = CCSprite::create("crop2.png");
    CCSprite *pSpr3 = CCSprite::create("crop3.png");
    CCSprite *pSpr4 = CCSprite::create("crop4.png");
    
    pSpr1->setPosition(ccp(100, 180));
    pSpr2->setPosition(ccp(100, 180));
    pSpr3->setPosition(ccp(100, 180));
    pSpr4->setPosition(ccp(100, 180));
    
    pSpr1->setVisible(true);
    pSpr2->setVisible(false);
    pSpr3->setVisible(false);
    pSpr4->setVisible(false);
    
    this->addChild(pSpr1,0,0);
    this->addChild(pSpr2, 0, 1);
    this->addChild(pSpr3, 0, 2);
    this->addChild(pSpr4, 0, 3);
    
    this->schedule(schedule_selector(HelloWorld::myUpdate), 1.0);

    return true;
}

void HelloWorld::myUpdate(float dt)
{
    CCLOG("HelloWorld::myUpdate");
    
    currentFramIdx ++;
    
    CCArray *array = this->getChildren();
    if (currentFramIdx >= array->count())
    {
        currentFramIdx = 0;
    }
    
    for (int i = 0; i < array->count(); i ++)
    {
        CCSprite *pSpr = (CCSprite *)this->getChildByTag(i);
        pSpr->setVisible(false);
    }
    
    CCSprite *pSpr = (CCSprite *)array->objectAtIndex(currentFramIdx);
    pSpr->setVisible(true);
}

<pre name="code" class="cpp">//
// HelloWorldScene.h
//

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;

class HelloWorld : public cocos2d::CCLayer
{
public:
    virtual bool init();
    static cocos2d::CCScene* scene();

    CREATE_FUNC(HelloWorld);
};

#endif

 
//
// HelloWorldScene.cpp
//

#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    CCScene *scene = CCScene::create();
    HelloWorld *layer = HelloWorld::create();
    scene->addChild(layer);
    
    return scene;
}

bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    // 手动添加帧序列创建动画
    CCSprite *pSpr = CCSprite::create("crop1.png");
    pSpr->setPosition(ccp(170, 200));
    this->addChild(pSpr);
    
    CCAnimation *animation = CCAnimation::create();
    animation->addSpriteFrameWithFileName("crop1.png");
    animation->addSpriteFrameWithFileName("crop2.png");
    animation->addSpriteFrameWithFileName("crop3.png");
    animation->addSpriteFrameWithFileName("crop4.png");
    animation->setDelayPerUnit(1.0f);
    animation->setRestoreOriginalFrame(true);
    animation->setLoops(-1);
    CCActionInterval *animate = CCAnimate::create(animation);
    pSpr->runAction(animate);
    
    // 通过资源文件创建动画
    CCTexture2D::PVRImagesHavePremultipliedAlpha(true);
    
    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("crop.plist");
    
    CCSprite *pSpr = CCSprite::createWithSpriteFrameName("crop1.png");
    pSpr->setPosition(ccp(170, 200));
    this->addChild(pSpr);
    
    CCArray *arrayFrames = CCArray::createWithCapacity(4);
    char str[100] = {0};
    for (int i = 1; i < 5; i ++)
    {
        sprintf(str, "crop%d.png",i);
        CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(str);
        arrayFrames->addObject(frame);
    }
    
    CCAnimation *animation = CCAnimation::createWithSpriteFrames(arrayFrames);
    animation->setLoops(-1);
    animation->setDelayPerUnit(1.0f);
    animation->setRestoreOriginalFrame(true);
    
    CCAnimate *animate = CCAnimate::create(animation);
    pSpr->runAction(animate);
    
    CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFramesFromFile("crop.plist");
    
    
    return true;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值