void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) { // If there is already a background music source we stop it first if (s_backgroundSource != AL_NONE) stopBackgroundMusic(false); // Changing file path to full path std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath); BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath); if (it == s_backgroundMusics.end()) { preloadBackgroundMusic(fullPath.c_str()); it = s_backgroundMusics.find(fullPath); } if (it != s_backgroundMusics.end()) { s_backgroundSource = it->second->source; alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE); setBackgroundVolume(s_volume); alSourcePlay(s_backgroundSource); checkALError("playBackgroundMusic:alSourcePlay"); } }
bool AppDelegate::applicationDidFinishLaunching() { // initialize director auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) { glview = GLViewImpl::createWithRect("UNOLink", Rect(0, 0, 640, 960)); //´´½¨WINDOWS´°¿Ú director->setOpenGLView(glview); } director->getOpenGLView()->setDesignResolutionSize(WIDTH, HEIGHT, ResolutionPolicy::SHOW_ALL); //ÆÁÄ»ÊÊÅä director->setDisplayStats(false); //²»ÏÔʾFPS director->setAnimationInterval(1.0 / 60); //ÉèÖÃFPS srand(time(NULL)); //ÉèÖÃËæ»úÊýÖÖ×Ó //Ô¤¼ÓÔØÒôÀÖÒôЧ auto audio = SimpleAudioEngine::getInstance(); audio->preloadBackgroundMusic(BGM); audio->preloadEffect(START_LINK_EFFECT); audio->preloadEffect(LINK_EFFECT); audio->preloadEffect(UNDO_EFFECT); audio->preloadEffect(REMOVE_EFFECT); audio->preloadEffect(WRONG_REMOVE_EFFECT); audio->preloadEffect(GAME_OVER_EFFECT); audio->preloadEffect(NEW_RECORD_EFFECT); //¼ÓÔØcocos studioµ¼³öµÄÎļþ FileUtils::getInstance()->addSearchPath("res"); //½øÈëStart³¡¾° auto scene = StartScene::create(); director->runWithScene(scene); return true; }
// on "init" you need to initialize your instance bool VictoryScene::init() { if (!Layer::init()) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); // Victory label auto victory = Label::createWithTTF("Victory!", "fonts/arial.ttf", 32); victory->setPosition(Point(visibleSize.width / 2, visibleSize.height / 4 * 3)); victory->setColor(cocos2d::Color3B(0, 255, 0)); victory->setScale(2); this->addChild(victory); // Menu auto retry = MenuItemFont::create("Retry", CC_CALLBACK_1(VictoryScene::Retry, this)); retry->setPosition(Point(visibleSize.width / 2, visibleSize.height / 4 * 2)); auto goBack = MenuItemFont::create("Go Back", CC_CALLBACK_1(VictoryScene::GoBack, this)); goBack->setPosition(Point(visibleSize.width / 2, visibleSize.height / 4 * 1)); auto *menu = Menu::create(retry, goBack, NULL); menu->setPosition(Point(0, 0)); this->addChild(menu); // Music auto audio = CocosDenshion::SimpleAudioEngine::getInstance(); audio->preloadBackgroundMusic(WIN_MUSIC); audio->playBackgroundMusic(WIN_MUSIC, true); return true; }
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) { if (!s_isBackgroundInitialized) preloadBackgroundMusic(pszFilePath); alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE); alSourcePlay(s_backgroundSource); checkALError("playBackgroundMusic"); }
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) { // Changing file path to full path std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath); if (!s_isBackgroundInitialized) preloadBackgroundMusic(fullPath.c_str()); alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE); alSourcePlay(s_backgroundSource); checkALError("playBackgroundMusic"); }
bool HelloWorld::init() { if (!Layer::init()) { return false; } auto audio = CocosDenshion::SimpleAudioEngine::getInstance(); audio->preloadBackgroundMusic("sfx/An_Adventure_Awaits.mp3"); audio->preloadBackgroundMusic("sfx/BEAD.mp3"); audio->preloadEffect("sfx/sound4.wav"); audio->preloadEffect("sfx/sound5.wav"); audio->playBackgroundMusic("sfx/BEAD.mp3"); Size visibleSize = Director::getInstance()->getVisibleSize(); auto startGameButton = Label::createWithTTF("Start the game", "fonts/Marker Felt.ttf", 24); startGameButton->setPosition( Vec2( visibleSize.width/2, visibleSize.height/2 - startGameButton->getContentSize().height ) ); auto touchListener = EventListenerTouchOneByOne::create(); touchListener->onTouchBegan = [](Touch* touch, Event* event) -> bool { auto scene = Game::createScene(); Director::getInstance()->pushScene(scene); return true; }; Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority( touchListener, startGameButton ); this->addChild(startGameButton, 1); return true; }
void LoadingScene::LoadingMusic() { audioloaded = true; auto Audio = CocosDenshion::SimpleAudioEngine::getInstance(); Audio->preloadEffect("explodeEffect.mp3"); Audio->preloadEffect("hit.mp3"); Audio->preloadEffect("boom2.mp3"); Audio->preloadEffect("boom.mp3"); Audio->preloadBackgroundMusic("Orbital Colossus_0.mp3"); //Audio->preloadBackgroundMusic("Star_Chaser.mp3"); // Music By Matthew Pable (http://www.matthewpablo.com/) // Licensed under CC-BY 3.0 (http://creativecommons.org/licenses/by/3.0/) Audio->playBackgroundMusic("Flux2.mp3"); }
bool AudioScene::init() { if (!Layer::init()) { return false; } auto audio = CocosDenshion::SimpleAudioEngine::getInstance(); audio->preloadBackgroundMusic("hungerland.wav"); audio->playBackgroundMusic("hungerland.wav"); auto eventListener = cocos2d::EventListenerKeyboard::create(); eventListener->onKeyPressed = [audio](cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event* event) { switch (keyCode) { case cocos2d::EventKeyboard::KeyCode::KEY_SPACE: { if (audio->isBackgroundMusicPlaying()) { audio->pauseBackgroundMusic(); } else { audio->resumeBackgroundMusic(); } break; } case cocos2d::EventKeyboard::KeyCode::KEY_RIGHT_ARROW: { audio->playBackgroundMusic("noncombat.wav"); break; } case cocos2d::EventKeyboard::KeyCode::KEY_LEFT_ARROW: { audio->playBackgroundMusic("hungerland.wav"); break; } } }; _eventDispatcher->addEventListenerWithFixedPriority(eventListener, 2); return true; }
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) { if (0 != strcmp(s_currentBackgroundStr.c_str(), pszFilePath)) { stopBackgroundMusic(true); } else { if (s_playStatus == PAUSED) resumeBackgroundMusic(); else rewindBackgroundMusic(); } if (!s_isBackgroundInitialized) preloadBackgroundMusic(pszFilePath); if (bLoop) { // set it up to loop strm_dict_t *dictionary = strm_dict_new(); s_repeatDictionary = strm_dict_set(dictionary, "repeat", "all"); if (mmr_input_parameters(s_mmrContext, s_repeatDictionary) != 0) { mmrerror(s_mmrContext, "input parameters (loop)"); return; } } if (s_hasMMRError || !s_mmrContext) return; if (mmr_play(s_mmrContext) < 0) { mmrerror(s_mmrContext, "mmr_play"); s_hasMMRError = true; } if (!s_hasMMRError) s_playStatus = PLAYING; }
KDvoid SimpleAudioEngine::playBackgroundMusic ( const KDchar* szFilePath, bool bLoop ) { if ( !szFilePath ) { return; } std::string sPath = FileUtils::getInstance ( )->fullPathForFilename ( szFilePath ); KDuint uRet = _Hash ( sPath.c_str ( ) ); preloadBackgroundMusic ( sPath.c_str ( ) ); SoundList::iterator it = l_aMusicList.find ( uRet ); if ( it != l_aMusicList.end ( ) ) { l_uCurrentMusic = uRet; xmSoundSetRepeat ( it->second, bLoop ); this->rewindBackgroundMusic ( ); } }
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) { if (s_backgroundSource != AL_NONE) stopBackgroundMusic(false); // Changing file path to full path std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath); BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath); if (it == s_backgroundMusics.end()) { preloadBackgroundMusic(fullPath.c_str()); it = s_backgroundMusics.find(fullPath); } if (it != s_backgroundMusics.end()) { s_backgroundSource = it->second->source; alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE); alSourcePlay(s_backgroundSource); checkALError("playBackgroundMusic"); } }
bool ComAudio::serialize(void* r) { bool ret = false; do { CC_BREAK_IF(r == nullptr); SerData *serData = (SerData *)(r); const rapidjson::Value *v = serData->_rData; stExpCocoNode *cocoNode = serData->_cocoNode; const char *className = nullptr; const char *comName = nullptr; const char *file = nullptr; std::string filePath; int resType = 0; bool loop = false; if (v != nullptr) { className = DICTOOL->getStringValue_json(*v, "classname"); CC_BREAK_IF(className == nullptr); comName = DICTOOL->getStringValue_json(*v, "name"); const rapidjson::Value &fileData = DICTOOL->getSubDictionary_json(*v, "fileData"); CC_BREAK_IF(!DICTOOL->checkObjectExist_json(fileData)); file = DICTOOL->getStringValue_json(fileData, "path"); CC_BREAK_IF(file == nullptr); resType = DICTOOL->getIntValue_json(fileData, "resourceType", -1); CC_BREAK_IF(resType != 0); loop = DICTOOL->getIntValue_json(*v, "loop") != 0? true:false; } else if (cocoNode != nullptr) { className = cocoNode[1].GetValue(); CC_BREAK_IF(className == nullptr); comName = cocoNode[2].GetValue(); stExpCocoNode *pfileData = cocoNode[4].GetChildArray(); CC_BREAK_IF(!pfileData); file = pfileData[0].GetValue(); CC_BREAK_IF(file == nullptr); resType = atoi(pfileData[2].GetValue()); CC_BREAK_IF(resType != 0); loop = atoi(cocoNode[5].GetValue()) != 0? true:false; ret = true; } if (comName != nullptr) { setName(comName); } else { setName(className); } if (file != nullptr) { if (strcmp(file, "") == 0) { continue; } filePath.assign(cocos2d::FileUtils::getInstance()->fullPathForFilename(file)); } if (strcmp(className, "CCBackgroundAudio") == 0) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) // no MP3 support for CC_PLATFORM_WP8 std::string::size_type pos = filePath.find(".mp3"); if (pos == filePath.npos) { continue; } filePath.replace(pos, filePath.length(), ".wav"); #endif preloadBackgroundMusic(filePath.c_str()); setLoop(loop); playBackgroundMusic(filePath.c_str(), loop); } else if(strcmp(className, "CCComAudio") == 0) { preloadEffect(filePath.c_str()); } else { CC_BREAK_IF(true); } ret = true; }while (0); return ret; }
bool GameScene::init() { if (!Layer::init())return false; initPlist(); initEffect(); //set game status Config::getInstance()->setGameSatus(ING); Config::getInstance()->setScore(0);//rest score //loding effect auto _SimpleAudioEngine = SimpleAudioEngine::getInstance(); _SimpleAudioEngine->preloadEffect(air_shoot_effect1); _SimpleAudioEngine->preloadEffect(ship_explode_effect0); //init game map //init enemy type switch (levelNum) { case GAMEMAP1://0 if (Config::getInstance()->getmusicState()) { auto simpleAudio = SimpleAudioEngine::getInstance(); simpleAudio->preloadBackgroundMusic(battle_bg_stage0); simpleAudio->setBackgroundMusicVolume(1.0f); simpleAudio->playBackgroundMusic(battle_bg_stage0, true); } addMap("Map1.png"); levelScore = BOOS_LEVEL1; break; case GAMEMAP2://1 if (Config::getInstance()->getmusicState()) { auto simpleAudio = SimpleAudioEngine::getInstance(); simpleAudio->preloadBackgroundMusic(battle_bg_stage1); simpleAudio->setBackgroundMusicVolume(1.0f); simpleAudio->playBackgroundMusic(battle_bg_stage1, true); } addMap("Map2.png"); levelScore = BOOS_LEVEL2; //add particle Kit::addParticle(snow_plist, this, VisibleRect::top()); break; case GAMEMAP3://2 if (Config::getInstance()->getmusicState()) { auto simpleAudio = SimpleAudioEngine::getInstance(); simpleAudio->preloadBackgroundMusic(battle_bg_stage2); simpleAudio->setBackgroundMusicVolume(1.0f); simpleAudio->playBackgroundMusic(battle_bg_stage2, true); } addMap("Map3.png"); levelScore = BOOS_LEVEL3; break; case GAMEMAP4://3 if (Config::getInstance()->getmusicState()) { auto simpleAudio = SimpleAudioEngine::getInstance(); simpleAudio->preloadBackgroundMusic(battle_bg_stage2); simpleAudio->setBackgroundMusicVolume(1.0f); simpleAudio->playBackgroundMusic(battle_bg_stage2, true); } addMap("Map4.png"); levelScore = BOOS_LEVEL4; //add fire paticle Kit::addParticle(battle_fire_bg_particle_plist, this, VisibleRect::top()); break; default: log("there's no game map to do !"); return false; break; } //add logo one top-left auto logoShip = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(SHIP_FILE_NAME)); float offset = logoShip->getContentSize().width / 2; logoShip->setScale(0.8f); auto winsize = Director::getInstance()->getWinSize(); logoShip->setPosition(Vec2(offset, winsize.height - offset)); this->addChild(logoShip, SHIP_Z_ORDER); //set life value beside of logoShip Config::getInstance()->setShipLife(SHIP_LIFES); __String life("X"); life.appendWithFormat("%02d", Config::getInstance()->getShipLife()); lifeValue = Label::createWithSystemFont(life.getCString(), "Arial", 12); lifeValue->setPosition(Vec2(offset * 3, winsize.height - offset)); lifeValue->setColor(Color3B::RED); this->addChild(lifeValue, SHIP_Z_ORDER); //add score __String tempscore("得分:"); tempscore.appendWithFormat("%07d", Config::getInstance()->getScore()); score = Label::createWithSystemFont(tempscore.getCString(), "Arial", 12); score->setPosition(Vec2(winsize.width - 45, winsize.height - offset)); score->setColor(Color3B::RED); this->addChild(score, SHIP_Z_ORDER); ship = Ship::creatShip(this, 0, SHIP_LIFE_VALUE); auto initPosition = VisibleRect::center(); ship->setPosition(Vec2(initPosition.x, initPosition.y - 120.0f)); this->addChild(ship, SHIP_Z_ORDER); //add bone btn auto bone_btn_sprite = Sprite::create(btn_bone_res); bone_btn_sprite->setScale(0.8f); auto bone_btn = MenuItemSprite::create(bone_btn_sprite, nullptr, [&](Ref* _bone_btn){ //处理炸弹效果 log("bone released ......"); //can shake auto j = JumpBy::create(0.5f, Vec2::ZERO, 5, 5); this->runAction(j); auto config = Config::getInstance(); //remove enemy and bullet for (auto bullet : *config->enemy_bullet_list) { bullet->boneDestory(); } for (auto enemy : *config->enemy_list) { //effect it log("effect it ......"); enemy->boneDestory(this); } config->enemy_bullet_list->clear(); config->enemy_list->clear(); }); auto menue = Menu::create(bone_btn, NULL); menue->setPosition(Vec2(35, 200)); this->addChild(menue, BTN_BACK_Z_ORDER); menue->runAction(Kit::creatEaseSineInOut(100.0f)); //add back btn auto btn_sprite = Sprite::createWithTexture(Director::getInstance()->getTextureCache()->addImage(btn_back_res)); btn_sprite->setColor(Color3B(0, 245, 255)); auto btn_back = MenuItemSprite::create(btn_sprite, nullptr, [&](Ref* item){ auto menuSprite = (MenuItemSprite*)item; auto scale = ScaleBy::create(0.2f, 1.1); auto jumpToGameLevel = CallFunc::create([](){ auto config=Config::getInstance(); if (config->getmusicState()) SimpleAudioEngine::getInstance()->stopBackgroundMusic();// 停止当前背景音乐 if (config->getmusicState()) { SimpleAudioEngine::getInstance()->playBackgroundMusic(main_bg_stage1, true); } Director::getInstance()->replaceScene(TransitionFade::create(1.2f, GameLevelLayer::creatScene())); }); //call back std::function<void()> releaseResources = [&](){ this->releaseR(); }; menuSprite->runAction(Sequence::create(CallFunc::create(releaseResources), scale, scale->reverse(), jumpToGameLevel, NULL)); }); menue = Menu::create(btn_back, NULL); auto btn_back_position = VisibleRect::rightBottom(); offset = btn_sprite->getContentSize().width / 2; menue->setPosition(Vec2(btn_back_position.x - 50, btn_back_position.y + 25)); this->addChild(menue, BTN_BACK_Z_ORDER); menue->runAction(Kit::creatEaseSineInOut(100.0f)); //添加敌机 this->schedule(schedule_selector(GameScene::addEnemy), 2.0f, kRepeatForever, 1.2f); this->scheduleUpdate(); //初始化地图移动 this->scheduleOnce(schedule_selector(GameScene::initMoveMap), 1.2f); return true; }
bool ComAudio::serialize(void* r) { bool ret = false; do { CC_BREAK_IF(r == nullptr); SerData *serData = (SerData *)(r); const rapidjson::Value *v = serData->_rData; stExpCocoNode *cocoNode = serData->_cocoNode; CocoLoader *cocoLoader = serData->_cocoLoader; const char *className = nullptr; const char *comName = nullptr; const char *file = nullptr; std::string filePath; int resType = 0; bool loop = false; if (v != nullptr) { className = DICTOOL->getStringValue_json(*v, "classname"); CC_BREAK_IF(className == nullptr); comName = DICTOOL->getStringValue_json(*v, "name"); const rapidjson::Value &fileData = DICTOOL->getSubDictionary_json(*v, "fileData"); CC_BREAK_IF(!DICTOOL->checkObjectExist_json(fileData)); file = DICTOOL->getStringValue_json(fileData, "path"); CC_BREAK_IF(file == nullptr); resType = DICTOOL->getIntValue_json(fileData, "resourceType", -1); CC_BREAK_IF(resType != 0); loop = DICTOOL->getIntValue_json(*v, "loop") != 0? true:false; } else if (cocoNode != nullptr) { className = cocoNode[1].GetValue(cocoLoader); CC_BREAK_IF(className == nullptr); comName = cocoNode[2].GetValue(cocoLoader); stExpCocoNode *pfileData = cocoNode[4].GetChildArray(cocoLoader); CC_BREAK_IF(!pfileData); file = pfileData[0].GetValue(cocoLoader); CC_BREAK_IF(file == nullptr); resType = atoi(pfileData[2].GetValue(cocoLoader)); CC_BREAK_IF(resType != 0); loop = atoi(cocoNode[5].GetValue(cocoLoader)) != 0? true:false; ret = true; } if (comName != nullptr) { setName(comName); } else { setName(className); } if (file != nullptr) { if (strcmp(file, "") == 0) { continue; } filePath.assign(cocos2d::FileUtils::getInstance()->fullPathForFilename(file)); } if (strcmp(className, "CCBackgroundAudio") == 0) { preloadBackgroundMusic(filePath.c_str()); setLoop(loop); playBackgroundMusic(filePath.c_str(), loop); } else if(strcmp(className, COMPONENT_NAME.c_str()) == 0) { preloadEffect(filePath.c_str()); } else { CC_BREAK_IF(true); } ret = true; }while (0); return ret; }