Cocos2d-x3.0 从代码中获取cocostudio编辑的UI控件

依据名字查找控件

须要包括的头文件及名字空间:

  1. #include "cocostudio/CocoStudio.h"
  2. #include "ui/CocosGUI.h"
  3. using namespace cocos2d::ui;
  4. using namespace cocostudio;
注:project中须要附加包括的头文件文件夹:$(EngineRoot)cocoseditor-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;
       }
}


原文地址:https://www.cnblogs.com/yfceshi/p/6937014.html