首页 > 代码库 > cocos3.0使用cocostudio动画帧结合地图对象键值创建精灵动画

cocos3.0使用cocostudio动画帧结合地图对象键值创建精灵动画

内容如下:

 

#include "cocos2d.h"
#include "cocostudio/CocoStudio.h"
//精灵猫和其他精灵的tag
typedef  enum{
	  catTag =1,
};

//cocostudio 动画帧tag
typedef  enum{

	catWalkTag = 1001,
};


class Base :public  cocos2d::Layer
{
public:
	Base();
	~Base();

	//在地图中创建一个活多个空精灵节点
	void createSpriteInMap(cocos2d::TMXTiledMap* map, const std::string& strObj,int _tag, const  std::string&  strJosn, const std::string&  _index, float scale);
	
   //创建精灵的动画
	cocostudio::Armature*  createArmature(const  std::string&  strJosn, const std::string&  _index, float scale);

	//移除动画
	void removeArmature(int _tag1,int _tag2);
	
};


 

#include "Base.h"
USING_NS_CC;
using  namespace cocostudio;
Base::Base()
{
}


Base::~Base()
{
}

void  Base::createSpriteInMap(TMXTiledMap* map, const std::string& strObj,int _tag, const  std::string&  strJosn, const std::string&  _index, float scale)
{
	auto  group  =map->getObjectGroup(strObj);
	auto& objs   = group->getObjects();
	for (auto& obj : objs)
	{
		ValueMap& dict =obj.asValueMap();
		float x =dict["x"].asFloat();
		float y =dict["y"].asFloat();
	    auto  sprite = Sprite::create();
		sprite->setPosition(Point(x,y));
		ArmatureDataManager::getInstance()->addArmatureFileInfo(strJosn);
		Armature *armature = Armature::create(_index);
		armature->getAnimation()->playWithIndex(0);
		armature->setScale(scale);
		switch (_tag)
		{
		  case  catTag:
			  sprite->addChild(armature, 2, catWalkTag);
			  this->addChild(sprite, 2,catTag);
		}
	}

}

cocostudio::Armature*  Base::createArmature(const  std::string&  strJosn, const std::string&  _index, float scale)
{
	ArmatureDataManager::getInstance()->addArmatureFileInfo(strJosn);
	Armature *armature = Armature::create(_index);
	armature->getAnimation()->playWithIndex(0);
	armature->setScale(scale);
	return armature;
}



void Base::removeArmature(int _tag1,int _tag2)
{
	auto sprite =this->getChildByTag(_tag1);
	sprite->removeChildByTag(_tag2);
}


 

this->createSpriteInMap(myMap, "jlmPoint", 1, "cat/CatWalks/CatWalks.ExportJson", "CatWalks", 0.5f);