Widget* Helper::seekWidgetByName(Widget* root, const std::string& name) { if (!root) { return nullptr; } if (root->getName() == name) { return root; } const auto& arrayRootChildren = root->getChildren(); for (auto& subWidget : arrayRootChildren) { Widget* child = dynamic_cast<Widget*>(subWidget); if (child) { Widget* res = seekWidgetByName(child,name); if (res != nullptr) { return res; } } } return nullptr; }
Widget* Helper::seekWidgetByName(Widget* root, const char *name) { if (!root) { return nullptr; } if (strcmp(root->getName(), name) == 0) { return root; } const auto& arrayRootChildren = root->getChildren(); for (auto& subWidget : arrayRootChildren) { Widget* child = static_cast<Widget*>(subWidget); Widget* res = seekWidgetByName(child,name); if (res != nullptr) { return res; } } return nullptr; }
UIWidget* UIHelper::seekWidgetByName(UIWidget* root, const char *name) { if (!root) { return NULL; } if (strcmp(root->getName(), name) == 0) { return root; } ccArray* arrayRootChildren = root->getChildren()->data; int length = arrayRootChildren->num; for (int i=0;i<length;i++) { UIWidget* child = (UIWidget*)(arrayRootChildren->arr[i]); UIWidget* res = seekWidgetByName(child,name); if (res != NULL) { return res; } } return NULL; }
void AnimationCommandLayer::createLayer() { MusicAudio::getInstance()->stopMusic(); visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); std::string pathStr = "Animation/"; if (m_aniType == IntroAnimation) { pathStr.append("IntroLesson").append(to_string(GameData::getInstance()->voUser->m_lessonId)).append(".csb"); m_animationVO = m_AnimVO; } else if (m_aniType == EndAnimation) { pathStr.append("EndLesson").append(to_string(GameData::getInstance()->voUser->m_lessonId)).append(".csb"); m_animationVO = m_AnimEndVO; } CCLOG(pathStr.c_str()); if (! FileUtils::getInstance()->isFileExist(pathStr)) { CocosToast::createToastWithIconv("此动画文件不存在"); return; } MusicAudio::getInstance()->play(m_animationVO->m_AnimBgMusic, 0, true); pNode = CSLoader::createNode(pathStr); pNode->setPosition(visibleSize.width / 2, visibleSize.height / 2); pNode->setAnchorPoint(Vec2(0.5f, 0.5f)); this->addChild(pNode); for (int i = 0; i<m_animationVO->m_SpineAnimVector.size(); i++) { Player* pPlayer = Player::create(m_animationVO->m_SpineAnimVector[i]);//创建spine动画player m_pSpinePlayerVec.push_back(pPlayer); Sprite *sp = (Sprite *)(seekWidgetByName(pNode, m_animationVO->m_CocosAnimVector[i]));//从csb获取每一个需要加载动作的精灵 sp->addChild(pPlayer, 0, SPINE_ANIMATION_PLAYER_TAG);//创建spine动画Player,并加载到相对应的精灵中(绑定) m_PlayerVec.push_back(sp); } auto action = CSLoader::createTimeline(pathStr); //帧事件监听 action->setFrameEventCallFunc(CC_CALLBACK_1(AnimationCommandLayer::onFrameEvent, this)); //关于CC_CALLBACK_1需要点C++11的基础才能知道是咋回事,这里只要照着写就行。想了解可以查下std::Bind action->gotoFrameAndPlay(0, false); pNode->runAction(action); //创建button里面的参数必填,不能设为NULL,就算没有也得用"",因为该函数是形参为引用变量。 //播放暂停按钮 Button* playBtn = createButton("image/button/ui_pauseU.png", "", "", Vec2(10, 10), "pause"); playBtn->setAnchorPoint(Vec2(0, 0)); this->addChild(playBtn, 10); auto theCallBack = [this](Ref* sender, Widget::TouchEventType touchType)->void{ Button* p = static_cast<Button*>(sender); std::string name = p->getName(); if (touchType == Widget::TouchEventType::ENDED) { if (p->getName() == "pause") { CCLOG("Button pause"); MusicAudio::getInstance()->play("audio/effect/ui_button.wav", 1, false); Director::getInstance()->pause(); SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); p->loadTextureNormal("image/button/ui_playU.png"); p->setName("play"); } else if (p->getName() == "play") { CCLOG("Button play"); MusicAudio::getInstance()->play("audio/effect/ui_button.wav", 1, false); SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); Director::getInstance()->resume(); p->loadTextureNormal("image/button/ui_pauseU.png"); p->setName("pause"); } } }; playBtn->addTouchEventListener(theCallBack); }