cocos2d-x三种动画播放

#pragma once
#include"cocos2d.h"
#include"ui/CocosGUI.h"
#include"cocostudio/CocoStudio.h"
#include"HelloWorldScene.h"
#include"StoryScene.h"
using namespace cocos2d::ui;
USING_NS_CC;
//动画,合图
class CgScene:public cocos2d::Layer
{
public:
	CREATE_FUNC(CgScene);
	bool init();

	CgScene();
	~CgScene();
	static Scene* creatSecene();
	//创建骨骼,播放骨骼动画
	void createArmature();
	//跳回主场景
	void ToMainScene(cocos2d::Ref * ref);
	//创建帧动画
	void createTimeline();
	void onFrameEndEvent();
	//播放timeline动画
	void playRoleAnimation(std::string name);
	//播放帧动画
	void playFrameAnimation();
private:
	Node* rootNote;
};

.cpp

#include "CgScene.h"



bool CgScene::init()
{
	//解析场景
	rootNote = CSLoader::createNode("CScene.csb");
	//获得精灵
	auto sp = rootNote->getChildByName("Sprite");
	//精灵缓存,获得精灵帧
	SpriteFrame *frame = SpriteFrameCache::getInstance()->getSpriteFrameByName("icon/item1_001.png");
	//设置精灵帧
	sp->setSpriteFrame(frame);
	//获得按钮
	auto btn = rootNote->getChildByName("Button");
	//设置按钮正常显示图片
	btn->loadTextureNormal("icon/item1_001.png",Widget::TextureResType::PLIST);
	//设置按钮按下图片
	btn->loadTexturePressed("icon/item1_002.png", Widget::TextureResType::PLIST);

	ui::Button* btn1 = rootNote->getChildByName("Button_4");
	btn1->addClickEventListener(CC_CALLBACK_1(CgScene::ToMainScene, this));

	addChild(rootNote);
	createTimeline();
	playFrameAnimation();
	StoryScene *scene = StoryScene::create();
	addChild(scene);
	return true;
}

CgScene::CgScene()
{
}


CgScene::~CgScene()
{
}

Scene * CgScene::creatSecene()
{
	auto scene = Scene::create();
	auto cg = CgScene::create();
	scene->addChild(cg);

	return scene;
}

void CgScene::createArmature()
{
	//动态加载一个骨骼对象
	//ArmatureDataManager:格式化和管理电枢配置和电枢动画
	cocostudio::ArmatureDataManager* adm = cocostudio::ArmatureDataManager::getInstance();
	//获得资源
	cocostudio::ArmatureDataManager *adm = cocostudio::ArmatureDataManager::getInstance();

    adm->addArmatureFileInfo("anim/ui/ui_finger0.png", "anim/ui/ui_finger0.plist", "anim/ui/ui_finger.ExportJson");

    armature = cocostudio::Armature::create("ui_finger");

    armature->getAnimation()->play("finger");
	//设置动作执行速度(大于1 加速, 小于1 减速
	animation->setSpeedScale(0.5);
	armature->setPosition(100, 180);
	armature->setScale(0.5);
	rootNote->addChild(armature);
}

void CgScene::ToMainScene(cocos2d::Ref * ref)
{
	auto sc = HelloWorld::createScene();
	Director::getInstance()->replaceScene(sc);
}

void CgScene::createTimeline()
{
	playRoleAnimation("attack");
	//解析场景中的动画帧
	cocostudio::timeline::ActionTimeline* lint = CSLoader::createTimeline("CScene.csb");
	//播放动画
	rootNote->runAction(lint);
	//播放全部场景
	lint->gotoFrameAndPlay(0, false);
	//设置在那帧播放
	lint->addFrameEndCallFunc(35,"rol",CC_CALLBACK_0(CgScene::onFrameEndEvent,this));
}

void CgScene::onFrameEndEvent()
{
	//创建骨骼
	createArmature();
}

void CgScene::playRoleAnimation(std::string name)
{
	//获取骨骼动画对象
	cocostudio::Armature* role = rootNote->getChildByName("ArmatureNode_1");
	//设置动画状态
	role->getAnimation()->play(name,1,2);
}

void CgScene::playFrameAnimation()
{
	Animation *anima = Animation::create();
	Sprite *iamgeview = static_cast( rootNode->getChildByName("Sprite_1"));
	for (int i = 1; i <= count; i++)
	{
		std::string name = StringUtils::format("images/role_0%d.png",i);
        //将帧动画资源加载进animation中,这一步可以做到加载精灵缓存
		anima->addSpriteFrameWithFileName(name);
		anima->setDelayPerUnit(0.2);
	}
	Animate *animate = Animate::create(anima);
	Repeat *repeat = Repeat::create(animate,10);
	iamgeview->runAction(repeat);

}

 

你可能感兴趣的:(Cocos2d-x,cocos-2dx)