本文是阅读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 的实例,然后 AppController 在 applicationDidFinishLaunching 方法中创建了 main window.
// Create the main window
window_ = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
如果我们想从XIB文件启动的话,可以这样做:
把Supporting Files\main.m上UIApplicationMain的参数修改成nilint retVal = UIApplicationMain(argc, argv, nil, nil);
这样,UIkit启动的时候,会根据Info.plist文件中的NSMainNibFile的值来加载相应的XIB文件
在Info.plist添加值
![](https://img-my.csdn.net/uploads/201212/01/1354365518_7802.png)
然后创建一个空的XIB文件
![](https://img-my.csdn.net/uploads/201212/01/1354365582_4648.png)
设置File’s Owner为UIApplication
![](https://img-my.csdn.net/uploads/201212/01/1354365753_5443.png)
然后拖一个Object对象进去设置成AppController类
![](https://img-my.csdn.net/uploads/201212/01/1354365807_6872.png)
这样,XIB就会生成一个AppController的实例
然后把File’s Owner的delegate设置为AppController
![](https://img-my.csdn.net/uploads/201212/01/1354365854_7218.png)
这样,UIApplication就会调用AppController的UIApplicationDelegate方法例如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对象,绑定起来
![](https://img-my.csdn.net/uploads/201212/01/1354365961_5892.png)
接下来,用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
![](https://img-my.csdn.net/uploads/201212/01/1354366103_1283.png)
然后在里面放了1个背景图和2个按钮
![](https://img-my.csdn.net/uploads/201212/01/1354366154_4488.png)
在Xcode 4.5版本的里面,拖拽控件会有Constraints这个东西出现,在ios5的系统运行就会报错,如果想在ios6以下系统运行,必须在xib文件的这个选项取消掉Use Autolayout
接下来打开MainWindow.xib添加一个NavigationController,并把window的rootViewController设置为它,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中把按钮处理函数绑定起来
![](https://img-my.csdn.net/uploads/201212/01/1354366497_7800.png)
然后在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];
}
然后运行发现
![](https://img-my.csdn.net/uploads/201212/01/1354366623_2113.png)
通过调试发现,在创建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_OES
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];
然后在RootViewController.xib拖拽2个按钮,并在RootViewController里面添加-(IBAction)homeTapped:(id)sender方法
-(IBAction)homeTapped:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
将这个方法和按钮绑定,点击按钮就可以回到主菜单
![](https://img-my.csdn.net/uploads/201212/01/1354366729_2189.png)
接下来添加手势识别
添加点击,双击,左滑动,右滑动
在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就可以了。