加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-x_CCAnimate(动画类)介绍

发布时间:2020-12-14 19:10:36 所属栏目:百科 来源:网络整理
导读:动画原理: //// 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(); st

动画原理:

//
// 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);
    this->addChild(pSpr2,1);
    this->addChild(pSpr3,2);
    this->addChild(pSpr4,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;
}


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读