Spine是2d遊戲骨骼動畫軟件,導出json後可以提供給Cocos2d-x使用。
關鍵代碼:
auto skeletonNode = spine::SkeletonAnimation::createWithFile("player.json", "player.atlas", 0.2F);//0.2是設置圖片的縮放比例
skeletonNode->setPosition(Point(400, 200));
skeletonNode->setAnimation(0, "run", true);//true是指循環播放walk動作
this->addChild(skeletonNode);
全部代碼:
#include "HelloWorldScene.h"
#include<spine\spine-cocos2dx.h>
USING_NS_CC;
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
// return the scene
return scene;
}
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
//spine 骨骼動畫
auto skeletonNode = spine::SkeletonAnimation::createWithFile("player.json", "player.atlas", 0.2F);//0.2是設置圖片的縮放比例
skeletonNode->setPosition(Point(400, 200));
skeletonNode->setAnimation(0, "run", true);//true是指循環播放walk動作
this->addChild(skeletonNode);
//創建骨骼動畫
auto skeletonNode2 = spine::SkeletonAnimation::createWithFile("player2.json", "player2.atlas", 0.7F);
skeletonNode2->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
skeletonNode2->setAnimation(0, "run", true);
this->addChild(skeletonNode2,5);
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
label->setPosition(Vec2(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - label->getContentSize().height));
this->addChild(label, 1);
auto sprite = Sprite::create("HelloWorld.png");
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
this->addChild(sprite, 0);
return true;
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}