1. 程式人生 > >Cocos2d-x3.0 從代碼中獲取cocostudio編輯的UI控件

Cocos2d-x3.0 從代碼中獲取cocostudio編輯的UI控件

ucid 編輯 top ica sans sce nero val -i

依據名字查找控件

須要包括的頭文件及名字空間:

  1. #include "cocostudio/CocoStudio.h"
  2. #include "ui/CocosGUI.h"
  3. using namespace cocos2d::ui;
  4. using namespace cocostudio;
註:project中須要附加包括的頭文件文件夾:$(EngineRoot)cocos\editor-support。由於cocostudio在此文件夾下。

獲取UI控件的方法例如以下:

   m_achievementLayer = dynamic_cast<Layout*>(GUIReader::getInstance()->widgetFromJsonFile("achievements/achievements.json"));
   addChild(m_achievementLayer);

   Widget* scoreWidget = dynamic_cast<Widget*>(m_achievementLayer->getChildByName("ImageView_231"));
   m_score = dynamic_cast<TextAtlas*>(scoreWidget->getChildByName("LabelAtlas_307"));
   m_score->setStringValue("45");



加入button回調事件

   Button* startButton = dynamic_cast<Button*>(m_achievementLayer->getChildByName("Button_336"));
   startButton->addTouchEventListener(this, toucheventselector(GameScene::touchStartButton)); 

利用addTouchEventListener函數就能夠綁定button的回調事件了~

回調函數實現:

void GameScene::touchStartButton(Ref* pSender, TouchEventType type)
{
       switch (type)
       {
        case TOUCH_EVENT_ENDED:
        //do something
        break;
       }
}



   

Cocos2d-x3.0 從代碼中獲取cocostudio編輯的UI控件