目录:
一、Cocos2d游戏引擎介绍
Cocos2d是一个开源框架,用于构建2D游戏、演示程序和其他图形界面交互应用等.
二、Cocos2d游戏引擎安装
说明:从0.99.0版本开始,Cocos2d-iPhone的模板类要求苹果SDK3.0及以上版本。
![](http://hi.csdn.net/attachment/201111/9/0_13208104142yy1.gif)
三、Cocos2d基本概念
开发cocos2d应用程序,必须先了解如下四个概念:
是app 工作流上独立块。一个app可能拥有多个scenes, 但在任何时候只能有一个是激活状态的。一个CCScene对象由一个或多个layers组成,layers之间是前后相连的。layer提供了scene的外观 appearance和行为behavior.
CCDirector负责scenes之间的前进或后退。CCDirector是singleton模式的共享的对象,它知道当前哪个scene是激活。
CCLayer对象定义了可描绘的区域,定义了描绘的规则。当编写cocos2d程序的时候,大部分工作就是编写CCLayer的子类去实现所要的效果。
sprite对象就是一个可以移动、选择、伸缩、动画的2d图形,CCSprite 对象的子成员中可以包含其它的CCSprite 对象。
四、Cocos2d游戏动作
![](http://hi.csdn.net/attachment/201111/9/0_1320810567aKYC.gif)
id actionTo = [MoveTo actionWithDuration: 2 position:ccp(s.width-40,s.height-40)];
id actionBy = [MoveByactionWithDuration:2 position: ccp(80,80)];
id actionCallFunc = [CallFunc actionWithTarget:self selector:@selector(doATask)];
id actionSequence = [Sequence actions: actionTo, actionBy, actionCallFunc, nil];
-(void) doATask
{
}
id actionCallFuncN = [CallFuncN actionWithTarget:self selector:@selector(doATaskN:)];
id actionCallFuncND = [CallFuncND actionWithTarget:self selector:@selector(doATaskN:data:) [/pre]data:pointerToSomeData];
- (void) doATaskN: (id)node
{//some code
}
- (void) doATaskND: (id)node data:(void*)d
{//some code
}
五、Cocos2d的Touch事件
withEvent:(UIEvent *)event {}
withEvent:(UIEvent *)event {}
withEvent:(UIEvent *)event {}
withEvent:(UIEvent *)event {}
核心:就是如何将系统用户输入信息传递给每个Layer对象。
![](http://hi.csdn.net/attachment/201111/9/0_1320810654mP91.gif)
(六)帧动画序列制作工具——Zwoptex
![](http://hi.csdn.net/attachment/201111/9/0_1320810709QOd7.gif)
![](http://hi.csdn.net/attachment/201111/9/0_1320810747666E.gif)
添加动画
利用软件Zwoptex制作的png和plist文件添加动画
// 将花朵的png图片序列和plist载入场景
CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
[cacheaddSpriteFramesWithFile:@"Flower.plist"];
idsheet = [CCSpriteSheet spriteSheetWithFile:@"Flower.png"
capacity:60];
[selfaddChild:sheet];
// 将花朵Sprite添加到场景中
FlowerSprite *sprite = [FlowerSprite node];
CGSize size = [[CCDirector sharedDirector] winSize];
sprite.position = ccp(size.width / 2, size.height / 2);
[selfaddChild:sprite];
CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
NSMutableArray *frames = [[NSMutableArray array] retain];
// 构造每一个帧的实际图像数据
for (int i = 1; i <= FLOWER_SPRITE_SHEET_CAPACITY; i++) {
NSString *frameName = [NSStringstringWithFormat:@"Flower%04d.png", i];
CCSpriteFrame *frame = [cache spriteFrameByName:frameName];
[framesaddObject:frame];
}
// 使用CCAnimation和CCRepeatForever构造一个一直重复的动画
NSString *firstFrameName = [NSStringstringWithFormat:@"Flower%04d.png", 1];
idsprite = [CCSprite spriteWithSpriteFrameName:firstFrameName];
CCAnimation *animation = [CCAnimation animationWithName:@"Flower“ delay:1.0f / 30 frames:frames];
[spriterunAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]]];
// 将构造好的动画加入显示列表
[self addChild:sprite];
播放声音
使用cocos2d的SimpleAudioEngine可以很简单的播放背景音乐和音效。
1,准备工作
引入头文件:#import "SimpleAudioEngine.h"
2,播放背景音乐
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"background.wav"];
3,播放音效
[[SimpleAudioEngine sharedEngine]playEffect:@"effect1.wav"];
4,暂停背景音乐
[[SimpleAudioEngine sharedEngine] pauseBackgroundMusic];
5,预先加载背景音乐
[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic];
使用CDAudioManager来更改音乐的属性(更改属性之前必须有背景音乐在播放)
6,更改音量(音量大小从0到1)
[CDAudioManager sharedManager].backgroundMusic.volume = 1.0f;
7,循环播放N次背景音乐
[CDAudioManager sharedManager].backgroundMusic.numberOfLoops = N;
8,背景音乐停止时触发事件
[[CDAudioManager sharedManager] setBackgroundMusicCompletionListener:self
selector:@selector()];