Exemple #1
0
void MainLayer::onPlay(CCObject* pSender)
{
    CocosDenshion::SimpleAudioEngine *audioEngine = CocosDenshion::SimpleAudioEngine::sharedEngine();
    audioEngine->playBackgroundMusic("background.mp3",true);

    CCDirector::sharedDirector()->replaceScene(PlayLayer::scene());
}
Exemple #2
0
void GameScene::startBackgroundMusic()
{
    CocosDenshion::SimpleAudioEngine* instance = CocosDenshion::SimpleAudioEngine::sharedEngine();
    instance->stopBackgroundMusic();
    
    
    if(GameData::isMusicSound)
    {
        instance->playBackgroundMusic("game.wav", true);
    }
}
Exemple #3
0
void HelloWorld::menuMusicCallback(CCObject* pSender)
{
	CocosDenshion::SimpleAudioEngine* audio = CocosDenshion::SimpleAudioEngine::sharedEngine();
	if(!audio->isBackgroundMusicPlaying())
	{
		audio->playBackgroundMusic("akon.mp3", true);
	}
	else
	{
		audio->stopBackgroundMusic();
	}
}
int lua_cocos2dx_cocosdenshion_SimpleAudioEngine_playBackgroundMusic(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_playBackgroundMusic'", 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:playBackgroundMusic"); arg0 = arg0_tmp.c_str();
        if(!ok)
        {
            tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_cocosdenshion_SimpleAudioEngine_playBackgroundMusic'", nullptr);
            return 0;
        }
        cobj->playBackgroundMusic(arg0);
        lua_settop(tolua_S, 1);
        return 1;
    }
    if (argc == 2) 
    {
        const char* arg0;
        bool arg1;

        std::string arg0_tmp; ok &= luaval_to_std_string(tolua_S, 2, &arg0_tmp, "cc.SimpleAudioEngine:playBackgroundMusic"); arg0 = arg0_tmp.c_str();

        ok &= luaval_to_boolean(tolua_S, 3,&arg1, "cc.SimpleAudioEngine:playBackgroundMusic");
        if(!ok)
        {
            tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_cocosdenshion_SimpleAudioEngine_playBackgroundMusic'", nullptr);
            return 0;
        }
        cobj->playBackgroundMusic(arg0, arg1);
        lua_settop(tolua_S, 1);
        return 1;
    }
    luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "cc.SimpleAudioEngine:playBackgroundMusic",argc, 1);
    return 0;

#if COCOS2D_DEBUG >= 1
    tolua_lerror:
    tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_cocosdenshion_SimpleAudioEngine_playBackgroundMusic'.",&tolua_err);
#endif

    return 0;
}
Scene* SceneManager::generateLevel(int levelIndex, int score)
{
	if (levelIndex == 0) {
		Director* director = Director::getInstance();

		float height = director->getWinSize().height;
		float width = director->getWinSize().width;

		// Create start scene
		ParallaxBackground* bckMenu = new ParallaxBackground();
		bckMenu->addImage("night.png", Vec2(width / 2, height / 1.3), Vec2(0.05, 0));
		bckMenu->addImage("city.png", Vec2(width / 2, height / 1.6), Vec2(0.3, 0));
		bckMenu->addImage("street.png", Vec2(width / 2, height / 2.6), Vec2(1.0, 0), true);
		bckMenu->scheduleUpdate();

		auto startScene = IntermediaryScene::create(IntermediaryScene::MENU);
		startScene->setBackground(bckMenu);

		// Create dummy player
		auto dummy = DummyPlayer::create();
		dummy->setPosition(Vec2(width / 2, 130));
		startScene->setPlayer(dummy);

		// Create music
		CocosDenshion::SimpleAudioEngine* audioEngine = CocosDenshion::SimpleAudioEngine::getInstance();
		audioEngine->playBackgroundMusic("level0.mp3", true);

		Label* playButtonLabel = Label::createWithTTF("Play", "font.ttf", 35);
		MenuItem* playButton = MenuItemLabel::create(playButtonLabel, [&](Ref* sender){SceneManager::getInstance().fillStack();});

		Label* optionsButtonLabel = Label::createWithTTF("Options", "font.ttf", 35);
		MenuItem* optionsButton = MenuItemLabel::create(optionsButtonLabel);

		Label* exitButtonLabel = Label::createWithTTF("Exit", "font.ttf", 35);
		MenuItem* exitButton = MenuItemLabel::create(exitButtonLabel, [&](Ref* sender){Director::getInstance()->end(); });

		startScene->addMenuItem(playButton);
		startScene->addMenuItem(optionsButton);
		startScene->addMenuItem(exitButton);
		startScene->createMenu();

		return startScene;
	}
	if (levelIndex == 1) {
		Director* director = Director::getInstance();

		// Create a new GameLayer
		GameLayer* firstLevelLayer = GameLayer::create();

		// Create a background
		ParallaxBackground* bckFirstLevel = new ParallaxBackground();

		float height = director->getWinSize().height;
		float width = director->getWinSize().width;

		// Create animations and bullets
		bckFirstLevel->addImage("night.png", Vec2(width / 2, height / 1.3), Vec2(0.05, 0));
		bckFirstLevel->addImage("city.png", Vec2(width / 2, height / 1.6), Vec2(0.3, 0));
		bckFirstLevel->addImage("street.png", Vec2(width / 2, height / 2.6), Vec2(1.0, 0),true);
		bckFirstLevel->scheduleUpdate();

		// Create music
		CocosDenshion::SimpleAudioEngine* audioEngine = CocosDenshion::SimpleAudioEngine::getInstance();
		audioEngine->playBackgroundMusic("level1.mp3", true);

		// Create player
		Player* hero = Player::create();
		hero->setPosition(Vec2(width / 2, 130));
		hero->setTag(PLAYER_TAG);
		hero->setScore(score);

		// Create boss
		Boss* boss = Boss::create(OBJECT_FIRSTBOSS);

		BossCannon* cannon1 = BossCannon::create(OBJECT_FIRSTBOSS_CANNON_1, OBJECT_FIRSTBOSS_CANNON_1_D, OBJECT_BOSSBULLET_LASER);
		cannon1->setPosition(Vec2(123, 105));
		cannon1->setFireMethod(1, 10, 50);

		BossCannon* cannon2 = BossCannon::create(OBJECT_FIRSTBOSS_CANNON_2, OBJECT_FIRSTBOSS_CANNON_2_D, OBJECT_BOSSBULLET_SPIKEBALL);
		cannon2->setPosition(Vec2(150, 183));
		cannon2->setFireMethod(2, 10, 50);

		BossCannon* cannon3 = BossCannon::create(OBJECT_FIRSTBOSS_CANNON_3, OBJECT_FIRSTBOSS_CANNON_3_D, OBJECT_BOSSBULLET_BALL);
		cannon3->setPosition(Vec2(59, 148));
		cannon3->setFireMethod(3, 3, 50);

		boss->addCannon(1, cannon2);
		boss->addCannon(2, cannon1);
		boss->addCannon(3, cannon3);

		// Create Interactive object factory
		InteractiveObjectFactory* mailboxFactory = InteractiveObjectFactory::create(OBJECT_MAILBOX, director->getWinSize().height * 1.6 / 800, false, MAILBOX_COLLISION_BITMASK, true, false, true);
		mailboxFactory->setPositionInterval(Vec2(height / GROUND_PERCENTAGE_FOR_BOX + 10, height / GROUND_PERCENTAGE_FOR_BOX + 10));
		mailboxFactory->setSpawnFrequency(5);
		mailboxFactory->setSpeed(Vec2(-(Director::getInstance()->getWinSize().width * 10.0 / 800), 0));

		InteractiveObjectFactory* rocketFactory = InteractiveObjectFactory::create(OBJECT_ROCKET, director->getWinSize().height * 1.6 / 800, false, ROCKET_COLLISION_BITMASK, true, false, false);
		rocketFactory->setPositionInterval(Vec2(100, height));
		rocketFactory->setSpeed(Vec2(-(Director::getInstance()->getWinSize().width * 25.0 / 800), 0));
		rocketFactory->setSpawnFrequency(20);

		InteractiveObjectFactory* spikesFactory = InteractiveObjectFactory::create(OBJECT_SPIKES, director->getWinSize().height * 0.5 / 800, false, SPIKES_COLLISION_BITMASK, false, true, true);
		spikesFactory->setPositionInterval(Vec2(height / GROUND_PERCENTAGE_FOR_BOX + 10, height / GROUND_PERCENTAGE_FOR_BOX + 10));
		spikesFactory->setSpawnFrequency(5);
		spikesFactory->setSpeed(Vec2(-(Director::getInstance()->getWinSize().width * 10.0 / 800), 0));


		firstLevelLayer->setBackground(bckFirstLevel);
		firstLevelLayer->setPlayer(hero);
		firstLevelLayer->setBoss(boss);
		firstLevelLayer->setDistanceToBoss(1000);
		firstLevelLayer->addObjectFactory(mailboxFactory);
		firstLevelLayer->addObjectFactory(rocketFactory);
		firstLevelLayer->addObjectFactory(spikesFactory);

		//Create Scene
		GameLevel* firstLevel = GameLevel::create(firstLevelLayer);

		return firstLevel;
	}
	if (levelIndex == 2)
	{
		Director* director = Director::getInstance();

		// Create a new GameLayer
		GameLayer* secondLevelLayer = GameLayer::create();

		// Create a background
		ParallaxBackground* bckSecondLevel = new ParallaxBackground();

		float height = director->getWinSize().height;
		float width = director->getWinSize().width;

		// Create animations and bullets
		bckSecondLevel->addImage("night.png", Vec2(width / 2, height / 1.3), Vec2(0.05, 0));
		bckSecondLevel->addImage("city.png", Vec2(width / 2, height / 1.6), Vec2(0.3, 0));
		bckSecondLevel->addImage("street.png", Vec2(width / 2, height / 2.6), Vec2(1.0, 0), true);
		bckSecondLevel->scheduleUpdate();

		// Create music
		CocosDenshion::SimpleAudioEngine* audioEngine = CocosDenshion::SimpleAudioEngine::getInstance();
		audioEngine->playBackgroundMusic("level2.mp3", true);

		// Create player
		Player* hero = Player::create();
		hero->setPosition(Vec2(width / 2, 130));
		hero->setTag(PLAYER_TAG);
		hero->setScore(score);

		// Create boss
		Boss* boss = Boss::create(OBJECT_SECONDBOSS,true);

		BossCannon* cannon1 = BossCannon::create(OBJECT_SECONDBOSS_CANNON_1, OBJECT_SECONDBOSS_CANNON_1_D, OBJECT_BOSSBULLET_ENERGYBALL);
		cannon1->setPosition(Vec2(85, 38));
		cannon1->setFireMethod(1, 10, 50);

		BossCannon* cannon2 = BossCannon::create(OBJECT_SECONDBOSS_CANNON_2, OBJECT_SECONDBOSS_CANNON_2_D, OBJECT_BOSSBULLET_SAWBLADE);
		cannon2->setPosition(Vec2(34, 137));
		cannon2->setFireMethod(2, 10, 50);

		BossCannon* cannon3 = BossCannon::create(OBJECT_SECONDBOSS_CANNON_3, OBJECT_SECONDBOSS_CANNON_3_D, OBJECT_BOSSBULLET_FLAMEBALL);
		cannon3->setPosition(Vec2(20, 135));
		cannon3->setFireMethod(3, 3, 50);

		boss->addCannon(1, cannon1);
		boss->addCannon(2, cannon2);
		boss->addCannon(3, cannon3);

		// Create Interactive object factory
		InteractiveObjectFactory* mailboxFactory = InteractiveObjectFactory::create(OBJECT_MAILBOX, director->getWinSize().height * 1.6 / 800, false, MAILBOX_COLLISION_BITMASK, true, false, true);
		mailboxFactory->setPositionInterval(Vec2(height / GROUND_PERCENTAGE_FOR_BOX + 10, height / GROUND_PERCENTAGE_FOR_BOX + 10));
		mailboxFactory->setSpawnFrequency(5);
		mailboxFactory->setSpeed(Vec2(-(Director::getInstance()->getWinSize().width * 10.0 / 800), 0));

		InteractiveObjectFactory* rocketFactory = InteractiveObjectFactory::create(OBJECT_ROCKET, director->getWinSize().height * 1.6 / 800, false, ROCKET_COLLISION_BITMASK, true, false, false);
		rocketFactory->setPositionInterval(Vec2(100, height));
		rocketFactory->setSpeed(Vec2(-(Director::getInstance()->getWinSize().width * 25.0 / 800), 0));
		rocketFactory->setSpawnFrequency(20);

		InteractiveObjectFactory* spikesFactory = InteractiveObjectFactory::create(OBJECT_SPIKES, director->getWinSize().height * 0.5 / 800, false, SPIKES_COLLISION_BITMASK, false, true, true);
		spikesFactory->setPositionInterval(Vec2(height / GROUND_PERCENTAGE_FOR_BOX + 10, height / GROUND_PERCENTAGE_FOR_BOX + 10));
		spikesFactory->setSpawnFrequency(5);
		spikesFactory->setSpeed(Vec2(-(Director::getInstance()->getWinSize().width * 10.0 / 800), 0));


		secondLevelLayer->setBackground(bckSecondLevel);
		secondLevelLayer->setPlayer(hero);
		secondLevelLayer->setBoss(boss);
		secondLevelLayer->setDistanceToBoss(100);
		secondLevelLayer->addObjectFactory(mailboxFactory);
		secondLevelLayer->addObjectFactory(rocketFactory);
		secondLevelLayer->addObjectFactory(spikesFactory);

		//Create Scene
		GameLevel* firstLevel = GameLevel::create(secondLevelLayer);

		return firstLevel;
	}
	if (levelIndex == 3)
	{
		Director* director = Director::getInstance();

		// Create a new GameLayer
		GameLayer* thirdLevelLayer = GameLayer::create();

		// Create a background
		ParallaxBackground* bckThirdLevel = new ParallaxBackground();

		float height = director->getWinSize().height;
		float width = director->getWinSize().width;

		// Create animations and bullets
		bckThirdLevel->addImage("night.png", Vec2(width / 2, height / 1.3), Vec2(0.05, 0));
		bckThirdLevel->addImage("city.png", Vec2(width / 2, height / 1.6), Vec2(0.3, 0));
		bckThirdLevel->addImage("street.png", Vec2(width / 2, height / 2.6), Vec2(1.0, 0), true);
		bckThirdLevel->scheduleUpdate();

		// Create music
		CocosDenshion::SimpleAudioEngine* audioEngine = CocosDenshion::SimpleAudioEngine::getInstance();
		audioEngine->playBackgroundMusic("level3.mp3", true);

		// Create player
		Player* hero = Player::create();
		hero->setPosition(Vec2(width / 2, 130));
		hero->setTag(PLAYER_TAG);
		hero->setScore(score);

		// Create boss
		Boss* boss = Boss::create(OBJECT_THIRDBOSS);

		BossCannon* cannon1 = BossCannon::create(OBJECT_THIRDBOSS_CANNON_1, OBJECT_THIRDBOSS_CANNON_1_D, OBJECT_BOSSBULLET_BOMB);
		cannon1->setPosition(Vec2(68, 218));
		cannon1->setFireMethod(2, 10, 10);

		BossCannon* cannon2 = BossCannon::create(OBJECT_THIRDBOSS_CANNON_2, OBJECT_THIRDBOSS_CANNON_2_D, OBJECT_BOSSBULLET_RASENGAN);
		cannon2->setPosition(Vec2(28, 148));
		cannon2->setFireMethod(3, 1, 10);

		boss->addCannon(1, cannon1);
		boss->addCannon(2, cannon2);

		// Create Interactive object factory
		InteractiveObjectFactory* mailboxFactory = InteractiveObjectFactory::create(OBJECT_MAILBOX, director->getWinSize().height * 1.6 / 800, false, MAILBOX_COLLISION_BITMASK, true, false, true);
		mailboxFactory->setPositionInterval(Vec2(height / GROUND_PERCENTAGE_FOR_BOX + 10, height / GROUND_PERCENTAGE_FOR_BOX + 10));
		mailboxFactory->setSpawnFrequency(5);
		mailboxFactory->setSpeed(Vec2(-(Director::getInstance()->getWinSize().width * 10.0 / 800), 0));

		InteractiveObjectFactory* rocketFactory = InteractiveObjectFactory::create(OBJECT_ROCKET, director->getWinSize().height * 1.6 / 800, false, ROCKET_COLLISION_BITMASK, true, false, false);
		rocketFactory->setPositionInterval(Vec2(100, height));
		rocketFactory->setSpeed(Vec2(-(Director::getInstance()->getWinSize().width * 25.0 / 800), 0));
		rocketFactory->setSpawnFrequency(20);

		InteractiveObjectFactory* spikesFactory = InteractiveObjectFactory::create(OBJECT_SPIKES, director->getWinSize().height * 0.5 / 800, false, SPIKES_COLLISION_BITMASK, false, true, true);
		spikesFactory->setPositionInterval(Vec2(height / GROUND_PERCENTAGE_FOR_BOX + 10, height / GROUND_PERCENTAGE_FOR_BOX + 10));
		spikesFactory->setSpawnFrequency(5);
		spikesFactory->setSpeed(Vec2(-(Director::getInstance()->getWinSize().width * 10.0 / 800), 0));

		thirdLevelLayer->setBackground(bckThirdLevel);
		thirdLevelLayer->setPlayer(hero);
		thirdLevelLayer->setBoss(boss);
		thirdLevelLayer->setDistanceToBoss(1000);
		thirdLevelLayer->addObjectFactory(mailboxFactory);
		thirdLevelLayer->addObjectFactory(rocketFactory);
		thirdLevelLayer->addObjectFactory(spikesFactory);

		//Create Scene
		GameLevel* firstLevel = GameLevel::create(thirdLevelLayer);

		return firstLevel;
	}

	return nullptr;
}
Exemple #6
0
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();
        }