// on "init" you need to initialize your instance bool OptionsMenu::init() { Size visibleSize = CCDirector::sharedDirector()->getVisibleSize(); Vec2 origin = CCDirector::sharedDirector()->getVisibleOrigin(); scrollingBgLayer = new ScrollingBgLayer(3.0); this->addChild(scrollingBgLayer); LabelBMFont *nameLabel = LabelBMFont::create("Options Menu","PixelFont.fnt"); nameLabel->setPosition(visibleSize.width/2, visibleSize.height * 0.8); this->addChild(nameLabel); MenuItemImage *presetItem = MenuItemImage::create("_bookgame_UI__resume.png", "_bookgame_UI__resume.png", CC_CALLBACK_1(OptionsMenu::reset, this)); presetItem->setPosition(ccp(visibleSize.width/2 - visibleSize.width * 0.125, visibleSize.height * 0.5)); MenuItemImage *pmainMenuItem = MenuItemImage::create("_bookgame_UI_mainmenu.png", "_bookgame_UI_mainmenu.png", CC_CALLBACK_1(OptionsMenu::mainMenu, this)); pmainMenuItem->setPosition(Vec2(visibleSize.width/2 + visibleSize.width * 0.125, visibleSize.height * 0.5 )); //sound onoff items soundOnItem = MenuItemImage::create("_bookgame_UI_soundON.png","_bookgame_UI_soundON.png", this,NULL); soundOffItem = MenuItemImage::create("_bookgame_UI_soundOFF.png","_bookgame_UI_soundOFF.png", this,NULL); bool isPaused = CCUserDefault::sharedUserDefault()->getBoolForKey("tinyBazooka_kSoundPausedKey"); MenuItemToggle* soundToggleItem; if(isPaused == false) { soundToggleItem = MenuItemToggle::createWithTarget(this,menu_selector(OptionsMenu::SoundOnOff), soundOnItem, soundOffItem,NULL); } else { soundToggleItem = MenuItemToggle::createWithTarget(this,menu_selector(OptionsMenu::SoundOnOff), soundOffItem, soundOnItem,NULL); } soundToggleItem->setPosition(ccp(visibleSize.width* 0.5, visibleSize.height * 0.5 )); // create menu, it's an autorelease object Menu* pMenu = Menu::create(pmainMenuItem, presetItem,soundToggleItem, NULL); pMenu->setPosition(Vec2::ZERO); this->addChild(pMenu, 10); this->scheduleUpdate(); return true; }
bool UILayoutTest_Layout_Relative_Location_Editor::init() { if (UIScene_Editor::init()) { _layout = static_cast<Layout*>(cocostudio::GUIReader::getInstance()->widgetFromJsonFile("cocosui/UIEditorTest/UILayout/Relative_Align_Location/windows_ui_relative_align_location.json")); _touchGroup->addChild(_layout); this->configureGUIScene(); Menu* menu = static_cast<Menu*>(getChildByTag(1)); MenuItemToggle* menuItemToggle = static_cast<MenuItemToggle*>(menu->getChildByTag(1)); MenuItem* selectedItem = menuItemToggle->getSelectedItem(); menuItemToggle->setPosition(Vec2(VisibleRect::center().x, VisibleRect::center().y + selectedItem->getContentSize().height * 4.25f)); return true; } return false; }
void HelloWorld::okCallback(Ref* pSender) { if(!m_bg) { m_runLayer = Layer::create(); this->addChild(m_runLayer); int dirSel = m_dirSelItem->getSelectedIndex(); switch (dirSel) { case 0: m_bg = ScrollingBackground::create(ScrollDirection::SD_RIGHT_TO_LEFT); m_direction = ScrollDirection::SD_RIGHT_TO_LEFT; break; case 1: m_bg = ScrollingBackground::create(ScrollDirection::SD_LEFT_TO_RIGHT); m_direction = ScrollDirection::SD_LEFT_TO_RIGHT; break; case 2: m_bg = ScrollingBackground::create(ScrollDirection::SD_TOP_TO_BOTTOM); m_direction = ScrollDirection::SD_TOP_TO_BOTTOM; break; case 3: m_bg = ScrollingBackground::create(ScrollDirection::SD_BOTTOM_TO_TOP); m_direction = ScrollDirection::SD_BOTTOM_TO_TOP; break; default: break; } m_runLayer->addChild(m_bg,1); for (int i = 1; i < 5; i++) { addBgSprite(i); } /* //当有一个背景精灵彻底离开屏幕的时候的消息,两种方式 if(m_modeSelItem->getSelectedIndex()==0) m_bg->setBgChangeEventListener(this,bgSpriteChangeEvent(HelloWorld::loopBgChangeCallback)); //循环模式 else m_bg->setBgChangeEventListener(this,bgSpriteChangeEvent(HelloWorld::dropBgChangeCallback)); //丢弃模式 */ //当有一个背景精灵彻底离开屏幕的时候的消息,两种方式 if(m_modeSelItem->getSelectedIndex()==0) m_bg->setBgChangeEventListener(CC_CALLBACK_3(HelloWorld::loopBgChangeCallback,this)); //循环模式 else m_bg->setBgChangeEventListener(CC_CALLBACK_3(HelloWorld::dropBgChangeCallback,this)); //丢弃模式 /* //当背景精灵移动时候的消息,两种方式 m_bg->setBgMoveEventListener(this,bgSpriteMoveEvent(HelloWorld::bgMoveCallback)); */ //当背景精灵移动时候的消息,两种方式 m_bg->setBgMoveEventListener(CC_CALLBACK_2(HelloWorld::bgMoveCallback,this)); m_velocity = 300.0f; m_bgSprite = m_bg->getCurBgSprite(); m_bg->beginContinuousScroll(m_velocity); //一个跟随背景移动的物体 m_pItem = Sprite::create("item.png"); m_pItem->setAnchorPoint(Vec2(0.5f,0.5f)); m_pItem->setPosition(Vec2(0.0f,0.0f)); m_runLayer->addChild(m_pItem,2); //pItem锚点一直都是(0.5,0.5),坐标一直是(0.0,0.0),注意看当画面切换的时候他会出现在哪里 //背景精灵精灵的锚点不同,会影响pItem的位置 m_pItem->setPosition(m_bg->getCurBgSprite()->getPosition()); /*下面的是一些菜单的设置,不看也可以*/ Size visibleSize = Director::getInstance()->getVisibleSize(); //返回按钮 MenuItemFont* back = MenuItemFont::create("back",CC_CALLBACK_1(HelloWorld::backCallback,this)); back->setAnchorPoint(Vec2(1.0f,1.0f)); back->setPosition(Vec2(visibleSize.width/2.0f,visibleSize.height/2.0f)); //设置相反速度的按钮 MenuItemFont* reverse = MenuItemFont::create("reverse",CC_CALLBACK_1(HelloWorld::reverseCallback,this)); //暂停 MenuItemToggle* pause = MenuItemToggle::createWithCallback(CC_CALLBACK_1(HelloWorld::pauseCallback,this), MenuItemFont::create("pause"), MenuItemFont::create("resume"), NULL); pause->setPosition(Vec2(0.0f,50.0f)); //上一个画面 MenuItemImage* lastBg = MenuItemImage::create("lastBgNor.png","lastBgCli.png","",CC_CALLBACK_1(HelloWorld::nextBgCallback,this)); lastBg->setTag(0); lastBg->setAnchorPoint(Vec2(0.0f,0.5f)); lastBg->setPosition(Vec2(-visibleSize.width/2.0f,0.0f)); //下一个画面 MenuItemImage* nextBg = MenuItemImage::create("NextBgNor.png","NextBgCli.png","",CC_CALLBACK_1(HelloWorld::nextBgCallback,this)); nextBg->setTag(1); nextBg->setAnchorPoint(Vec2(1.0f,0.5f)); nextBg->setPosition(Vec2(visibleSize.width/2.0f,0.0f)); //增加背景精灵 MenuItemFont* add = MenuItemFont::create("add background",CC_CALLBACK_1(HelloWorld::addBgSpriteCallback,this)); add->setPosition(Vec2(0.0f,-50.0f)); Menu* pMenu = Menu::create(pause,reverse,add,lastBg,nextBg,back,NULL); pMenu->setPosition(Vec2(visibleSize.width/2.0f,visibleSize.height/2.0f)); m_runLayer->addChild(pMenu,3); //文字提示: Color3B color(255,0,0); m_itemTipsLabel = Label::createWithTTF("","fonts/arial.ttf",17.0f); m_itemTipsLabel->setColor(color); m_itemTipsLabel->setAnchorPoint(Vec2(0.5f,0.5f)); m_itemTipsLabel->setPosition(Vec2(visibleSize.width/2.0f,visibleSize.height/6.0f)); m_runLayer->addChild(m_itemTipsLabel,3); m_numBgLabel = Label::createWithTTF("","fonts/arial.ttf",17.0f); m_numBgLabel->setColor(color); m_numBgLabel->setAnchorPoint(Vec2(0.5f,0.5f)); m_numBgLabel->setPosition(Vec2(visibleSize.width/2.0f,visibleSize.height/10.0f)); m_runLayer->addChild(m_numBgLabel,3); m_addTipsLabel = Label::createWithTTF("","fonts/arial.ttf",17.0f); m_addTipsLabel->setColor(color); m_addTipsLabel->setAnchorPoint(Vec2(0.5f,0.5f)); m_addTipsLabel->setPosition(Vec2(visibleSize.width/2.0f,visibleSize.height/10.0f*9.0f)); m_addTipsLabel->setString(""); m_runLayer->addChild(m_addTipsLabel,3); setTextTips(m_bg->getCurBgSprite()); this->removeChild(m_setLayer); m_setLayer = 0; } }
bool Developer::init() { if( !Layer::init() ){ return false; } UserDefault *defaults = UserDefault::getInstance(); // 添加背景图片 auto sprite = Sprite::create("scene_abo.png"); sprite->setPosition(Point(GAME_SCREEN_WIDTH/2,GAME_SCREEN_HEIGHT/2)); sprite->setScaleX(GAME_SCREEN_WIDTH / sprite->getContentSize().width); sprite->setScaleY(GAME_SCREEN_HEIGHT / sprite->getContentSize().height); this->addChild(sprite); // 添加返回主菜单按钮 auto backItem = MenuItemImage::create( "btn_ret.png", "btn_ret.png", CC_CALLBACK_1(Developer::menuBackCallback, this)); backItem->setPosition(Vec2(backItem->getContentSize().width/2,GAME_SCREEN_HEIGHT-backItem->getContentSize().height/2)); MenuItemImage *_turnOn,*_turnOff; _turnOn = MenuItemImage::create( "btn_musOn.png", "btn_musOn.png"); _turnOff = MenuItemImage::create( "btn_musOff.png", "btn_musOff.png"); MenuItemImage *_trnOn,*_trnOff; _trnOn = MenuItemImage::create( "btn_musOn.png", "btn_musOn.png"); _trnOff = MenuItemImage::create( "btn_musOff.png", "btn_musOff.png"); // 音乐开关 MenuItemToggle *musicItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(Developer::menuMusicCallback, this), _turnOn,_turnOff, NULL); musicItem->setPosition(Point(GAME_SCREEN_WIDTH/1.4,GAME_SCREEN_HEIGHT/1.8)); // 音效开关 MenuItemToggle *soundItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(Developer::menuSoundCallback, this), _trnOn,_trnOff, NULL); soundItem->setPosition(Point(GAME_SCREEN_WIDTH/1.4,GAME_SCREEN_HEIGHT/1.4)); auto menu = Menu::create(backItem,musicItem,soundItem, NULL); menu->setPosition(Vec2::ZERO); this -> addChild( menu ); TTFConfig config("Marker Felt.ttf",28); // 关于 auto labelAbout = Label::createWithTTF(config, "追梦赤子心"); labelAbout -> setPosition(Point( GAME_SCREEN_WIDTH/2 , GAME_SCREEN_HEIGHT/1.2)); labelAbout->setColor(Color3B(35,35,35)); this->addChild(labelAbout); if (defaults->getBoolForKey(MUSIC_KEY)) { musicItem->setSelectedIndex(0); } else { musicItem->setSelectedIndex(1); } if (defaults->getBoolForKey(SOUND_KEY)) { soundItem->setSelectedIndex(0); } else { soundItem->setSelectedIndex(1); } return true; }