通过cocos studio导出的帧动画,怎样在程序中实现水平翻转
1个回答
展开全部
Mac版本的cocos studio1.0导出的帧动画和Windows版本下的cocos studio1.6 导出的帧动画,使用方式是不一样的,看了官方demo才明白,Mac cocos studio1.0版本已经采用新的动画系统,对应类型是timeline,目前编辑器还没添加动画列表,所以,动画需要根据帧区域来播放。 不多说,链薯上代码:
AnimationUI.h
#ifndef __Demo__Animation__
#define __Demo__Animation__
#include <iostream>
#include "cocos2d.h"
#include "editor-support/cocostudio/CocoStudio.h"
class AnimationUI : public cocos2d::Layer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// implement the "static node()" method manually
CREATE_FUNC(AnimationUI);
};
#endif /* defined(__Demo__Animation__) */
AnimationUI.cpp
#include "Animation.h"
#include "cocos2d.h"
USING_NS_CC;
using namespace cocostudio;
Scene* AnimationUI::createScene()
{
auto scene = Scene::create();
auto layer = AnimationUI::create();
scene->addChild(layer);
return scene;
}
bool AnimationUI::init()
{
if(!Layer::init()) //初始化父类
return false;
//获取屏幕大小
Size visibleSize = Director::getInstance()->getVisibleSize();
auto fileUtils = FileUtils::getInstance();
std::vector<std::string> searchPaths;
searchPaths.push_back("AniTest_1"); // 不同系统下导出的敏唤樱动画记得修改路劲哟
fileUtils->setSearchPaths(searchPaths);
// mac cocos studio1.0 导出的动画使用方法
// SpriteFrameCache::getInstance()->addSpriteFramesWithFile("fish040.plist", "fish040.png");
Node* node = cocostudio::timeline::NodeReader::getInstance()->createNode("AniTest_1.ExportJson");
cocostudio::timeline::ActionTimeline* action = cocostudio::timeline::ActionTimelineCache::getInstance()->createAction("AniTest_1.ExportJson");
node->runAction(action);
action->桥丛gotoFrameAndPlay(0, 60, true);
node->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
node->setScale(0.2);
this->addChild(node);
// 下面是Windows下导出的动画使用方法,这里注释了
/*
ArmatureDataManager::getInstance()->addArmatureFileInfo("fish040.png","fish040.plist","Fish.ExportJson");
Armature* armature = Armature::create("Fish");
armature->getAnimation()->playWithIndex(0);
armature->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
this->addChild(armature);
*/
return true;
}
所用资源是官网上的,这里给出吧AntiTest_1是mac版本导出的,win是windows版本下导出的,也可以自己用plist和png自己再导出尝试下
AnimationUI.h
#ifndef __Demo__Animation__
#define __Demo__Animation__
#include <iostream>
#include "cocos2d.h"
#include "editor-support/cocostudio/CocoStudio.h"
class AnimationUI : public cocos2d::Layer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// implement the "static node()" method manually
CREATE_FUNC(AnimationUI);
};
#endif /* defined(__Demo__Animation__) */
AnimationUI.cpp
#include "Animation.h"
#include "cocos2d.h"
USING_NS_CC;
using namespace cocostudio;
Scene* AnimationUI::createScene()
{
auto scene = Scene::create();
auto layer = AnimationUI::create();
scene->addChild(layer);
return scene;
}
bool AnimationUI::init()
{
if(!Layer::init()) //初始化父类
return false;
//获取屏幕大小
Size visibleSize = Director::getInstance()->getVisibleSize();
auto fileUtils = FileUtils::getInstance();
std::vector<std::string> searchPaths;
searchPaths.push_back("AniTest_1"); // 不同系统下导出的敏唤樱动画记得修改路劲哟
fileUtils->setSearchPaths(searchPaths);
// mac cocos studio1.0 导出的动画使用方法
// SpriteFrameCache::getInstance()->addSpriteFramesWithFile("fish040.plist", "fish040.png");
Node* node = cocostudio::timeline::NodeReader::getInstance()->createNode("AniTest_1.ExportJson");
cocostudio::timeline::ActionTimeline* action = cocostudio::timeline::ActionTimelineCache::getInstance()->createAction("AniTest_1.ExportJson");
node->runAction(action);
action->桥丛gotoFrameAndPlay(0, 60, true);
node->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
node->setScale(0.2);
this->addChild(node);
// 下面是Windows下导出的动画使用方法,这里注释了
/*
ArmatureDataManager::getInstance()->addArmatureFileInfo("fish040.png","fish040.plist","Fish.ExportJson");
Armature* armature = Armature::create("Fish");
armature->getAnimation()->playWithIndex(0);
armature->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
this->addChild(armature);
*/
return true;
}
所用资源是官网上的,这里给出吧AntiTest_1是mac版本导出的,win是windows版本下导出的,也可以自己用plist和png自己再导出尝试下
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询