zoukankan      html  css  js  c++  java
  • cocos2d-x创建精灵动画

    创建动画一般过程:

    1、创建精灵框架缓存,并向其中添加相应的动画文件(plist),最后,通过动画集缓存生产动画

        CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache();
        cache->addSpriteFramesWithFile("animations/grossini.plist");
        cache->addSpriteFramesWithFile("animations/grossini_gray.plist", "animations/grossini_gray.png");
        CCSpriteBatchNode *spritebatch = CCSpriteBatchNode::create("animations/grossini.png");//用于批量生产精灵 
        CCArray* animFrames = CCArray::createWithCapacity(15);//动态生成数组,类似于vector
        for(int i = 1; i < 4; i++)
        {
            //sprintf的作用是字符串格式化,主要功能是把格式化的数据写入某个字符串中。sprintf(str, “ani_conch_%d.png”, 1)后str的值就变成了:ani_conch_1.png
            sprintf(str, "grossini_blue_%02d.png",i);//两位数字,不够的话,用零补全
            CCSpriteFrame *frame = cache->spriteFrameByName(str);
            animFrames->addObject(frame);
        }
        animation = CCAnimation::createWithSpriteFrames(animFrames, 0.2f);   //创建动画集,切换时间为0.2s
        // Add an animation to the Cache
        CCAnimationCache::sharedAnimationCache()->addAnimation(animation, "dance_blue"); // 把此动画集加入动画集缓存中,并命名为dance_blue
        CCAnimationCache *animCache = CCAnimationCache::sharedAnimationCache();     //共享动画集缓存 
        CCAnimation *normal = animCache->animationByName("dance");      //获得动画集缓存
        CCAnimate *animN = CCAnimate::create(normal);      //创建动画
    


       2、直接传人多个图片文件,生成动画

     

        CCSprite *mainsprite=CCSprite::create("catBody1.png");
        CCAnimation *animation=CCAnimation::create();
        animation->addSpriteFrameWithFileName("catBody1.png");
        animation->addSpriteFrameWithFileName("catBody2-4.png");
        animation->addSpriteFrameWithFileName("catBody3.png");
        animation->addSpriteFrameWithFileName("catBody2-4.png");
        animation->setDelayPerUnit(0.1f);//设置动画的间隔时间
        animation->setRestoreOriginalFrame(true);//是否返回第一帧
        mainsprite->runAction(CCRepeatForever::create(CCAnimate::create(animation)));

       

  • 相关阅读:
    由浅入深——从ArrayList浅谈并发容器
    Lambda表达式
    JVM初体验
    Redis主从复制
    Redis事务
    Redis基本命令
    Redis概述与安装
    Linux之SSH免密登录
    源码安装Nginx以及用systemctl管理
    CentOS 7 :Failed to start IPv4 firewall with iptables.
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3249184.html
Copyright © 2011-2022 走看看