zoukankan      html  css  js  c++  java
  • [转帖]Cocos2d-x 3.0rc0 的Win32工程添加CocoStudio库

    转自 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  
        http://cdn.cocos2d-x.org/cocos2d-x-3.0rc0.zip  
    2. CocoStudio 1.3  
        在CocoStudio 1.3中可以直接创建Cocos2d-x项目, 所以这次的安装包有3个.  
        仅含CocoStudio 1.3  
        http://126.am/YDrVk1  
        CocoStudio 1.3包含了Cocos2d-x 3.0rc0  
        http://126.am/lorYp2  
        CocoStudio 1.3 包含了Cocos2d-x 2.2.3  
        http://126.am/eGo4f4  
    3. Python 2.7.6  
        Cocos2d-x的脚本用到了Python, Win下没有自带Python, 大家还需要下载Python.  
        Python的版本我这里推荐大家用最新的2.7.6. 因为Python的2.x和3.x不兼容, 所以一定不能用3.x的版本  
        大家可以根据自己的系统下载对应的版本.  
        https://www.python.org/download/releases/2.7.6/  
    4. Visual Studio  
        地球人都知道啦. 我今天演示采用的版本是Visual Studio 2013.  
        VS2010, VS2012, 下面提到的需要设置的地方, 都是相同的. 请大家自行下载安装.  
     
    二 Python的安装和设置  

      
    安装基本上就是下一步, 下一步就好了. 中间安装路径, 我选择的是"C:Python27", 等下要用到这个路径.  
    右键单击“计算机”,选择“属性”----》“高级系统设置”,在“高级”选项卡下选择“环境变量”!  
    在“系统变量”下选中“Path”,并点击下方的编辑按钮:  
    在弹出的“编辑系统变量”对话框中编辑“变量值”,在“变量值”的后面添加Python的安装路径(C:Python27),用英文分号(;)将其与后面的其他内容隔开。  

      
    同样地,在“PATHEXT“的变量值中添加 .PY;.PYM .  
    打开cmd控制台,输入 python,如果出现如下提示,则说明python安装成功.  
    如果你设置环境变量前已经打开了cmd, 需要重新打开cmd.  
    三 创建Cocos2d-x 3.0rc0项目  
    在Cocos2d-x 3.0rc0中, 引入cocos2d-console这一新的Cocos2d-x工具.  
    在console中你可以完成2d-x的新建项目, 编译甚至在模拟器里面运行项目.  
    我们先来看下如何使用cocos2d-console创建新的项目.  
    我们将下载好的2d-x 3.0rc0解压到C:cocos2d-x-3.0rc0.  
    我们打开cmd, 切换到C:cocos2d-x-3.0rc0 目录  
    运行python setup.py.  
    中间会询问几个Android开发需要的地址, 我们今天不涉及Android开发, 直接回车跳过即可.  

      
    最后会提示我们重启电脑, 我们重启电脑.  
    接下来我们在C:Work目录中创建新项目HelloCocos.  
    打开终端, 切换到C:Work目录中.运行cocos new命令.  
    这里我们可以看到cocos new的用法.  

      
    我们接着输入命令 cocos new -p com.young40.hellococos -l cpp HelloCocos  
    其中-p是包名, -l 选择了cpp即C++开发, HelloCocos是项目名称.你可以替换为自己喜欢的名称.  
    我们打开C:WorkHelloCocosproj.win32HelloCocos.sln, 如果提示升级项目, 选择确定即可.  
    点击F5, 运行项目. 稍等编译完成后, 我们就可以看到我们熟悉的HellWorld了.  
    四 在项目中添加CocoStudio支持  
    我们在VS中找到"解决方案资源管理器", 在解决方案"HelloCocos"上点击右键, 选择添加现有项目.  

      
    在弹出的对话框中选择C:WorkHelloCocoscocos2dcocoseditor-supportcocostudioproj.win32libCocosStudio.vcxproj  
    然后继续添加C:WorkHelloCocoscocos2dextensionsproj.win32libExtensions.vcxproj  
    然后继续添加C:WorkHelloCocoscocos2dcocosuiproj.win32libGUI.vcxproj  
    然后我们在HelloCocos项目上点击右键选择添加引用  

      
    在弹出的对话框中选择添加引用, 并勾选libCocosStudio, libGUI和libExtensions后点击确定.  

      
    如果大家使用的是VS2012或者VS2013, 可能会看到项目被标记为VS2010, 这里我们升级下项目设置, 否则可能无法编译.  

      
    然后, 我们HelloCocos项目属性(点击右键选择属性)中选择 "配置属性" => "C/C++" => "附加包含目录" 并编辑.  
    添加下面几行  
    $(EngineRoot)cocos  
    $(EngineRoot)extensions  
    $(EngineRoot)cocoseditor-support  
    $(EngineRoot)cocoseditor-supportcocostudio  

      
    编译一下工程, 还是能运行吧. 如果遇到问题, 不妨来CocoaChina论坛的CocoStudio专区来继续交流.  
    五 添加代码  
    我们打开CocoStudio 1.3的动画编辑器, 打开一个HeroAnimation的示例, 并直接导出, 将导出的文件放C:WorkHelloCocosResources下面.  
    我们打开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 }
    HelloWorldScene.cpp
  • 相关阅读:
    剑指offer 二叉树中和为某一值的路径
    C++ 中头文件<bits/stdc++.h>的优缺点
    剑指offer 按之字形顺序打印二叉树
    hihocoder 1039 : 字符消除
    剑指offer 分行从上到下打印二叉树
    STL 之 queue
    剑指offer 栈的压入、弹出序列
    剑指offer 包含min函数的栈
    深度学习之depthwise separable convolution,计算量及参数量
    深度学习之group convolution,计算量及参数量
  • 原文地址:https://www.cnblogs.com/slysky/p/3818426.html
Copyright © 2011-2022 走看看