The Cocos2d-x3.0 gets the UI control edited by cocostudio from the code

Source: Internet
Author: User

Search for controls by name

Header files and namespace to be included:

 
 
  1. # Include "cocostudio/CocoStudio. h"
  2. # Include "ui/CocosGUI. h"
  3. Using namespace cocos2d: ui;
  4. Using namespace cocostudio; note: the header file directory included must be appended to the project: $ (EngineRoot) cocos \ editor-support, because cocostudio is in this directory.

The method to obtain the UI control is as follows:

   m_achievementLayer = dynamic_cast
 
  (GUIReader::getInstance()->widgetFromJsonFile("achievements/achievements.json"));   addChild(m_achievementLayer);   Widget* scoreWidget = dynamic_cast
  
   (m_achievementLayer->getChildByName("ImageView_231"));   m_score = dynamic_cast
   
    (scoreWidget->getChildByName("LabelAtlas_307"));   m_score->setStringValue("45");
   
  
 


 

Add button callback event

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

You can use the addTouchEventListener function to bind the callback event of the button ~

Callback function implementation:

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


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.