Cocos2d-x 动画小结

 动画:

创建 :  Vector<SpritFrame*>frames     Animation        Animate    runAction


            {

                         //通常的做法

                         VectorSpriteFrame * > frames;


                         //加载plist到frames

                         SpriteFrameCache::getInstance ()->addSpriteFramesWithFile ("xxx.plist" );

              for ( int i = 1; i < 14; ++ i)   //14是有多少的个图片

                        {

                        char buf[256];

                         sprintf_s(buf , "%s%02d%s" , "xxxxx" , i , ".png" );

                         SpriteFrame * frame = SpriteFrameCache::getInstance ()->getSpriteFrameByName (buf );

                                     framespushBackframe);

                        }

             //执行动画

                         Animation * animation = Animation:: createWithSpriteFrames(frames , 0.1f);


                         //动画缓存

                         AnimationCache::getInstance ()->addAnimation (animation , "Myanimation" );

                        

                         auto ev = EventListenerTouchOneByOne::create ();


                         ev-> onTouchBegan = [&]( Touch * touch , Event *)

                        {

                                     this-> runAnimation();

                        };


                         this-> _eventDispatcher->addEventListenerWithSceneGraphPriority (ev , this );

void  runAnimation ()

{

             //加入缓存实现动画

             Animate * animate;

             animate = Animate::create (AnimationCache ::getInstance ()->getAnimation ("Myanimatio" ));


             Sprite * sp = Sprite:: create();

             sp-> runActionRepeatForever::create (animate ));

             this-> addChildsp);

             sp-> setPosition(100, 200);


}



动画缓存的删除方式:先动画缓存,再精灵帧缓存,再纹理缓存,依依依靠,一个引用一个

             //删除 先删除动画缓存,然后精灵帧缓存, 纹理缓存,依依释放

             AnimationCache ::destroyInstance ();

             SpriteFrameCache ::destroyInstance ();

             Director:: getInstance()-> getTextureCache ()->removeAllTextures ();


cocos2d-3.6版本提供了接口,可以直接使用Plist的文件。但是这个对Plist的文件有很高的要求。一般的Plist都是不可以的



封装一个动作的接口

Animation*  getAnimation (const char * plist , float delay )

{

             Animationret = AnimationCache::getInstance ()->getAnimation (plist );

             if ( retreturn ret;


             VectorSpriteFrame*> frames;

             SpriteFrameCache::getInstance ()->addSpriteFramesWithFile (plist );


             std:: vector<std ::stringframeNames;


             /* 通过plist文件,获取所有的帧的名字 */

             ValueMap vm = FileUtils:: getInstance()-> getValueMapFromFile(plist );

             ValueMapvmFrames = vm"frames"].asValueMap ();

             for ( auto it = vmFramesbegin(); it != vmFramesend(); ++ it)

            {

                         frameNamespush_back(it ->first);

            }

            // 对已经取出的图片进行排序

             std:: sortframeNamesbegin(), frameNamesend());

             for ( auto it = frameNamesbegin(); it != frameNamesend(); ++ it)

            {

                         CCLOG"%s", (*it ).c_str ());

                         SpriteFrameframe = SpriteFrameCache::getInstance ()->getSpriteFrameByName (*it );

                         framespushBackframe);

            }


             ret = Animation:: createWithSpriteFrames(frames , delay );

             AnimationCache::getInstance ()->addAnimation (ret , plist );

             return ret;

}

// 动画第一帧用来创建精灵


Sprite * sprite = Sprite::createWithSpriteFrame(animation->getFrames().at(0)->getSpriteFrame());





骨骼动画:

它的配置文件格式是   .atlas  

添加骨骼动画必须的头文件

并且在编译器属性中加入骨骼动画所在的文件路径

$(EngineRoot)cocos\editor-support

#include "spine/spine.h"
#include "spine/spine-cocos2dx.h"

创建与执行骨骼动画

spine::SkeletonAnimation* ske =
          spine::SkeletonAnimation::createWithFile("spine/spineboy.json", "spine/spineboy.atlas");
ske->setAnimation(0, "run", true);  // 三个参数 第一个默认音符,第二个是执行动画,第三个是循环播放

设置两个动作切换的时间差

     ske->setMix("run", "hit", 1.0f);
     ske->setMix("hit", "walk", 0.5f);

时间缩放比例, 越小越慢

ske->setTimeScale(time.0f);

设置是否显示骨骼

ske->setDebugBonesEnabled(true);

捕捉信号

设置回调函数,在setAnimation()中设置

spTrackEntry* entry = ske->setAnimation(0, "run", true);
// 两个参数  std::string(ev->data->name)  获取名字
ske->setEventListener([](int trackIndex, spEvent* ev){
          if (std::string(ev->data->name) == "footstep")
          {
               CCLOG("foot step event");
          }
     });

给骨骼主角设置别的属性

ske->setAttachment( "gun" , 0 ); //0就是没有东西

骨骼主角的外切矩形

在定时器中绘制

void  update(float dt)
{
     Rect rc = _ske->getBoundingBox();
     if (_node) _node->removeFromParent();
     _node = DrawNode::create();
     addChild(_node);                   
     _node->drawRect(rc.origin, rc.origin + rc.size, Color4F::RED);
     //origin    获取坐标     Size有一个强制类型重载可以把他转化为Vec2()
}



转载于:https://my.oschina.net/u/1181104/blog/507888

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值