cocos2d学习笔记(五)CCMenu与CocosDenshion

20 篇文章 0 订阅

一、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住程序,所以不要在主线程中进行声音的预加载

[soundEnginepreloadBackgroundMusic:trackFileName];

[soundEngineplayBackgroundMusic:trackFileNameloop:YES];

[CDSoundEngine setMixerSampleRate:CD_SAMPLE_RATE_MID];
设置采样率,可以在CocosDenshion.h中看到各种采样率的宏定义,可以根据自己的声音文件来设置,注意:当你声音文件低于设置的采样率时,会使用设置的采样率而产生内存的浪费。

    [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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值