Cocos2d 2.0与UIKit结合使用

来源:互联网 发布:算法4 epub 编辑:程序博客网 时间:2024/06/04 18:04

本文是阅读How To Integrate Cocos2D and UIKit后的总结,这篇教程里面cocos2d的版本比较老,和现在的模版有点差别,所以在重复上面的例子时候进行了相应的修改

原文地址为:http://www.raywenderlich.com/4817/how-to-integrate-cocos2d-and-uikit

Cocos2d新建的项目中,启动流程是这样的:

在SupportingFiles\main.m 中调用了如下方法

int retVal = UIApplicationMain(argc, argv, nil, @"AppController");

启动的时候,UIKit创建了一个AppController的实例,然后AppControllerapplicationDidFinishLaunching方法中创建了main window.

// Create the main windowwindow_ = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

如果我们想从XIB文件启动的话,可以这样做:

Supporting Files\main.m上UIApplicationMain的参数修改成nil

int retVal = UIApplicationMain(argc, argv, nil, nil);

这样,UIkit启动的时候,会根据Info.plist文件中的NSMainNibFile的值来加载相应的XIB文件

Info.plist添加值

然后创建一个空的XIB文件



设置File’s OwnerUIApplication

然后拖一个Object对象进去设置成AppController

这样,XIB就会生成一个AppController的实例

然后把File’s Ownerdelegate设置为AppController

这样,UIApplication就会调用AppControllerUIApplicationDelegate方法例如applicationDidFinishLaunching方法

如果想用XIB来创建UIWindow,那么将AppDelegate.h的代码改成:

@property (nonatomic, retain) IBOutlet UIWindow *window;

并将.m文件中的创建代码注释掉

// Create the main window//window_ = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

在XIB文件拖一个window对象,绑定起来

接下来,用IB创建一个菜单界面,所以得先把这些代码先删了

//// 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_ setRootViewController:navController_];

注意:

我在例子里多删了一个函数,结果导致了UITextfield输入不了文字

[window_ makeKeyAndVisible];

现在添加一个菜单viewController,把默认朝向改成Landscape


然后在里面放了1个背景图和2个按钮


Xcode 4.5版本的里面,拖拽控件会有Constraints这个东西出现,在ios5的系统运行就会报错,如果想在ios6以下系统运行,必须在xib文件的这个选项取消掉Use Autolayout


接下来打开MainWindow.xib添加一个NavigationController,并把windowrootViewController设置为它,MainMenuViewController设置为它的viewController




接下来创建一个RootViewController,当点击View按钮时候,弹出这个界面,这个界面显示cocos2d的绘制界面

 

RootViewController.m添加一个方法,将NavigationBar给隐藏了。

// Add new method- (void) viewWillAppear:(BOOL)animated{    [self.navigationController setNavigationBarHidden:YES animated:animated];    [super viewWillAppear:animated];}

然后控制该界面的朝向

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    return UIInterfaceOrientationIsLandscape(interfaceOrientation);}

MainMenuViewController里面添加RootViewController对象,和点击处理方法

@interface MainMenuViewController : UIViewController{    RootViewController *_rootViewController;}@property (retain) RootViewController *rootViewController;-(IBAction)viewTapped:(id)sender;@end

然后在MainMenuViewContoller.m文件里添加

- (void)showCocos2DView:(id)arg{    if (_rootViewController == nil) {        self.rootViewController = [[[RootViewController alloc] initWithNibName:nil bundle:nil] autorelease];    }    [self.navigationController pushViewController:_rootViewController animated:YES];}-(IBAction)viewTapped:(id)sender{    [self showCocos2DView:nil];}

XIB中把按钮处理函数绑定起来

然后在RootViewController中添加:


- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.        CCDirector* director_ = [CCDirector sharedDirector];    [self.view insertSubview:director_.view atIndex:0];    [director_ runWithScene:[HelloWorldLayer scene]];    [director_ startAnimation];}

然后运行发现


通过调试发现,在创建AppDelegate里面创建CCGLView的时候,使用的CGRect(0,0,320,480)

Cocos2d 2.0版本里,CCDirector类的父类是UIViewController,在朝向发生改变的时候,UIViewController的朝向相关的函数进行相应的处理,为了方便起见,我直接设置为CGRect(0,0,480,320)

CCGLView *glView = [CCGLView viewWithFrame:CGRectMake(0, 0, 480, 320)   pixelFormat:kEAGLColorFormatRGB565//kEAGLColorFormatRGBA8   depthFormat:0//GL_DEPTH_COMPONENT24_OESpreserveBackbuffer:NOsharegroup:nil multiSampling:NO   numberOfSamples:0];

然后在RootViewController.xib拖拽2个按钮,并在RootViewController里面添加-(IBAction)homeTapped:(id)sender方法

-(IBAction)homeTapped:(id)sender{    [self.navigationController popViewControllerAnimated:YES];}

将这个方法和按钮绑定,点击按钮就可以回到主菜单



接下来添加手势识别

添加点击,双击,左滑动,右滑动

HelloWorldLayer.h里添加如下手势:

// HelloWorldLayer@interface HelloWorldLayer : CCLayer <GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate>{    UITapGestureRecognizer *_tapRecognizer;    UITapGestureRecognizer *_doubleTapRecognizer;    UISwipeGestureRecognizer *_swipeLeftRecognizer;    UISwipeGestureRecognizer *_swipeRightRecognizer;}@property (retain) UITapGestureRecognizer * tapRecognizer;@property (retain) UITapGestureRecognizer * doubleTapRecognizer;@property (retain) UISwipeGestureRecognizer * swipeLeftRecognizer;@property (retain) UISwipeGestureRecognizer * swipeRightRecognizer;

HelloWorldLayer.m中添加

@synthesize tapRecognizer = _tapRecognizer;@synthesize doubleTapRecognizer = _doubleTapRecognizer;@synthesize swipeLeftRecognizer = _swipeLeftRecognizer;@synthesize swipeRightRecognizer = _swipeRightRecognizer;- (void)onEnter {    self.doubleTapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)] autorelease];    _doubleTapRecognizer.numberOfTapsRequired = 2;    [[CCDirector sharedDirector].view addGestureRecognizer:_doubleTapRecognizer];        self.tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)] autorelease];    [_tapRecognizer requireGestureRecognizerToFail:_doubleTapRecognizer];    [[CCDirector sharedDirector].view addGestureRecognizer:_tapRecognizer];        self.swipeLeftRecognizer = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)] autorelease];    _swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;    [[CCDirector sharedDirector].view addGestureRecognizer:_swipeLeftRecognizer];        self.swipeRightRecognizer = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)] autorelease];    _swipeRightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;    [[CCDirector sharedDirector].view addGestureRecognizer:_swipeRightRecognizer];}- (void)onExit {[[CCDirector sharedDirector].view removeGestureRecognizer:_tapRecognizer];     [[CCDirector sharedDirector].view removeGestureRecognizer:_doubleTapRecognizer];     [[CCDirector sharedDirector].view removeGestureRecognizer:_swipeLeftRecognizer];     [[CCDirector sharedDirector].view removeGestureRecognizer:_swipeRightRecognizer];}- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer {    CCLOG(@"Tap!");}- (void)handleDoubleTap:(UITapGestureRecognizer *)doubletapRecognizer {    CCLOG(@"Double Tap!");}- (void)handleLeftSwipe:(UISwipeGestureRecognizer *)swipeRecognizer {    CCLOG(@"Swipe Left!");}- (void)handleRightSwipe:(UISwipeGestureRecognizer *)swipeRecognizer {    CCLOG(@"Swipe Right!");}// on "dealloc" you need to release all your retained objects- (void) dealloc{// in case you have something to dealloc, do it in this method// in this particular example nothing needs to be released.// cocos2d will automatically release all the children (Label)[_tapRecognizer release];    _tapRecognizer = nil;    [_doubleTapRecognizer release];    _doubleTapRecognizer = nil;    [_swipeLeftRecognizer release];    _swipeLeftRecognizer = nil;    [_swipeRightRecognizer release];    _swipeRightRecognizer = nil;// don't forget to call "super dealloc"[super dealloc];}

在这里需要说明的是,为了防止双击事件的第一次点击触发单击的相应函数,所以调用了这个方法:

[_tapRecognizer requireGestureRecognizerToFail:_doubleTapRecognizer];


Layer与RootViewController的通讯的话,把RootViewController的指针传给Layer就可以了。




原创粉丝点击