注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

熊猫正正的博客

熊猫正正的天空

 
 
 

日志

 
 

cocos2d-音乐与音效(1)  

2013-06-02 13:09:11|  分类: ios游戏开发 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

#import "cocos2d.h"


#import "AppDelegate.h"

#import "IntroLayer.h"

#import "CDAudioManager.h"

#import "CocosDenshion.h"


// This is a shared header

// Don't mind too much what is in this file yet, it'll be explained below.

/** CDAudioManager supports two long audio source channels called left and right

 typedef enum {

 kASC_Left = 0,

 kASC_Right = 1

 } tAudioSourceChannel; */

#define CGROUP_BG       kASC_Left    // Channel for background music

#define CGROUP_EFFECTS  kASC_Right   // Channel for sound effects

#define SND_BG_LOOP 1     // Identifier for background music audio

#define SND_ROCKET   2     // Identifier for click sound effect

#define SND_CANNON  3

#define SND_BLAST   4

// Helper macro for playing sound effects

#define playEffect(__ID__)      [[CDAudioManager sharedManager].soundEngine playSound:__ID__ sourceGroupId:CGROUP_EFFECTS pitch:1.0f pan:0.0f gain:1.0f loop:NO]


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

// Create the main window

window_ = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];



// Create an CCGLView with a RGB565 color buffer, and a depth buffer of 0-bits

CCGLView *glView = [CCGLView viewWithFrame:[window_ bounds]

  pixelFormat:kEAGLColorFormatRGB565 //kEAGLColorFormatRGBA8

  depthFormat:0 //GL_DEPTH_COMPONENT24_OES

preserveBackbuffer:NO

sharegroup:nil

multiSampling:NO

  numberOfSamples:0];


director_ = (CCDirectorIOS*) [CCDirector sharedDirector];


director_.wantsFullScreenLayout = YES;


// Display FSP and SPF

[director_ setDisplayStats:YES];


// set FPS at 60

[director_ setAnimationInterval:1.0/60];


// attach the openglView to the director

[director_ setView:glView];


// for rotation and other messages

[director_ setDelegate:self];


// 2D projection

[director_ setProjection:kCCDirectorProjection2D];

// [director setProjection:kCCDirectorProjection3D];


// Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices

if( ! [director_ enableRetinaDisplay:YES] )

CCLOG(@"Retina Display Not supported");


// Default texture format for PNG/BMP/TIFF/JPEG/GIF images

// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565

// You can change anytime.

[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];


// If the 1st suffix is not found and if fallback is enabled then fallback suffixes are going to searched. If none is found, it will try with the name without suffix.

// On iPad HD  : "-ipadhd", "-ipad",  "-hd"

// On iPad     : "-ipad", "-hd"

// On iPhone HD: "-hd"

CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils];

[sharedFileUtils setEnableFallbackSuffixes:NO]; // Default: NO. No fallback suffixes are going to be used

[sharedFileUtils setiPhoneRetinaDisplaySuffix:@"-hd"]; // Default on iPhone RetinaDisplay is "-hd"

[sharedFileUtils setiPadSuffix:@"-ipad"]; // Default on iPad is "ipad"

[sharedFileUtils setiPadRetinaDisplaySuffix:@"-ipadhd"]; // Default on iPad RetinaDisplay is "-ipadhd"


// Assume that PVR images have premultiplied alpha

[CCTexture2D PVRImagesHavePremultipliedAlpha:YES];

    

    

    //启用CDSoundTest

    //SOUND INIT

    

    CDSoundEngine *sse = [CDAudioManager sharedManager].soundEngine;

    

    /**

     A source group is another name for a channel

     Here I have 2 channels, the first index allows for only a single effect... my background music

     The second channel I have reserved for my sound effects.  This is set to 31 because you can

     have up to 32 effects at once

     */

    NSArray *sourceGroups = [NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:31], nil];

    [sse defineSourceGroups:sourceGroups];

    

    //Initialise audio manager asynchronously as it can take a few seconds

    /** Different modes of the engine

     typedef enum {

     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

     } tAudioManagerMode;*/

    [CDAudioManager initAsynchronously:kAMM_FxPlusMusicIfNoOtherAudio];

    

    //Load sound buffers asynchrounously

    NSMutableArray *loadRequests = [[[NSMutableArray alloc] init] autorelease];

    

    /**

     Here we set up an array of sounds to load

     Each CDBufferLoadRequest takes an integer as an identifier (to call later)

     and the file path.  Pretty straightforward here.

     */

    [loadRequests addObject:[[[CDBufferLoadRequest alloc] init:SND_BG_LOOP filePath:@"bgmusic.mp3"] autorelease]];

    [loadRequests addObject:[[[CDBufferLoadRequest alloc] init:SND_ROCKET filePath:@"rocket.mp3"] autorelease]];

    [loadRequests addObject:[[[CDBufferLoadRequest alloc] init:SND_CANNON filePath:@"cannon.mp3"] autorelease]];

    [loadRequests addObject:[[[CDBufferLoadRequest alloc] init:SND_BLAST filePath:@"explosion.mp3"] autorelease]];

    [sse loadBuffersAsynchronously:loadRequests];


// and add the scene to the stack. The director will run it when it automatically when the view is displayed.

[director_ pushScene: [IntroLayer scene]]; 


// Create a Navigation Controller with the Director

navController_ = [[UINavigationController alloc] initWithRootViewController:director_];

navController_.navigationBarHidden = YES;

// set the Navigation Controller as the root view controller

// [window_ addSubview:navController_.view]; // Generates flicker.

[window_ setRootViewController:navController_];

// make main window visible

[window_ makeKeyAndVisible];

return YES;

}

---------------------

#import "HelloWorldLayer.h"

#import "CDAudioManager.h"

#import "CocosDenshion.h"



// 使用以下宏定义来简化播放音乐的代码

/** CDAudioManager 支持左右两个声道

 typedef enum {

 kASC_Left = 0,

 kASC_Right = 1

 } tAudioSourceChannel; */

#define CGROUP_BG       kASC_Left    // 背景音乐所在的声道

#define CGROUP_EFFECTS  kASC_Right   // 音效所在的声道

#define SND_BG_LOOP 1     // 背景音乐的标识

#define SND_ROCKET   2     // 火箭炮音效的标识

#define SND_CANNON  3     //大炮音效的标识

#define SND_BLAST   4    //爆炸音效的标识

// 使用下面的宏来播放音乐

#define playEffect(__ID__)      [[CDAudioManager sharedManager].soundEngine playSound:__ID__ sourceGroupId:CGROUP_EFFECTS pitch:1.0f pan:0.0f gain:1.0f loop:NO]



-(id) init

{

// always call "super" init

// Apple recommends to re-assign "self" with the "super" return value

if( (self=[super init])) {

        

        //使用CCDirector单例对象来获取屏幕大小

        CGSize winSize = [[CCDirector sharedDirector] winSize];

        

        //定义一个字体

        NSString *fontName = @"myfont.fnt";

        

//创建大炮标签,当触碰该标签时,会调用shootCannon方法

CCLabelBMFont *cannonLabel = [CCLabelBMFont labelWithString:@"Cannon" fntFile:fontName];

        CCMenuItemLabel  *cannonItem = [CCMenuItemLabel itemWithLabel:cannonLabel target:self selector:@selector(shootCannon:)];

        

        cannonItem.scale = 0.5;

        cannonItem.position = ccp(winSize.width/2,winSize.height *0.5);

        

        //创建火箭标签,当触碰该标签时,会调用shootRocket方法

        

        CCLabelBMFont *rocketLabel = [CCLabelBMFont labelWithString:@"Rocket" fntFile:fontName];

        CCMenuItemLabel  *rocketItem = [CCMenuItemLabel itemWithLabel:rocketLabel target:self selector:@selector(shootRocket:)];

        

        rocketItem.scale = 0.5;

        rocketItem.position = ccp(winSize.width/2,winSize.height *0.35);

        

        //创建爆炸标签,当触碰该标签时,会调用blast方法

        

        CCLabelBMFont *blastLabel = [CCLabelBMFont labelWithString:@"Blast" fntFile:fontName];

        CCMenuItemLabel  *blastItem = [CCMenuItemLabel itemWithLabel:blastLabel target:self selector:@selector(blast:)];

        

        blastItem.scale = 0.5;

        blastItem.position = ccp(winSize.width/2,winSize.height *0.7);

        

        //创建播放标签,当触碰该标签时,会调用playMusic方法

        

        CCLabelBMFont *playLabel = [CCLabelBMFont labelWithString:@"Play Music" fntFile:fontName];

        CCMenuItemLabel  *playItem = [CCMenuItemLabel itemWithLabel:playLabel target:self selector:@selector(playMusic:)];

        

        playItem.scale = 0.5;

        playItem.position = ccp(winSize.width/2,winSize.height *0.9);

        

        //创建控制菜单,并将以上标签添加进去

        

CCMenu *menu =[CCMenu menuWithItems:cannonItem,rocketItem,blastItem,playItem, nil];

        menu.position = CGPointZero;

        [self addChild:menu];

}

return self;

}


-(void)playMusic:(id)sender{

    

    [[CDAudioManager sharedManager] playBackgroundMusic:@"bgmusic.mp3" loop:YES];

    

}


-(void)blast:(id)sender{

    //   playEffect(SND_BLAST);

    [[CDAudioManager sharedManager].soundEngine playSound:SND_BLAST sourceGroupId:CGROUP_BG pitch:1.0f pan:0.0f gain:1.0f loop:NO];

}



-(void)shootCannon:(id)sender{

    playEffect(SND_CANNON);

}


-(void)shootRocket:(id)sender{

    playEffect(SND_ROCKET);

}


  评论这张
 
阅读(87)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017