zoukankan      html  css  js  c++  java
  • 【Cocos2d-X开发学习笔记】第21期:动画类(CCAnimate)的使用

    本系列学习教程使用的是cocos2d-x-2.1.4(最新版为3.0alpha0-pre) ,PC开发环境Windows7,C++开发环境VS2010

        

           之前我们已经学习过一些方法让节点“动”起来,Cocos2D-X中还有一种动作,就是动画类CCAnimate。要实现

    CCAnimate,还需要定义CCAnimation等类。在这一期中,我们主要使用两种方法来实现动画的效果。

    一、手动添加帧序列实现动画

    1、CCAnimate和CCAnimation类的使用方法。

    <1> CCAnimate::create(CCAnimation * pAnimation)

    作用:根据CCAnimation中的帧序列、帧间隔,不断切换精灵帧,形成动画效果。

    参数:CCAnimation对象。

    <2> CCAnimation::create()

    作用:创建一个动画的帧序列信息,其中包括帧间隔和循环次数。

    2、CCAnimation类的常用函数。

    <1> setDelayPerUnit(float var)

    作用:设置帧间隔的时间。

    参数:帧间隔。

    注意:如果此函数不设置,动画将无法动态播放!

    <2> setRestoreOriginalFrame(bool var)

    作用:当动画播放完后,帧序是否重设为默认第一帧。

    参数:其值为true,表示动画播完后回到第一帧。

    <3> setLoops(unsigned int var)

    作用:设置循环次数。

    参数:循环次数,其值为-1时,动画无线循环。

    <4> setFrames(CCArray * var)

    作用:设置动画帧数组。

    参数:帧数组。

    3、项目示例。


    首先新建Cocos2D-X项目,取名为“MyAnimation01”,然后在HelloWorldScene.cpp文件的init函数中添加如下代码。

    bool HelloWorld::init()
    {
        bool bRet = false;
        do 
        {
            CC_BREAK_IF(! CCLayer::init());
    
            CCSprite* sp = CCSprite::create("crop1.png");
    		sp->setPosition(ccp(170,200));
    		addChild(sp);
        
    	    CCAnimation* animation = CCAnimation::create();
    		animation->addSpriteFrameWithFileName("crop1.png");
    		animation->addSpriteFrameWithFileName("crop2.png");
    		animation->addSpriteFrameWithFileName("crop3.png");
    		animation->addSpriteFrameWithFileName("crop4.png");
    		
    		animation->setDelayPerUnit(2.8f / 14.0f);//必须设置否则不会动态播放
    		animation->setRestoreOriginalFrame(true);//是否回到第一帧
    		animation->setLoops(-1);//重复次数 (-1:无限循环)
    		
    		CCFiniteTimeAction * animate = CCAnimate::create(animation);
    		sp->runAction(animate);
    
            bRet = true;
        } while (0);
    
        return bRet;
    }


     

    4、示例效果图。

                         

    二、使用plist配置文件实现动画

          在Mac系统的Cocoa等编程框架中,属性列表文件是一种用来存储串行化后的对象的文件。属性列表文件的扩展

    名为plist,因此通常被称为plist文件。plist文件通常用于存储用户设置,也可以用于存储捆绑的信息,该功能在旧式的

    Mac系统中是由资源分支提供的。通过plist文件也可以定义动画。

    1、首先来介绍两个函数。

    <1> CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(const char * pszPlist)

    作用:根据plist中的每个精灵图片名,创建对应的帧CCSpriteFrame放入帧缓存中。

    参数:plist资源文件名。

    <2> CCAnimation::createWithSpriteFrames(CCArray * frames,float delay)

    作用:根据帧数组与帧间隔创建一个动画的帧序列信息。

    参数1:帧数组。

    参数2:帧间隔。

    2、项目示例。


    首先新建Cocos2D-X项目,取名为“MyAnimation02”,然后在HelloWorldScene.cpp文件的init函数中添加如下代码。

    bool HelloWorld::init()
    {
        bool bRet = false;
        do 
        {
            CC_BREAK_IF(! CCLayer::init());		
    
            CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("crop.plist");
        
    		//利用帧缓存创建精灵
    		CCSprite* sp = CCSprite::createWithSpriteFrameName("crop1.png");
    		sp->setPosition(ccp(170,200));
    		addChild(sp);
        
    		CCArray* animFrames = CCArray::createWithCapacity(4);
    		char str[100] = {0};
    		for(int i = 1; i < 5; i++)
    		{
    			sprintf(str, "crop%i.png", i);
    			CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(str);
    			animFrames->addObject(frame);
    		}
    
    		CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames, 0.3f);
    		animation->setLoops(-1);
    		sp->runAction(CCAnimate::create(animation));
        
    	//CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFrameByName("crop.plist")
       
    
            bRet = true;
        } while (0);
    
        return bRet;
    }


     

    3、示例效果图。

                         

    源码下载地址

  • 相关阅读:
    ajax01
    django04
    数据库
    WeakHashMap类
    IdentityHashMap
    Hashtable类
    LinkedHashMap类
    HashMap和TreeMap类
    PriorityQueue
    Synchronized
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3265183.html
Copyright © 2011-2022 走看看