动画:
创建 : Vector<SpritFrame*>frames Animation Animate runAction
{ //通常的做法 Vector< SpriteFrame * > 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 ); frames. pushBack( frame); } //执行动画 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-> runAction( RepeatForever::create (animate )); this-> addChild( sp); 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 ) { Animation* ret = AnimationCache::getInstance ()->getAnimation (plist ); if ( ret) return ret; Vector< SpriteFrame*> frames; SpriteFrameCache::getInstance ()->addSpriteFramesWithFile (plist ); std:: vector<std ::string> frameNames; /* 通过plist文件,获取所有的帧的名字 */ ValueMap vm = FileUtils:: getInstance()-> getValueMapFromFile(plist ); ValueMap& vmFrames = vm[ "frames"].asValueMap (); for ( auto it = vmFrames. begin(); it != vmFrames. end(); ++ it) { frameNames. push_back(it ->first); } // 对已经取出的图片进行排序 std:: sort( frameNames. begin(), frameNames. end()); for ( auto it = frameNames. begin(); it != frameNames. end(); ++ it) { CCLOG( "%s", (*it ).c_str ()); SpriteFrame* frame = SpriteFrameCache::getInstance ()->getSpriteFrameByName (*it ); frames. pushBack( frame); } 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() } |