HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "SimpleAudioEngine.h"
class HelloWorld : public cocos2d::CCLayer
{
public:
// Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
virtual bool init();
// there's no 'id' in cpp, so we recommend to return the class instance pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// preprocessor macro for "static create()" constructor ( node() deprecated )
CREATE_FUNC(HelloWorld);
unsigned int nSoundId;//音效id
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d;
using namespace CocosDenshion;
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
//预价值背景音乐
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("地上.mp3");
//预价值背景音乐
SimpleAudioEngine::sharedEngine()->preloadEffect("加一条命.mp3");
SimpleAudioEngine::sharedEngine()->preloadEffect("吃到蘑菇或花.mp3");
CCMenuItemLabel* playBg =CCMenuItemFont::create("播放背景音乐", this, menu_selector(HelloWorld::menuCloseCallback));
playBg->setTag(1);
CCMenuItemLabel* stopBg =CCMenuItemFont::create("停止背景音乐", this, menu_selector(HelloWorld::menuCloseCallback));
stopBg->setTag(2);
CCMenuItemLabel* pauseBg =CCMenuItemFont::create("暂停背景音乐", this, menu_selector(HelloWorld::menuCloseCallback));
pauseBg->setTag(3);
CCMenuItemLabel* resumeBg =CCMenuItemFont::create("恢复背景音乐", this, menu_selector(HelloWorld::menuCloseCallback));
resumeBg->setTag(4);
CCMenuItemLabel* rewindBg =CCMenuItemFont::create("重头播放背景音乐", this, menu_selector(HelloWorld::menuCloseCallback));
rewindBg->setTag(5);
CCMenuItemLabel* jiaBg =CCMenuItemFont::create("背景音乐:加", this, menu_selector(HelloWorld::menuCloseCallback));
jiaBg->setTag(6);
CCMenuItemLabel* jianBg =CCMenuItemFont::create("背景音乐:减", this, menu_selector(HelloWorld::menuCloseCallback));
jianBg->setTag(7);
CCMenu* leftMenu = CCMenu::create(playBg,stopBg,pauseBg,resumeBg,rewindBg,jiaBg,jianBg,NULL);
leftMenu->alignItemsVerticallyWithPadding(5);
leftMenu->setPositionX(CCDirector::sharedDirector()->getWinSize().width/4);
this->addChild(leftMenu);
CCMenuItemLabel* playEffect =CCMenuItemFont::create("播放音效1", this, menu_selector(HelloWorld::menuCloseCallback));
playEffect->setTag(10);
CCMenuItemLabel* stopEffect =CCMenuItemFont::create("停止音效1", this, menu_selector(HelloWorld::menuCloseCallback));
stopEffect->setTag(11);
CCMenuItemLabel* pauseEffect =CCMenuItemFont::create("暂停音效1", this, menu_selector(HelloWorld::menuCloseCallback));
pauseEffect->setTag(12);
CCMenuItemLabel* resumeEffect =CCMenuItemFont::create("恢复音效1", this, menu_selector(HelloWorld::menuCloseCallback));
resumeEffect->setTag(13);
CCMenuItemLabel* unloadEffect =CCMenuItemFont::create("从缓存移除音效1", this, menu_selector(HelloWorld::menuCloseCallback));
unloadEffect->setTag(14);
CCMenuItemLabel* playEffect2 =CCMenuItemFont::create("播放音效2", this, menu_selector(HelloWorld::menuCloseCallback));
playEffect2->setTag(15);
CCMenuItemLabel* stopALLEffect =CCMenuItemFont::create("停止所有音效", this, menu_selector(HelloWorld::menuCloseCallback));
stopALLEffect->setTag(16);
CCMenuItemLabel* pauseAllEffect =CCMenuItemFont::create("暂停所有音效", this, menu_selector(HelloWorld::menuCloseCallback));
pauseAllEffect->setTag(17);
CCMenuItemLabel* resumeALLEffect =CCMenuItemFont::create("恢复所有音效", this, menu_selector(HelloWorld::menuCloseCallback));
resumeALLEffect->setTag(18);
CCMenu* rightMenu = CCMenu::create(playEffect,stopEffect,pauseEffect,resumeEffect,unloadEffect,playEffect2,stopALLEffect,pauseAllEffect,resumeALLEffect,NULL);
rightMenu->alignItemsVerticallyWithPadding(5);
rightMenu->setPositionX(CCDirector::sharedDirector()->getWinSize().width * 3/4);
this->addChild(rightMenu);
return true;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCNode* tagNode = (CCNode*) pSender;
int tag = tagNode->getTag();
switch (tag) {
case 1:
//播放背景音乐 一个参数默认不循环
SimpleAudioEngine::sharedEngine()->playBackgroundMusic("地上.mp3");
break;
case 2:
//停止背景音乐
SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
break;
case 3:
//暂停背景音乐
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
break;
case 4:
//恢复背景音乐
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
break;
case 5:
//重头播放背景音乐
SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();
break;
case 6:
//背景音乐:加
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(
SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume()+0.1f);
break;
case 7:
//背景音乐:减
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(
SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume()-0.1f);
break;
case 10:
//播放音效1 循环
nSoundId = SimpleAudioEngine::sharedEngine()->playEffect("吃到蘑菇或花.mp3", true);
break;
case 11:
//停止音效1
SimpleAudioEngine::sharedEngine()->stopEffect(nSoundId);
break;
case 12:
//暂停音效1
SimpleAudioEngine::sharedEngine()->pauseEffect(nSoundId);
break;
case 13:
//恢复音效1
SimpleAudioEngine::sharedEngine()->resumeEffect(nSoundId);
break;
case 14:
//从缓存移除音效1
SimpleAudioEngine::sharedEngine()->unloadEffect("吃到蘑菇或花.mp3");
break;
case 15:
//播放音效2
nSoundId = SimpleAudioEngine::sharedEngine()->playEffect("加一条命.mp3"); break;
case 16:
//停止所有音效
SimpleAudioEngine::sharedEngine()->stopAllEffects();
break;
case 17:
//暂停所有音效
SimpleAudioEngine::sharedEngine()->pauseAllEffects();
break;
case 18:
//恢复所有音效
SimpleAudioEngine::sharedEngine()->resumeAllEffects();
break;
default:
break;
}