void UIUtils::addLeadBuyMapLayer(UIBase* uiBase, int curMapID, std::function<void()> buyEvent) { auto rootWidget = uiBase->m_RootWidget; const char * LAYER_NAME = "leadBuyMapLayer"; auto leadBuyLayer = rootWidget->getChildByName(LAYER_NAME); if (!leadBuyLayer) { leadBuyLayer = CSLoader::createNode("LeadBuyLayer.csb"); leadBuyLayer->setName(LAYER_NAME); rootWidget->addChild(leadBuyLayer); } leadBuyLayer->getChildByName("Layout_LeadBuy")->setVisible(false); leadBuyLayer->setVisible(true); auto layout_leadBuy = leadBuyLayer->getChildByName("Layout_LeadBuyMap"); if (!layout_leadBuy) return; layout_leadBuy->setVisible(true); int langID = 0; int langCostID = 0; int buyMapBagID = 0; if (curMapID == 909000005) { langID = 25131; langCostID = 25132; buyMapBagID = (int)CfgIdEnum::BuyMap6ID; } else if (curMapID == 909000007) { langID = 25133; langCostID = 25134; buyMapBagID = (int)CfgIdEnum::BuyMap8ID; } else if (curMapID == 909000009) { langID = 25135; langCostID = 25136; buyMapBagID = (int)CfgIdEnum::BuyMap10ID; } auto langStr = MultiLanguage::getInstance()->GetText(langID); auto langCostStr = MultiLanguage::getInstance()->GetText(langCostID); auto Text_ShowText = layout_leadBuy->getChildByName<Text *>("Text_Desc"); Text_ShowText->setString(langStr); auto Btn_Cancel = layout_leadBuy->getChildByName<Button *>("Button_Cancel"); Btn_Cancel->addClickEventListener( [=](Ref* sender) { layout_leadBuy->setVisible(false); leadBuyLayer->setVisible(false); TiledMapUI::getInstance()->SetTouchEnabled(true); }); auto Btn_Buy = layout_leadBuy->getChildByName<Button *>("Button_Buy"); Btn_Buy->addClickEventListener( [=](Ref* sender) { layout_leadBuy->setVisible(false); leadBuyLayer->setVisible(false); if(DiamondBuyData::getInstance()->BuyEvent(buyMapBagID)) { // 购买成功 buyEvent(); } else { // 购买失败,跳至商城充值界面 uiBase->UnLoad(); // 扣除钻石失败,跳至充值界面 DiamondBuyData::getInstance()->goToTopUp(uiBase->m_ResName); } }); auto Text_BuyNum = Btn_Buy->getChildByName<Text *>("Text_BuyNum"); Text_BuyNum->setString(langCostStr); }
// Event - BarrierUI_Click // Event - Barrier_Build void BarrierUI::BarrierAreaUIVisible(EventCustom *event) { Sprite *barrierArea = (Sprite*)getChildByName("Area"); barrierArea->setVisible(false); }
void WelcomeScene::showMenu() { getChildByName("loadingLayer")->removeFromParent(); addChild(_pMenusLayer); Config::getInstance()->setIsLoadSource(true); }
void UIUtils::addLeadBuyLayer(UIBase* uiBase, string showText, int totalDiamonds, std::function<void()> buyEvent, umengEventID eventID) { auto rootWidget = uiBase->m_RootWidget; if (totalDiamonds <= 0) return; if(UserData::getInstance()->GetUserData(UserDataEnum::ShopItem)->asValueMap().empty()) return; const char * LAYER_NAME = "leadBuyLayer"; auto leadBuyLayer = rootWidget->getChildByName(LAYER_NAME); if (!leadBuyLayer) { leadBuyLayer = CSLoader::createNode("LeadBuyLayer.csb"); leadBuyLayer->setName(LAYER_NAME); rootWidget->addChild(leadBuyLayer); } leadBuyLayer->getChildByName("Layout_LeadBuyMap")->setVisible(false); leadBuyLayer->setVisible(true); auto layout_leadBuy = leadBuyLayer->getChildByName("Layout_LeadBuy"); if (!layout_leadBuy) return; layout_leadBuy->setVisible(true); auto Text_ShowText = layout_leadBuy->getChildByName<Text *>("Text_Desc"); Text_ShowText->setString(showText); auto Btn_Cancel = layout_leadBuy->getChildByName<Button *>("Button_Cancel"); Btn_Cancel->addClickEventListener( [=](Ref* sender) { layout_leadBuy->setVisible(false); leadBuyLayer->setVisible(false); if(eventID != 0) { RecordUtils::getInstance()->sdkRecord((int)eventID, "取消"); } }); auto Btn_Buy = layout_leadBuy->getChildByName<Button *>("Button_Buy"); Btn_Buy->setTitleText(StringUtils::format("%d钻石", totalDiamonds)); Btn_Buy->addClickEventListener( [=](Ref* sender) { layout_leadBuy->setVisible(false); leadBuyLayer->setVisible(false); if(DiamondBuyData::getInstance()->costDiamond(totalDiamonds)) { // 扣除钻石成功,则执行传入buyEvent buyEvent(); } else { uiBase->UnLoad(); // 扣除钻石失败,跳至充值界面 DiamondBuyData::getInstance()->goToTopUp(uiBase->m_ResName); } if(eventID != 0) { RecordUtils::getInstance()->sdkRecord((int)eventID, "确定"); } }); }
// Event - Object_Menu_Close void Barrier::ObjectMenuClose(EventCustom *event) { ui::Button *buttonDemolish = (ui::Button*)getChildByName("Button_Demolish"); buttonDemolish->setVisible(false); }
///all screen move functions should use this void HelloWorld::lookAt(Vec2 pos) { Vec2 visible = Director::getInstance()->getVisibleSize(); this->setPosition(-1*(pos - visible/2)); protractor->setPosition(lookingAt()); notifier->setPosition(lookingAt()); //draw grid Vec2 botleft = screenspaceToWorldspace(Vec2::ZERO); Vec2 topright = screenspaceToWorldspace(visible); Vec2 drawSpaceBotLeft = botleft - GRID_LABEL_SPACING; Vec2 drawSpaceTopRight = topright + GRID_LABEL_SPACING; DrawNode* gridSprite = (DrawNode*)getChildByName("gridSprite"); if (gridSprite) { gridSprite->clear(); std::vector<std::string> toRemove; for (auto child : gridSprite->getChildren()) { if (!onScreen(child->getPosition(), Vec2(GRID_LABEL_SPACING))) toRemove.push_back(child->getName()); } for (auto name : toRemove) { gridSprite->removeChildByName(name); } } else { gridSprite = DrawNode::create(); gridSprite->setName("gridSprite"); addChild(gridSprite, 0); } //vertical lines for (int i= drawSpaceBotLeft.x; i<drawSpaceTopRight.x; i++) { if(i % (int)GRID_SPACING.x) continue; gridSprite->drawSegment(Vec2(i, drawSpaceBotLeft.y), Vec2(i, drawSpaceBotLeft.y+visible.y+2*GRID_LABEL_SPACING.y), GRID_LINE_THICKNESS, Color4F(1,1,1,0.1)); if(!(i % (int)GRID_LABEL_SPACING.x)) { std::string txt = std::to_string(i); auto label = gridSprite->getChildByName("v" + txt); if (!label) { label = Label::createWithTTF(txt, "fonts/arial.ttf", GRID_LABEL_SIZE); label->setName("v" + txt); gridSprite->addChild(label); } label->setPosition(Vec2(i, topright.y - GRID_LABEL_SIZE * 2)); } } //horizontal lines for (int i= drawSpaceBotLeft.y; i<drawSpaceTopRight.y; i++) { if(i % (int)GRID_SPACING.y) continue; gridSprite->drawSegment(Vec2(drawSpaceBotLeft.x, i), Vec2(drawSpaceBotLeft.x+visible.x + 2 * GRID_LABEL_SPACING.x, i), GRID_LINE_THICKNESS, Color4F(1,1,1,0.1)); if(!(i % (int)GRID_LABEL_SPACING.y)) { std::string txt = std::to_string(i); auto label = gridSprite->getChildByName("h" + txt); if (!label) { label = Label::createWithTTF(txt, "fonts/arial.ttf", GRID_LABEL_SIZE); label->setName("h" + txt); gridSprite->addChild(label); } label->setPosition(Vec2(botleft.x + GRID_LABEL_SIZE * 2, i)); } } repaintCursor(); Commorose* c = (Commorose*)commorose; c->setPosition(screenspaceToWorldspace(c->getPositionOnScreen())); }
void NodeNameTest::test(float dt) { auto parent = Node::create(); // setName(), getName() and getChildByName() char name[20]; for (int i = 0; i < 10; ++i) { sprintf(name, "node%d", i); auto node = Node::create(); node->setName(name); parent->addChild(node); } for (int i = 0; i < 10; ++i) { sprintf(name, "node%d", i); auto node = parent->getChildByName(name); log("find child: %s", node->getName().c_str()); } // enumerateChildren() // name = regular expression int i = 0; parent = Node::create(); for (int i = 0; i < 100; ++i) { auto node = Node::create(); sprintf(name, "node%d", i); node->setName(name); parent->addChild(node); } i = 0; parent->enumerateChildren("node[[:digit:]]+", [&i](Node* node) -> bool { ++i; return false; }); CCAssert(i == 100, ""); i = 0; parent->enumerateChildren("node[[:digit:]]+", [&i](Node* node) -> bool { ++i; return true; }); CCAssert(i == 1, ""); // enumerateChildren // name = node[[digit]]+/node parent = Node::create(); for (int i = 0; i < 100; ++i) { auto node = Node::create(); sprintf(name, "node%d", i); node->setName(name); parent->addChild(node); for (int j = 0; j < 100; ++j) { auto child = Node::create(); child->setName("node"); node->addChild(child); } } i = 0; parent->enumerateChildren("node1/node", [&i](Node* node) -> bool { ++i; return false; }); CCAssert(i == 100, ""); i = 0; parent->enumerateChildren("node1/node", [&i](Node* node) -> bool { ++i; return true; }); CCAssert(i == 1, ""); // search from root parent = Node::create(); for (int i = 0; i < 100; ++i) { auto node = Node::create(); sprintf(name, "node%d", i); node->setName(name); parent->addChild(node); for (int j = 0; j < 100; ++j) { auto child = Node::create(); child->setName("node"); node->addChild(child); } } i = 0; parent->enumerateChildren("node[[:digit:]]+/node", [&i](Node* node) -> bool { ++i; return false; }); CCAssert(i == 10000, ""); i = 0; parent->enumerateChildren("node[[:digit:]]+/node", [&i](Node* node) -> bool { ++i; return true; }); CCAssert(i == 1, ""); // search from parent // name is xxx/.. i = 0; parent->enumerateChildren("node/..", [&i](Node* node) -> bool { ++i; return true; }); CCAssert(i == 1, ""); i = 0; parent->enumerateChildren("node/..", [&i](Node* node) -> bool { ++i; return false; }); CCAssert(i == 10000, ""); // name = //xxx : search recursively parent = Node::create(); for (int j = 0; j < 100; j++) { auto node = Node::create(); sprintf(name, "node%d", j); node->setName(name); parent->addChild(node); for (int k = 0; k < 100; ++k) { auto child = Node::create(); sprintf(name, "node%d", k); child->setName(name); node->addChild(child); } } i = 0; parent->enumerateChildren("//node[[:digit:]]+", [&i](Node* node) -> bool { ++i; return false; }); CCAssert(i == 10100, ""); // 10000(children) + 100(parent) i = 0; parent->enumerateChildren("//node[[:digit:]]+", [&i](Node* node) -> bool { ++i; return true; }); CCAssert(i == 1, ""); i = 0; parent->enumerateChildren("//node[[:digit:]]+/..", [&i](Node* node) -> bool { ++i; return false; }); CCAssert(i == 10000, ""); // utils::findChildren() parent = Node::create(); for (int i = 0; i < 50; ++i) { auto child = Node::create(); child->setName("node"); parent->addChild(child); } auto findChildren = utils::findChildren(*parent, "node"); CCAssert(findChildren.size() == 50, ""); }
void UICtrl::onEnter(){ Layer::onEnter(); auto vSize = Director::getInstance()->getVisibleSize(); playLayer = Layer::create(); playLayer->setCascadeOpacityEnabled(true); playLayer->setOpacity(GLubyte(255*0.6)); addChild(playLayer,10); auto stopButton = Sprite::create("stopButton.png"); stopButton->setPosition(vSize); stopButton->setAnchorPoint(Vec2(1.0f,1.0f)); playLayer->addChild(stopButton); auto leftButton = Sprite::create("leftButton.png"); leftButton->setPosition(Vec2(0,0)); leftButton->setAnchorPoint(Vec2(0,0)); playLayer->addChild(leftButton); auto rightButton = Sprite::create("rightButton.png"); rightButton->setPosition(Vec2(100,0)); rightButton->setAnchorPoint(Vec2(0,0)); playLayer->addChild(rightButton); moveButton = Sprite::create("moveButton2.png"); moveButton->setPosition(Vec2(780,0)); moveButton->setAnchorPoint(Vec2(1,0)); playLayer->addChild(moveButton); NotificationCenter::getInstance()->addObserver(this, callfuncO_selector(UICtrl::showBuff), strShowBuff, NULL); NotificationCenter::getInstance()->addObserver(this, callfuncO_selector(UICtrl::hideBuff), strHideBuff, NULL); NotificationCenter::getInstance()->addObserver(this, callfuncO_selector(UICtrl::dieShow), strDieShow, NULL); auto hitButton = Sprite::create("hitButton.png"); hitButton->setPosition(Vec2(870,0)); hitButton->setAnchorPoint(Vec2(1,0)); playLayer->addChild(hitButton); auto jumpButton = Sprite::create("jumpButton.png"); jumpButton->setPosition(Vec2(960,0)); jumpButton->setAnchorPoint(Vec2(1,0)); playLayer->addChild(jumpButton); stopLayer = Layer::create(); stopLayer->setCascadeOpacityEnabled(true); stopLayer->setOpacity(GLubyte(255*0.75)); stopLayer->setScale(0.5); addChild(stopLayer,8); stopLayer->setVisible(false); auto node = CSLoader::createNode("GameStop.csb"); stopLayer->addChild(node); auto backButton = dynamic_cast<ui::Button*>(node->getChildByName("backButton")); backButton->addClickEventListener([=](Ref* pSender){ hideLayer(); }); auto restartButton = dynamic_cast<ui::Button*>(node->getChildByName("restartButton")); restartButton->addClickEventListener([=](Ref* pSender){ Hero::heroLife = 10; ShareData::getInstance()->heroScore = 0; Director::getInstance()->replaceScene(MainScene::createScene()); Director::getInstance()->resume(); }); auto quitButton = dynamic_cast<ui::Button*>(node->getChildByName("quitButton")); quitButton->addClickEventListener([=](Ref* pSender){ Director::getInstance()->replaceScene(StartScene::createScene()); Director::getInstance()->resume(); }); listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = [=](Touch* touch, Event* e){ if( stopButton->getBoundingBox().containsPoint(touch->getLocation())){ showLayer(); }else if( leftButton->getBoundingBox().containsPoint(touch->getLocation())){ NotificationCenter::getInstance()->postNotification(strLeftButtonDown); }else if( rightButton->getBoundingBox().containsPoint(touch->getLocation())){ NotificationCenter::getInstance()->postNotification(strRightButtonDown); }else if( moveButton->getBoundingBox().containsPoint(touch->getLocation())){ NotificationCenter::getInstance()->postNotification(strQuickMove); }else if( hitButton->getBoundingBox().containsPoint(touch->getLocation())){ NotificationCenter::getInstance()->postNotification(strHeroShoot); }else if( jumpButton->getBoundingBox().containsPoint(touch->getLocation())){ NotificationCenter::getInstance()->postNotification(strJumpButtonDown); } return true; }; listener->onTouchEnded = [=](Touch* touch, Event* e){ if( leftButton->getBoundingBox().containsPoint(touch->getLocation())){ NotificationCenter::getInstance()->postNotification(strLeftButtonUp); }else if( rightButton->getBoundingBox().containsPoint(touch->getLocation())){ NotificationCenter::getInstance()->postNotification(strRightButtonUp); } }; listener->setSwallowTouches(true); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this); dieLayer = Layer::create(); addChild(dieLayer); dieLayer->setVisible(false); auto dieSprite = Sprite::create("die.png"); dieSprite->setPosition(vSize.width/2,vSize.height/2+60); dieLayer->addChild(dieSprite); ui::Button* restart = ui::Button::create("continue.png"); restart->setPosition(Vec2(vSize.width/2,vSize.height/2-60)); restart->addClickEventListener([=](Ref* pSender){ Hero::heroLife = 10; ShareData::getInstance()->heroScore = 0; ShareData::getInstance()->rankScore = 0; MainScene::level = 1; Director::getInstance()->replaceScene(MainScene::createScene()); SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); Director::getInstance()->resume(); }); dieLayer->addChild(restart); }
void WalkerLayer::surpriseTrigger() { Sprite *walker = dynamic_cast<Sprite*>(getChildByName("walker")); walker->stopAllActions(); walker->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(_name + "_jing.png")); }
bool BackLayer::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } ///////////////////////////// // 3. add your codes below... // add scene auto rootNode = CSLoader::createNode("BackgroundLayer.csb"); this->addChild(rootNode); // get back images auto nodeBack = dynamic_cast<Node*>(rootNode->getChildByName("Node_BackImage")); m_mBackImages.insert(BACKIMAGE_MAINMENU, dynamic_cast<Layout*>(nodeBack->getChildByName("Panel_MainMenu"))); m_mBackImages.insert(BACKIMAGE_GAMESCENE_BIRTH, dynamic_cast<Layout*>(nodeBack->getChildByName("Panel_GameScene_Birth"))); m_mBackImages.insert(BACKIMAGE_GAMESCENE_CHILDHOOD, dynamic_cast<Layout*>(nodeBack->getChildByName("Panel_GameScene_Childhood"))); m_mBackImages.insert(BACKIMAGE_GAMESCENE_TEENAGE, dynamic_cast<Layout*>(nodeBack->getChildByName("Panel_GameScene_Teenage"))); m_mBackImages.insert(BACKIMAGE_GAMESCENE_YOUTH, dynamic_cast<Layout*>(nodeBack->getChildByName("Panel_GameScene_Youth"))); m_mBackImages.insert(BACKIMAGE_GAMESCENE_MIDAGE, dynamic_cast<Layout*>(nodeBack->getChildByName("Panel_GameScene_Midage"))); m_mBackImages.insert(BACKIMAGE_GAMESCENE_ELDER, dynamic_cast<Layout*>(nodeBack->getChildByName("Panel_GameScene_Elder"))); // get sprites node Vector<Sprite*> tempSprites; auto nodeBirth = dynamic_cast<Node*>(rootNode->getChildByName("Node_Birth")); tempSprites.clear(); tempSprites.pushBack(dynamic_cast<Sprite*>(nodeBirth->getChildByName("Sprite_babyBottle_1"))); tempSprites.pushBack(dynamic_cast<Sprite*>(nodeBirth->getChildByName("Sprite_babyBottle_2"))); m_mBackSprites.insert(pair<int, Vector<Sprite*>>(GAMESTAGE_BIRTH, tempSprites)); auto nodeChildhood = dynamic_cast<Node*>(rootNode->getChildByName("Node_Childhood")); tempSprites.clear(); tempSprites.pushBack(dynamic_cast<Sprite*>(nodeChildhood->getChildByName("Sprite_Pinao"))); tempSprites.pushBack(dynamic_cast<Sprite*>(nodeChildhood->getChildByName("Sprite_guitar"))); tempSprites.pushBack(dynamic_cast<Sprite*>(nodeChildhood->getChildByName("Sprite_mircophone"))); tempSprites.pushBack(dynamic_cast<Sprite*>(nodeChildhood->getChildByName("Sprite_saxphone"))); tempSprites.pushBack(dynamic_cast<Sprite*>(nodeChildhood->getChildByName("Sprite_violin"))); tempSprites.pushBack(dynamic_cast<Sprite*>(nodeChildhood->getChildByName("Sprite_paint"))); m_mBackSprites.insert(pair<int, Vector<Sprite*>>(GAMESTAGE_CHILDHOOD, tempSprites)); auto nodeTeenage = dynamic_cast<Node*>(rootNode->getChildByName("Node_Teenage")); tempSprites.clear(); tempSprites.pushBack(dynamic_cast<Sprite*>(nodeTeenage->getChildByName("Sprite_books_1"))); tempSprites.pushBack(dynamic_cast<Sprite*>(nodeTeenage->getChildByName("Sprite_books_2"))); m_mBackSprites.insert(pair<int, Vector<Sprite*>>(GAMESTAGE_TEENAGE, tempSprites)); auto nodeYouth = dynamic_cast<Node*>(rootNode->getChildByName("Node_Youth")); tempSprites.clear(); tempSprites.pushBack(dynamic_cast<Sprite*>(nodeYouth->getChildByName("Sprite_gift"))); tempSprites.pushBack(dynamic_cast<Sprite*>(nodeYouth->getChildByName("Sprite_graduate"))); m_mBackSprites.insert(pair<int, Vector<Sprite*>>(GAMESTAGE_YOUTH, tempSprites)); auto nodeMidage = dynamic_cast<Node*>(rootNode->getChildByName("Node_Midage")); tempSprites.clear(); tempSprites.pushBack(dynamic_cast<Sprite*>(nodeMidage->getChildByName("Sprite_car"))); tempSprites.pushBack(dynamic_cast<Sprite*>(nodeMidage->getChildByName("Sprite_wallet"))); m_mBackSprites.insert(pair<int, Vector<Sprite*>>(GAMESTAGE_MIDAGE, tempSprites)); auto nodeElder = dynamic_cast<Node*>(rootNode->getChildByName("Node_Elder")); tempSprites.clear(); tempSprites.pushBack(dynamic_cast<Sprite*>(nodeElder->getChildByName("Sprite_crutch"))); tempSprites.pushBack(dynamic_cast<Sprite*>(nodeElder->getChildByName("Sprite_syringe"))); tempSprites.pushBack(dynamic_cast<Sprite*>(nodeElder->getChildByName("Sprite_wheelchair"))); m_mBackSprites.insert(pair<int, Vector<Sprite*>>(GAMESTAGE_ELDER, tempSprites)); tempSprites.clear(); return true; }
void StartScene::startCallBack(Ref* pSender) { getChildByName("main_menu")->setVisible(false); getChildByName("gamemode_menu")->setVisible(true); }