Cocos2d-x3.0 从代码中获取cocostudio编辑的UI控件
依据名字查找控件
须要包括的头文件及名字空间:
获取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; } }