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的实例,然后AppController在applicationDidFinishLaunching方法中创建了main window.
// Create the main windowwindow_ = [[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添加值
然后创建一个空的XIB文件
设置File’s Owner为UIApplication
然后拖一个Object对象进去设置成AppController类
这样,XIB就会生成一个AppController的实例
然后把File’s Owner的delegate设置为AppController
这样,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对象,绑定起来
接下来,用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,并把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中把按钮处理函数绑定起来
然后在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就可以了。
- Cocos2d 2.0与UIKit结合使用
- Cocos2d和UIKit的结合使用
- 如何结合使用Cocos2d和UIKit
- UIKit框架-高级控件:6.UIPickerView与UIImageView结合使用
- 解决“UIkit与Vue.js结合使用时,UIkit的图标会显示两个”的问题
- cocos2d 中使用UiKit 各种view
- 怎样在 UIKit 中使用 Cocos2d-x
- Cocos2D-iPhone和Uikit混合使用问题
- Cocos2d-x 与Cocosbuilder结合使用的一些技巧
- UIKit框架-高级控件:8.UIPickerView, UILabel, UIImageView结合使用
- cocos2d+uikit常用函数
- iOS开发概述UIkit动力学,讲述UIKit的Dynamic特性。以及手势和动力结合使用的案例
- Cocos2d-X Scrollview与CCMenu的结合使用在屏幕滑动实例
- iOS 【UIKit-UINavigationController详解与使用(一)添加UIBarButtonItem】
- Box2D 使用setTarget 函数拖动body (结合cocos2d-x 2.0版)
- FCKEditor与ext结合使用
- Memcached 与 PHP 结合使用
- gridview与modalpopup结合使用
- android:descendantFocusability用法简析
- ubuntu常用命令
- c++类构造函数理解
- 斐波那契logn算法
- msSQL注入通杀,只要有注入点就有系统权限
- Cocos2d 2.0与UIKit结合使用
- 一个坐标点围绕任意中心点旋转--C#实现
- 第15讲 进程的介绍和管理
- 浅谈acm之路
- 明年薪酬涨幅或同比下降 85后离职率居高不下
- 每个人心中都有每个人的伤害
- 模块化的javaScript开发
- 定长串的基本操作
- U盘安装双系统