Cocos2d iPhone基础教程 part 2

接下来回到你的项目中


选择Project菜单 -> Add Files to Project …然后浏览你刚刚解压缩的目录,将这个目录添加到您的项目中,你可以勾选 Copy items into destination group's folder (if needed)前的复选框,点击Add按钮.

接下来,这是很重要的一个步骤,删除External\Chipmunk目录下的Demo文件夹.在这之后呢,你可以先尝试编译一下你的项目,确保没有什么问题.(这种编译过程中的问题,早发现早修复,以后再找问题会相对麻烦很多)
现在,回到项目窗口选择 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,就像下图中的那样.

在之后弹出的窗口中,点击窗口下方的”+”号按钮(在Linked Libraries列表的下方),在弹出的frameworks列表中选择 OpenGLES.framework和 QuartzCore.framework.

点击add按钮之后,你的linked libraries列表应该和下图一样.

现在,当你build你的项目时,它们将被编译到项目中.当然,现在还没法做任何事情,但是没有编译失败,这就是最棒的了.
整理项目.让我们高效的整理一下项目结构,现在你很难找出SimpleGameAppDelegate文件了吧,因为他们一大堆东西都显示在项目的根级别下.
在项目下创建两个Groups:Support 和 Classes

首先移动 Chipmunk 和 cocos2d到Support组下.接下来移动SimpleGameAppDelegate.h 和 SimpleGameAppDelegate.m到Classes组下.完成后你的项目层级看起来就像下图:

这并不必要的步骤,但是保持项目层级的清晰是会让你的工作事半功倍的.
创建一个主菜单.现在我们终于要正是开始使用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的头文件)

现在打开MenuScene.h文件,并将代码改成看起如下.

#import
#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
#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这个项目了,你将看到下面这个丑陋画面.

幸运的是,我们已经学到了很多Cocos2D的用法,这将让后面的事情变得简单许多.
加入一个游戏场景.接下来,我们要创建一个简单的游戏场景,它将在点击Start Game选项后显示.让我们加入另一个不同的背景图片,当我们进入游戏时就会现实它了.
加入这个图片到项目中,它将成为游戏场景的背景.接着创建GameScene.h 和 GameScene.m文件至Classes组中.在GameScene.h文件中添加如下代码:

#import
#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最好的地方就是从这里开始一切都变得简单了,困难的地方已经结束了.
原文地址:https://www.cnblogs.com/lm3515/p/1806212.html