const std::string CCNative::getDeviceID(void) { #define UDID_KEY "__UDID_KEY__" std::string udid; UserDefault* userDefault = UserDefault::getInstance(); if( NULL != userDefault ) { udid = userDefault->getStringForKey( UDID_KEY ); } if( udid.size() == 0 ) { //GUID uuid; //CoCreateGuid(&uuid); //// Spit the address out //char mac_addr[18]; //sprintf(mac_addr,"%02X:%02X:%02X:%02X:%02X:%02X", //uuid.Data4[2],uuid.Data4[3],uuid.Data4[4], //uuid.Data4[5],uuid.Data4[6],uuid.Data4[7]); udid = GetNetBiosMacAddresses(); if( userDefault ) { userDefault->setStringForKey(UDID_KEY, udid); userDefault->flush(); } } return udid; }
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(); } }
void TutorialScene::resetSaveData() { const int maxNumMonsters = 3; const int maxNumKnown = 31; //データをリセット //Set every SaveDataKey for Monsters and Equipment to 0 (eEmpty) UserDefault* def = UserDefault::getInstance(); for (int i = 0; i < maxNumMonsters; i++) { def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eMonsterData01 + i].c_str(), n_MonsterData::eEmpty); } for (int i = 0; i < maxNumKnown; i++) { def->setBoolForKey(SaveDataKey[n_DataKeyNum::eKnownData01 + i].c_str(), false); } def->setBoolForKey(SaveDataKey[n_DataKeyNum::eKnownData01].c_str(), true); def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eMonster1Exp].c_str(), 0); def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eMonster2Exp].c_str(), 0); def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eMonster3Exp].c_str(), 0); def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eWinNum01].c_str(), 0); def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eWinNum02].c_str(), 0); def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eWinNum03].c_str(), 0); def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eLossNum01].c_str(), 0); def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eLossNum02].c_str(), 0); def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eLossNum03].c_str(), 0); def->flush(); }
// on "init" you need to initialize your instance bool GameOverScene::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto backgroundSprite = Sprite::create("Background.png"); backgroundSprite->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)); this->addChild(backgroundSprite); auto retryItem = MenuItemImage::create("Retry Button.png", "Retry Button Clicked.png", CC_CALLBACK_1(GameOverScene::GoToGameScene, this)); retryItem->setPosition(Point(visibleSize.width /2 + origin.x, visibleSize.height / 4 * 3)); auto mainMenuItem = MenuItemImage::create("Menu Button.png", "Menu Button Clicked.png", CC_CALLBACK_1(GameOverScene::GoToMainMenuScene, this)); mainMenuItem->setPosition(Point(visibleSize.width /2 + origin.x, visibleSize.height / 4 )); auto menu = Menu::create(retryItem, mainMenuItem, NULL); menu->setPosition(Point::ZERO); this->addChild(menu); UserDefault *def = UserDefault::getInstance(); auto highScore = def->getIntegerForKey("HIGHSCORE", 0); if (score > highScore) { highScore = score; def->setIntegerForKey("HIGHSCORE", highScore); } def->flush(); __String *tempScore = __String::createWithFormat("Current : %i", score); auto currentScore = LabelTTF::create(tempScore->getCString(), "fonts/Marker Felt.ttf", visibleSize.height * SCORE_FONT_SIZE); currentScore->setPosition(Point(visibleSize.width * 0.25 + origin.x, visibleSize.height /2 + origin.y )); this->addChild(currentScore); __String *temHighpScore = __String::createWithFormat("High : %i", highScore); auto highScoreLabel = LabelTTF::create(temHighpScore->getCString(), "fonts/Marker Felt.ttf", visibleSize.height * SCORE_FONT_SIZE); highScoreLabel->setColor(Color3B::YELLOW); highScoreLabel->setPosition(Point(visibleSize.width * 0.75 + origin.x, visibleSize.height / 2 + origin.y)); this->addChild(highScoreLabel); return true; }
void TitleScene::menuSceneTransition(cocos2d::Ref *pSender, bool newGame) { const int maxNumMonsters = 3; const int maxNumEquip = 10; if (newGame) { if (Generic::checkSaveData()) { auto nextScene = TutorialScene::createScene(); auto pScene = TransitionFade::create(1.0f, nextScene); Director::getInstance()->replaceScene(pScene); } else { const int maxNumMonsters = 3; const int maxNumKnown = 31; //データをリセット //Set every SaveDataKey for Monsters and Equipment to 0 (eEmpty) UserDefault* def = UserDefault::getInstance(); for (int i = 0; i < maxNumMonsters; i++) { def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eMonsterData01 + i].c_str(), n_MonsterData::eEmpty); } for (int i = 0; i < maxNumKnown; i++) { def->setBoolForKey(SaveDataKey[n_DataKeyNum::eKnownData01 + i].c_str(), false); } def->setBoolForKey(SaveDataKey[n_DataKeyNum::eKnownData01].c_str(), true); def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eMonster1Exp].c_str(), 0); def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eMonster2Exp].c_str(), 0); def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eMonster3Exp].c_str(), 0); def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eWinNum01].c_str(), 0); def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eWinNum02].c_str(), 0); def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eWinNum03].c_str(), 0); def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eLossNum01].c_str(), 0); def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eLossNum02].c_str(), 0); def->setIntegerForKey(SaveDataKey[n_DataKeyNum::eLossNum03].c_str(), 0); def->flush(); auto nextScene = MenuScene::createScene(); auto pScene = TransitionFade::create(1.0f, nextScene); Director::getInstance()->replaceScene(pScene); } return; } auto nextScene = MenuScene::createScene(); auto pScene = TransitionFade::create(0.5f, nextScene); Director::getInstance()->replaceScene(pScene); }
void NegiSprite::setSprite() { this->setPosition(Vec2(WINSIZE.width/2, WINSIZE.height/2)); float setLine = 3.0; UserDefault *userDef = UserDefault::getInstance(); userDef->setFloatForKey("SetLine", setLine); userDef->flush(); }
void BookSprite::setSprite() { Size visibleSize = WINSIZE; Vec2 origin = Director::getInstance()->getVisibleOrigin(); this->setPosition(Vec2(WINSIZE.width/1.4, WINSIZE.height/2)); float setLine = 2.2; UserDefault *userDef = UserDefault::getInstance(); userDef->setFloatForKey("SetLine", setLine); userDef->flush(); }
bool GameOverScene::init() { if(!Layer::init()) { return false; } auto visibleSize = Director::getInstance()->getVisibleSize(); auto origin = Director::getInstance()->getVisibleOrigin(); UserDefault* userScore = UserDefault::getInstance(); if(score >= userScore->getIntegerForKey( "highscore" )) { userScore->setIntegerForKey( "highscore", score ); } userScore->flush(); //create background for game over scene auto background = Sprite::create("FlappyResources/Background.png"); background->setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2); this->addChild(background); //create retry item auto retry = MenuItemImage::create( "FlappyResources/Retry_Button.png", "FlappyResources/Retry_Button_Clicked.png", CC_CALLBACK_1(GameOverScene::goToGameScene, this) ); retry->setPosition( origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2 ); //create quit item auto quit = MenuItemImage::create( "FlappyResources/Menu_Button.png", "FlappyResources/Menu_Button_Clicked.png", CC_CALLBACK_1(GameOverScene::goToMainMenuScene, this) ); quit->setPosition( origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2 - quit->getContentSize().height * 2); //create menu auto menu = Menu::create( retry, quit, NULL ); menu->setPosition( Vec2(0, 0) ); //create label score auto stringScore = __String::createWithFormat( "Your score: %d", score ); auto labelScore = LabelTTF::create( stringScore->getCString(), "fonts/Marker Belt.ttf", 50 ); labelScore->setPosition( origin.x + labelScore->getContentSize().width, origin.y + visibleSize.height / 2 + labelScore->getContentSize().height * 4 ); labelScore->setFontFillColor( Color3B::YELLOW ); //create label display best score auto stringBestScore = __String::createWithFormat( "High score: %d", userScore->getIntegerForKey( "highscore" ) ); auto labelHighScore = LabelTTF::create( stringBestScore->getCString(), "fonts/Marker Belt.ttf", 50 ); labelHighScore->setPosition( origin.x + labelScore->getContentSize().width + visibleSize.width / 2, origin.y + visibleSize.height / 2 + labelHighScore->getContentSize().height * 4); labelHighScore->setFontFillColor( Color3B::RED ); this->addChild( menu ); this->addChild( labelScore ); this->addChild( labelHighScore ); return true; }
//ハイスコア記録・表示 void GameScene::saveHighScore() { UserDefault* userDefault = UserDefault::getInstance(); //ハイスコアを取得する int oldHighScore = userDefault->getIntegerForKey(KEY_HIGHSCORE,0); if(oldHighScore < m_score) { //ハイスコアを保存する userDefault->setIntegerForKey(KEY_HIGHSCORE, m_score); userDefault->flush(); //ハイスコアを表示する showHighScoreLabel(); } }
void BBGameDataManager::setGameOverData(int star, int leftTime) { int themeId = m_themeId; int levelId = m_levelId + 1; UserDefault* ud = UserDefault::getInstance(); // 更新星级 __String *starkey = __String::createWithFormat("star_%d_%d", m_themeId, m_levelId); int curStar = ud->getIntegerForKey(starkey->getCString(), 0); if (curStar < star) { ud->setIntegerForKey(starkey->getCString(), star); } __String *bestkey = __String::createWithFormat("best_%d_%d", m_themeId, m_levelId); int bestTime = ud->getIntegerForKey(bestkey->getCString(), 0); if (bestTime == 0 || bestTime > leftTime) { ud->setIntegerForKey(bestkey->getCString(), leftTime); } // 更新关卡进度 int themeIdFramCache = ud->getIntegerForKey("themeId", 1); int levelIdFramCache = ud->getIntegerForKey("levelId", 1); if (themeIdFramCache > themeId) { return; } else if (themeIdFramCache == themeId) { if (levelIdFramCache >= levelId) { return; } } if (levelId > 9) { // 进入下一个主题 themeId++; levelId = 1; } ud->setIntegerForKey("themeId", themeId); ud->setIntegerForKey("levelId", levelId); ud->flush(); }
/*--------------------------/ Yesボタン生成 /--------------------------*/ void StageSelectActionLayer::buttonYes(cocos2d::Layer* layer,Menu* menu,int tag){ auto buttonYes = MenuItemImage::create("Texture/GamePlay/GameScene/StageSelect/StageSelect_Yes.png", "Texture/GamePlay/GameScene/StageSelect/StageSelect_Yes.png", [ = ](Ref* sender) { UserDefault* useDef = UserDefault::getInstance(); useDef->setIntegerForKey("selectStage",tag); useDef->setIntegerForKey("selectGimmick", tag); useDef->flush(); auto nextScene = SceneCreator::createPhysicsScene(GameMainScene::create(), Vect(0, -9.8f), 5.0f); auto scene = TransitionFade::create( 1.5f, nextScene, Color3B::BLACK ); auto dir = Director::getInstance(); dir->replaceScene(scene); }); buttonYes->setPosition(400,300); buttonYes->setScale(1.2f); menu->addChild(buttonYes); }
void GeneralUtils::setControllerType(ControllerType controllerType) { int controllerTypeInt = 0; if (controllerType == E_CONTROLLER_TOUCH) { controllerTypeInt = 1; } else if (controllerType == E_CONTROLLER_TILT) { controllerTypeInt = 2; } else { return; } UserDefault *ud = UserDefault::getInstance(); ud->setIntegerForKey("control_type", controllerTypeInt); ud->flush(); }
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(); } }
//Callback void TrivialAPI::onHttpRequestCompleted(HttpClient* client, HttpResponse* response) { const char *responseTag; if (!response) return; if (!response->isSucceed()) { log("response failed"); log("error buffer: %s", response->getErrorBuffer()); log("HTTP Status Code: %ld", response->getResponseCode()); std::vector<char> *buffer = response->getResponseData(); printf("Http Test, dump data: "); for (unsigned int i = 0; i < buffer->size(); i++) { printf("%c", (*buffer)[i]); } printf("\n"); UserDefault *pUserDefaults = UserDefault::getInstance(); std::string hash = pUserDefaults->getStringForKey("hash"); if (hash.length() == 0) { MessageBox("Interneta beharrezkoa da", "Error"); } else { _finishCalbback->execute(); } } else { responseTag = response->getHttpRequest()->getTag(); int statusCode = (int) response->getResponseCode(); char statusString[64] = {}; sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, responseTag); //log(statusString); } std::vector<char> *buffer = response->getResponseData(); std::string str(buffer->begin(), buffer->end()); if (!strcmp(responseTag, "generalToken")) { UserDefault *pUserDefault = UserDefault::getInstance(); std::string savedHash = pUserDefault->getStringForKey("hash"); std::string token = parseGeneralToken(str); if (savedHash.compare(token) != 0) { _hash = token; _sqlHelper->resetDB(); updateKategoriak(emptyPrivateCode); } else { //updatePrivateData(); _finishCalbback->execute(); } } else if(!strcmp(responseTag, "kategoriak")) { parseKategoriak(str); updateMultzoak(emptyPrivateCode); } else if(!strcmp(responseTag, "multzoak")) { parseMultzoak(str); updateGalderak(emptyPrivateCode); } else if(!strcmp(responseTag, "galderak")) { parseGalderak(str); UserDefault *pUserDefault = UserDefault::getInstance(); std::string savedHash = pUserDefault->getStringForKey("hash"); if (savedHash.empty()) { _sqlHelper->initUserData(); } else { _sqlHelper->updateUserData(); } pUserDefault->setStringForKey("hash", _hash); pUserDefault->flush(); //updatePrivateData(); _finishCalbback->execute(); } }
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); } }
Bird::Bird( cocos2d::Layer *layer, b2World *world ) { visibleSize = Director::getInstance()->getVisibleSize(); int visibleWidth = visibleSize.width; int visibleHeight = visibleSize.height; float positionX, positionY; long whichBird; auto region = rand() % 4 + 1; // Adjust random region based on previous regions UserDefault *def = UserDefault::getInstance(); auto velocity = def->getFloatForKey("velocity", 0); auto power3_activated = def->getIntegerForKey("power3_activated",0); auto bird1_region = def->getIntegerForKey("bird1_region", 0); auto bird2_region = def->getIntegerForKey("bird2_region", 0); auto blue1 = def->getIntegerForKey("blue1"); auto blue2 = def->getIntegerForKey("blue2"); auto blue3 = def->getIntegerForKey("blue3"); if(bird1_region == 0) { def->setIntegerForKey("bird1_region", region); whichBird = 1; } else { if(bird2_region == 0){ while(region == bird1_region){ region = rand() % 4 + 1; } def->setIntegerForKey("bird2_region", region); whichBird = 2; } else { while(region == bird1_region || region == bird2_region){ region = rand() % 4 + 1; } whichBird = 3; } } if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { bird = Sprite::create("bird_left.png"); } else { bird = Sprite::create("blue_left.png"); } def->flush(); auto rotation = rand() % 46 + 1; int halfBird = bird->getContentSize().width / 2; auto birdY = rand() % (visibleHeight - halfBird) + halfBird; auto birdX = rand() % (visibleWidth - halfBird) + halfBird; bool top = false; if(region == 1 || region == 3) { 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); // Set the bird's position based on region if(region == 1) // Left { bird->setPosition(Point(-halfBird,birdY)); if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { bird->setTexture("bird_right.png"); } else { bird->setTexture("blue_right.png"); } if(top) // Above middle of screen { bird->setRotation(rotation); bird->setTag(1); positionX = bird->getPositionX() + xPos; positionY = -(bird->getPositionY() + yPos); } else // Below middle of screen { bird->setRotation(-rotation); bird->setTag(2); positionX = bird->getPositionX() + xPos; positionY = bird->getPositionY() + yPos; } } if(region == 2) // Top { bird->setPosition(Point(birdX, visibleHeight + halfBird)); if(top) // Past middle of screen on x axis { bird->setRotation(-rotation); bird->setTag(3); positionX = -(bird->getPositionX() + xPos); positionY = -(bird->getPositionY() + yPos); } else // Before middle of screen on x axis { bird->setTag(4); if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { bird->setTexture("bird_right.png"); } else { bird->setTexture("blue_right.png"); } bird->setRotation(rotation); positionX = bird->getPositionX() + xPos; positionY = -(bird->getPositionY() + yPos); } } if(region == 3) // Right { bird->setPosition(Point(visibleWidth + halfBird, birdY)); if(top) // Above middle of screen { bird->setRotation(-rotation); bird->setTag(5); positionX = -(bird->getPositionX() + xPos); positionY = -(bird->getPositionY() + yPos); } else // Below middle of screen { bird->setRotation(rotation); bird->setTag(6); positionX = -(bird->getPositionX() + xPos); positionY = bird->getPositionY() + yPos; } } if(region == 4) // Bottom { bird->setPosition(Point(birdX, -halfBird)); if(top) // Past middle of screen on x axis { bird->setRotation(rotation); bird->setTag(7); positionX = -(bird->getPositionX() + xPos); positionY = bird->getPositionY() + yPos; } else // Before middle of screen on x axis { if((whichBird == 1 && !blue1) || (whichBird == 2 && !blue2) || (whichBird == 3 && !blue3)) { bird->setTexture("bird_right.png"); } else { bird->setTexture("blue_right.png"); } bird->setRotation(-rotation); bird->setTag(8); positionX = bird->getPositionX() + xPos; positionY = bird->getPositionY() + yPos; } } // Create ball body b2BodyDef ballBodyDef; ballBodyDef.type = b2_dynamicBody; ballBodyDef.userData = bird; ballBodyDef.position.Set(bird->getPositionX() / PTM_RATIO,bird->getPositionY() / PTM_RATIO); b2Body *ballBody = world->CreateBody(&ballBodyDef); // Create circle shape b2CircleShape circle; circle.m_radius = (bird->getContentSize().height / 2) / PTM_RATIO; // Create shape definition and add body b2FixtureDef ballShapeDef; ballShapeDef.shape = &circle; ballShapeDef.density = 1.0f; ballShapeDef.friction = 0.f; ballShapeDef.restitution = 0.f; ballShapeDef.isSensor = true; ballShapeDef.filter.categoryBits = BIRD_COLLISION_BITMASK; ballShapeDef.filter.maskBits = BIRD_COLLISION_BITMASK; ballShapeDef.userData = (void*) whichBird; ballBody->SetLinearVelocity( b2Vec2(positionX * velocity / PTM_RATIO, positionY * velocity / PTM_RATIO)); ballBody->CreateFixture(&ballShapeDef); if(!power3_activated){ auto tmpScale = bird->getScale(); //tmpScale = tmpScale / 1.5; bird->setScale(tmpScale); } else { bird->setScale(1.5); } layer->addChild(bird, 100); //setup touch listener auto touchListener = EventListenerTouchOneByOne::create(); touchListener->setSwallowTouches( true ); touchListener->onTouchBegan = CC_CALLBACK_2( Bird::onTouchBegan, this ); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, bird); }
// on "init" you need to initialize your instance bool GameOverScene::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto backgroundSprite = Sprite::create( "BackGroundFlappy.png"); auto backgroundSprite2 = Sprite::create("BackGroundFlappy.png"); auto backgroundSprite3 = Sprite::create("BackGroundFlappy.png"); auto groundSprite = Sprite::create("Ground.png"); auto groundSprite2 = Sprite::create("Ground.png"); auto groundSprite3 = Sprite::create("Ground.png"); backgroundSprite->setScale(1.4); backgroundSprite2->setScale(1.4); backgroundSprite3->setScale(1.4); backgroundSprite->setPosition(visibleSize.width/2.2 + origin.x/2, visibleSize.height/2 - origin.y/2); groundSprite->setPosition(visibleSize.width/2.2, 0); backgroundSprite->setAnchorPoint(Vec2(0.5,0.4)); backgroundSprite2->setAnchorPoint(Vec2(0.5,0.4)); backgroundSprite3->setAnchorPoint(Vec2(0.5,0.4)); auto sizeBack = backgroundSprite->getContentSize(); backgroundSprite2->setPosition(backgroundSprite->getPositionX() + sizeBack.width/2, backgroundSprite->getPositionY()); backgroundSprite3->setPosition(backgroundSprite2->getPositionX() + sizeBack.width/2, backgroundSprite->getPositionY()); groundSprite->setScale(1.95); groundSprite2->setScale(1.95); groundSprite3->setScale(1.95); auto sizeGround = groundSprite->getContentSize(); groundSprite->setAnchorPoint(Vec2(0.5, 0.4)); groundSprite2->setAnchorPoint(Vec2(0.5, 0.4)); groundSprite3->setAnchorPoint(Vec2(0.5, 0.4)); groundSprite2->setPosition(groundSprite->getPositionX() + sizeGround.width/2, groundSprite->getPositionY()); groundSprite3->setPosition(groundSprite2->getPositionX() + sizeGround.width/2, groundSprite->getPositionY()); this->addChild( backgroundSprite); this->addChild( backgroundSprite2); this->addChild( backgroundSprite3); this->addChild(groundSprite); this->addChild(groundSprite2); this->addChild(groundSprite3); auto retryItem = MenuItemImage::create( "Restart.png", "Restart.png", CC_CALLBACK_1( GameOverScene::GoToGameScene, this ) ); retryItem->setPosition( Point( visibleSize.width / 2 + origin.x, visibleSize.height / 4 * 3 ) ); auto menu = Menu::create( retryItem, NULL ); menu->setPosition( Point::ZERO ); this->addChild( menu); UserDefault *def = UserDefault::getInstance( ); auto highScore = def->getIntegerForKey( "HIGHSCORE FLAPPY", 0 ); if ( score > highScore ) { highScore = score; def->setIntegerForKey( "HIGHSCORE FLAPPY", highScore ); } def->flush( ); __String *tempScore = __String::createWithFormat( "%i", score ); auto currentScore = Label::createWithTTF (tempScore->getCString( ), "fonts/FlappyFont.TTF", visibleSize.height * SCORE_FONT_SIZE ); currentScore->setPosition( Point( visibleSize.width * 0.25 + origin.x, visibleSize.height / 2 + origin.y ) ); currentScore->setColor( Color3B::BLACK ); auto currentLabel = Label::createWithTTF("SCORE", "fonts/FlappyFont.TTF", visibleSize.height * SCORE_FONT_SIZE ); currentLabel->setPosition( Point( visibleSize.width * 0.25 + origin.x, visibleSize.height / 1.7 + origin.y ) ); currentLabel->setColor( Color3B::BLACK ); currentLabel->setScale(0.9); this->addChild( currentScore ); this->addChild( currentLabel ); __String *tempHighScore = __String::createWithFormat( "%i", highScore ); auto highScoreLabel = Label::createWithTTF( tempHighScore->getCString( ), "fonts/FlappyFont.TTF", visibleSize.height * SCORE_FONT_SIZE ); highScoreLabel->setColor( Color3B::BLACK ); highScoreLabel->setPosition( Point( visibleSize.width * 0.75 + origin.x, visibleSize.height / 2 + origin.y ) ); auto bestLabel = Label::createWithTTF("BEST", "fonts/FlappyFont.TTF", visibleSize.height * SCORE_FONT_SIZE ); bestLabel->setColor( Color3B::BLACK ); bestLabel->setPosition( Point( visibleSize.width * 0.75 + origin.x, visibleSize.height / 1.7 + origin.y ) ); bestLabel->setScale(0.9); this->addChild( highScoreLabel ); this->addChild( bestLabel ); return true; }
void BBGameDataManager::addCanUseTipsCount() { UserDefault* ud = UserDefault::getInstance(); ud->setIntegerForKey("fingerLeftTimes", 3); ud->flush(); }