Search for controls by name
Header files and namespace to be included:
- # Include "cocostudio/CocoStudio. h"
- # Include "ui/CocosGUI. h"
- Using namespace cocos2d: ui;
- 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; }}