bool MainLayer::init() { if (!CCLayer::init()) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCPoint centerPos = CCPoint(visibleSize.width / 2, visibleSize.height / 2); //add backgroud CCSprite *spriteBG = CCSprite::create("MainBG.png"); spriteBG->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); this->addChild(spriteBG, 0); //add menu item CCMenuItemFont::setFontSize(60); CCMenuItemImage *menuItemPlay = CCMenuItemImage::create("start_n.png", "start_s.png", this, menu_selector(MainLayer::onPlay)); menuItemPlay->setPosition(centerPos); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(menuItemPlay, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); // preload music CocosDenshion::SimpleAudioEngine *audioEngine = CocosDenshion::SimpleAudioEngine::sharedEngine(); audioEngine->preloadBackgroundMusic("background.mp3"); audioEngine->preloadEffect("jump.mp3"); audioEngine->preloadEffect("crouch.mp3"); audioEngine->setBackgroundMusicVolume(0.3); audioEngine->setEffectsVolume(0.3); return true; }
int lua_cocos2dx_cocosdenshion_SimpleAudioEngine_preloadEffect(lua_State* tolua_S) { int argc = 0; CocosDenshion::SimpleAudioEngine* cobj = nullptr; bool ok = true; #if COCOS2D_DEBUG >= 1 tolua_Error tolua_err; #endif #if COCOS2D_DEBUG >= 1 if (!tolua_isusertype(tolua_S,1,"cc.SimpleAudioEngine",0,&tolua_err)) goto tolua_lerror; #endif cobj = (CocosDenshion::SimpleAudioEngine*)tolua_tousertype(tolua_S,1,0); #if COCOS2D_DEBUG >= 1 if (!cobj) { tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_cocosdenshion_SimpleAudioEngine_preloadEffect'", nullptr); return 0; } #endif argc = lua_gettop(tolua_S)-1; if (argc == 1) { const char* arg0; std::string arg0_tmp; ok &= luaval_to_std_string(tolua_S, 2, &arg0_tmp, "cc.SimpleAudioEngine:preloadEffect"); arg0 = arg0_tmp.c_str(); if(!ok) { tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_cocosdenshion_SimpleAudioEngine_preloadEffect'", nullptr); return 0; } cobj->preloadEffect(arg0); lua_settop(tolua_S, 1); return 1; } luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "cc.SimpleAudioEngine:preloadEffect",argc, 1); return 0; #if COCOS2D_DEBUG >= 1 tolua_lerror: tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_cocosdenshion_SimpleAudioEngine_preloadEffect'.",&tolua_err); #endif return 0; }
void AppDelegate::preload_sounds() { #ifdef _WIN32 CCLOG("on windows, skipping audio preload"); #else CocosDenshion::SimpleAudioEngine* cache = CocosDenshion::SimpleAudioEngine::getInstance(); CCLOG("start loading audio"); cache->preloadEffect(clean_path("sounds\\old\\Swoosh.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\barrel.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\beep.mp3").c_str()); // cache->preloadEffect(clean_path("sounds\\old\\block.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\brick_hit.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\brick_scrape.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\cheering.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\coin.mp3").c_str()); //cache->preloadEffect(clean_path("sounds\\old\\ding.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\doom_rocket.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\dsnoway.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\glassbreak.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\ice_freeze.mp3").c_str()); //cache->preloadEffect(clean_path("sounds\\old\\lighter.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\radio_wave.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\real_punch.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\shield_die.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\shield_disable.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\shield_hit.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\shotgun.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\shotgun_reload.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\suicide_bomber.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\wall_destroy.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\barrel\\C_barrel_1.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\block\\C_block+1.mp3").c_str()); // cache->preloadEffect(clean_path("sounds\\new\\block\\C_block+2.mp3").c_str()); // cache->preloadEffect(clean_path("sounds\\new\\block\\C_block_1.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\charged\\C_charged_1.mp3").c_str()); //cache->preloadEffect(clean_path("sounds\\new\\cheering\\C_cheering_11.mp3").c_str()); //TODO Find a use for this //cache->preloadEffect(clean_path("sounds\\new\\cheering\\C_cheering_12.mp3").c_str()); //TODO Find a use for this cache->preloadEffect(clean_path("sounds\\new\\cheering\\C_cheering_13.mp3").c_str()); //cache->preloadEffect(clean_path("sounds\\new\\coin\\C_coin_1.mp3").c_str()); //cache->preloadEffect(clean_path("sounds\\new\\coin\\C_coin_2.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\doom_rocket\\C_grenade_1.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\doom_rocket\\C_rocket_big_1.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\doom_rocket\\C_rocket_small_1.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\dsnoway\\C_Grunt_11.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\dsnoway\\C_Grunt_12.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\dsnoway\\C_Grunt_13.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\dsnoway\\C_Grunt_14.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\dsnoway\\C_Grunt_15.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\dsnoway\\C_Grunt_16.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\dsnoway\\C_Grunt_17.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\dsnoway\\C_Grunt_18.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\dsnoway\\C_Hit_11.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\dsnoway\\C_Hit_12.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\dsnoway\\C_Hit_13.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\dsnoway\\C_Hit_14.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\dsnoway\\C_Hit_15.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\dsnoway\\C_Hit_16.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\dsnoway\\C_Hit_17.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\dsnoway\\C_Hit_18.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\extra\\C_explosion_1.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\glassbreak\\C_glassbreak_1.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\ice_freeze\\C_ice_freeze+.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\ice_freeze\\C_ice_freeze_1.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\lighter\\C_lighter_1.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\radio_wave\\C_radio_wave_1.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\radio_wave\\C_radio_wave_2.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\radio_wave\\C_radio_wave_3.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\real_punch\\C_combo_1.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\real_punch\\C_punch+1.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\real_punch\\C_punch+2.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\real_punch\\C_punch+3.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\real_punch\\C_punch_11.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\real_punch\\C_punch_21.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\real_punch\\C_punch_22.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\real_punch\\C_punch_23.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\real_punch\\C_punch_24.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\real_punch\\C_punch_25.mp3").c_str()); // cache->preloadEffect(clean_path("sounds\\new\\real_punch\\C_punch_26.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\real_punch\\C_punch_27.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\real_punch\\C_punch_28.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\real_punch\\C_punch_31.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\shotgun\\C_shotgun_1.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\shotgun_reload\\C_shotgun_reload_1.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\suicide_bomber\\C_rocket_big+1.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\suicide_bomber\\C_rocket_small+2.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\Swoosh\\C_Swoosh_1.wav").c_str()); cache->preloadEffect(clean_path("sounds\\new\\Swoosh\\C_Swoosh_11.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\Swoosh\\C_Swoosh_12.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\Swoosh\\C_Swoosh_13.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\Swoosh\\C_Swoosh_14.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\Swoosh\\C_Swoosh_15.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\Swoosh\\C_Swoosh_21.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\Swoosh\\C_Swoosh_22.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\Swoosh\\C_Swoosh_23.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\Swoosh\\C_Swoosh_24.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\Swoosh\\C_Swoosh_25.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\Swoosh\\C_Swoosh_26.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\Swoosh\\C_Swoosh_27.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\new\\woop\\C_woop_1.mp3").c_str()); cache->preloadEffect(clean_path("sounds\\old\\woop.mp3").c_str()); CCLOG("done loading audio"); #endif };
void CCSWFNodeExt::handleFrameChanged(cocos2d::CCObject * obj){ CCSWFNode * node = (CCSWFNode *)obj; if (m_logicDict) { CocosDenshion::SimpleAudioEngine * audioEngine = CocosDenshion::SimpleAudioEngine::sharedEngine(); std::stringstream frameKey; frameKey << (node->getCurrentFrame() + 1); CCDictionary * dict = (CCDictionary *)m_logicDict->objectForKey(frameKey.str()); if (dict) { //切换背景音乐 CCString * music = (CCString *)dict->objectForKey("music"); if (music && m_music.compare(music->m_sString) != 0) { m_music = music->m_sString; if (m_music.length() > 0) { CCLOG("CSWFNodeExt::handleFrameChanged %d playBackgroundMusic %s",node->getCurrentFrame(),m_music.c_str()); audioEngine->stopBackgroundMusic(true); audioEngine->preloadBackgroundMusic(m_music.c_str()); audioEngine->playBackgroundMusic(m_music.c_str(),true); } } //播放音效 CCString * effect = (CCString *)dict->objectForKey("effect"); if (effect && effect->length() > 0) { const std::string tag = "#"; std::string srcString = effect->m_sString; size_t startPos = srcString.find_first_of(tag); if (startPos != std::string::npos) { std::string tmp; do { tmp = srcString.substr(0,startPos); CCLOG("CSWFNodeExt::handleFrameChanged %d playEffect %s",node->getCurrentFrame(),tmp.c_str()); audioEngine->preloadEffect(tmp.c_str()); audioEngine->playEffect(tmp.c_str(), false); srcString = srcString.substr(startPos+tag.length()); startPos = srcString.find_first_of(tag); if(startPos == std::string::npos){ CCLOG("CSWFNodeExt::handleFrameChanged %d playEffect %s",node->getCurrentFrame(),tmp.c_str()); audioEngine->preloadEffect(tmp.c_str()); audioEngine->playEffect(tmp.c_str(), false); } } while(startPos != std::string::npos) ; }else{ CCLOG("CSWFNodeExt::handleFrameChanged %d playEffect %s",node->getCurrentFrame(),srcString.c_str()); audioEngine->preloadEffect(srcString.c_str()); audioEngine->playEffect(srcString.c_str(), false); } } } if (node->getCurrentFrame() == node->getFrameCount() - 1) { audioEngine->stopAllEffects(); if (m_stopBgMusic) { audioEngine->stopBackgroundMusic(true); } CCObject * effectFile = NULL; CCARRAY_FOREACH(m_loadEffects, effectFile){ audioEngine->unloadEffect(((CCString *)effectFile)->getCString()); } m_loadEffects->removeAllObjects(); }