cocos2d-x 使用Cocostudio UI編輯器篇


cocos2d-x版本,2.2

cocostudio版本:1.0.2.0

使用cocos2d-x 2.1.3沒有成功,cocos2d-x 2.2版本內嵌cocostdio了,所以用2.2

使用cocostudio新建了一個項目,名字“Ma”,里面有兩個控件,一個Button,名字“Button”。一個TextView,名字“text”

然后把導出的cocostudio項目添加到vc項目的resource中,然后是代碼.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC_EXT;
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 recommand to return the exactly class pointer
    static cocos2d::CCScene* scene();
    
    // a selector callback
    void menuCloseCallback(CCObject* pSender);
    void touchEvent(CCObject *pSender, TouchEventType type);
    void countrytouch(CCObject*pSender,TouchEventType type);
    // implement the "static node()" method manually
    UILabel*mText;
    UILabel*mCountryLabel;
    CREATE_FUNC(HelloWorld);
};

#endif  // __HELLOWORLD_SCENE_H__


.ccp

#include "HelloWorldScene.h"

using namespace cocos2d;

CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do 
    {
        scene = CCScene::create();
        CC_BREAK_IF(! scene);
        HelloWorld *layer = HelloWorld::create();
        CC_BREAK_IF(! layer);
        scene->addChild(layer);
    } while (0);
    return scene;
}
bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        CC_BREAK_IF(! CCLayer::init());
        //創建一個畫布
        UILayer* ul =UILayer::create();
        //把畫布添加到場景中
        this->addChild(ul);
        //創建一個文本框
        mText=UILabel::create();
        mText->setText("text");
        mText->setFontName("");
        mText->setFontSize(32);
        mText->setAnchorPoint(ccp(0.5f, -1));
        mText->setPosition(ccp(100,100));
        ul->addWidget(mText);
        //創建一個Button按鈕
        UIButton*playBtn=UIButton::create();
        playBtn->setTouchEnable(true);
        playBtn->setTag(1);
        playBtn->loadTextures("image/btn-play-normal.png","image/btn-play-selected.png","");
        playBtn->addTouchEventListener(this,toucheventselector(HelloWorld::touchEvent));
        playBtn->setPosition(ccp(50,50));
        ul->addWidget(playBtn);

        //調用UI編輯器編輯的按鈕
        //把UI編輯的地圖添加到畫布中
        UIWidget*pUI=CCUIHELPER->createWidgetFromJsonFile("Ma.json");
        ul->addWidget(pUI);
        //獲取UI 上Button的的控件
        UIWidget* countryBtn = UIHelper::instance()->seekWidgetByName(pUI,"Button"); 
        //dynamic_cast<UIWidget*>(pUI)
        countryBtn->addTouchEventListener(this,toucheventselector(HelloWorld::touchEvent));
        //獲取UI上的Label控件
        mCountryLabel=(UILabel*)(UIHelper::instance()->seekWidgetByName(pUI,"text")); 
        bRet = true;
    } while (0);

    return bRet;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->end();
}
void HelloWorld::touchEvent(CCObject *pSender, TouchEventType type){
    int tag=((UIWidget*)pSender)->getTag();
    switch(tag){
    case 1:
        switch(type){
        case TOUCH_EVENT_BEGAN:
            mText->setText("1");
            break;
        case TOUCH_EVENT_MOVED:
            mText->setText("2");
            break;
        case  TOUCH_EVENT_ENDED:
            mText->setText("3");
            break;
        }
        break;
    case 8:
        switch(type){
        case TOUCH_EVENT_BEGAN:
            mCountryLabel->setText("1");
            break;
        case TOUCH_EVENT_MOVED:
            mCountryLabel->setText("2");
            break;
        case  TOUCH_EVENT_ENDED:
            mCountryLabel->setText("3");
            break;
        }
        break;
    }
};

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM