程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> Cocos2d-x中配景音樂和音效應用實例

Cocos2d-x中配景音樂和音效應用實例

編輯:關於C++

Cocos2d-x中配景音樂和音效應用實例。本站提示廣大學習愛好者:(Cocos2d-x中配景音樂和音效應用實例)文章只能為提供參考,不一定能成為您想要的結果。以下是Cocos2d-x中配景音樂和音效應用實例正文


游戲中的音樂和音效是必弗成少的,好的音樂可以給我們留下很深的印象也決議了這個游戲的品德。明天我們來進修一下cocos2d-x中若何應用音樂和音效。這些用到的函數都比擬簡略,如今直接上代碼。

//聲響事後處置類的頭文件
#ifndef _AUDIO_PRETREAT_H
#define _AUDIO_PRETREAT_H
#include "cocos2d.h"
//應用聲響引擎必需包括SimpleAudioEngeim.h頭文件
#include "SimpleAudioEngine.h"

using namespace cocos2d;
//聲響引擎的定名空間
using namespace CocosDenshion;

//類AudioPretrea采取單例設計形式

//把用到的音樂做為列舉類型
enum MUSIC_TYPE
{
	MUSIC_TYPE_BACKGROUND //配景音樂
};

//把用到的音樂做為列舉類型
enum EFFECT_TYPE
{
	EFFECT_TYPE_EFFECT1
};

class AudioPretreat
{
private:
	static AudioPretreat * m_audioPretreat;
public:
	//單例設計的時刻須要用到的倆個函數
	static AudioPretreat * sharedAudioPretreat();
	static void freeAudioPretreat();
	//可以依據分歧的平台獲得分歧的聲響文件
	char * getMusic(MUSIC_TYPE type);
	char * getEffect(EFFECT_TYPE type);
	//在init中完成聲響的事後加載
	bool init();
};

#endif
//聲響事後處置類的完成
#include "AudioPretreat.h"
//單例設計形式采取的習用方法
AudioPretreat * AudioPretreat::m_audioPretreat = NULL;

AudioPretreat * AudioPretreat::sharedAudioPretreat()
{
	if(NULL == m_audioPretreat)
	{
		m_audioPretreat = new AudioPretreat();
	}
	return m_audioPretreat;
}

//在AppDelegate析構的時刻挪用這個函數
void AudioPretreat::freeAudioPretreat()
{
	if(m_audioPretreat)
	{
		delete m_audioPretreat;
		m_audioPretreat = NULL;
	}
}

//依據分歧的平台取得分歧的聲響文件
char * AudioPretreat::getMusic(MUSIC_TYPE type)
{
	switch(type)
	{
	case MUSIC_TYPE_BACKGROUND:
		//WIN32,ANDROID,IOS都應用MP3格局的配景音樂
#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	return "background-music.mp3";
#else(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	return "background-music.mp3";
#endif
	break;
	}
	return NULL;
}

//依據分歧的平台取得分歧的音效文件
char * AudioPretreat::getEffect(EFFECT_TYPE type)
{
	switch(type)
	{
	case EFFECT_TYPE_EFFECT1:
#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
		return "pew-pew-lei.wav";
#else(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
		return "pew-pew-lei.ogg";
#endif
	}
	return NULL;
}

//init函數中完成一些初始化的操作
bool AudioPretreat::init()
{
	bool bRet = false;
	do
	{
		//事後加載聲響,參數是聲響文件
		SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(this->getMusic(MUSIC_TYPE_BACKGROUND));
		//加載音效
		SimpleAudioEngine::sharedEngine()->preloadEffect(this->getEffect(EFFECT_TYPE_EFFECT1));
		//設置聲響的年夜小0.0-1.0
		SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.1f);
		bRet = true;
	}
	while(0);

	return bRet;
}
// 上面就來看一下若何應用聲響引擎
bool HelloWorld::init()
{
  bool bRet = false;
  do
  {

    CC_BREAK_IF(! CCLayer::init());

		CCSize size = CCDirector::sharedDirector()->getVisibleSize();

		AudioPretreat::sharedAudioPretreat()->init();

		//以下是對配景音樂的一些操作

		//設置字符串,而且轉碼
		std::string str1 = "播放配景音樂";
		std::string str2 = "停滯配景音樂";
		std::string str3 = "暫停配景音樂";
		std::string str4 = "恢復配景音樂";
		std::string str5 = "重頭播放配景音樂";
		std::string str6 = "配景音樂:加";
		std::string str7 = "配景音樂:減";
		GBKToUTF8(str1,"GB2312","UTF-8");
		GBKToUTF8(str2,"GB2312","UTF-8");
		GBKToUTF8(str3,"GB2312","UTF-8");
		GBKToUTF8(str4,"GB2312","UTF-8");
		GBKToUTF8(str5,"GB2312","UTF-8");
		GBKToUTF8(str6,"GB2312","UTF-8");
		GBKToUTF8(str7,"GB2312","UTF-8");

		CCMenuItemFont::setFontSize(24);
		CCMenuItemFont::setFontName("Arial");
		CCMenuItemFont * menu1 = CCMenuItemFont::create(str1.c_str(),this,menu_selector(HelloWorld::menu));
		menu1->setTag(1);
		CCMenuItemFont * menu2 = CCMenuItemFont::create(str2.c_str(),this,menu_selector(HelloWorld::menu));
		menu2->setTag(2);
		CCMenuItemFont * menu3 = CCMenuItemFont::create(str3.c_str(),this,menu_selector(HelloWorld::menu));
		menu3->setTag(3);
		CCMenuItemFont * menu4 = CCMenuItemFont::create(str4.c_str(),this,menu_selector(HelloWorld::menu));
		menu4->setTag(4);
		CCMenuItemFont * menu5 = CCMenuItemFont::create(str5.c_str(),this,menu_selector(HelloWorld::menu));
		menu5->setTag(5);
		CCMenuItemFont * menu6 = CCMenuItemFont::create(str6.c_str(),this,menu_selector(HelloWorld::menu));
		menu6->setTag(6);
		CCMenuItemFont * menu7 = CCMenuItemFont::create(str7.c_str(),this,menu_selector(HelloWorld::menu));
		menu7->setTag(7);

		CCMenu * leftMenu = CCMenu::create(menu1,menu2,menu3,menu4,menu5,menu6,menu7,NULL);
		leftMenu->alignItemsVerticallyWithPadding(5);
		leftMenu->setPositionX(size.width/4);
		this->addChild(leftMenu);

		//以下是對音效的操作
		std::string str11 = "播放音效";
		std::string str12 = "停滯音效";
		std::string str13 = "暫停音效";
		std::string str14 = "恢復音效";
		std::string str15 = "從緩存中移除音效";
		std::string str16 = "音效:加";
		std::string str17 = "音效:減";
		GBKToUTF8(str11,"GB2312","UTF-8");
		GBKToUTF8(str12,"GB2312","UTF-8");
		GBKToUTF8(str13,"GB2312","UTF-8");
		GBKToUTF8(str14,"GB2312","UTF-8");
		GBKToUTF8(str15,"GB2312","UTF-8");
		GBKToUTF8(str16,"GB2312","UTF-8");
		GBKToUTF8(str17,"GB2312","UTF-8");

		CCMenuItemFont * menu11 = CCMenuItemFont::create(str11.c_str(),this,menu_selector(HelloWorld::menu));
		menu11->setTag(11);
		CCMenuItemFont * menu12 = CCMenuItemFont::create(str12.c_str(),this,menu_selector(HelloWorld::menu));
		menu12->setTag(12);
		CCMenuItemFont * menu13 = CCMenuItemFont::create(str13.c_str(),this,menu_selector(HelloWorld::menu));
		menu13->setTag(13);
		CCMenuItemFont * menu14 = CCMenuItemFont::create(str14.c_str(),this,menu_selector(HelloWorld::menu));
		menu14->setTag(14);
		CCMenuItemFont * menu15 = CCMenuItemFont::create(str15.c_str(),this,menu_selector(HelloWorld::menu));
		menu15->setTag(15);
		CCMenuItemFont * menu16 = CCMenuItemFont::create(str16.c_str(),this,menu_selector(HelloWorld::menu));
		menu16->setTag(16);
		CCMenuItemFont * menu17 = CCMenuItemFont::create(str17.c_str(),this,menu_selector(HelloWorld::menu));
		menu17->setTag(17);

		CCMenu * rightMenu = CCMenu::create(menu11,menu12,menu13,menu14,menu15,menu16,menu17,NULL);
		rightMenu->alignItemsVerticallyWithPadding(5);
		rightMenu->setPositionX(size.width*3/4);
		this->addChild(rightMenu);

    bRet = true;
  } while (0);

  return bRet;
}

void HelloWorld::menu(CCObject * sender)
{
	CCNode * node = (CCNode *)sender;
	int tag = node->getTag();
	switch(tag)
	{
	case 1:
		//播放聲響,第二個參數代表能否輪回播放
		SimpleAudioEngine::sharedEngine()->playBackgroundMusic(
			AudioPretreat::sharedAudioPretreat()->getMusic(MUSIC_TYPE_BACKGROUND),true);
		break;
	case 2:
		SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
		break;
	case 3:
		//pause和resume成對應用
		SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
		break;
	case 4:
		SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
		break;
	case 5:
		SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();
		break;
	case 6:
		//這個函數在win32上沒法轉變聲響的年夜小,年夜家用f12跟進函數的時刻裡邊是空,然則在android平台上有完成
		SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(
			SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume()+0.1f);
		break;
	case 7:
		//這個函數在win32上沒法轉變聲響的年夜小,年夜家用f12跟進函數的時刻裡邊是空,然則在android平台上有完成
		SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(
			SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume()-0.1f);
		break;
	case 11:
		//第二個參數設為true的話則輪回播放,soundID是在HelloWorld的頭文件中界說的
		//音效函數分歧於配景音樂的是會前往一個ID
		soundID = SimpleAudioEngine::sharedEngine()->playEffect(
			AudioPretreat::sharedAudioPretreat()->getEffect(EFFECT_TYPE_EFFECT1),true);
		break;
	case 12:
		//音效函數年夜多會傳入一個ID,由於游戲中的音效不止一個
		SimpleAudioEngine::sharedEngine()->stopEffect(soundID);
		break;
	case 13:
		SimpleAudioEngine::sharedEngine()->pauseEffect(soundID);
		break;
	case 14:
		SimpleAudioEngine::sharedEngine()->resumeEffect(soundID);
	case 15:
		//從緩存中移除音效,須要傳入移除的音效的名字
		SimpleAudioEngine::sharedEngine()->unloadEffect(
			AudioPretreat::sharedAudioPretreat()->getEffect(EFFECT_TYPE_EFFECT1));
	case 16:
		SimpleAudioEngine::sharedEngine()->setEffectsVolume(
			SimpleAudioEngine::sharedEngine()->getEffectsVolume()+0.1);
		break;
	case 17:
		SimpleAudioEngine::sharedEngine()->setEffectsVolume(
			SimpleAudioEngine::sharedEngine()->getEffectsVolume()-0.1);
		break;
	}
}
//編碼格局轉化函數
int GBKToUTF8(std::string & gbkStr, const char* toCode, const char* fromCode)
{
	iconv_t iconvH;
	iconvH = iconv_open(fromCode, toCode);

	if (iconvH == 0)

	{
		return -1;
	}

	const char* strChar = gbkStr.c_str();
	const char** pin = &strChar;
	size_t strLength = gbkStr.length();
	char* outbuf = (char*) malloc(strLength*4);
	char* pBuff = outbuf;

	memset( outbuf, 0, strLength*4);
	size_t outLength = strLength*4;

	if (-1 == iconv(iconvH, pin, &strLength, &outbuf, &outLength))
	{
		iconv_close(iconvH);
		return -1;
	}

	gbkStr = pBuff;
	iconv_close(iconvH);

	return 0;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved