转自 http://www.cocoachina.com/bbs/read.php?tid=194668
前天, 在CocoaChina 2014春季大会上, 激动人心的Cocos2d-x 3.0和CocoStudio 1.3发布了. Cocos2d-x 3.0做出令人兴奋的性能, 易用性等一系列改进, CocoStudio 1.3带来了稳定性和新特性. 今天让我们看下如何在新建Cocos 3.0rc0的Win32的工程中添加CocoStudio的支持.
一 软件及其下载
1. Cocos2d-x 3.0rc
编译一下工程, 还是能运行吧. 如果遇到问题, 不妨来CocoaChina论坛的CocoStudio专区来继续交流.
五 添加代码
我们打开CocoStudio 1.3的动画编辑器, 打开一个HeroAnimation的示例, 并直接导出, 将导出的文件放C:\Work\HelloCocos\Resources下面.
我们打开HelloWorld.cpp给其添加引用以及命名空间
#include "CocoStudio.h"
using namespace cocostudio;
然后修改下HelloWorld::init函数, 在return前加入
ArmatureDataManager::getInstance()->addArmatureFileInfo("Hero0.png" , "Hero0.plist" , "Hero.ExportJson");
Armature *armature = Armature::create( "Hero");
armature->setPosition(ccp(visibleSize.width * 0.5, visibleSize.height * 0.5));
armature->getAnimation()->playWithIndex(0);
this->addChild(armature);
return true;
上文为转载的帖子,基本全部操作完成,最后加入cocostudio时有点坑。时间太久,忘记了当时遇到的都是哪些具体问题。归根结底就是,cocos2d更新太快,相应文档更新则太慢,不同版本间的代码兼容性略差造成的。
扒出来了自己的代码,运行了一下,可以出界面。
源码如下:
1 #include "HelloWorldScene.h" 2 #include "CocoStudio.h" 3 using namespace cocostudio; 4 USING_NS_CC; 5 6 Scene* HelloWorld::createScene() 7 { 8 // 'scene' is an autorelease object 9 auto scene = Scene::create(); 10 11 // 'layer' is an autorelease object 12 auto layer = HelloWorld::create(); 13 14 // add layer as a child to scene 15 scene->addChild(layer); 16 17 // return the scene 18 return scene; 19 } 20 21 // on "init" you need to initialize your instance 22 bool HelloWorld::init() 23 { 24 ////////////////////////////// 25 // 1. super init first 26 if ( !Layer::init() ) 27 { 28 return false; 29 } 30 31 Size visibleSize = Director::getInstance()->getVisibleSize(); 32 Point origin = Director::getInstance()->getVisibleOrigin(); 33 34 ///////////////////////////// 35 // 2. add a menu item with "X" image, which is clicked to quit the program 36 // you may modify it. 37 38 // add a "close" icon to exit the progress. it's an autorelease object 39 auto closeItem = MenuItemImage::create( 40 "CloseNormal.png", 41 "CloseSelected.png", 42 CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); 43 44 closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , 45 origin.y + closeItem->getContentSize().height/2)); 46 47 // create menu, it's an autorelease object 48 auto menu = Menu::create(closeItem, NULL); 49 menu->setPosition(Point::ZERO); 50 this->addChild(menu, 1); 51 52 ///////////////////////////// 53 // 3. add your codes below... 54 55 // add a label shows "Hello World" 56 // create and initialize a label 57 58 auto label = LabelTTF::create("Hello World", "Arial", 24); 59 60 // position the label on the center of the screen 61 label->setPosition(Point(origin.x + visibleSize.width/2, 62 origin.y + visibleSize.height - label->getContentSize().height)); 63 64 // add the label as a child to this layer 65 this->addChild(label, 1); 66 67 // add "HelloWorld" splash screen" 68 auto sprite = Sprite::create("HelloWorld.png"); 69 70 // position the sprite on the center of the screen 71 sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); 72 73 // add the sprite as a child to this layer 74 this->addChild(sprite, 0); 75 76 cocos2d::ui::Widget * layout = cocostudio::GUIReader::getInstance()->widgetFromJsonFile("testingMyUI.json"); 77 layout->setPosition(Point::ZERO); 78 layout->setScale(this->getContentSize().width / layout->getContentSize().width); 79 this->addChild(layout, 3); 80 layout->setOpacity(0); 81 82 auto fadeIn = FadeIn::create(1); 83 auto delay = DelayTime::create(1); 84 auto fadeOut = FadeOut::create(1); 85 auto callback = CallFuncN::create([=](Ref* pSender){ 86 87 }); 88 auto finalAction = Sequence::create(fadeIn,delay,fadeOut,callback,NULL); 89 layout->runAction(finalAction); 90 91 return true; 92 } 93 94 95 void HelloWorld::menuCloseCallback(Ref* pSender) 96 { 97 Director::getInstance()->end(); 98 99 #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 100 exit(0); 101 #endif 102 }