一、CCMenu
游戏中的菜单必不可少,CCMenu正是你想要的。
要创建CCMenu,你必须有CCMenuItem对象,CCMenuItem便是你菜单中的某个选项,可以为图片、文字等
cocos2d为menu item提供了必要的转换方法,比如CCMenuItemLabel你可以通过CCLabelBMFont得到;游戏中某些开关,比如声音开关可以用CCMenuItemToggle对象
每一个CCMenuItem都可以在创建的时候绑定某个对象的方法,当这个CCMenuItem被点击的时候会触发这个方法
二、CocosDenshion
cocos2d中集成了CocosDenshion,不过游戏中我们一般都是整个背景音乐或者来点音效,所以我们只需要用SimpleAudioEngine就可以了。
SimpleAudioEngine其实是对CDAudioManager进行了一些封装,我们暂时不去关注细节实现,先看看怎么使用
这是一段通用的代码,用来确保建立SimpleAudioEngine对象成功
- // Indicate that we are trying to start up the Audio Manager
- [CDSoundEngine setMixerSampleRate:CD_SAMPLE_RATE_MID];
- //Init audio manager asynchronously as it can take a few seconds
- //The FXPlusMusicIfNoOtherAudio mode will check if the user is
- // playing music and disable background music playback if
- // that is the case.
- [CDAudioManager initAsynchronously:kAMM_FxPlusMusicIfNoOtherAudio];
- //Wait for the audio manager to initialise
- while ([CDAudioManager sharedManagerState] != kAMStateInitialised)
- {
- [NSThread sleepForTimeInterval:0.1];
- }
- //At this point the CocosDenshion should be initialized
- // Grab the CDAudioManager and check the state
- CDAudioManager *audioManager = [CDAudioManager sharedManager];
- if (audioManager.soundEngine == nil ||
- audioManager.soundEngine.functioning == NO) {
- CCLOG(@"CocosDenshion failed to init, no audio will play.");
- managerSoundState = kAudioManagerFailed;
- } else {
- [audioManager setResignBehavior:kAMRBStopPlay autoHandle:YES];
- soundEngine = [SimpleAudioEngine sharedEngine];
- managerSoundState = kAudioManagerReady;
- CCLOG(@"CocosDenshion is Ready");
- }
当对象成功创建以后,便可以使用这个对象了,预加载的过程会hang住程序,所以不要在主线程中进行声音的预加载
[soundEngine preloadBackgroundMusic:trackFileName];
[soundEngine playBackgroundMusic:trackFileNameloop:YES];
- [CDSoundEngine setMixerSampleRate:CD_SAMPLE_RATE_MID];
- [CDAudioManager initAsynchronously:kAMM_FxPlusMusicIfNoOtherAudio];
kAMM_FxOnly,//!Other apps will be able to play audio
kAMM_FxPlusMusic,//!Only this app will play audio
kAMM_FxPlusMusicIfNoOtherAudio,//!If another app is playing audio at start up then allow it to continue and don't play music
kAMM_MediaPlayback,//!This app takes over audio e.g music player app
kAMM_PlayAndRecord//!App takes over audio and has input and output
都很好懂,就不一一翻译了
1 // 预先加载背景音乐 - init
2 CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("audio_game_back.mp3");
3 // 播放背景音乐 - init
4 CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("audio_game_back.mp3", true);
5 // 预先加载音效 - init
6 CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("audio_card_click.mp3");、
7 // 播放音效 - touch
8 CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("audio_card_click.mp3");
cocosDenshion是cocos2d的音频引擎。对cocos2d工程提供了很方便的声音素材的调用和管理。在0.99.4版本中有了很多相关的扩展。下面对0.99.4版简要说说几个常见的应用。
1. preload:
SimpleAudioEngine *sae = [SimpleAudioEngine sharedEngine]; //定义SimpleAudioEngine的单件实例
[sae preloadEffect:@"dp1.caf"]; //preload的作用在于不需要在使用音频文件时才去内存中读取素材,从而降低卡机的可能性
[sae preloadBackgroundMusic:@"bgm.mp3"]; //背景音乐:与Effect的区别在于可以循环播放
sound1 = [[sae soundSourceForFile:@"dp1.caf"] retain]; //在使用音频时进行retain在增加对文件的引用。
sound3.gain = 0.0f; //音频增益,1.0f是最大
CDSoundSourceFader *sourceFader = [[CDSoundSourceFader alloc] init:sound1 interpolationType:kIT_SCurve startVal:1.0f endVal:0.0f]; //定义音量的渐变:interpolationType是变化的类型,可以是线性,曲线,指数型
[sourceFader setStopTargetWhenComplete:YES]; //?
CDXPropertyModifierAction *faderAction = [CDXPropertyModifierAction actionWithDuration:1.0f modifier:sourceFader]; //将音量渐变作为声音的Modifier
[faderAction retain];
[[SimpleAudioEnginesharedEngine] isBackgroundMusicPlaying] //是否再播放背景音乐
[[SimpleAudioEnginesharedEngine] preloadBackgroundMusic:file_name];//预加载
[[SimpleAudioEnginesharedEngine] playBackgroundMusic:file_name];//播放背景音乐
[[SimpleAudioEnginesharedEngine] pauseBackgroundMusic];//暂停背景音乐
[[SimpleAudioEnginesharedEngine] stopBackgroundMusic];//停止播放背景音乐
[[SimpleAudioEnginesharedEngine] resumeBackgroundMusic];
CocosDenshion下载:http://download.csdn.net/detail/daiyelang/6628605