cocos2dx 程序中处理cocostdio导出的帧动画

在 “形体模式” 下(文件下面),从资源中添加动画的起始图片, 然后切换到 “动画模式” 下,下面会出来一个动画帧的框, 旁边默认生成的layer几就是你刚才添加的起始动画的纹理文件, 然后从资源中, 选中剩下的动画,图片,用鼠标拖动到该区域。OK,动画已经添加好, 上面可以播放。



然后说一下动作列表: 一个动作列表中可以添加很多动画 ,这些动画都是跟着这个动作列表一起播放的。

如果想单独的播放某个动画,就新建一个动作列表,然后添加动画吧。

程序效果图:(工程名称:D:\cocos2d-x-3.0alpha1\projects\cocostudioAnimationTest1)

<!–more–>



导入的纹理解析出来会花屏, 不知道怎么回事。

主要代码:helloworld.h

#ifndef __HELLOWORLD_SCENE_H__

#define __HELLOWORLD_SCENE_H__

#include “HelloWorldScene.h”

#include “cocos2d.h” 

#include “extensions\cocos-ext.h” 

#include “cocostudio\CocoStudio.h” 

#include “GUI\CocosGUI.h” 

USING_NS_CC; 

USING_NS_CC_EXT; 

using namespace cocostudio; 

using namespace gui; 

class HelloWorld : public cocos2d:ayer

{

public:

// there’s no ‘id’ in cpp, so we recommend returning the class instance pointer

static cocos2d::Scene* createScene();

// Here’s a difference. Method ‘init’ in cocos2d-x returns bool, instead of returning ‘id’ in cocos2d-iphone

virtual bool init();

void buttonTextCallBack(Object *pSender, gui::TouchEventType type);

// a selector callback

void menuCloseCallback(Object* pSender);

map<int, char *> UIButtonTextMap;

// implement the “static create()” method manually

CREATE_FUNC(HelloWorld);

};

#endif // __HELLOWORLD_SCENE_H__

hellpworldscene.cpp

#include “HelloWorldScene.h”

USING_NS_CC;

const int cocostdioTest = 10;

Scene* HelloWorld::createScene()

{

// ‘scene’ is an autorelease object

auto scene = Scene::create();

// ‘layer’ is an autorelease object

auto layer = HelloWorld::create();

// add layer as a child to scene

scene->addChild(layer);

// return the scene

return scene;

}

// on “init” you need to initialize your instance

bool HelloWorld::init()

{

//////////////////////////////

// 1. super init first

if ( !Layer::init() )

{

return false;

}

Size visibleSize = Director::getInstance()->getVisibleSize();

Point origin = Director::getInstance()->getVisibleOrigin();

/////////////////////////////

// 2. add a menu item with “X” image, which is clicked to quit the program

//you may modify it.

// add a “close” icon to exit the progress. it’s an autorelease object

auto closeItem = MenuItemImage::create(

“CloseNormal.png”,

“CloseSelected.png”,

CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

closeItem->setPosition(Point(origin.x + visibleSize.width – closeItem->getContentSize().width/2 ,

origin.y + closeItem->getContentSize().height/2));

// create menu, it’s an autorelease object

auto menu = Menu::create(closeItem, NULL);

menu->setPosition(Point::ZERO);

this->addChild(menu, 1);

/////////////////////////////

// 3. add your codes below…

// add a label shows “Hello World”

// create and initialize a label

auto label = LabelTTF::create(“Hello World”, “Arial”, 24);

// position the label on the center of the screen

label->setPosition(Point(origin.x + visibleSize.width/2,

origin.y + visibleSize.height – label->getContentSize().height));

// add the label as a child to this layer

this->addChild(label, 1);

// add “HelloWorld” splash screen”

auto sprite = Sprite::create(“HelloWorld.png”);

// position the sprite on the center of the screen

sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

// add the sprite as a child to this layer

this->addChild(sprite, 0);

//加入UI 编辑器

UILayer * uiLayer = UILayer::create();

auto myLayOut = GUIReader::shareReader()->widgetFromJsonFile(“NewProject_1/NewProject_1.ExportJson”);

uiLayer->addWidget(myLayOut);

this->addChild(uiLayer, 5);

//添加buttonText 名字

UIButtonTextMap.insert(map<int, char*>::value_type(1, “TextButton_23″));

UIButtonTextMap.insert(map<int, char*>::value_type(2, “TextButton_27″));

UIButtonTextMap[3] = “TextButton_28″;

UIButtonTextMap[4] = “TextButton_29″;

UIButtonTextMap[5] = “TextButton_31″;

UIButtonTextMap[6] = “TextButton_32″;

UIButtonTextMap[7] = “TextButton_33″;

UIButtonTextMap[8] = “TextButton_34″;

//c++11 

auto mapbag = UIButtonTextMap.begin();

//decltype() 获取当前类型

typedef decltype(mapbag) CurrentType; 

CurrentType mapend = UIButtonTextMap.end();

for (;mapbag != mapend; ++mapbag)

{

/*log(“-frist:%d”, mapbag->first);

log(“-second:%s”, mapbag->second);*/

auto pButtonText = dynamic_cast<UIButton*>(myLayOut->getChildByName(mapbag->second));

pButtonText->setTouchEnabled(true);

pButtonText->setTag(mapbag->first);

pButtonText->setTitleText(mapbag->second);

pButtonText->addTouchEventListener(this, toucheventselector(HelloWorld::buttonTextCallBack));

}

ArmatureDataManager::getInstance()->addArmatureFileInfo(“test1_2/test1.ExportJson”);

//create 中间的名字为 项目的名称 也就是test1.ExportJson 去掉后缀的名字或是打开该文件 中的第一个name中的 字符串

Armature * armature = Armature::create(“test1″);

armature->setTag(cocostdioTest);

armature->setPosition(Point(visibleSize.width/2, visibleSize.height/2+100));

addChild(armature);

return true;

}

void HelloWorld::menuCloseCallback(Object* pSender)

{

Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

exit(0);

#endif

}

void HelloWorld::buttonTextCallBack(Object *pSender, gui::TouchEventType type)

{

//获取调用该方法的的tag 从而确定是那个buttonText

auto uiBt = dynamic_cast<UIButton*>(pSender);

auto armature = (Armature *) this->getChildByTag(cocostdioTest);//this == scene

if (!uiBt)

{

return;

}

int uiBtTag = uiBt->getTag();

switch (type)

{

case TouchEventType::TOUCH_EVENT_ENDED:

switch (uiBtTag)

{

case 1:

//play 中的名字为cocostdio 动画编辑器中的动作列表的名字

armature->getAnimation()->play(“Animation1″);

break;

case 2:

armature->getAnimation()->play(“Animation2″);

break;

case 3:

armature->getAnimation()->play(“Animation3″);

break;

case 4:

armature->getAnimation()->play(“Animation2_Copy1″);

break;

case 5:

armature->getAnimation()->play(“Animation4″);

break;

default:

break;

}

break;

default:

break;