Example #1
0
void SettingsDialog::onPressMusic(cocos2d::Ref *sender, cocos2d::extension::Control::EventType pControlEvent)
{
	if (pControlEvent == Control::EventType::TOUCH_UP_INSIDE)
	{
        UserDefault *ud = UserDefault::getInstance();
        bool value = ud->getBoolForKey("music_enabled", true);
        ud->setBoolForKey("music_enabled", !value);
        
        if (value)
        {
            _checkBoxMusic->removeAllChildren();
            CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic(false);
        }
        else
        {
            Label *l = Label::create();
            l->setString("X");
            l->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
            
            _checkBoxMusic->addChild(l);

            CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("playing.mp3", true);
        }
        
        ud->flush();
	}
}
Example #2
0
void BBHandleLayer::initData()
{
    m_visibleSize = Director::getInstance()->getVisibleSize();
    
    m_blockLength = kBlockLength * BBGameDataManager::getInstance()->getScreenScale();
    
    m_row = 5;
    m_col = 4;
    
    m_countDown = 2;
    
    m_blockArr = Array::create();
    m_blockArr->retain();
    
    m_isReady = false;
    m_showTips = false;
    m_moves = 0;
    m_over = false;
    
    m_mixTimes = 100;
    m_blockAnimOver = true;
    m_idleBlockLastDirection = 0;
    m_curIndex = m_row * m_col + 1;
    
    UserDefault *ud = UserDefault::getInstance();
    if (ud->getBoolForKey("hasLocalData")) {
        m_moves = ud->getIntegerForKey("moves");
    }
}
void PlaySceneOne::onEnterTransitionDidFinish()
{
	Layer::onEnterTransitionDidFinish();
	log("GamePlayLayer onEnterTransitionDidFinish");
	UserDefault *defaults = UserDefault::getInstance();
	if (defaults->getBoolForKey(MUSIC_KEY)) {
		SimpleAudioEngine::getInstance()->playBackgroundMusic(bg_music_2, true);
	}
}
Example #4
0
void SettingLayer::menuSoundToggleCallback(Ref* pSender)
{
	UserDefault *defaults  = UserDefault::getInstance();
	if (defaults->getBoolForKey(SOUND_KEY)) {
		defaults->setBoolForKey(SOUND_KEY, false);
	} else {
		defaults->setBoolForKey(SOUND_KEY, true);
		SimpleAudioEngine::getInstance()->playEffect(sound_1);
	}
}
Example #5
0
void SettingLayer::menuMusicToggleCallback(Ref* pSender)
{
	UserDefault *defaults  = UserDefault::getInstance();
	if (defaults->getBoolForKey(MUSIC_KEY)) {
		defaults->setBoolForKey(MUSIC_KEY, false);
		SimpleAudioEngine::getInstance()->stopBackgroundMusic();
	} else {
		defaults->setBoolForKey(MUSIC_KEY, true);
		SimpleAudioEngine::getInstance()->playBackgroundMusic(bg_music_2, true);
	}
}
Example #6
0
void Developer::menuSoundCallback( Ref *pSender )
{
	auto soundToggleMenuItem = (MenuItemToggle*)pSender;   
  
    UserDefault *defaults  = UserDefault::getInstance();  
	if (defaults->getBoolForKey(SOUND_KEY)) {                                      
        defaults->setBoolForKey(SOUND_KEY, false);  
    } 
	else {  
        defaults->setBoolForKey(SOUND_KEY, true);   
    }
}
Example #7
0
void Developer::menuMusicCallback( Ref *pSender )
{
	auto musicToggleMenuItem = (MenuItemToggle*)pSender;   
  
    UserDefault *defaults  = UserDefault::getInstance();  
    if (defaults->getBoolForKey(MUSIC_KEY)) {                                      
        defaults->setBoolForKey(MUSIC_KEY, false);  
        SimpleAudioEngine::getInstance()->stopBackgroundMusic();  
    } 
	else {  
        defaults->setBoolForKey(MUSIC_KEY, true);  
        SimpleAudioEngine::getInstance()->playBackgroundMusic("music_mainScene.wav");  
    }                                          
}
Example #8
0
bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) {
        glview = GLView::create("My Game");
        director->setOpenGLView(glview);
    }
    
    auto frameSize = glview->getFrameSize();
    std::vector<std::string> searchPaths;
    if ((int)frameSize.width == 1536) {
        log("hdr");
        searchPaths.push_back("hdr");
        BBGameDataManager::getInstance()->setScreenScale(2);
    }else{
        log("hd");
        searchPaths.push_back("hd");
    }
    
    FileUtils::getInstance()->setSearchPaths(searchPaths);
    // turn on display FPS
    director->setDisplayStats(false);

    // set FPS. the default value is 1.0/60 if you don't call this
    director->setAnimationInterval(1.0 / 60);
    
    GameCenterApi::getInstance()->authenticateLocalUser();
    
    // Umeng
    MobClickCpp::startWithAppkey("5347fa3f56240b0b0f001049");
//    MobClickCpp::setLogEnabled(true);
    
    // create a scene. it's an autorelease object
    UserDefault* ud = UserDefault::getInstance();
    if (ud->getBoolForKey("notNewUser")) {
        director->runWithScene(BBGameScene::createScene());
    } else {
        director->runWithScene(BBWelcomeScene::createScene());
    }

    return true;
}
void UserDefaultManager::setDefault()
{
    
    UserDefault *userdefault = UserDefault::getInstance();
    if (!userdefault->getBoolForKey("isSetUp")) {
        userdefault->setBoolForKey("isSetUp", true);
        setIntegerForKey(DiamondCount, 10);
        setIntegerForKey(QuestionCount, 0);
        setIntegerForKey(StarCount, 0);
        setIntegerForKey(RecieveDiamond, 0);
        setIntegerForKey(StarCount, 0);
        setBoolForKey(kIsRemoveAds, false);
        setIntegerForKey(kTotalNum, 0);
        setBoolForKey(kFirstTime, true);
        time_t t = time(0);
        tm * now = localtime(&t);
        __String* timeString = __String::createWithFormat("%d年%d月%d日", now->tm_year, now->tm_mon, now->tm_mday-1);
        log("%d年%d月%d日", now->tm_year, now->tm_mon, now->tm_mday);
        setStringForKey(kLoginTime, timeString->getCString());
        setIntegerForKey(kLastCount, 10);
        setStringForKey(kRecordeTime1, timeString->getCString());
        setIntegerForKey(kViewState, 0);
    }
}
Example #10
0
void SettingsDialog::onPressSound(cocos2d::Ref *sender, cocos2d::extension::Control::EventType pControlEvent)
{
	if (pControlEvent == Control::EventType::TOUCH_UP_INSIDE)
    {
        UserDefault *ud = UserDefault::getInstance();
        bool value = ud->getBoolForKey("sound_enabled", true);
        ud->setBoolForKey("sound_enabled", !value);
        
        if (value)
        {
            _checkBoxSound->removeAllChildren();
        }
        else
        {
            Label *l = Label::create();
            l->setString("X");
            l->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
            
            _checkBoxSound->addChild(l);
        }
        
        ud->flush();
	}
}
bool Controller::isVibrateEnabled()
{
    UserDefault * def = UserDefault::getInstance();
    return def->getBoolForKey(SETTINGS_VIBRATE, true);
}
Example #12
0
void Bestiary::showDesc(cocos2d::Ref *pSender, int monsterNum)
{
	m_descNode = Node::create();

	m_bestiaryScroll->setVisible(false);
	//m_border->setVisible(false);
	m_isOnDesc = true;

	string name;
	string description;
	auto fileU = FileUtils::getInstance();
	auto fileContent = fileU->getStringFromFile("data/animData.xml");
	doc.Parse(fileContent.c_str());
	fileContent = fileU->getStringFromFile("data/monsterData.xml");
	mData.Parse(fileContent.c_str());
	UserDefault* def = UserDefault::getInstance();

	auto monsterName = BestiaryNameData[monsterNum];

	//------------------------------------------------------------------------------------
	//Monster BG effect
	//------------------------------------------------------------------------------------
	if (def->getBoolForKey(SaveDataKey[n_DataKeyNum::eKnownData01 + monsterNum].c_str()))
	{
		string cardType = mData.FirstChildElement()->FirstChildElement(monsterName.c_str())->FirstChildElement("special")->FirstChildElement("type")->GetText();
		if (cardType == "0")
		{
			m_descBG = Sprite::create("img/effect/effects/red/background_red_00000.png");
			//bg->setColor(Color3B(150, 150, 150));
			Animation* anim = Animation::create();

			anim->addSpriteFrameWithFileName("img/effect/effects/red/background_red_00001.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/red/background_red_00002.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/red/background_red_00003.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/red/background_red_00004.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/red/background_red_00005.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/red/background_red_00006.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/red/background_red_00007.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/red/background_red_00008.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/red/background_red_00009.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/red/background_red_00010.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/red/background_red_00011.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/red/background_red_00012.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/red/background_red_00013.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/red/background_red_00014.png");

			anim->setLoops(-1);
			anim->setDelayPerUnit(0.1f);
			//sprite->setColor(Color3B(100,100,100));
			m_descBG->runAction(Animate::create(anim));
			m_descBG->setPosition(Vec2(m_visibleSize.width / 2, m_visibleSize.height / 1.6));
			m_descBG->setScale(m_mag * 1.4);

			m_descNode->addChild(m_descBG);
		}
		else if (cardType == "1")
		{
			m_descBG = Sprite::create("img/effect/effects/yellow/background_Yellow_00000.png");
			//bg->setColor(Color3B(150, 150, 150));
			Animation* anim = Animation::create();

			anim->addSpriteFrameWithFileName("img/effect/effects/yellow/background_Yellow_00001.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/yellow/background_Yellow_00002.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/yellow/background_Yellow_00003.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/yellow/background_Yellow_00004.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/yellow/background_Yellow_00005.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/yellow/background_Yellow_00006.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/yellow/background_Yellow_00007.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/yellow/background_Yellow_00008.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/yellow/background_Yellow_00009.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/yellow/background_Yellow_00010.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/yellow/background_Yellow_00011.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/yellow/background_Yellow_00012.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/yellow/background_Yellow_00013.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/yellow/background_Yellow_00014.png");

			anim->setLoops(-1);
			anim->setDelayPerUnit(0.1f);
			//sprite->setColor(Color3B(100,100,100));
			m_descBG->runAction(Animate::create(anim));
			m_descBG->setPosition(Vec2(m_visibleSize.width / 2, m_visibleSize.height / 1.6));
			m_descBG->setScale(m_mag * 1.4);

			m_descNode->addChild(m_descBG);
		}
		else if (cardType == "2")
		{
			m_descBG = Sprite::create("img/effect/effects/blue/background_blue_00000.png");
			//bg->setColor(Color3B(150, 150, 150));
			Animation* anim = Animation::create();

			anim->addSpriteFrameWithFileName("img/effect/effects/blue/background_blue_00001.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/blue/background_blue_00002.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/blue/background_blue_00003.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/blue/background_blue_00004.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/blue/background_blue_00005.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/blue/background_blue_00006.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/blue/background_blue_00007.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/blue/background_blue_00008.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/blue/background_blue_00009.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/blue/background_blue_00010.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/blue/background_blue_00011.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/blue/background_blue_00012.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/blue/background_blue_00013.png");
			anim->addSpriteFrameWithFileName("img/effect/effects/blue/background_blue_00014.png");

			anim->setLoops(-1);
			anim->setDelayPerUnit(0.1f);
			//sprite->setColor(Color3B(100,100,100));
			m_descBG->runAction(Animate::create(anim));
			m_descBG->setPosition(Vec2(m_visibleSize.width / 2, m_visibleSize.height / 1.6));
			m_descBG->setScale(m_mag * 1.4);

			m_descNode->addChild(m_descBG);
		}
		
	}
	
	auto basePath = doc.FirstChildElement()->FirstChildElement(monsterName.c_str())->FirstChildElement("spriteList")->FirstChildElement("animation")->FirstChildElement("stand")->GetText();


	if (!def->getBoolForKey(SaveDataKey[n_DataKeyNum::eKnownData01 + monsterNum].c_str()))
	{
		auto monsterSprite = Sprite::create(basePath);
		monsterSprite->setScale(m_mag* 0.8);
		monsterSprite->setPosition(Vec2(m_visibleSize.width / 2, m_visibleSize.height / 1.45f));
		monsterSprite->setColor(Color3B::BLACK);
		name = "????";
		description = mData.FirstChildElement()->FirstChildElement(monsterName.c_str())->FirstChildElement("sDesc")->GetText();
		m_descNode->addChild(monsterSprite);
	}
	else
	{
		int key = monsterNum;

		float scale;
		float posX;
		float posY;

		switch (key)
		{
		case 4:
			scale = 1.0f;
			posX = 0.5f;
			posY = 1.25f;
			break;
		case 14:
			scale = 0.6f;
			posX = 0.3f;
			posY = 0.9f;
			break;
		case 16:
			scale = 0.55f;
			posX = 0.3f;
			posY = 0.85f;
			break;
		case 20:
			scale = 0.6f;
			posX = 0.3f;
			posY = 0.82f;
			break;
		default:
			scale = 0.6f;
			posX = 0.3f;
			posY = 0.9f;
			break;
		}

		auto pixie = Live2DSprite::createDrawNode(MonsterNameData[key], scale, scale, posX, posY);
		m_descNode->addChild(pixie);
		name = mData.FirstChildElement()->FirstChildElement(monsterName.c_str())->FirstChildElement("name")->GetText();
		description = mData.FirstChildElement()->FirstChildElement(monsterName.c_str())->FirstChildElement("lDesc")->GetText();
	}

	auto descRect = DrawNode::create();
	descRect->drawSolidRect(Vec2(m_visibleSize.width / 10.f, (m_visibleSize.height * 1) / 10.0f), Vec2((m_visibleSize.width * 9) / 10.f, (m_visibleSize.height) / 2.4f), Color4F(0, 0, 0, 90));
	descRect->setAnchorPoint(Vec2(0.5f, 0.5f));
	m_descNode->addChild(descRect);

	float textPosY = ( ((m_visibleSize.height) / 2.4f) - ((m_visibleSize.height * 1) / 10.0f * 2.5) );

	auto nameLabel = Label::createWithTTF(name, "fonts/ant-maru.ttf", 55);
	nameLabel->setPosition(Vec2(m_visibleSize.width * 2 / 10.f, m_visibleSize.height / 2.2));
	nameLabel->enableOutline(Color4B::BLACK, 2);
	m_descNode->addChild(nameLabel);


	auto descText = RichText::create();
	descText->ignoreContentAdaptWithSize(false);
	descText->setContentSize(Size(Vec2(800, 300)));
	descText->pushBackElement(RichElementText::create(0, Color3B::WHITE, 255, description, "fonts/ant-maru.ttf", 35));
	descText->setAnchorPoint(Vec2(0.5f, 0.5f));
	descText->setPosition(Vec2(m_visibleSize.width / 2, textPosY));
	
	m_descNode->addChild(descText);

	this->addChild(m_descNode);
}
Example #13
0
void Bestiary::createBestiary()
{
	//border
	m_border = DrawNode::create();
	m_border->drawSolidRect(Vec2(m_visibleSize.width / 10, m_visibleSize.height * 9.1 / 10), Vec2(m_visibleSize.width * 9 / 10, m_visibleSize.height * 0.9  / 10), Color4F::BLACK);
	//this->addChild(m_border);

	//scroll creation
	int numberOfRows = ceil(MAX_NUM_MONSTERS / 4);
	m_bestiaryScroll = ScrollView::create();
	m_bestiaryScroll->setContentSize(Size(m_visibleSize.width / 1.3f, m_visibleSize.height / 1.3));
	float squareDimention = m_bestiaryScroll->getContentSize().width / 4;
	m_bestiaryScroll->setInnerContainerSize(Size(m_visibleSize.width / 1.3f, squareDimention * 8));
	m_bestiaryScroll->setAnchorPoint(Vec2(0.5f, 0.5f));
	m_bestiaryScroll->setPosition(Vec2(m_visibleSize.width / 2, m_visibleSize.height / 2));
	m_bestiaryScroll->setDirection(ScrollView::Direction::VERTICAL);
	m_bestiaryScroll->setBackGroundColorType(LayoutBackGroundColorType::SOLID);
	m_bestiaryScroll->setBackGroundColor(Color3B(80, 140, 230));
	m_bestiaryScroll->setBackGroundColorOpacity(90);
	m_bestiaryScroll->setBounceEnabled(true);
	this->addChild(m_bestiaryScroll);

	//monster selection
	auto fileU = FileUtils::getInstance();
	auto fileContent = fileU->getStringFromFile("data/animData.xml");
	doc.Parse(fileContent.c_str());
	UserDefault* def = UserDefault::getInstance();

	int monsterNum = 0;

	for (int j = 0; j < 8; j++)
	{
		for (int i = 0; i < 4; i++)
		{
			if (monsterNum < MAX_NUM_MONSTERS)
			{
				auto monsterName = BestiaryNameData[monsterNum];
				auto basePath = doc.FirstChildElement()->FirstChildElement(monsterName.c_str())->FirstChildElement("spriteList")->FirstChildElement("animation")->FirstChildElement("stand")->GetText();
				auto button = Button::create(basePath);
				button->addClickEventListener(CC_CALLBACK_1(Bestiary::showDesc, this, monsterNum));
				button->setScale(m_mag*0.45);
				button->setAnchorPoint(Vec2(0, 1));
				button->setPosition(Vec2(i * squareDimention, m_bestiaryScroll->getInnerContainerSize().height - j * squareDimention));
				if (!def->getBoolForKey(SaveDataKey[n_DataKeyNum::eKnownData01 + monsterNum].c_str()))
				{
						button->setColor(Color3B::BLACK);
				}
				string text = Generic::intToStrnig(monsterNum + 1);
				if (monsterNum >= 10)
					text = "No.0" + text;
				else
					text = "No.00" + text;
				auto numberText = Label::createWithTTF(text, "fonts/ant-maru.ttf", 35);
				numberText->enableOutline(Color4B::BLACK, 1);
				numberText->setPosition(Vec2((i * squareDimention) + (squareDimention / 2), m_bestiaryScroll->getInnerContainerSize().height - (j * squareDimention) - squareDimention*0.85f));
				
				m_bestiaryScroll->addChild(button);
				m_bestiaryScroll->addChild(numberText);
			}

			monsterNum++;
		}
	}
}
Example #14
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;
}
Example #15
0
// on "init" you need to initialize your instance
bool SettingLayer::init()
{
	//////////////////////////////
	// 1. super init first
	if ( !BaseLayer::init() )
	{
		return false;
	}
	Size visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();

	auto top = Sprite::createWithSpriteFrameName("setting-top.png");
	top->setPosition(Vec2(visibleSize.width/2, visibleSize.height - top->getContentSize().height /2));
	addChild(top);

	
	//音效.
	auto soundOnSprite  = Sprite::createWithSpriteFrameName("setting.check-on.png");
	auto soundOffSprite  = Sprite::createWithSpriteFrameName("setting.check-off.png");
	auto soundOnMenuItem = MenuItemSprite::create(soundOnSprite, NULL);
	auto soundOffMenuItem = MenuItemSprite::create(soundOffSprite, NULL);
	auto soundToggleMenuItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(SettingLayer::menuSoundToggleCallback, this), soundOnMenuItem,soundOffMenuItem, NULL);

	//音乐.
	auto musicOnSprite  = Sprite::createWithSpriteFrameName("setting.check-on.png");
	auto musicOffSprite  = Sprite::createWithSpriteFrameName("setting.check-off.png");
	auto musicOnMenuItem = MenuItemSprite::create(musicOnSprite, NULL);
	auto musicOffMenuItem = MenuItemSprite::create(musicOffSprite, NULL);
	auto musicToggleMenuItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(SettingLayer::menuMusicToggleCallback, this), musicOnMenuItem,musicOffMenuItem, NULL);

	auto  menu = Menu::create(soundToggleMenuItem, musicToggleMenuItem, NULL);

	menu->setPosition(Vec2(visibleSize.width / 2, visibleSize.height /2) + Vec2(70, 50));
	menu->alignItemsVerticallyWithPadding(20.0f);
	addChild(menu, 1);

	auto lblSound = Label::createWithTTF(MyUtility::getUTF8Char("lblSound"), "fonts/hanyi.ttf", 36);
	lblSound->setColor(Color3B(14,83,204));	
	lblSound->setPosition(menu->getPosition() - Vec2(100, -30));
	addChild(lblSound, 1);

	auto lblMusic = Label::createWithTTF(MyUtility::getUTF8Char("lblMusic"), "fonts/hanyi.ttf", 36);
	lblMusic->setColor(Color3B(14,83,204));	
	lblMusic->setPosition(lblSound->getPosition() - Vec2(0, 60));
	addChild(lblMusic, 1);

	//设置音效和音乐选中状态
	UserDefault *defaults  = UserDefault::getInstance();
	if (defaults->getBoolForKey(MUSIC_KEY)) {
		musicToggleMenuItem->setSelectedIndex(0);
	} else {
		musicToggleMenuItem->setSelectedIndex(1);		
	}
	if (defaults->getBoolForKey(SOUND_KEY)) {
		soundToggleMenuItem->setSelectedIndex(0);
	} else {
		soundToggleMenuItem->setSelectedIndex(1);
	}

	return true;
}
Example #16
0
void MyContactListener::BeginContact(b2Contact* contact)
{
    Size visibleSize = Director::getInstance()->getVisibleSize();
    
    UserDefault *def = UserDefault::getInstance();
    int visibleWidth = visibleSize.width;
    int visibleHeight = visibleSize. height;
    int halfVisibleWidth = visibleSize.width / 2;
    int halfVisibleHeight = visibleSize.height / 2;
    float positionX, positionY;
    long whichBird;
    
    b2Body* bird;
    b2Body* border;
    b2Fixture* a = contact->GetFixtureA();
    b2Fixture* b = contact->GetFixtureB();
    
    //make sure only one of the fixtures was a sensor
    bool sensorA = a->IsSensor();
    bool sensorB = b->IsSensor();
    if ( ! (sensorA && sensorB) )
    {
        if(a->IsSensor()){
            bird = a->GetBody();
            border = b->GetBody();
            whichBird = (long)a->GetUserData();
        } else {
            border = a->GetBody();
            bird = b->GetBody();
            whichBird = (long)b->GetUserData();
        }
        Sprite* birdSprite = (Sprite*)bird->GetUserData();
        float birdX = birdSprite->getPositionX();
        float birdY = birdSprite->getPositionY();
        auto birdTag = birdSprite->getTag();
        long borderCheck = (long)border->GetUserData();
        
        auto blue1 = def->getIntegerForKey("blue1");
        auto blue2 = def->getIntegerForKey("blue2");
        auto blue3 = def->getIntegerForKey("blue3");
        auto velocity = def->getFloatForKey("velocity", 0);
        
        auto rotation = rand() % 46 + 1;
        
        bool top = false;
        
        if(borderCheck == 1 || borderCheck == 3)  // bottom and right
        {
            if(birdY > visibleHeight / 2){
                top = true;
                rotation = rand() % 26 + 1;
            }
        } else {
            rotation = rotation + 45;
            if(birdX > visibleWidth / 2)
                top = true;
        }
        
        auto angle = rotation * M_PI / 180;
        auto xPos = cos(angle) * (visibleWidth * 1.5);
        auto yPos = sin(angle) * (visibleWidth * 1.5);
        
        // Add missed bird and stop redirection if 3 collisions. GameOver if 3 misses in total
        if(((birdTag == 1 || birdTag == 4) && (borderCheck == 1 || borderCheck == 3)) || ((birdTag == 2 || birdTag == 8) && (borderCheck == 2 || borderCheck == 3)) || ((birdTag == 3 || birdTag == 5) && (borderCheck == 1 || borderCheck == 4)) || ((birdTag == 6 || birdTag == 7) && (borderCheck == 2 || borderCheck == 4)))
        {
            if(whichBird == 1)
            {
                auto round1_misses = def->getBoolForKey("bird1_missed", 0);
                int bird1_collisions = def->getIntegerForKey("bird1_collisions", 0);
                bird1_collisions++;
                def->setIntegerForKey("bird1_collisions", bird1_collisions);
                if(bird1_collisions >= 3 && !round1_misses) {
                    auto misses = def->getIntegerForKey("misses", 0);
                    misses++;
                    def->setIntegerForKey("misses", misses);
                    auto bird_count = def->getIntegerForKey("bird_count", 0);
                    bird_count++;
                    def->setIntegerForKey("bird_count", bird_count);
                    def->setBoolForKey("bird1_missed", 1);
                    Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(birdSprite);
                    return;
                } else {
                    if(bird1_collisions >= 3) {
                        return;
                    }
                }
            }
            if(whichBird == 2)
            {
                auto round2_misses = def->getBoolForKey("bird2_missed", 0);
                int bird2_collisions = def->getIntegerForKey("bird2_collisions", 0);
                bird2_collisions++;
                def->setIntegerForKey("bird2_collisions", bird2_collisions);
                if(bird2_collisions >= 3  && !round2_misses) {
                    auto misses = def->getIntegerForKey("misses", 0);
                    misses++;
                    def->setIntegerForKey("misses", misses);
                    auto bird_count = def->getIntegerForKey("bird_count", 0);
                    bird_count++;
                    def->setIntegerForKey("bird_count", bird_count);
                    def->setBoolForKey("bird2_missed", 1);
                    Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(birdSprite);
                    return;
                } else {
                    if(bird2_collisions >= 3) {
                        return;
                    }
                }
            }
            if(whichBird == 3)
            {
                auto round3_misses = def->getBoolForKey("bird3_missed", 0);
                int bird3_collisions = def->getIntegerForKey("bird3_collisions", 0);
                bird3_collisions++;
                def->setIntegerForKey("bird3_collisions", bird3_collisions);
                if(bird3_collisions >= 3 && !round3_misses) {
                    auto misses = def->getIntegerForKey("misses", 0);
                    misses++;
                    def->setIntegerForKey("misses", misses);
                    auto bird_count = def->getIntegerForKey("bird_count", 0);
                    bird_count++;
                    def->setIntegerForKey("bird_count", bird_count);
                    def->setBoolForKey("bird3_missed", 1);
                    Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(birdSprite);
                    return;
                } else {
                    if(bird3_collisions >= 3) {
                        return;
                    }
                }
            }
        }
        
        // Redirect Bird
        if((birdTag == 1 || birdTag == 4) && (borderCheck == 1 || borderCheck == 3))  // LeftTop || TopLeft -> bottom and right
        {
            if(borderCheck == 1)  // Bottom
            {
                if( birdX > halfVisibleWidth )
                {
                    if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) {
                        birdSprite->setTexture("bird_left.png");
                    } else {
                        birdSprite->setTexture("blue_left.png");
                    }
                    birdSprite->setTag(7);
                    birdSprite->setRotation(rotation);
                    positionX = -(birdX + xPos);
                    positionY = birdY + yPos;
                } else {
                    if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) {
                        birdSprite->setTexture("bird_right.png");
                    } else {
                        birdSprite->setTexture("blue_right.png");
                    }
                    birdSprite->setTag(8);
                    birdSprite->setRotation(-rotation);
                    positionX = birdX + xPos;
                    positionY = birdY + yPos;
                }
            } else {  // Right
                if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) {
                    birdSprite->setTexture("bird_left.png");
                } else {
                    birdSprite->setTexture("blue_left.png");
                }
                if( birdY > halfVisibleHeight )
                {
                    birdSprite->setTag(5);
                    birdSprite->setRotation(-rotation);
                    positionX = -(birdX + xPos);
                    positionY = -(birdY + yPos);
                } else {
                    birdSprite->setTag(6);
                    birdSprite->setRotation(rotation);
                    positionX = -(birdX + xPos);
                    positionY = birdY + yPos;
                }
            }
        }
        if((birdTag == 2 || birdTag == 8) && (borderCheck == 2 || borderCheck == 3))  // LeftBottom || BottomLeft -> top and right
        {
            if(borderCheck == 2)  // Top
            {
                if( birdX > halfVisibleWidth )
                {
                    if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) {
                        birdSprite->setTexture("bird_left.png");
                    } else {
                        birdSprite->setTexture("blue_left.png");
                    }

                    birdSprite->setTag(3);
                    birdSprite->setRotation(-rotation);
                    positionX = -(birdX + xPos);
                    positionY = -(birdY + yPos);
                } else {
                    if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) {
                        birdSprite->setTexture("bird_right.png");
                    } else {
                        birdSprite->setTexture("blue_right.png");
                    }
                    birdSprite->setTag(4);
                    birdSprite->setRotation(rotation);
                    positionX = birdX + xPos;
                    positionY = -(birdY + yPos);
                }
            } else {  // Right
                if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) {
                    birdSprite->setTexture("bird_left.png");
                } else {
                    birdSprite->setTexture("blue_left.png");
                }

                if( birdY > halfVisibleHeight )
                {
                    birdSprite->setTag(5);
                    birdSprite->setRotation(-rotation);
                    positionX = -(birdX + xPos);
                    positionY = -(birdY + yPos);
                } else {
                    birdSprite->setTag(6);
                    birdSprite->setRotation(rotation);
                    positionX = -(birdX + xPos);
                    positionY = birdY + yPos;
                }
            }
        }
        if((birdTag == 3 || birdTag == 5) && (borderCheck == 1 || borderCheck == 4))  // TopRight || RightTop -> bottom and left
        {
            if(borderCheck == 1)  // Bottom
            {
                if( birdX > halfVisibleWidth )
                {
                    if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) {
                        birdSprite->setTexture("bird_left.png");
                    } else {
                        birdSprite->setTexture("blue_left.png");
                    }

                    birdSprite->setTag(7);
                    birdSprite->setRotation(rotation);
                    positionX = -(birdX + xPos);
                    positionY = birdY + yPos;
                } else {
                    if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) {
                        birdSprite->setTexture("bird_right.png");
                    } else {
                        birdSprite->setTexture("blue_right.png");
                    }
                    birdSprite->setTag(8);
                    birdSprite->setRotation(-rotation);
                    positionX = birdX + xPos;
                    positionY = birdY + yPos;
                }
            } else {  // Left
                if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) {
                    birdSprite->setTexture("bird_right.png");
                } else {
                    birdSprite->setTexture("blue_right.png");
                }
                if( birdY > halfVisibleHeight )
                {
                    birdSprite->setTag(1);
                    birdSprite->setRotation(rotation);
                    positionX = birdX + xPos;
                    positionY = -(birdY + yPos);
                } else {
                    birdSprite->setTag(2);
                    birdSprite->setRotation(-rotation);
                    positionX = birdX + xPos;
                    positionY = birdY + yPos;
                }
            }
        }
        if((birdTag == 6 || birdTag == 7) && (borderCheck == 2 || borderCheck == 4))  // RightBottom || BottomRight -> top and left
        {
            if(borderCheck == 2)  // Top
            {
                if( birdX > halfVisibleWidth )
                {
                    if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) {
                        birdSprite->setTexture("bird_left.png");
                    } else {
                        birdSprite->setTexture("blue_left.png");
                    }

                    birdSprite->setTag(3);
                    birdSprite->setRotation(-rotation);
                    positionX = -(birdX + xPos);
                    positionY = -(birdY + yPos);
                } else {
                    if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) {
                        birdSprite->setTexture("bird_right.png");
                    } else {
                        birdSprite->setTexture("blue_right.png");
                    }
                    birdSprite->setTag(4);
                    birdSprite->setRotation(rotation);
                    positionX = birdX + xPos;
                    positionY = -(birdY + yPos);
                }
            } else {  // Left
                if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) {
                    birdSprite->setTexture("bird_right.png");
                } else {
                    birdSprite->setTexture("blue_right.png");
                }
                if( birdY > halfVisibleHeight )
                {
                    birdSprite->setTag(1);
                    birdSprite->setRotation(rotation);
                    positionX = birdX + xPos;
                    positionY = -(birdY + yPos);
                } else {
                    birdSprite->setTag(2);
                    birdSprite->setRotation(-rotation);
                    positionX = birdX + xPos;
                    positionY = birdY + yPos;
                }
            }
        }
        
        if(((birdTag == 1 || birdTag == 4) && (borderCheck == 1 || borderCheck == 3)) || ((birdTag == 2 || birdTag == 8) && (borderCheck == 2 || borderCheck == 3)) || ((birdTag == 3 || birdTag == 5) && (borderCheck == 1 || borderCheck == 4)) || ((birdTag == 6 || birdTag == 7) && (borderCheck == 2 || borderCheck == 4)))
        {
            bird->SetLinearVelocity( b2Vec2(positionX * velocity / PTM_RATIO, positionY * velocity / PTM_RATIO));
        }
        
        def->flush();
        
        contact->SetEnabled(false);
    }
}