Imrazor's Blog

Stay Hungry, Stay Foolish

Cocos2d中的动画、字体、菜单和声音

一、Action和Animation。cocos2d提供了格式各样的action和animation

举个例子

1
2
CCAction *moveAction = [CCMoveBy actionWithDuration:2.0f position:ccp(200.0f,0.0f)];
[vikingSprite runAction:moveAction];

假如vikingSprite是我们已经建立好的一个精灵,那么这个精灵就会朝着position的位置移动,两秒后就到指定位置了

一般来讲,精灵的跟动画相关的有CCAction,CCAnimation,CCAnimate三个对象。一般都是先创建CCAnimation对象,通过CCAnimation创建CCAnimate,再通过CCAnimate创建CCAction,最后精灵对象run一下这个action对象,动画就跑起来了

比较常用的几个action有CCRepeatForever,一个无限重复的动作。CCSequence队列执行一个动作,CCSpawn同时执行多个动作,这三个类都是CCAction的子类,多个action可以互相嵌套着用

二、CCLabelTTF或CCLabelBMFont。当我们在游戏中添加文字时,比如分数,金钱之类的,就需要用到CCLabelTTF或CCLabelBMFont了

CCLabelTTF只支持系统的字体,或者你自行添加的ttf字体,用法:

1
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];

CCLabelBMFont是CCSpriteBatchNode的子类,可以理解为一个font atlas,用法

1
CCLabelBMFont *gameBeginLabel = [CCLabelBMFont labelWithString:@"Game Start" fntFile:@"SpaceVikingFont.fnt"];

因为CCLabelTTF和CCLabelBMFont都是CCNode的子类,所以他们也可以通过runAction:做一些动画效果:

1
2
3
4
5
6
7
[gameBeginLabel setPosition:ccp(screenSize.width/2,screenSize.height/2)];
[self addChild:gameBeginLabel];
id labelAction = [CCSpawn actions:
                   [CCScaleBy actionWithDuration:2.0f scale:4],
                   [CCFadeOut actionWithDuration:2.0f],
                 nil];
[gameBeginLabel runAction:labelAction];

另外,他们还有anchor point的概念,锚点范围为(0,0)到(1,1)之间,默认是(0.5,0.5),对象在变化的时候会以锚点为基准进行,如果锚点设为(0,0),那么上面放大的动画会朝向右上的位置进行放大

三、CCMenu。游戏中的菜单必不可少,CCMenu正是你想要的。 要创建CCMenu,你必须有CCMenuItem对象,CCMenuItem便是你菜单中的某个选项,可以为图片、文字等 cocos2d为menu item提供了必要的转换方法,比如CCMenuItemLabel你可以通过CCLabelBMFont得到;游戏中某些开关,比如声音开关可以用CCMenuItemToggle对象 每一个CCMenuItem都可以在创建的时候绑定某个对象的方法,当这个CCMenuItem被点击的时候会触发这个方法

四、CocosDenshion。cocos2d中集成了CocosDenshion,不过游戏中我们一般都是整个背景音乐或者来点音效,所以我们只需要用SimpleAudioEngine就可以了。

SimpleAudioEngine其实是对CDAudioManager进行了一些封装,我们暂时不去关注细节实现,先看看怎么使用 这是一段通用的代码,用来确保建立SimpleAudioEngine对象成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 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住程序,所以不要在主线程中进行声音的预加载

1
2
[soundEnginepreloadBackgroundMusic:trackFileName];
[soundEngineplayBackgroundMusic:trackFileNameloop:YES];

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

1
[CDSoundEngine setMixerSampleRate:CD_SAMPLE_RATE_MID];

Comments