void BScene::showPauseMenu() { //pause game and input Director::getInstance()->pause(); playerLayer->stopMovement(); keyHandler->disableByReceiver(playerLayer); Size windowSize = Director::getInstance()->getWinSize(); overlay = new OverLayer(); //create menu Menu* pMenu = Menu::create(); //create menu items const Point origin = Director::getInstance()->getVisibleOrigin(); MenuItemLabel* title; MenuItemLabel* exitGame; MenuItemLabel* levelSelect; MenuItemLabel* resume; //set up Main menu item resume = MenuItemLabel::create(LabelTTF::create("Resume", "Arial", 20), BScene::resumeCB); resume->setPosition(0,0); pMenu->addChild(resume); //set up Snake Mode item levelSelect = MenuItemLabel::create(LabelTTF::create("Level Select", "Arial", 20), BScene::levelSelectCB); levelSelect->setPosition(0, resume->getPosition().y + resume->getContentSize().height+10); pMenu->addChild(levelSelect); //set up Standard Mode item exitGame = MenuItemLabel::create(LabelTTF::create("Exit Game", "Arial", 20), BScene::exitGameCB); exitGame->setPosition(0, levelSelect->getPosition().y + levelSelect->getContentSize().height+10); pMenu->addChild(exitGame); //set up Title item title = MenuItemLabel::create(LabelTTF::create("Pause Menu", "Arial", 30)); title->setPosition(0, exitGame->getPosition().y + exitGame->getContentSize().height+20); pMenu->addChild(title); //center the menu float heightofitems = exitGame->getBoundingBox().getMaxY() - resume->getBoundingBox().getMinY(); pMenu->setPosition(origin.x + windowSize.width/2, origin.y + windowSize.height/2 - heightofitems/2); overlay->initWithColorMenu( Color4B(0,0,0,150), pMenu); addChild(overlay,4); layerFocus = 1; overlay->release(); }
void ShowHeroScene::CreateMenu() { auto strings = FileUtils::getInstance()->getValueMapFromFile("fonts/menu.xml"); std::string strBeginGame = strings["BeginGame"].asString(); std::string strCreateGame = strings["CreateGame"].asString(); std::string strJoinGame = strings["JoinGame"].asString(); std::string strExitGame = strings["ExitGame"].asString(); if (/*GameLogic::GetInstance()->IsSingleGame()*/GameLogic::GetInstance()->GetGameType() == SINGLE_GAME_TYPE) { MenuItemLabel* beginGameItem = MenuItemLabel::create(LabelTTF::create(strBeginGame, "Arial", 40), [&](Ref* sender) { BeginGame(); } ); beginGameItem->setPosition(Point(500,100)); auto menu = Menu::create(beginGameItem,NULL); menu->setPosition(Point(0, 0)); addChild(menu); } else { if (/*GameLogic::GetInstance()->IsCreator()*/GameLogic::GetInstance()->GetGameType() == CREATE_GAME_TYPE) { MenuItemLabel* createGameItem = MenuItemLabel::create(LabelTTF::create(strCreateGame, "Arial", 40), [&](Ref* sender) { CreateGame(); } ); createGameItem->setPosition(Point(500,100)); auto menu = Menu::create(createGameItem,NULL); menu->setPosition(Point(0, 0)); addChild(menu); } else { MenuItemLabel* joinGameItem = MenuItemLabel::create(LabelTTF::create(strJoinGame, "Arial", 40), [&](Ref* sender) { JoinGame(); } ); joinGameItem->setPosition(Point(500,100)); auto menu = Menu::create(joinGameItem,NULL); menu->setPosition(Point(0, 0)); addChild(menu); } } }
BLevelMenu::BLevelMenu(std::function<void(Object*)> cb, LD levels[], int ldCount) : beginPos(Point::ZERO), callback(cb), levelCount(ldCount) { levelData = levels; std::cout<<levels[0].level_name<<std::endl; Size visSize = Director::getInstance()->getVisibleSize(); Point visOrigin = Director::getInstance()->getVisibleOrigin(); visRect = Rect(visOrigin.x, visOrigin.y, visSize.width, visSize.height); // add menu items for levels itemMenu = Menu::create(); for (int i = 0; i < levelCount; ++i) { LabelTTF* label = LabelTTF::create( levelData[i].level_name.c_str(), "Arial", 24); MenuItemLabel* menuItem = MenuItemLabel::create(label, cb); itemMenu->addChild(menuItem, i + 10000); menuItem->setPosition( Point( visRect.getMidX(), (visRect.getMaxY() - (i + 1) * LINE_SPACE) )); } itemMenu->setContentSize(Size(visRect.size.width, (levelCount + 1) * (LINE_SPACE))); itemMenu->setPosition(s_tCurPos); addChild(itemMenu); setTouchEnabled(true); }
void TestArmatureNesting2::onEnter() { ArmatureTestLayer::onEnter(); auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesEnded = CC_CALLBACK_2(TestArmatureNesting2::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); touchedMenu = false; auto label = Label::createWithTTF("Change Mount", "fonts/arial.ttf", 20); MenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, CC_CALLBACK_1(TestArmatureNesting2::changeMountCallback, this)); Menu* pMenu =Menu::create(pMenuItem, nullptr); pMenu->setPosition( Vec2() ); pMenuItem->setPosition(VisibleRect::right().x - 67, VisibleRect::bottom().y + 50); addChild(pMenu, 2); //Create a hero hero = Hero::create("hero"); hero->setLayer(this); hero->playWithIndex(0); hero->setPosition(VisibleRect::left().x + 20, VisibleRect::left().y); addChild(hero); //Create 3 mount horse = createMount("horse", VisibleRect::center()); horse2 = createMount("horse", Vec2(120, 200)); horse2->setOpacity(200); bear = createMount("bear", Vec2(300,70)); }
TestController::TestController() : _beginPos(PointZero) { // add close menu MenuItemImage *pCloseItem = MenuItemImage::create(s_pPathClose, s_pPathClose, CC_CALLBACK_1(TestController::closeCallback, this) ); Menu* pMenu =Menu::create(pCloseItem, NULL); pMenu->setPosition( PointZero ); pCloseItem->setPosition(ccp( VisibleRect::right().x - 30, VisibleRect::top().y - 30)); // add menu items for tests _itemMenu = Menu::create(); for (int i = 0; i < g_testCount; ++i) { // #if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE) // LabelBMFont* label = LabelBMFont::create(g_aTestNames[i].c_str(), "fonts/arial16.fnt"); // #else LabelTTF* label = LabelTTF::create( g_aTestNames[i].test_name, "Arial", 24); // #endif MenuItemLabel* pMenuItem = MenuItemLabel::create(label, CC_CALLBACK_1(TestController::menuCallback, this)); _itemMenu->addChild(pMenuItem, i + 10000); pMenuItem->setPosition( ccp( VisibleRect::center().x, (VisibleRect::top().y - (i + 1) * LINE_SPACE) )); } _itemMenu->setContentSize(CCSizeMake(VisibleRect::getVisibleRect().size.width, (g_testCount + 1) * (LINE_SPACE))); _itemMenu->setPosition(s_tCurPos); addChild(_itemMenu); setTouchEnabled(true); addChild(pMenu, 1); }
void HelloWorld::showRestartMenu() { Size winSize = Director::getInstance()->getWinSize(); const char *message; if (_won) { message = "You win!"; } else { message = "You lose!"; } LabelBMFont *label = LabelBMFont::create(message, "Arial.fnt"); label->setScale(0.1f); label->setPosition(winSize.width / 2, winSize.height * 0.6f); this->addChild(label); LabelBMFont *restartLabel = LabelBMFont::create("Restart", "Arial.fnt"); MenuItemLabel *restartItem = MenuItemLabel::create(restartLabel, [](Object *sender){ Director::getInstance()->replaceScene(TransitionZoomFlipX::create(0.5f, HelloWorld::createScene())); }); restartItem->setScale(0.1f); restartItem->setPosition(winSize.width / 2, winSize.height * 0.4f); Menu *menu = Menu::createWithItem(restartItem); menu->setPosition(Point::ZERO); this->addChild(menu); restartItem->runAction(ScaleTo::create(0.5f, 1.0f)); label->runAction(ScaleTo::create(0.5f, 1.0f)); }
void HelloWorld::aboutGameCallback(Object* pSender) { Size size = Director::sharedDirector()->getWinSize(); //显示关于的内容 LabelTTF* label = LabelTTF::create("Made by LJW", "Arial", 30); label->setPosition(Point(size.width / 2, size.height / 2)); Scene* s = Scene::create(); Layer* layer = Layer::create(); layer->addChild(label, 1); //返回按钮 LabelTTF* pBackLabel = LabelTTF::create("Back", "Arial", 30); MenuItemLabel* pBackItem = MenuItemLabel::create(pBackLabel, this, menu_selector(HelloWorld::backGameCallback)); Menu* pBackMenu = Menu::create(pBackItem, NULL); pBackItem->setPosition(Point(size.width - 50, 50)); pBackMenu->setPosition(CCPointZero); layer->addChild(pBackMenu, 1); s->addChild(layer); Director::sharedDirector()->replaceScene(s); //s->release(); //layer->release(); }
// on "init" you need to initialize your instance bool MLogin::init() { ////////////////////////////// // 1. super init first if (!Layer::init()) { return false; } Point visibleOrigin = Director::getInstance()->getVisibleOrigin(); Size visibleSize = Director::getInstance()->getVisibleSize(); Size editBoxSize = Size(visibleSize.width - 20, 20); m_pEditBoxName = cocos2d::ui::EditBox::create(editBoxSize, cocos2d::ui::Scale9Sprite::create("green_edit.png")); m_pEditBoxName->setPosition(Vec2(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height * 3 / 4)); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) m_pEditBoxName->setFont("Paint Boy", 15); #else m_pEditBoxName->setFont("Arial", 15); #endif m_pEditBoxName->setFontColor(Color3B::BLACK); m_pEditBoxName->setPlaceHolder("Name:"); m_pEditBoxName->setPlaceholderFontColor(Color3B::WHITE); m_pEditBoxName->setMaxLength(8); m_pEditBoxName->setReturnType(cocos2d::ui::EditBox::KeyboardReturnType::DONE); m_pEditBoxName->setDelegate(this); this->addChild(m_pEditBoxName, 1); m_pEditBoxChannel = cocos2d::ui::EditBox::create(editBoxSize, cocos2d::ui::Scale9Sprite::create("green_edit.png")); m_pEditBoxChannel->setPosition(Vec2(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height / 2)); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) m_pEditBoxChannel->setFont("Paint Boy", 15); #else m_pEditBoxChannel->setFont("Arial", 15); #endif m_pEditBoxChannel->setFontColor(Color3B::BLACK); m_pEditBoxChannel->setPlaceHolder("Channel:"); m_pEditBoxChannel->setPlaceholderFontColor(Color3B::WHITE); m_pEditBoxChannel->setMaxLength(8); m_pEditBoxChannel->setReturnType(cocos2d::ui::EditBox::KeyboardReturnType::DONE); m_pEditBoxChannel->setDelegate(this); this->addChild(m_pEditBoxChannel, 2); Label *label = Label::createWithSystemFont("Login", "Arial", 20); void (*onloginCB)(Ref *ref) = &MLogin::onLogin; MenuItemLabel *pMenuItem = MenuItemLabel::create(label, onloginCB);// const ccMenuCallback &callback Menu *pMenu = Menu::create(pMenuItem, NULL); pMenu->setPosition(Vec2(0,0)); pMenuItem->setPosition(Vec2(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height / 4)); this->addChild(pMenu, 1); Label *pLabel = Label::createWithSystemFont("pomelo-cocos2dchat", "Arial", 10); Size size = Director::getInstance()->getWinSize(); pLabel->setPosition(Vec2(size.width / 2, size.height - 30)); this->addChild(pLabel, 1); return true; }
CocosDenshionTest::CocosDenshionTest() : _itmeMenu(NULL), _beginPos(PointZero), _soundId(0) { std::string testItems[] = { "play background music", "stop background music", "pause background music", "resume background music", "rewind background music", "is background music playing", "play effect", "play effect repeatly", "stop effect", "unload effect", "add background music volume", "sub background music volume", "add effects volume", "sub effects volume", "pause effect", "resume effect", "pause all effects", "resume all effects", "stop all effects" }; // add menu items for tests _itmeMenu = Menu::create(); _testCount = sizeof(testItems) / sizeof(testItems[0]); for (int i = 0; i < _testCount; ++i) { //#if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE) // LabelBMFont* label = LabelBMFont::create(testItems[i].c_str(), "fonts/arial16.fnt"); //#else LabelTTF* label = LabelTTF::create(testItems[i].c_str(), "Arial", 24); //#endif MenuItemLabel* pMenuItem = MenuItemLabel::create(label, CC_CALLBACK_1(CocosDenshionTest::menuCallback, this)); _itmeMenu->addChild(pMenuItem, i + 10000); pMenuItem->setPosition( ccp( VisibleRect::center().x, (VisibleRect::top().y - (i + 1) * LINE_SPACE) )); } _itmeMenu->setContentSize(CCSizeMake(VisibleRect::getVisibleRect().size.width, (_testCount + 1) * LINE_SPACE)); _itmeMenu->setPosition(PointZero); addChild(_itmeMenu); setTouchEnabled(true); // preload background music and effect SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( MUSIC_FILE ); SimpleAudioEngine::sharedEngine()->preloadEffect( EFFECT_FILE ); // set default volume SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5); SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5); }
// on "init" you need to initialize your instance bool ItemLayer::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } this->setKeypadEnabled(true); Size size = CCDirector::getInstance()->getWinSize(); auto title = LabelTTF::create("Item", "Arial", 24); title->setPosition( Point(size.width / 2, size.height - 50)); this->addChild(title, 1); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object MenuItemImage *pCloseItem = MenuItemImage::create( "b1.png", "b2.png", CC_CALLBACK_1(ItemLayer::menuBackCallback,this) ); pCloseItem->setPosition( Point(pCloseItem->getContentSize().width / 2 , 50) ); // create menu, it's an autorelease object auto pMenu = Menu::create(pCloseItem, NULL); pMenu->setPosition( Point::ZERO ); this->addChild(pMenu, 1); // add next menu MenuItemImage *pNextItem = MenuItemImage::create( "f1.png", "f2.png", CC_CALLBACK_1(ItemLayer::menuNextCallback,this) ); pNextItem->setPosition( Point(size.width - pNextItem->getContentSize().width / 2, 50) ); // create menu, it's an autorelease object Menu* pNextMenu = Menu::create(pNextItem, NULL); pNextMenu->setPosition(Point::ZERO); this->addChild(pNextMenu, 1); float step = 35; float yPos = 0; for (int i = 0; i < sizeof(s_EventMenuItem)/sizeof(s_EventMenuItem[0]); i++) { char buf[64]; sprintf(buf, s_EventMenuItem[i].id.c_str(), itemid, counts[i]); auto label = LabelTTF::create(buf, "Arial", 30); MenuItemLabel* pMenuItem = MenuItemLabel::create(label, CC_CALLBACK_1(ItemLayer::eventMenuCallback,this)); pMenu->addChild(pMenuItem, 0, s_EventMenuItem[i].tag); yPos = size.height - step*i - 100; pMenuItem->setPosition( Point(size.width / 2, yPos)); } return true; }
bool MenuLayer::init(){ if (!Layer::init()) { return false; } GameUtile::Pause(); m_visibleSize = Director::getInstance()->getVisibleSize(); //Ìí¼Ómenu°´Å¥ Label* labelSave = Label::create("Save", "Comic Sans MS", 30); MenuItemLabel* menuSave = MenuItemLabel::create(labelSave, CC_CALLBACK_0(MenuLayer::saveGame, this)); menuSave->setPosition(Vec2(m_visibleSize.width * 0.5, m_visibleSize.height * 0.7)); Label* labelRead = Label::create("Read", "Comic Sans MS", 30); MenuItemLabel* menuRead = MenuItemLabel::create(labelRead, CC_CALLBACK_0(MenuLayer::readGame, this)); menuRead->setPosition(Vec2(m_visibleSize.width * 0.5, m_visibleSize.height * 0.6)); Label* labelMusic = Label::create("Muisc", "Comic Sans MS", 30); global->uiLayer->addChild(labelMusic, 110); Label* labelON = Label::create("ON", "Comic Sans MS", 30); Label* labelOFF = Label::create("OFF", "Comic Sans MS", 30); MenuItemLabel* menuMusic0 = MenuItemLabel::create(labelON, CC_CALLBACK_0(MenuLayer::musicON, this)); MenuItemLabel* menuMusic1 = MenuItemLabel::create(labelOFF, CC_CALLBACK_0(MenuLayer::musicOFF, this)); menuMusic0->setPosition(Vec2(m_visibleSize.width * 0.5, m_visibleSize.height * 0.5)); menuMusic1->setPosition(Vec2(m_visibleSize.width * 0.6, m_visibleSize.height * 0.5)); Label* labelReturn = Label::create("Return", "Comic Sans MS", 30); MenuItemLabel* menuReturn = MenuItemLabel::create(labelReturn, CC_CALLBACK_0(MenuLayer::returnGame, this)); menuReturn->setPosition(Vec2(m_visibleSize.width * 0.5, m_visibleSize.height * 0.4)); Label* labelExit = Label::create("Exit Game", "Comic Sans MS", 30); MenuItemLabel* menuExit = MenuItemLabel::create(labelExit, CC_CALLBACK_0(MenuLayer::exitGame, this)); menuExit->setPosition(Vec2(m_visibleSize.width * 0.5, m_visibleSize.height * 0.3)); Menu* menu0 = Menu::create(menuSave, menuRead, menuMusic0, menuMusic1, menuReturn, menuExit, NULL); menu0->setPosition(Vec2::ZERO); global->uiLayer->addChild(menu0, 110); menu0->setTag(0); return true; }
// on "init" you need to initialize your instance bool TestAnalytics::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } _pluginAnalytics = NULL; loadPlugins(); Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); Point posBR = Point(origin.x + visibleSize.width, origin.y); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object MenuItemFont *pBackItem = MenuItemFont::create("Back", CC_CALLBACK_1(TestAnalytics::menuBackCallback, this)); Size backSize = pBackItem->getContentSize(); pBackItem->setPosition(posBR + Point(- backSize.width / 2, backSize.height / 2)); // create menu, it's an autorelease object Menu* pMenu = Menu::create(pBackItem, NULL); pMenu->setPosition( Point::ZERO ); this->addChild(pMenu, 1); float yPos = 0; for (int i = 0; i < sizeof(s_EventMenuItem)/sizeof(s_EventMenuItem[0]); i++) { LabelTTF* label = LabelTTF::create(s_EventMenuItem[i].id.c_str(), "Arial", 24); MenuItemLabel* pMenuItem = MenuItemLabel::create(label, CC_CALLBACK_1(TestAnalytics::eventMenuCallback, this)); pMenu->addChild(pMenuItem, 0, s_EventMenuItem[i].tag); yPos = visibleSize.height - 35*i - 100; pMenuItem->setPosition( Point(visibleSize.width / 2, yPos)); } std::string strName = _pluginAnalytics->getPluginName(); std::string strVer = _pluginAnalytics->getSDKVersion(); char ret[256] = { 0 }; sprintf(ret, "Plugin : %s, Ver : %s", strName.c_str(), strVer.c_str()); LabelTTF* pLabel = LabelTTF::create(ret, "Arial", 18, Point(visibleSize.width, 0), Label::HAlignment::CENTER); pLabel->setPosition(Point(visibleSize.width / 2, yPos - 80)); addChild(pLabel); return true; }
bool GameMenu::init(void) { Size sizeWin = Director::getInstance()->getWinSize(); Menu* pItemMenu = Menu::create(); //TTFConfig ttfConfig(CF_F("fonts_en").c_str(), 48); TTFConfig ttfConfig2(CF_F("fonts_cn").c_str(), CF_FT("font_menu")); auto label = Label::createWithTTF(ttfConfig2, CF_S("label_normal_start")); MenuItemLabel* pMenuItem = MenuItemLabel::create(label, CC_CALLBACK_1(GameMenu::menuNormalGameCallback, this)); auto labelInput = Label::createWithTTF(ttfConfig2, CF_S("label_login")); MenuItemLabel* pMenuInput = MenuItemLabel::create(labelInput, CC_CALLBACK_1(GameMenu::menuUserLoginCallback, this)); auto labelLan = Label::createWithTTF(ttfConfig2, CF_S("label_net_start")); MenuItemLabel* pMenuItemLanTest = MenuItemLabel::create(labelLan, CC_CALLBACK_1(GameMenu::menuNetGameCallback, this)); pItemMenu->addChild(pMenuItem); pItemMenu->addChild(pMenuInput); pItemMenu->addChild(pMenuItemLanTest); pMenuItem->setPosition(0, sizeWin.height * CF_P("pos_main_start")); pMenuInput->setPosition(0, sizeWin.height * CF_P("pos_main_login")); pMenuItemLanTest->setPosition(0, sizeWin.height * CF_P("pos_main_netstart")); pItemMenu->setPositionY(0); this->addChild(pItemMenu); MenuItemImage *pCloseItem = MenuItemImage::create(CF_F("image_closeNormal"), CF_F("image_closeSelect"), CC_CALLBACK_1(GameMenu::menuCloseCallback, this)); Menu* pCloseMenu = Menu::create(pCloseItem, NULL); pCloseMenu->setPositionY(sizeWin.height * CF_P("pos_main_close")); this->addChild(pCloseMenu); return true; }
void CocostudioParserJsonScene::onEnter() { CCScene::onEnter(); auto label = Label::createWithTTF("Back", "fonts/arial.ttf", 20); //#endif MenuItemLabel* pMenuItem = MenuItemLabel::create(label, CC_CALLBACK_1(CocostudioParserJsonScene::BackCallback, this)); Menu* pMenu = Menu::create(pMenuItem, nullptr); pMenu->setPosition( Vec2::ZERO ); pMenuItem->setPosition(VisibleRect::right().x - 50, VisibleRect::bottom().y + 25); addChild(pMenu, 1); }
void TestScene::onEnter() { Scene::onEnter(); //add the menu item for back to main menu //#if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE) // LabelBMFont* label = LabelBMFont::create("MainMenu", "fonts/arial16.fnt"); //#else LabelTTF* label = LabelTTF::create("MainMenu", "Arial", 20); //#endif MenuItemLabel* pMenuItem = MenuItemLabel::create(label, [](Object *sender) { /* ****** GCC Compiler issue on Android and Linux (CLANG compiler is ok) ****** We couldn't use 'Scene::create' directly since gcc will trigger an error called "error: 'this' was not captured for this lambda function". This is because 'Scene' is the super class of TestScene, if we invoke 'Scene::create' directly in this lambda expression, gcc compiler found 'Scene::create' and it think that was the member function of 'TestScene' 's super class, but this lambda function doesn't capture anything like 'this', so it has no access to invoke 'Scene::create'. Solution (1): Passing 'this' to this lambda function. Solution (2): Don't use 'Scene::create' and don't pass 'this' to this lambda function, instead, we just need to new the 'Scene' and initialize the Scene. Semantically, I think in this lambda function, we shouldn't capture any varibles outside the scope. So I choose the (2) solution. Commented by James Chen. */ // Scene *pScene = Scene::create(); Scene *pScene = new Scene(); if (pScene && pScene->init()) { Layer* pLayer = new TestController(); pScene->addChild(pLayer); pLayer->release(); Director::sharedDirector()->replaceScene(pScene); pScene->release(); } }); Menu* pMenu =Menu::create(pMenuItem, NULL); pMenu->setPosition( PointZero ); pMenuItem->setPosition( ccp( VisibleRect::right().x - 50, VisibleRect::bottom().y + 25) ); addChild(pMenu, 1); }
void HelloWorld::showStartGameLayer() { // Get the dimensions of the window for calculation purposes Size winSize = Director::getInstance()->getWinSize(); startGameLayer = StartGameLayer::create(); addChild(startGameLayer); LabelTTF *buttonTitle = LabelTTF::create("Start Game", "Marker Felt", 30); buttonTitle->setColor(Color3B::BLACK); MenuItemLabel *startGameButton = MenuItemLabel::create(buttonTitle, CC_CALLBACK_1(HelloWorld::connectToAppWarp, this)); startGameButton->setPosition(Point(winSize.width/2,winSize.height/2)); Menu *pMenu = Menu::create(startGameButton,NULL); pMenu->setPosition(Point::ZERO); startGameLayer->addChild(pMenu, 1); }
void CustomParticleWidgetScene::onEnter() { CCScene::onEnter(); Layer* pLayer = new CustomParticleWidgetLayer(); addChild(pLayer); pLayer->release(); auto label = Label::createWithTTF("Back", "fonts/arial.ttf", 20); //#endif MenuItemLabel* pMenuItem = MenuItemLabel::create(label, CC_CALLBACK_1(CustomParticleWidgetScene::BackCallback, this)); Menu* pMenu = Menu::create(pMenuItem, nullptr); pMenu->setPosition( Vec2::ZERO ); pMenuItem->setPosition( Vec2( VisibleRect::right().x - 50, VisibleRect::bottom().y + 25) ); addChild(pMenu, 1); }
bool CoordinateScene::init() { if(!Layer::init()){ return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); CCLayerColor *bg = CCLayerColor::create(ccc4(10, 10, 100, 255), visibleSize.width, visibleSize.height); bg->setPosition(Vec2(0,0)); this->addChild(bg); Label *label = Label::create("go back", "Arial", 32); MenuItemLabel *backLable = MenuItemLabel::create(label, CC_CALLBACK_1(CoordinateScene::goback, this)); backLable->setPosition(Vec2(100, 500)); auto backMenu = Menu::createWithItem(backLable); backMenu->setPosition(Vec2::ZERO); this->addChild(backMenu); return true; }
void ShowHeroScene::CreateGameRoundMenu(vector<GameRoundInfo>& gameRounfInfo) { for (int i = 0 ; i < gameRounfInfo.size(); ++i) { std::string strInfo = "GameIndex:"; strInfo += Util::ConvertToStr(gameRounfInfo[i].m_nGameIndex); strInfo += " PeerID:"; strInfo+= Util::ConvertToStr(gameRounfInfo[i].m_nCreateHeroID); MenuItemLabel* joinGameItem = MenuItemLabel::create(LabelTTF::create(strInfo, "Arial", 40), [&](Ref* sender) { //JoinGame(); } ); joinGameItem->setPosition(Point(500,150+5*i)); auto menu = Menu::create(joinGameItem,NULL); menu->setPosition(Point(0, 0)); addChild(menu); } }
bool MenuScene::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } m_menu = Menu::create(); m_menu->setPosition(Point::ZERO); this->addChild(m_menu); for (size_t i = 0; i < g_fractals_size; ++i) { LabelTTF * label = LabelTTF::create(g_fractals[i].fractal_name, "fonts/Marker Felt.ttf", 24); MenuItemLabel * menuItem = MenuItemLabel::create(label, CC_CALLBACK_1(MenuScene::menuCallback, this)); menuItem->setPosition(Point(VisibleRect::center().x, (VisibleRect::top().y - (i + 1) * LINE_SPACE))); m_menu->addChild(menuItem, i); } EventListenerTouchOneByOne * touchListener = EventListenerTouchOneByOne::create(); touchListener->setSwallowTouches(true); touchListener->onTouchBegan = CC_CALLBACK_2(MenuScene::onTouchBegan, this); touchListener->onTouchMoved = CC_CALLBACK_2(MenuScene::onTouchMoved, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, this); EventListenerMouse * mouseListener = EventListenerMouse::create(); mouseListener->onMouseScroll = CC_CALLBACK_1(MenuScene::onMouseScroll, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(mouseListener, this); return true; }
// on "init" you need to initialize your instance bool TestAds::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } _listener = new MyAdsListener(); _admob = dynamic_cast<ProtocolAds*>(PluginManager::getInstance()->loadPlugin("AdsAdmob")); _flurryAds = dynamic_cast<ProtocolAds*>(PluginManager::getInstance()->loadPlugin("AdsFlurry")); TAdsDeveloperInfo devInfo; #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS devInfo["AdmobID"] = ADMOB_ID_IOS; devInfo["FlurryAppKey"] = FLURRY_KEY_IOS; #elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID devInfo["AdmobID"] = ADMOB_ID_ANDROID; devInfo["FlurryAppKey"] = FLURRY_KEY_ANDROID; #endif _admob->configDeveloperInfo(devInfo); _admob->setAdsListener(_listener); _admob->setDebugMode(true); _flurryAds->configDeveloperInfo(devInfo); _flurryAds->setAdsListener(_listener); _flurryAds->setDebugMode(true); Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); Point posMid = Point(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2); Point posBR = Point(origin.x + visibleSize.width, origin.y); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object MenuItemFont *pBackItem = MenuItemFont::create("Back", CC_CALLBACK_1(TestAds::menuBackCallback, this)); Size backSize = pBackItem->getContentSize(); pBackItem->setPosition(posBR + Point(- backSize.width / 2, backSize.height / 2)); // create menu, it's an autorelease object Menu* pMenu = Menu::create(pBackItem, NULL); pMenu->setPosition( Point::ZERO ); LabelTTF* label1 = LabelTTF::create("ShowAds", "Arial", 24); MenuItemLabel* pItemShow = MenuItemLabel::create(label1, CC_CALLBACK_1(TestAds::testShow, this)); pItemShow->setAnchorPoint(Point(0.5f, 0)); pMenu->addChild(pItemShow, 0); pItemShow->setPosition(posMid + Point(-100, -120)); LabelTTF* label2 = LabelTTF::create("HideAds", "Arial", 24); MenuItemLabel* pItemHide = MenuItemLabel::create(label2, CC_CALLBACK_1(TestAds::testHide, this)); pItemHide->setAnchorPoint(Point(0.5f, 0)); pMenu->addChild(pItemHide, 0); pItemHide->setPosition(posMid + Point(100, -120)); // create optional menu // cases item _caseItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(TestAds::caseChanged, this), MenuItemFont::create( s_aTestCases[0].c_str() ), NULL ); int caseLen = sizeof(s_aTestCases) / sizeof(std::string); for (int i = 1; i < caseLen; ++i) { _caseItem->getSubItems()->addObject( MenuItemFont::create( s_aTestCases[i].c_str() ) ); } _caseItem->setPosition(posMid + Point(-200, 120)); pMenu->addChild(_caseItem); // poses item _posItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(TestAds::posChanged, this), MenuItemFont::create( s_aTestPoses[0].c_str() ), NULL ); int posLen = sizeof(s_aTestPoses) / sizeof(std::string); for (int i = 1; i < posLen; ++i) { _posItem->getSubItems()->addObject( MenuItemFont::create( s_aTestPoses[i].c_str() ) ); } _posItem->setPosition(posMid + Point(200, 120)); pMenu->addChild(_posItem); // init options _ads = _admob; _pos = ProtocolAds::kPosCenter; // init the AdsInfo adInfo["AdmobType"] = "1"; adInfo["AdmobSizeEnum"] = "1"; adInfo["FlurryAdsID"] = "BANNER_MAIN_VC"; adInfo["FlurryAdsSize"] = "2"; this->addChild(pMenu, 1); return true; }
void GameLayer::addScore() { // label2048 Label *_2048 = Label::createWithTTF("2048", "fonts/ClearSans-Bold.ttf", _visibleSize.width/5.9); _2048->setColor(Color3B(121,110,100) ); _2048->setAnchorPoint(Vec2( 0, 0.5 )); _2048->setPosition(Vec2( 0, _visibleSize.height-_2048->getContentSize().height/2) ); this->addChild(_2048); // best score DrawNode *bestscoreback = DrawNode::create(); bestscoreback->drawSolidRect( Vec2(-_visibleSize.width/6 ,-g_tileSize/3), Vec2(_visibleSize.width/6 ,g_tileSize/3), Color4F(Color3B(190,173,158)) ); bestscoreback->setPosition( Vec2(_visibleSize.width - _visibleSize.width/6-10 , _2048->getPositionY()-10 ) ); this->addChild(bestscoreback); Label *best = Label::createWithTTF("BEST", "fonts/ClearSans-Bold.ttf", _visibleSize.width/25); best->setColor(Color3B(240,228,216)); best->setPositionY(g_tileSize/6.5 ); bestscoreback->addChild(best); sprintf(buff, "%d", Score::getInstance()->getTopScore()); bestscore = Label::createWithTTF(buff, "fonts/ClearSans-Bold.ttf", _visibleSize.width/15); bestscore->setPositionY( -g_tileSize/7.5); bestscoreback->addChild(bestscore); //score DrawNode *scoreback = DrawNode::create(); scoreback->drawSolidRect( Vec2(-_visibleSize.width/6 ,-g_tileSize/3), Vec2(_visibleSize.width/6 ,g_tileSize/3), Color4F(Color3B(190,173,158)) ); scoreback->setPosition( Vec2(_visibleSize.width - _visibleSize.width/6-10 , _2048->getPositionY() - g_tileSize/1 ) ); this->addChild(scoreback); Label *sco = Label::createWithTTF("SCORE", "fonts/ClearSans-Bold.ttf", _visibleSize.width/25); sco->setColor(Color3B(240,228,216)); sco->setPositionY(g_tileSize/6.5 ); scoreback->addChild(sco); sprintf(buff, "%d", 0); score = Label::createWithTTF(buff, "fonts/ClearSans-Bold.ttf", _visibleSize.width/15); score->setPositionY( -g_tileSize/7.5); scoreback->addChild(score); // new game DrawNode *newGame = DrawNode::create(); newGame->drawSolidRect( Vec2(-_visibleSize.width/6 ,-g_tileSize/4), Vec2(_visibleSize.width/6 ,g_tileSize/4), Color4F(Color3B(147,122,98)) ); newGame->setPosition( Vec2(_visibleSize.width/5.5 , _2048->getPositionY() - g_tileSize/1 ) ); this->addChild(newGame); Label *ngame = Label::createWithTTF("New Game", "fonts/ClearSans-Bold.ttf", _visibleSize.width/17); ngame->setColor(Color3B(250,246,241)); //newGame->addChild(ngame); // ai DrawNode *aiback = DrawNode::create(); aiback->drawSolidRect( Vec2(-_visibleSize.width/15 ,-g_tileSize/4), Vec2(_visibleSize.width/15 ,g_tileSize/4), Color4F(Color3B(147,122,98)) ); aiback->setPosition( Vec2(_visibleSize.width/2.3 , _2048->getPositionY() - g_tileSize/1 ) ); this->addChild(aiback); Label *ai = Label::createWithTTF("AI", "fonts/ClearSans-Bold.ttf", _visibleSize.width/17); ai->setColor(Color3B(250,246,241)); //aiback->addChild(ai); MenuItemLabel *newgamelabel = MenuItemLabel::create(ngame, CC_CALLBACK_1(GameLayer::startNewGame, this)); newgamelabel->setPosition(newGame->getPosition()); ailable = MenuItemLabel::create(ai, CC_CALLBACK_1(GameLayer::startAi, this)); ailable->setPosition(aiback->getPosition()); Menu *menu = Menu::create(newgamelabel,ailable, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu); }
void Dialog::updateLayout() { Size content_size = this->getContentSize(); Menu* menu = Menu::create(); menu->setPosition(15.0f, 10.0f); menu->setContentSize(Size(240.0f, 27.0f)); float button_size = (240.0f - 1.0f * (m_list_button.size() - 1)) / m_list_button.size(); int i = 0; for(auto it = m_list_button.begin(); it != m_list_button.end(); it++, i++) { if(it != m_list_button.begin()) { DrawObject* draw_line = DrawObject::create(); draw_line->setPosition(button_size + 1.0f * (i - 1), 1.0f); draw_line->setContentSize(Size(1.0f, 25.0f)); draw_line->setOnDraw([=] () { DrawPrimitives::setDrawColor4B(127, 127, 127, 255); DrawPrimitives::drawLine(Point(0.0f, 0.0f), Point(0.0f, draw_line->getContentSize().height)); }); draw_line->setAnchorPoint(Point(0.0f, 0.0f)); menu->addChild(draw_line); } LabelTTF* label = LabelTTF::create(it->first, FourCard::DEFAULT_FONT, 20.0f, Size(button_size, 27.0f), TextHAlignment::CENTER, TextVAlignment::CENTER); label->setColor(Color3B(127, 127, 127)); label->setAnchorPoint(Point(0.0f, 0.0f)); MenuItemLabel* menuItem = MenuItemLabel::create(label, [=] (Object* pSender) { it->second(); }); menuItem->setAnchorPoint(Point(0.0f, 0.0f)); menuItem->setPosition(button_size * i + 1.0f * i, 0.0f); menu->addChild(menuItem); } m_dialog->addChild(menu); DrawObject* draw_line_message = DrawObject::create(); draw_line_message->setPosition(15.0f, 47.0f); draw_line_message->setContentSize(Size(240.0f, 1.0f)); draw_line_message->setOnDraw([=] () { DrawPrimitives::setDrawColor4B(127, 127, 127, 255); DrawPrimitives::drawLine(Point(0.0f, 0.0f), Point(draw_line_message->getContentSize().width, 0.0f)); }); draw_line_message->setAnchorPoint(Point(0.0f, 0.0f)); m_dialog->addChild(draw_line_message); if(m_layer != nullptr) { m_layer->setPosition(15.0f + (240.0f - m_layer->getContentSize().width) / 2, 48.0f); m_dialog->addChild(m_layer); } LabelTTF* label_message = LabelTTF::create(m_message, FourCard::DEFAULT_FONT, 15.0f, Size(240.0f, 0.0f), TextHAlignment::CENTER); label_message->setPosition(15.0f, 63.0f + ((m_layer != nullptr) ? m_layer->getContentSize().height : 0)); label_message->setColor(Color3B(60, 60, 60)); label_message->setAnchorPoint(Point(0.0f, 0.0f)); m_dialog->addChild(label_message); DrawObject* draw_line_title = DrawObject::create(); draw_line_title->setPosition(15.0f, 59.0f + ((m_layer != nullptr) ? m_layer->getContentSize().height : 0) + label_message->getContentSize().height + 15.0f); draw_line_title->setContentSize(Size(240.0f, 1.0f)); draw_line_title->setOnDraw([=] () { DrawPrimitives::setDrawColor4B(127, 127, 127, 255); DrawPrimitives::drawLine(Point(0.0f, 0.0f), Point(draw_line_title->getContentSize().width, 0.0f)); }); draw_line_title->setAnchorPoint(Point(0.0f, 0.0f)); m_dialog->addChild(draw_line_title); LabelTTF* label_title = LabelTTF::create(m_title, FourCard::DEFAULT_FONT, 20.0f, Size(240.0f, 27.0f), TextHAlignment::LEFT, TextVAlignment::CENTER); label_title->setPosition(15.0f, 74.0f + ((m_layer != nullptr) ? m_layer->getContentSize().height : 0) + label_message->getContentSize().height + 1.0f + 10.0f); label_title->setColor(Color3B(60, 60, 60)); label_title->setAnchorPoint(Point(0.0f, 0.0f)); m_dialog->addChild(label_title); m_dialog->setContentSize(Size(270.0f, 124.0f + ((m_layer != nullptr) ? m_layer->getContentSize().height : 0) + label_message->getContentSize().height)); m_dialog->setPosition((content_size.width - m_dialog->getContentSize().width) / 2, (content_size.height - m_dialog->getContentSize().height) / 2); Scale9Sprite* sprite_background = Scale9Sprite::create(Rect(10, 10, 1, 1), "sprites/dialog/background.png"); sprite_background->setContentSize(m_dialog->getContentSize()); sprite_background->setAnchorPoint(Point(0.0f, 0.0f)); m_dialog->addChild(sprite_background, -1); }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); // TiledMapEditorで生成したマップを表示する _map = TMXTiledMap::create("TMX/stage1.tmx"); _mapLayer = _map->getLayer("layer1"); // マップのサイズ Size mapSize = Size(_map->getTileSize().width * _map->getMapSize().width, _map->getTileSize().height * _map->getMapSize().height); // スクロールビュー ScrollView *scrollView = ScrollView::create(mapSize*3); scrollView->setAnchorPoint(Vec2::ANCHOR_MIDDLE); scrollView->setPosition(Vec2(100, 100)); scrollView->setMaxScale(5.0f); scrollView->setBounceable(false); scrollView->setContentSize(mapSize*3); this->addChild(scrollView); // 中のレイヤー Layer *innerLayer = Layer::create(); scrollView->addChild(innerLayer); // スクロールビューの背景 Sprite *sprite = Sprite::create(); sprite->setTextureRect(Rect(0, 0, scrollView->getContentSize().width, scrollView->getContentSize().height)); sprite->setColor(Color3B(230,230,230)); sprite->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT); innerLayer->addChild(sprite); // マップを表示 _map->setScale(3); innerLayer->addChild(_map); // キャラクターを表示 _character = Sprite::create("TMX/character.png"); _character->setAnchorPoint(Vec2(0.5f, 0.25f)); _charaPos = Vec2(0, 0); _character->setPosition(_mapLayer->getPositionAt(_charaPos) + _map->getTileSize()/2); _map->addChild(_character); // サイコロボタン _isMoving = false; MenuItemLabel *dice = MenuItemLabel::create(Label::createWithSystemFont("サイコロ", "arial", 30.0f), CC_CALLBACK_1(HelloWorld::dice, this)); dice->setPosition(Vec2(600, 0)); this->addChild(Menu::create(dice, NULL)); // サイコロの目 Label *pips = Label::createWithSystemFont("-", "arial", 30.0f); pips->setPosition(Vec2(1200, 300)); pips->setTag(10); this->addChild(pips); return true; }
// on "init" you need to initialize your instance bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); closeItem->setPosition( Point(closeItem->getContentSize().width / 2 , 50) ); // create menu, it's an autorelease object auto menu = Menu::create(closeItem, NULL); menu->setPosition(Point::ZERO); this->addChild(menu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label auto label = LabelTTF::create("Hello World", "Arial", 24); // position the label on the center of the screen label->setPosition(Point(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height)); // add the label as a child to this layer this->addChild(label, 1); // add "HelloWorld" splash screen" auto sprite = Sprite::create("HelloWorld.png"); // position the sprite on the center of the screen sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer this->addChild(sprite, 0); float step = 35; float yPos = 0; for (int i = 0; i < sizeof(s_EventMenuItem)/sizeof(s_EventMenuItem[0]); i++) { char buf[64]; sprintf(buf, s_EventMenuItem[i].id.c_str(), ++_indexes[i]); auto label = LabelTTF::create(buf, "Arial", 24); MenuItemLabel* pMenuItem = MenuItemLabel::create(label, CC_CALLBACK_1(HelloWorld::menuEventCallback, this)); menu->addChild(pMenuItem, 0, s_EventMenuItem[i].tag); yPos = visibleSize.height - step*i - 100; pMenuItem->setPosition( Point(visibleSize.width / 2, yPos)); } // add next menu MenuItemImage *pNextItem = MenuItemImage::create("f1.png","f1.png",CC_CALLBACK_1(HelloWorld::menuNextCallback,this)); pNextItem->setPosition( Point(visibleSize.width - pNextItem->getContentSize().width / 2, 50) ); // create menu, it's an autorelease object Menu* pNextMenu = Menu::create(pNextItem, NULL); pNextMenu->setPosition( Point::ZERO ); this->addChild(pNextMenu, 1); return true; }
// on "init" you need to initialize your instance bool Game::init() { if ( !LayerColor::init()) {//initWithColor(Color4B(255, 0, 0, 255)) ) { return false; } // add background music CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("action.mp3", true); // enable touch events setTouchEnabled(true); // get screen size Size screenSize = Director::getInstance()->getVisibleSize(); // add menu button MenuItemLabel* menuItem = MainMenu::createButton("End Game", CC_CALLBACK_1(Game::menuCallback, this)); menuItem->setPosition(Point(screenSize.width - menuItem->getContentSize().width/2 - Letter::PADDING, menuItem->getContentSize().height/2 + Letter::PADDING)); // create menu, it's an autorelease object auto menu = Menu::create(menuItem, NULL); menu->setPosition(Point::ZERO); this->addChild(menu, 1); // display score on the screen scoreLabel = LabelTTF::create("Score: 0", "Arial", 35); scoreLabel->setColor(Color3B(255, 255, 255)); scoreLabel->setPosition(Point(Letter::PADDING + scoreLabel->getContentSize().width/2, screenSize.height-scoreLabel->getContentSize().height/2 - Letter::PADDING)); this->addChild(scoreLabel, 1); // display moves on the screen movesLabel = LabelTTF::create("Moves: 20", "Arial", 35); movesLabel->setColor(Color3B(255, 255, 255)); movesLabel->setPosition(Point(screenSize.width - movesLabel->getContentSize().width/2 - Letter::PADDING, screenSize.height - movesLabel->getContentSize().height/2 - Letter::PADDING)); this->addChild(movesLabel, 1); // set number of moves moves = STARTING_MOVES; // create board Board board = Board(this); // whether a letter has been selected bool letterSelected = false; auto listener = EventListenerTouch::create(Touch::DispatchMode::ONE_BY_ONE); listener->setSwallowTouches(false); listener->onTouchBegan = [=](Touch* touch, Event* event) mutable { //log("touch began"); Point location = touch->getLocation();//->getLocationInView(); // TODO: Detect touches on labels for (int i = 0; i < Board::BOARD_SIZE; i++) { vector<Letter> row = board.letters[i]; for (int j = 0; j < Board::BOARD_SIZE; j++) { Letter l = row[j]; if (location.getDistance(l.posn) < 30) { //log("touched %c", l.letter); l.label->setColor(cocos2d::Color3B(0, 255, 0)); //cocos2d::log("letter selected %s, %c", letterSelected ? "true" : "false", board.selected.letter); if (!letterSelected) { // first letter selected board.selected = l; letterSelected = true; //cocos2d::log("set selected to %c, %s", board.selected.letter, letterSelected ? "true" : "false"); } else if (board.selected != l) { // second selected letter is not the same as first board.letterSwap(l); letterSelected = false; UpdateScore(); UpdateMoves(); } else { // same letter was selected //cocos2d::log("you selected the same letter %c", board.selected.letter); // set letterSelected to false letterSelected = false; // reset selected color board.selected.label->setColor(cocos2d::Color3B(255, 255, 255)); // set selected letter to empty Letter board.selected = Letter(); } return true; } } } return false; }; // unused listeners //listener->onTouchMoved = [](Touch* touch, Event* event) { log("touch moved"); }; //listener->onTouchEnded = [](Touch* touch, Event* event) { log("touch ended"); }; //listener->onTouchCancelled = [](Touch* touch, Event* event) { log("touch canceled"); }; // The priority of the touch listener is based on the draw order of sprite //EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, sprite); // Or the priority of the touch listener is a fixed value EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 100); return true; }
SocketIOTestLayer::SocketIOTestLayer(void) : _sioClient(NULL) , _sioEndpoint(NULL) { //set the clients to NULL until we are ready to connect Size winSize = Director::getInstance()->getWinSize(); const int MARGIN = 40; const int SPACE = 35; LabelTTF *label = LabelTTF::create("SocketIO Extension Test", "Arial", 28); label->setPosition(Point(winSize.width / 2, winSize.height - MARGIN)); addChild(label, 0); Menu *menuRequest = Menu::create(); menuRequest->setPosition(Point::ZERO); addChild(menuRequest); // Test to create basic client in the default namespace LabelTTF *labelSIOClient = LabelTTF::create("Open SocketIO Client", "Arial", 22); MenuItemLabel *itemSIOClient = MenuItemLabel::create(labelSIOClient, CC_CALLBACK_1(SocketIOTestLayer::onMenuSIOClientClicked, this)); itemSIOClient->setPosition(Point(VisibleRect::left().x + labelSIOClient->getContentSize().width / 2 + 5, winSize.height - MARGIN - SPACE)); menuRequest->addChild(itemSIOClient); // Test to create a client at the endpoint '/testpoint' LabelTTF *labelSIOEndpoint = LabelTTF::create("Open SocketIO Endpoint", "Arial", 22); MenuItemLabel *itemSIOEndpoint = MenuItemLabel::create(labelSIOEndpoint, CC_CALLBACK_1(SocketIOTestLayer::onMenuSIOEndpointClicked, this)); itemSIOEndpoint->setPosition(Point(VisibleRect::right().x - labelSIOEndpoint->getContentSize().width / 2 - 5, winSize.height - MARGIN - SPACE)); menuRequest->addChild(itemSIOEndpoint); // Test sending message to default namespace LabelTTF *labelTestMessage = LabelTTF::create("Send Test Message", "Arial", 22); MenuItemLabel *itemTestMessage = MenuItemLabel::create(labelTestMessage, CC_CALLBACK_1(SocketIOTestLayer::onMenuTestMessageClicked, this)); itemTestMessage->setPosition(Point(VisibleRect::left().x + labelTestMessage->getContentSize().width / 2 + 5, winSize.height - MARGIN - 2 * SPACE)); menuRequest->addChild(itemTestMessage); // Test sending message to the endpoint '/testpoint' LabelTTF *labelTestMessageEndpoint = LabelTTF::create("Test Endpoint Message", "Arial", 22); MenuItemLabel *itemTestMessageEndpoint = MenuItemLabel::create(labelTestMessageEndpoint, CC_CALLBACK_1(SocketIOTestLayer::onMenuTestMessageEndpointClicked, this)); itemTestMessageEndpoint->setPosition(Point(VisibleRect::right().x - labelTestMessageEndpoint->getContentSize().width / 2 - 5, winSize.height - MARGIN - 2 * SPACE)); menuRequest->addChild(itemTestMessageEndpoint); // Test sending event 'echotest' to default namespace LabelTTF *labelTestEvent = LabelTTF::create("Send Test Event", "Arial", 22); MenuItemLabel *itemTestEvent = MenuItemLabel::create(labelTestEvent, CC_CALLBACK_1(SocketIOTestLayer::onMenuTestEventClicked, this)); itemTestEvent->setPosition(Point(VisibleRect::left().x + labelTestEvent->getContentSize().width / 2 + 5, winSize.height - MARGIN - 3 * SPACE)); menuRequest->addChild(itemTestEvent); // Test sending event 'echotest' to the endpoint '/testpoint' LabelTTF *labelTestEventEndpoint = LabelTTF::create("Test Endpoint Event", "Arial", 22); MenuItemLabel *itemTestEventEndpoint = MenuItemLabel::create(labelTestEventEndpoint, CC_CALLBACK_1(SocketIOTestLayer::onMenuTestEventEndpointClicked, this)); itemTestEventEndpoint->setPosition(Point(VisibleRect::right().x - labelTestEventEndpoint->getContentSize().width / 2 - 5, winSize.height - MARGIN - 3 * SPACE)); menuRequest->addChild(itemTestEventEndpoint); // Test disconnecting basic client LabelTTF *labelTestClientDisconnect = LabelTTF::create("Disconnect Socket", "Arial", 22); MenuItemLabel *itemClientDisconnect = MenuItemLabel::create(labelTestClientDisconnect, CC_CALLBACK_1(SocketIOTestLayer::onMenuTestClientDisconnectClicked, this)); itemClientDisconnect->setPosition(Point(VisibleRect::left().x + labelTestClientDisconnect->getContentSize().width / 2 + 5, winSize.height - MARGIN - 4 * SPACE)); menuRequest->addChild(itemClientDisconnect); // Test disconnecting the endpoint '/testpoint' LabelTTF *labelTestEndpointDisconnect = LabelTTF::create("Disconnect Endpoint", "Arial", 22); MenuItemLabel *itemTestEndpointDisconnect = MenuItemLabel::create(labelTestEndpointDisconnect, CC_CALLBACK_1(SocketIOTestLayer::onMenuTestEndpointDisconnectClicked, this)); itemTestEndpointDisconnect->setPosition(Point(VisibleRect::right().x - labelTestEndpointDisconnect->getContentSize().width / 2 - 5, winSize.height - MARGIN - 4 * SPACE)); menuRequest->addChild(itemTestEndpointDisconnect); // Sahred Status Label _sioClientStatus = LabelTTF::create("Not connected...", "Arial", 14, Size(320, 100), Label::HAlignment::LEFT); _sioClientStatus->setAnchorPoint(Point(0, 0)); _sioClientStatus->setPosition(Point(VisibleRect::left().x, VisibleRect::rightBottom().y)); this->addChild(_sioClientStatus); // Back Menu MenuItemFont *itemBack = MenuItemFont::create("Back", CC_CALLBACK_1(SocketIOTestLayer::toExtensionsMainLayer, this)); itemBack->setPosition(Point(VisibleRect::rightBottom().x - 50, VisibleRect::rightBottom().y + 25)); Menu *menuBack = Menu::create(itemBack, NULL); menuBack->setPosition(Point::ZERO); addChild(menuBack); }
// on "init" you need to initialize your instance bool TestSocial::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } MySocialManager::getInstance()->loadPlugins(); Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); Point posMid = Point(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2); Point posBR = Point(origin.x + visibleSize.width, origin.y); MenuItemFont *pBackItem = MenuItemFont::create("Back", CC_CALLBACK_1(TestSocial::menuBackCallback, this)); Size backSize = pBackItem->getContentSize(); pBackItem->setPosition(posBR + Point(- backSize.width / 2, backSize.height / 2)); // create menu, it's an autorelease object Menu* pMenu = Menu::create(pBackItem, NULL); pMenu->setPosition( Point::ZERO ); LabelTTF* label1 = LabelTTF::create("Submit Score", "Arial", 28); MenuItemLabel* pItemSubmit = MenuItemLabel::create(label1, CC_CALLBACK_1(TestSocial::testSubmit, this)); pItemSubmit->setAnchorPoint(Point(0.5f, 0)); pMenu->addChild(pItemSubmit, 0); pItemSubmit->setPosition(posMid + Point(-140, -60)); LabelTTF* label2 = LabelTTF::create("Unlock Achievement", "Arial", 28); MenuItemLabel* pItemUnlock = MenuItemLabel::create(label2, CC_CALLBACK_1(TestSocial::testUnlock, this)); pItemUnlock->setAnchorPoint(Point(0.5f, 0)); pMenu->addChild(pItemUnlock, 0); pItemUnlock->setPosition(posMid + Point(140, -60)); LabelTTF* label3 = LabelTTF::create("Show Leaderboard", "Arial", 28); MenuItemLabel* pItemLeader = MenuItemLabel::create(label3, CC_CALLBACK_1(TestSocial::testLeaderboard, this)); pItemLeader->setAnchorPoint(Point(0.5f, 0)); pMenu->addChild(pItemLeader, 0); pItemLeader->setPosition(posMid + Point(-140, -120)); LabelTTF* label4 = LabelTTF::create("Show Achievement", "Arial", 28); MenuItemLabel* pItemAchi = MenuItemLabel::create(label4, CC_CALLBACK_1(TestSocial::testAchievement, this)); pItemAchi->setAnchorPoint(Point(0.5f, 0)); pMenu->addChild(pItemAchi, 0); pItemAchi->setPosition(posMid + Point(140, -120)); // create optional menu // cases item _caseItem = MenuItemToggle::createWithCallback(NULL, MenuItemFont::create( s_aTestCases[0].c_str() ), NULL ); int caseLen = sizeof(s_aTestCases) / sizeof(std::string); for (int i = 1; i < caseLen; ++i) { _caseItem->getSubItems().pushBack( MenuItemFont::create( s_aTestCases[i].c_str() ) ); } _caseItem->setPosition(posMid + Point(0, 120)); pMenu->addChild(_caseItem); this->addChild(pMenu, 1); return true; }
void StartScene::toSetting(){ auto director = Director::getInstance(); RenderTexture* renderTexture = RenderTexture::create(visibleSize.width, visibleSize.height); renderTexture->begin(); this->getParent()->visit(); renderTexture->end(); auto scene = pausePopup::createScene(renderTexture, "images/popupBg1.png"); auto layer = pausePopup::create(renderTexture, "images/popupBg1.png"); scene->addChild(layer, 1); layer->contentText = Label::createWithTTF("游戏设置", "fonts/msyhbd.ttc", 20); layer->contentText->setPosition(layer->menuSprite->getContentSize().width / 2, layer->menuSprite->getContentSize().height / 5 * 4); layer->menuSprite->addChild(layer->contentText); auto musicSetting = Label::createWithTTF("音量设置", "fonts/msyhbd.ttc", 15); musicSetting->setPosition(layer->menuSprite->getContentSize().width / 3, layer->menuSprite->getContentSize().height / 5 * 3); layer->menuSprite->addChild(musicSetting); slider = ControlSlider::create("images/slider/slider1.png","images/slider/slider0.png","images/slider/slider2.png"); slider->setPosition(layer->menuSprite->getContentSize().width / 3*2, layer->menuSprite->getContentSize().height / 5 * 3); slider->setMinimumValue(0.0f); slider->setMaximumValue(100.0f); auto initValue = 50.0f; if (database->getBoolForKey("isExit")) initValue = database->getFloatForKey("music"); slider->setValue(initValue); slider->addTargetWithActionForControlEvents(this, cccontrol_selector(StartScene::musicSlider), Control::EventType::VALUE_CHANGED); layer->menuSprite->addChild(slider); auto resetLabel = Label::createWithTTF("重置游戏", "fonts/msyhbd.ttc", 15); MenuItemLabel* resetItem = MenuItemLabel::create(resetLabel, CC_CALLBACK_0(StartScene::resetGame,this)); resetItem->setPosition(0, 0); auto resetMenu = Menu::create(resetItem,NULL); resetMenu->setPosition(layer->menuSprite->getContentSize().width / 3, layer->menuSprite->getContentSize().height / 5 * 2); layer->menuSprite->addChild(resetMenu); auto backLabel = Label::createWithTTF("返回游戏", "fonts/msyhbd.ttc", 15); backLabel->setColor(Color3B::BLACK); auto backItem = MenuItemLabel::create(backLabel, CC_CALLBACK_0(pausePopup::backToGame, layer)); backItem->setPosition(backItem->getContentSize().width * 3 / 2, 0); layer->menu = Menu::create(backItem, NULL); layer->menu->setPosition(layer->menuSprite->getContentSize().width / 2, layer->menuSprite->getContentSize().height / 12); layer->menuSprite->addChild(layer->menu, 10); //auto popupScene = Popup::createScene(renderTexture,"images/popupBg.png"); //auto popup = Popup::create("images/popupBg.png"); //this->addChild(popup, 10); //Action* popupAction = Sequence::create(ScaleTo::create(0.0f, 0.0f), // ScaleTo::create(0.06f, 1.05f), // ScaleTo::create(0.08f, 0.95f), // ScaleTo::create(0.08f, 1.0f), NULL); //popup->runAction(popupAction); //Director::getInstance()->pause(); director->pushScene(scene); //Director::getInstance()->replaceScene(popupScene); }
bool MapEditer::init() { if (!Layer::init()) return false; m_layer = Layer::create(); this->addChild(m_layer, 1); Sprite* background = Sprite::create(FileStuff::BACKGROUND_BOTTOM); float scale = (Director::getInstance()->getVisibleSize().width) / (background->getContentSize().width); background->setAnchorPoint(Point::ZERO); background->setScale(scale); this->addChild(background); Sling* sling = Sling::create(); this->addChild(sling); EventListenerMouse* mouseListener = EventListenerMouse::create(); mouseListener->onMouseDown = CC_CALLBACK_1(MapEditer::onMouseDown, this); mouseListener->onMouseScroll = CC_CALLBACK_1(MapEditer::OnMouseScroll, this); m_clicked_sprite = nullptr; _eventDispatcher->addEventListenerWithSceneGraphPriority(mouseListener, this); EventListenerKeyboard* keyboardListener = EventListenerKeyboard::create(); keyboardListener->onKeyPressed = CC_CALLBACK_1(MapEditer::OnKeyPressed, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(keyboardListener, this); m_pressedKey = static_cast<EventKeyboard::KeyCode>(-1); MenuItemLabel* saveButton = MenuItemLabel::create(Label::create("Save", FileStuff::FONT_ARIAL, 20)); saveButton->setPosition(170, 10); saveButton->setName("saveButton"); this->addChild(saveButton); MenuItemLabel* playButton = MenuItemLabel::create(Label::create("Play", FileStuff::FONT_ARIAL, 20)); playButton->setPosition(220, 10); playButton->setName("playButton"); this->addChild(playButton); MenuItemLabel* backButton = MenuItemLabel::create(Label::create("Back", FileStuff::FONT_ARIAL, 20)); backButton->setPosition(270, 10); backButton->setName("backButton"); this->addChild(backButton); MenuItemLabel* warning = MenuItemLabel::create(Label::create("If complete making level\nCHANGE FILE NAME", FileStuff::FONT_ARIAL, 15)); warning->setPosition(200, 40); warning->setName("warning"); this->addChild(warning); vector<TargetInfo> infoList; string fileName = FileStuff::EDITING_FILENAME; //load from file if (!cppson::loadFile(infoList, fileName)) { CCLOG("Target Load Fail."); return true; } else { for (TargetInfo targetInfo : infoList) { Sprite* sprite = nullptr; Point point; switch (targetInfo.m_name.get()) { case TargetInfo::BUBBLE: sprite = Sprite::create(FileStuff::BUBBLE); break; case TargetInfo::ENEMY: sprite = Sprite::create(FileStuff::ENEMY); break; case TargetInfo::MIRROR: sprite = Sprite::create(FileStuff::MIRROR); break; case TargetInfo::VIRTICAL_MIRROR: sprite = Sprite::create(FileStuff::VIRTICAL_MIRROR); break; case TargetInfo::STAR: sprite = Sprite::create(FileStuff::BRICK); break; default: sprite = Sprite::create(FileStuff::ENEMY); //default sprite } sprite->setTag(targetInfo.m_name.get()); point = Point(targetInfo.m_position.get().x.get(), targetInfo.m_position.get().y.get()); sprite->setPosition(point); sprite->setRotation(targetInfo.m_rotation.get()); sprite->setScaleX(targetInfo.m_scale.get().x.get()); sprite->setScaleY(targetInfo.m_scale.get().y.get()); if (sprite) { m_layer->addChild(sprite); } } } return true; }