天天看點

(入門篇)cocos2d-x導入cocostudio檔案

轉載自 http://blog.csdn.net/bingyuxuan8/article/details/21471701

備注:這篇博文主要為新手提供。

cocostudio版本:1.2.0.1

cocos2d-x版本:2.2.1

步驟:

1. 在需要顯示的層或場景中加入以下代碼

//建立UIlayer

UILayer* m_pUILayer = UILayer::create();

//将組建添加到UILayer中

m_pUILayer ->addWidget(GUIReader::shareReader()->widgetFromJsonFile("main.ExportJson"));   

//設定層可點選

m_pUILayer ->setTouchEnabled(true);  

//加入到層或場景中

this->addChild(m_pUILayer );

2.給按鈕添加點選事件

a,首先要找到按鈕控件,可通過getWidgetByName或getWidgetByTag來找。

UIButton *m_pBtnClose = dynamic_cast<UIButton*>(m_pUILayer->getWidgetByTag(2));

注釋:2就是cocostudio中控件屬性中的Tag

b. 添加按鈕事件(新手在不知道的情況下,就用這個方法就好了。以後熟點再慢慢探索其它的事件)

m_pBtnClose->setTouchEnabled(true);

m_pBtnClose->addReleaseEvent(this, coco_moveselector(SettingLayer::onClickBtnsEvent));

注:SettingLayer::onClickBtnsEvent 需在頭檔案中添加這個方法,寫法為:void onClickBtnsEvent(CCObject *pSender);

其它控件用類似的方法去找。此博文寫得比較粗糙,以後會不斷提升自己的水準。