开端Cocos2d iPhone开发.
点击这里下载此教程的XCode项目.
加入IDP. 最重要的一步是注册成为一个专业的iPhone开发者.对开发而言它是免费的(只管当你需要在真机上运行你的程序时必需支付99美金),和其余的办法比 它可快多了.
接下来你需要下载iPhone SDK,这个下载的工程可有点大.你会巴不得它以最快的速度下载下来.
XCode入门. 下一步,我们需要在XCode中建破一个标准的iPhone项目.
点击File -> New Project
选择iPhone OS一栏下的Application
最后挑选 Window-Based Application模板
点击Choos按钮
=700) window.open('/cms/uploads/allimg/100126/163FBH5-0.png');" border=0 alt="" src=" 接下来这个项目将被打开,如下图:
=700) window.open('/cms/uploads/allimg/100126/163FAB6-1.png');" border=0 alt="" src=" Git , 这使得树立一个新的项目库无比快:
cd SimpleGame
git init
git commit -a -m "Initial commit."
你可能更爱好用Mercurial 或 SVN, 他们同样很棒, 那就按本人的爱好用吧!
在我们持续之前,先点击一下Build and Go按钮,你在就能iPhone模仿器中看见一个只有白色的屏幕(这就是SimpleGame最开始的样子).
去除window和nib文件. 基于Cocos2d iPhone的项目并不需要应用nib或者尺度的UI组件,所以我们需要对这个XCode创立的默认项目进行一些修改.(很可怜,在使用Cocos2d iPhone时不不需要修正就能用的完善模板).
首先, 删除MainWindow.xib文件.
然后, 在Info.plist文件中删除Main nib file base name这条内容.
=700) window.open('/cms/uploads/allimg/100126/163F63920-2.png');" border=0 alt="" src="
#import <UIKit/UIKit.h>int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil,
@”SimpleGameAppDelegate”);
[pool release];
return retVal;
}
其次是修改SimpleGameAppDelegate.h,修改后如下:
#import <UIKit/UIKit.h>@interface SimpleGameAppDelegate : NSObject <UIApplicationDelegate> {
}
@end
最后, 修改SimpleGameAppDelegate.m,修改后如下s:
#import “SimpleGameAppDelegate.h”@implementation SimpleGameAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)app {
}
@end
这 些修改弄好之后可以先不必管他们了,当你开始一个新的项目时,记得回这里看看就行(最好你能以此创建一个Cocos2d iPhone项目的模板,那你就能一劳永逸了).
整合Cocos2d iPhone.
我 们下一步是从Google Code上将Cocos2d iPhon最新的库抓下来.进入Cocos2d iPhone下载页 ,并且抉择0.7.0 版本 ,将其下载下来.
tar -xvf cocos2d-iphone-0.7.0.tar
接下来回到你的项目中
选 择Project菜单 -> Add Files to Project …而后阅读你刚解紧缩的目录,将这个目录增加到你的项目中,你可以勾选 Copy items into destination group's folder (if needed)前的复选框,点击Add按钮.
=700) window.open('/cms/uploads/allimg/100126/163F64403-3.png');" border=0 alt="" src="
整合Cocos2d iPhone.
我们下一步是从 Google Code上将Cocos2d iPhon最新的库抓下来.进入Cocos2d iPhone下载页 ,并且选择0.7.0 版本 ,将其下载下来.
tar -xvf cocos2d-iphone-0.7.0.tar
接下往返到你的项目中
选 择Project菜单 -> Add Files to Project …然后浏览你刚刚解压缩的目录,将这个目录添加到您的项目中,你可以勾选 Copy items into destination group's folder (if needed)前的复选框,点击Add按钮.
=700) window.open('/cms/uploads/allimg/100126/163F64403-3.png');" border=0 alt="" src=" 么问题.(这种编译进程中的问题,早发明早修复,当前再找问题会绝对麻烦良多)
现在,回到项目窗口选择 Project->Add Files to Project 将cocos2d-iphone-0.7.0/cocos2d文件夹加入你的项目中.
反复上面的操作将 cocos2d-iphone-0.7.0/cocoslive和 cocos2d-iphone-0.7.0/experimental文件夹加入到你的项目中.
最后,将cocos2d-iphone- 0.7.0/cocos2d/Resources/fps_images.png参加到你项目中的Resources组下.
我 们已经实现 cocos2d-iphone的配置,然而这个项目还没有编译过,在左侧的分栏中找到targets,点击SimpleGame左侧的小箭头开展它,选择 Link Binary With Libraries,弹出鼠标右键,选择Add->Add Existing Frameworks, 大盘破跌生命线5分钟K线技术分析买点图,就像下图中的那样.
=700) window.open('/cms/uploads/allimg/100126/163FC541-5.png');" border=0 alt="" src=" Libraries列表的下方),在弹出的frameworks列表中取舍 OpenGLES.framework和 QuartzCore.framework.
=700) window.open('/cms/uploads/allimg/100126/163F62F5-6.png');" border=0 alt="" src=" libraries列表应当和下图一样.
=700) window.open('/cms/uploads/allimg/100126/163FBU3-7.png');" border=0 alt="" src="
收拾项目.
让我们高效的整顿一下项目构造,现在你很难找出 SimpleGameAppDelegate文件了吧,由于他们一大堆货色都显示在项目的根级别下.
在项目下创建两个 Groups:Support 和 Classes
=700) window.open('/cms/uploads/allimg/100126/163FB228-8.png');" border=0 alt="" src=" Chipmunk 和 cocos2d到Support组下.接下来挪动SimpleGameAppDelegate.h 和 SimpleGameAppDelegate.m到Classes组下.完成后你的项目层级看起来就像下图:
=700) window.open('/cms/uploads/allimg/100126/163FB311-9.png');" border=0 alt="" src="
创建一个主菜单.
现 在我们终于要恰是开始使用Cocos2d iPhone了.在编写代码之前,我们先抽多少分钟的时间做一个概述,接下来的课程中我们将要使用.
在 Cocos2diPhone中,你将可以便利快捷的处置现场Scence,层Layer等.场景Scence就是一个当你创建了它之后,玩家就可以看见 它.它是由一个或多个层Layer组成的.要显示一个特定的场景Scence,你要告知Director(这是一个单例模式类)去显示它.
现在我 们要创建MenuScene ,这是一个场景的子类,还要创建一个MenuLayer ,我们的菜单中将包括他们。
最后,我们要衔接的 applicationDidFinishLaunching方告诉诉Dorector执行MenuScene 。
我们需要在项目中添加 一个图片来作为我们MENU 层的背景,你可以下载这张图片并加入到你项目中 现在开始写代码啦.鼠标右键单击Classes组,并选择 Add->New File...,选择Cocoa Touch Classes中的NSObject subclass文件类型,命名为 MenuScene.m(完成时将主动创建.h的头文件)
=700) window.open('/cms/uploads/allimg/100126/163FBa1-10.png');" border=0 alt="" src="
#import <UIKit/UIKit.h>
#import “cocos2d.h”@interface MenuScene : Scene {}
@end
@interface MenuLayer : Layer {}
-(void)startGame: (id)sender;
-(void)help: (id)sender;
@end
接下来我们要让MenuScene.m实现为MenuScene 和 MenuLayer类
#import “MenuScene.h”
@implementation MenuScene
- (id) init {
self = [super init];
if (self != nil) {
Sprite * bg = [Sprite spriteWithFile:@"menu.png"];
[bg setPosition:cpv(240, 160)];
[self add:bg z:0];
[self add:[MenuLayer node] z:1];
}
return self;
}
@end@implementation MenuLayer
- (id) init {
self = [super init];
if (self != nil) {
[MenuItemFont setFontSize:20];
[MenuItemFont setFontName:@"Helvetica"];
MenuItem *start = [MenuItemFont itemFromString:@"Start Game"
target:self
selector:@selector(startGame:)];
MenuItem *help = [MenuItemFont itemFromString:@"Help"
target:self
selector:@selector(help:)];
Menu *menu = [Menu menuWithItems:start, help, nil];
[menu alignItemsVertically];
[self add:menu];
}
return self;
}
-(void)startGame: (id)sender {
NSLog(@”start game”);
}
-(void)help: (id)sender {
NSLog(@”help”);
}
@end
现 在先放下这startGame和 help这两个方式,一会儿我们再回来更新它们.
当初我们有了可履行的menu,我们须要翻开 SimpleGameAppDelegate(.h跟.m)文件,让Director在利用程序启动时加载MenuScene
在 SimpleGameAppDelegate.h的顶部加入 cocos2d 和 MenuScene的import:
#import <UIKit/UIKit.h>
#import “cocos2d.h”
#import “MenuScene.h”接下来,打开SimpleGameAppDelegate.m,增添如下代码:
#import “SimpleGameAppDelegate.h”
@implementation SimpleGameAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window setUserInteractionEnabled:YES];
[window setMultipleTouchEnabled:YES];
[[Director sharedDirector] setLandscape: YES];
[[Director sharedDirector] attachInWindow:window];[window makeKeyAndVisible];
MenuScene * ms = [MenuScene node];
[[Director sharedDirector] runWithScene:ms];
}
@end
现 在你可以Build and Go这个项目了,你将看到下面这个丑恶画面.
=700) window.open('/cms/uploads/allimg/100126/163F64607-11.png');" border=0 alt="" src="
加入一个游 戏场景.
接下来, 海派甜心,我们要创建一个简略的游戏场景,它将在点击Start Game选项后显示.让我们加入另一个不同的背景图片,当我们进入 游戏时就会事实它了.
加入这个图片到项目中, 海派甜心,它将成为 游戏场景的背景. 接 着创建GameScene.h 和 GameScene.m文件至Classes组中.在GameScene.h文件中添加如下代码:
#import <UIKit/UIKit.h>
#import “cocos2d.h”@interface GameScene : Scene {}
@end
@interface GameLayer : Layer {}
@end
修改 GameScene.m使其看起来如下:
#import “GameScene.h”
#import “MenuScene.h”@implementation GameScene
- (id) init {
self = [super init];
if (self != nil) {
Sprite * bg = [Sprite spriteWithFile:@"game.png"];
[bg setPosition:cpv(240, 160)];
[self add:bg z:0];
[self add:[GameLayer node] z:1];
}
return self;
}
@end
@implementation GameLayer
- (id) init {
self = [super init];
if (self != nil) {
isTouchEnabled = YES;
}
return self;
}
- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
MenuScene * ms = [MenuScene node];
[[Director sharedDirector] replaceScene:ms];
return kEventHandled;
}
@end
最后,我们要回去MenuLayer类中更新startGame方法,修改如下:
-(void)startGame: (id)sender {
GameScene * gs = [GameScene node];
[[Director sharedDirector] replaceScene:gs];
}勇敢的创建这个项目,沉迷在胜利的喜悦几秒钟吧.Cocos2d iPhone最好的处所就是从这里开始所有都变得简单了,艰苦的地方已经停止了.
教养到这里就结 束了.
你可以下载我们为这个教程制造的项目文件.
咱们盼望这能起到辅助先容 Cocos2d iPhone 这个项目标作用,能够让你花一点时光来懂得一下这个巨大的名目.
点击这里下载此教程的XCode项目.
加入IDP. 最重要的一步是注册成为一个专业的iPhone开发者.对开发而言它是免费的(只管当你需要在真机上运行你的程序时必需支付99美金),和其余的办法比 它可快多了.
接下来你需要下载iPhone SDK,这个下载的工程可有点大.你会巴不得它以最快的速度下载下来.
XCode入门. 下一步,我们需要在XCode中建破一个标准的iPhone项目.
点击File -> New Project
选择iPhone OS一栏下的Application
最后挑选 Window-Based Application模板
点击Choos按钮
=700) window.open('/cms/uploads/allimg/100126/163FBH5-0.png');" border=0 alt="" src=" 接下来这个项目将被打开,如下图:
=700) window.open('/cms/uploads/allimg/100126/163FAB6-1.png');" border=0 alt="" src=" Git , 这使得树立一个新的项目库无比快:
cd SimpleGame
git init
git commit -a -m "Initial commit."
你可能更爱好用Mercurial 或 SVN, 他们同样很棒, 那就按本人的爱好用吧!
在我们持续之前,先点击一下Build and Go按钮,你在就能iPhone模仿器中看见一个只有白色的屏幕(这就是SimpleGame最开始的样子).
去除window和nib文件. 基于Cocos2d iPhone的项目并不需要应用nib或者尺度的UI组件,所以我们需要对这个XCode创立的默认项目进行一些修改.(很可怜,在使用Cocos2d iPhone时不不需要修正就能用的完善模板).
首先, 删除MainWindow.xib文件.
然后, 在Info.plist文件中删除Main nib file base name这条内容.
=700) window.open('/cms/uploads/allimg/100126/163F63920-2.png');" border=0 alt="" src="
#import <UIKit/UIKit.h>int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil,
@”SimpleGameAppDelegate”);
[pool release];
return retVal;
}
其次是修改SimpleGameAppDelegate.h,修改后如下:
#import <UIKit/UIKit.h>@interface SimpleGameAppDelegate : NSObject <UIApplicationDelegate> {
}
@end
最后, 修改SimpleGameAppDelegate.m,修改后如下s:
#import “SimpleGameAppDelegate.h”@implementation SimpleGameAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)app {
}
@end
这 些修改弄好之后可以先不必管他们了,当你开始一个新的项目时,记得回这里看看就行(最好你能以此创建一个Cocos2d iPhone项目的模板,那你就能一劳永逸了).
整合Cocos2d iPhone.
我 们下一步是从Google Code上将Cocos2d iPhon最新的库抓下来.进入Cocos2d iPhone下载页 ,并且抉择0.7.0 版本 ,将其下载下来.
tar -xvf cocos2d-iphone-0.7.0.tar
接下来回到你的项目中
选 择Project菜单 -> Add Files to Project …而后阅读你刚解紧缩的目录,将这个目录增加到你的项目中,你可以勾选 Copy items into destination group's folder (if needed)前的复选框,点击Add按钮.
=700) window.open('/cms/uploads/allimg/100126/163F64403-3.png');" border=0 alt="" src="
整合Cocos2d iPhone.
我们下一步是从 Google Code上将Cocos2d iPhon最新的库抓下来.进入Cocos2d iPhone下载页 ,并且选择0.7.0 版本 ,将其下载下来.
tar -xvf cocos2d-iphone-0.7.0.tar
接下往返到你的项目中
选 择Project菜单 -> Add Files to Project …然后浏览你刚刚解压缩的目录,将这个目录添加到您的项目中,你可以勾选 Copy items into destination group's folder (if needed)前的复选框,点击Add按钮.
=700) window.open('/cms/uploads/allimg/100126/163F64403-3.png');" border=0 alt="" src=" 么问题.(这种编译进程中的问题,早发明早修复,当前再找问题会绝对麻烦良多)
现在,回到项目窗口选择 Project->Add Files to Project 将cocos2d-iphone-0.7.0/cocos2d文件夹加入你的项目中.
反复上面的操作将 cocos2d-iphone-0.7.0/cocoslive和 cocos2d-iphone-0.7.0/experimental文件夹加入到你的项目中.
最后,将cocos2d-iphone- 0.7.0/cocos2d/Resources/fps_images.png参加到你项目中的Resources组下.
我 们已经实现 cocos2d-iphone的配置,然而这个项目还没有编译过,在左侧的分栏中找到targets,点击SimpleGame左侧的小箭头开展它,选择 Link Binary With Libraries,弹出鼠标右键,选择Add->Add Existing Frameworks, 大盘破跌生命线5分钟K线技术分析买点图,就像下图中的那样.
=700) window.open('/cms/uploads/allimg/100126/163FC541-5.png');" border=0 alt="" src=" Libraries列表的下方),在弹出的frameworks列表中取舍 OpenGLES.framework和 QuartzCore.framework.
=700) window.open('/cms/uploads/allimg/100126/163F62F5-6.png');" border=0 alt="" src=" libraries列表应当和下图一样.
=700) window.open('/cms/uploads/allimg/100126/163FBU3-7.png');" border=0 alt="" src="
收拾项目.
让我们高效的整顿一下项目构造,现在你很难找出 SimpleGameAppDelegate文件了吧,由于他们一大堆货色都显示在项目的根级别下.
在项目下创建两个 Groups:Support 和 Classes
=700) window.open('/cms/uploads/allimg/100126/163FB228-8.png');" border=0 alt="" src=" Chipmunk 和 cocos2d到Support组下.接下来挪动SimpleGameAppDelegate.h 和 SimpleGameAppDelegate.m到Classes组下.完成后你的项目层级看起来就像下图:
=700) window.open('/cms/uploads/allimg/100126/163FB311-9.png');" border=0 alt="" src="
创建一个主菜单.
现 在我们终于要恰是开始使用Cocos2d iPhone了.在编写代码之前,我们先抽多少分钟的时间做一个概述,接下来的课程中我们将要使用.
在 Cocos2diPhone中,你将可以便利快捷的处置现场Scence,层Layer等.场景Scence就是一个当你创建了它之后,玩家就可以看见 它.它是由一个或多个层Layer组成的.要显示一个特定的场景Scence,你要告知Director(这是一个单例模式类)去显示它.
现在我 们要创建MenuScene ,这是一个场景的子类,还要创建一个MenuLayer ,我们的菜单中将包括他们。
最后,我们要衔接的 applicationDidFinishLaunching方告诉诉Dorector执行MenuScene 。
我们需要在项目中添加 一个图片来作为我们MENU 层的背景,你可以下载这张图片并加入到你项目中 现在开始写代码啦.鼠标右键单击Classes组,并选择 Add->New File...,选择Cocoa Touch Classes中的NSObject subclass文件类型,命名为 MenuScene.m(完成时将主动创建.h的头文件)
=700) window.open('/cms/uploads/allimg/100126/163FBa1-10.png');" border=0 alt="" src="
#import <UIKit/UIKit.h>
#import “cocos2d.h”@interface MenuScene : Scene {}
@end
@interface MenuLayer : Layer {}
-(void)startGame: (id)sender;
-(void)help: (id)sender;
@end
接下来我们要让MenuScene.m实现为MenuScene 和 MenuLayer类
#import “MenuScene.h”
@implementation MenuScene
- (id) init {
self = [super init];
if (self != nil) {
Sprite * bg = [Sprite spriteWithFile:@"menu.png"];
[bg setPosition:cpv(240, 160)];
[self add:bg z:0];
[self add:[MenuLayer node] z:1];
}
return self;
}
@end@implementation MenuLayer
- (id) init {
self = [super init];
if (self != nil) {
[MenuItemFont setFontSize:20];
[MenuItemFont setFontName:@"Helvetica"];
MenuItem *start = [MenuItemFont itemFromString:@"Start Game"
target:self
selector:@selector(startGame:)];
MenuItem *help = [MenuItemFont itemFromString:@"Help"
target:self
selector:@selector(help:)];
Menu *menu = [Menu menuWithItems:start, help, nil];
[menu alignItemsVertically];
[self add:menu];
}
return self;
}
-(void)startGame: (id)sender {
NSLog(@”start game”);
}
-(void)help: (id)sender {
NSLog(@”help”);
}
@end
现 在先放下这startGame和 help这两个方式,一会儿我们再回来更新它们.
当初我们有了可履行的menu,我们须要翻开 SimpleGameAppDelegate(.h跟.m)文件,让Director在利用程序启动时加载MenuScene
在 SimpleGameAppDelegate.h的顶部加入 cocos2d 和 MenuScene的import:
#import <UIKit/UIKit.h>
#import “cocos2d.h”
#import “MenuScene.h”接下来,打开SimpleGameAppDelegate.m,增添如下代码:
#import “SimpleGameAppDelegate.h”
@implementation SimpleGameAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window setUserInteractionEnabled:YES];
[window setMultipleTouchEnabled:YES];
[[Director sharedDirector] setLandscape: YES];
[[Director sharedDirector] attachInWindow:window];[window makeKeyAndVisible];
MenuScene * ms = [MenuScene node];
[[Director sharedDirector] runWithScene:ms];
}
@end
现 在你可以Build and Go这个项目了,你将看到下面这个丑恶画面.
=700) window.open('/cms/uploads/allimg/100126/163F64607-11.png');" border=0 alt="" src="
加入一个游 戏场景.
接下来, 海派甜心,我们要创建一个简略的游戏场景,它将在点击Start Game选项后显示.让我们加入另一个不同的背景图片,当我们进入 游戏时就会事实它了.
加入这个图片到项目中, 海派甜心,它将成为 游戏场景的背景. 接 着创建GameScene.h 和 GameScene.m文件至Classes组中.在GameScene.h文件中添加如下代码:
#import <UIKit/UIKit.h>
#import “cocos2d.h”@interface GameScene : Scene {}
@end
@interface GameLayer : Layer {}
@end
修改 GameScene.m使其看起来如下:
#import “GameScene.h”
#import “MenuScene.h”@implementation GameScene
- (id) init {
self = [super init];
if (self != nil) {
Sprite * bg = [Sprite spriteWithFile:@"game.png"];
[bg setPosition:cpv(240, 160)];
[self add:bg z:0];
[self add:[GameLayer node] z:1];
}
return self;
}
@end
@implementation GameLayer
- (id) init {
self = [super init];
if (self != nil) {
isTouchEnabled = YES;
}
return self;
}
- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
MenuScene * ms = [MenuScene node];
[[Director sharedDirector] replaceScene:ms];
return kEventHandled;
}
@end
最后,我们要回去MenuLayer类中更新startGame方法,修改如下:
-(void)startGame: (id)sender {
GameScene * gs = [GameScene node];
[[Director sharedDirector] replaceScene:gs];
}勇敢的创建这个项目,沉迷在胜利的喜悦几秒钟吧.Cocos2d iPhone最好的处所就是从这里开始所有都变得简单了,艰苦的地方已经停止了.
教养到这里就结 束了.
你可以下载我们为这个教程制造的项目文件.
咱们盼望这能起到辅助先容 Cocos2d iPhone 这个项目标作用,能够让你花一点时光来懂得一下这个巨大的名目.