依据名字查找控件
须要包括的头文件及名字空间:
注:project中须要附加包括的头文件文件夹:$(EngineRoot)cocos\editor-support。由于cocostudio在此文件夹下。
- #include "cocostudio/CocoStudio.h"
- #include "ui/CocosGUI.h"
- using namespace cocos2d::ui;
- using namespace 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; } }
时间: 2024-10-20 16:54:44