AKever

导航

Cocos2dx(1)-The HelloWorld of CocoStudio !!

转自:http://blog.csdn.net/fansongy/article/details/12757411

CocoStudio支持cocos2dx 2.2,也支持3.0。本转载的是3.0的,本人用的是2.2。

1、概述

    CocoStudio的使用无疑是cocos2d-x 3.0的重要组成部分,接下来我们用它来创建一组UI,并将其读入到程序中显示出来。先上效果图:

2、创建、编辑UI

    首先,运行CocoStudio,选择UI Editer(第二个)。进入后,从“文件”->“新建项目”->输入相应的项目名称和路径。创建好的新项目应该是这样:

3、导出

    在导出之前,最好先创建一个新的工程。运行我们的脚本文件,给新工程起名为:HelloStudio。编译运行,保证它没问题。

    回到CocoStudio中,点选 文件 –> 导出项目 。在导出资源的位置,选择“导出使用大图”。同时将目录改为我们项目文件的Resource目录。

 

4、加载到程序中

 切换到VS2012中更改HelloWorld类中的init()方法:

(1)3.0版本代码:

HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
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();  
    // a selector callback
    void menuCloseCallback(Object* pSender);
    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp

#include "HelloWorldScene.h"
#include "cocos-ext.h"

USING_NS_CC;
USING_NS_CC_EXT;

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;
    }
    /////////////////////////////////
    UILayer* uiLayer = UILayer::create();
    //auto myLayout =    dynamic_cast<Layout*>(CCUIHELPER->createWidgetFromJsonFile("testUI.ExportJson"));
  UILayout* myLayout= dynamic_cast<UILayout*>(GUIReader::shareReader()->widgetFromJsonFile("NewProject_1.json")); uiLayer
->addWidget(myLayout); this->addChild(uiLayer); return true; } void HelloWorld::menuCloseCallback(Object* pSender) { Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif }

(2)cocos2dx 2.2版本

HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::CCLayer
{
public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init(); 
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::CCScene* scene();
    // a selector callback
    void menuCloseCallback(CCObject* pSender);
    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp

#include "HelloWorldScene.h"

#include "cocos-ext.h"

USING_NS_CC;
USING_NS_CC_EXT;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    // 'layer' is an autorelease object
    HelloWorld *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()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    UILayer* uiLayer = UILayer::create();
    //auto myLayout =    dynamic_cast<Layout*>(CCUIHELPER->createWidgetFromJsonFile("testUI.ExportJson"));//CCUIHELPER可能已经废弃不用!!!
  UILayout* myLayout= dynamic_cast<UILayout*>(GUIReader::shareReader()->widgetFromJsonFile("NewProject_1.json")); uiLayer
->addWidget(myLayout); this->addChild(uiLayer); return true; } void HelloWorld::menuCloseCallback(CCObject* pSender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); #else CCDirector::sharedDirector()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif #endif }

Warn: 使用CocoStudio,需要导入“cocos-ext.h”头文件和声明命名空间“USING_NS_CC_EXT”, CCUIHELPER很奇怪,第一次跑没注意,通过,后来就跑不通了,没找到原因

#include "cocos-ext.h"
USING_NS_CC_EXT;
Warn: 使用CocoStudio
转自:http://blog.csdn.net/fansongy/article/details/12757411

3.UI 动画

将编辑器切换到动画模式(左上角)

编辑好动画后,调用代码如下:

1.先从页面上获取控件,并添加事件响应。

UIButton* button = dynamic_cast<UIButton*>(ul08->getWidgetByName("TextButton_29"));
button->addTouchEventListener(this, toucheventselector(ShowGame::startUIAnimation));

2.在 ShowGame::startUIAnimation方法中调用动画

void ShowGame::startUIAnimation(CCObject* pSender, TouchEventType type)
{
    if(type == TOUCH_EVENT_ENDED) 
    {
     //此处"NewProject_1.json"只需要文件名,不需要路劲名,当然,在前面已经添加后“NewProject_1.json”这个文件啦 cocos2d::extension::ActionManager::shareManager()->playActionByName("NewProject_1.json", "Animation0"); } }

触发事件,运动开始!!!下面是Roronoa Zoro

 

posted on 2013-12-23 14:51  AKever  阅读(430)  评论(0)    收藏  举报