//
// HelloWorldScene.h
//
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
class HelloWorld : public cocos2d::CCLayer
{
public:
virtual bool init();
static cocos2d::CCScene* scene();
CREATE_FUNC(HelloWorld);
void menuCallBack(CCObject *pSender);
};
#endif
//
// HelloWorldScene.cpp
//
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;
using namespace std;
using namespace CocosDenshion;
// 根据不同平台使用预编译索引不同的音频文件
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#define EFFECT_FILE "effect2.ogg"
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
#define EFFECT_FILE "effect1.raw"
#else
#define EFFECT_FILE "effect1.wav"
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#define MUSIC_FILE "music.mid"
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY)
#define MUSIC_FILE "background.ogg"
#else
#define MUSIC_FILE "background.mp3"
#endif
string items[] = {"播放背景音乐", "停止背景音乐", "暂停背景音乐", "继续播放背景音乐", "后退背景音乐", "背景音乐是否在播放", "播放音效", "重复播放音效", "停止播放音效", "释放音效", "增加背景音乐音量", "减少背景音乐音量", "增加音效音量", "减少音效音量", "暂停音效", "继续播放音效", "暂停所有音效", "继续所有音效", "停止所有音效"};
int effectID = -1;
CCScene* HelloWorld::scene()
{
CCScene *scene = CCScene::create();
HelloWorld *layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
// 图层
bool HelloWorld::init()
{
if ( !CCLayer::init() )
{
return false;
}
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCMenu *pMenu = CCMenu::create();
int itemsCount = sizeof(items) / sizeof(items[0]);
for (int i = 0; i < itemsCount; i ++)
{
CCLabelTTF *plabTTF = CCLabelTTF::create(items[i].c_str(), "Arial", 15);
CCMenuItemLabel *pmenuLab = CCMenuItemLabel::create(plabTTF, this, menu_selector(HelloWorld::menuCallBack));
pmenuLab->setPosition(ccp(0, (winSize.height*0.5-20-(i+1)*15)));
pMenu->addChild(pmenuLab, 0, i);
}
this->addChild(pMenu,0,100);
// 预加载音乐和音效
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(MUSIC_FILE);
SimpleAudioEngine::sharedEngine()->preloadEffect(EFFECT_FILE);
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);
SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5);
return true;
}
void HelloWorld::menuCallBack(cocos2d::CCObject *pSender)
{
CCLOG("HelloWorld::menuCallBack");
CCMenuItem *pItem = (CCMenuItem *)pSender;
int nIdx = pItem->getTag();
switch (nIdx)
{
case 0:
SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE, true);
break;
case 1:
SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(true);
break;
case 2:
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
break;
case 3:
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
break;
case 4:
SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();
break;
case 5:
if (SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying())
{
CCLOG("背景音乐正在播放!");
}
else
{
CCLOG("背景音乐没在播放!");
}
break;
case 6:
effectID = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE);
break;
case 7:
effectID = SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE, true);
break;
case 8:
SimpleAudioEngine::sharedEngine()->stopEffect(effectID);
break;
case 9:
SimpleAudioEngine::sharedEngine()->unloadEffect(EFFECT_FILE);
break;
case 10:
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() + 0.1f);
break;
case 11:
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() - 0.1f);
break;
case 12:
SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() + 0.1f);
break;
case 13:
SimpleAudioEngine::sharedEngine()->setEffectsVolume(SimpleAudioEngine::sharedEngine()->getEffectsVolume() - 0.1f);
break;
case 14:
SimpleAudioEngine::sharedEngine()->pauseEffect(effectID);
break;
case 15:
SimpleAudioEngine::sharedEngine()->resumeEffect(effectID);
break;
case 16:
SimpleAudioEngine::sharedEngine()->pauseAllEffects();
break;
case 17:
SimpleAudioEngine::sharedEngine()->resumeAllEffects();
break;
case 18:
SimpleAudioEngine::sharedEngine()->stopAllEffects();
break;
default:
break;
}
}