之前cocos2d的文章都是由魏凯同学维护,从今天开始我也会抽时间写点cocos2d的文章。最近在研究如何将IOS游戏与软件结合起来。通常游戏开发中仅仅只需要一个ViewController即可,所有的图片动画渲染、场景的切换都是在这一个ViewControlelr中完成。但是软件开发中每一个界面都会是一个ViewController,为什么游戏和软件这点上区别很大呢?原因很简单,游戏属于自定义View 而软件需要借助系统提供的高级控件,这些高级控件也就是系统封装的View。比如Label、Button、ImageView 等等。。
大家可以仔细看一下cocos2d的源码,先找到入口函数我和大家分析一下。
1 2 3 |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
在方法中找到下面 ,CCDirector继承于ViewController,cocos2d的OpenGlView也是添加在这个ViewController之上。
1 2 3 |
director_ = (CCDirectorIOS*) [CCDirector sharedDirector]; |
接着往下走,我们可以看到这段代码,意思就是将游戏的viewcontroller加在一个带导航栏的viewController之上,然后隐藏这个导航栏。 最后把这个这个viewController加在windows中。
1 2 3 4 |
navController_ = [[UINavigationController alloc] initWithRootViewController:director_]; navController_.navigationBarHidden = YES; |
下面我们分析一下,IOS程序在切换ViewController的时候必需要用UINavigationController ,cocos2d的UINavitionController 就是 [CCDirector sharedDirector].navigationController
然后我们做一个简单的例子,大家就明白了如何来使用。创建两个游戏场景,如下图所示,这是第一个游戏场景,第二个游戏场景我就不在截图。左下角是一个cocos2d的按钮,点击该按钮后将从cocos2d切换至全新的viewcontroller当中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
// // HelloWorldLayer.m // Navigation // // Created by 雨松MOMO on 12-11-18. // Copyright __MyCompanyName__ 2012年. All rights reserved. // // Import the interfaces #import "HelloGameLayer.h" // Needed to obtain the Navigation Controller #import "AppDelegate.h" #pragma mark - HelloWorldLayer // HelloWorldLayer implementation @implementation HelloGameLayer // Helper class method that creates a Scene with the HelloWorldLayer as the only child. +(CCScene *) scene { // 'scene' is an autorelease object. CCScene *scene = [CCScene node]; // 'layer' is an autorelease object. HelloGameLayer *layer = [HelloGameLayer node]; // add layer as a child to scene [scene addChild: layer]; // return the scene return scene; } // on "init" you need to initialize your instance -(id) init { // always call "super" init // Apple recommends to re-assign "self" with the "super's" return value if( (self=[super init]) ) { // create and initialize a Label CCLabelTTF *label = [CCLabelTTF labelWithString:@"游戏场景2" fontName:@"Marker Felt" fontSize:64]; // ask director for the window size CGSize size = [[CCDirector sharedDirector] winSize]; // position the label on the center of the screen label.position = ccp( size.width /2 , size.height/2 ); // add the label as a child to this Layer [self addChild: label]; CCMenuItem *starMenuItem = [CCMenuItemImage itemWithNormalImage:@"Icon-72.png" selectedImage:@"Icon.png" disabledImage:@"Icon.png" target:self selector:@selector(starButtonTapped:)]; starMenuItem.position = ccp(60, 60); starMenuItem.tag = 0; CCMenu *starMenu = [CCMenu menuWithItems:starMenuItem, nil]; starMenu.position = CGPointZero; [self addChild:starMenu]; //进入返回游戏后隐藏导航栏 [[CCDirector sharedDirector].navigationController setNavigationBarHidden:YES animated:NO]; } return self; } - (void)starButtonTapped:(id)sender { //点击按钮后在这里 //在这里打开全新的Viewcontroller [[CCDirector sharedDirector].navigationController pushViewController:[[[MyViewController alloc]init]autorelease] animated:YES]; //显示导航栏,不需要导航栏删掉该代码即可 [[CCDirector sharedDirector].navigationController setNavigationBarHidden:NO animated:NO]; } // 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) // don't forget to call "super dealloc" [super dealloc]; } #pragma mark GameKit delegate -(void) achievementViewControllerDidFinish:(GKAchievementViewController *)viewController { AppController *app = (AppController*) [[UIApplication sharedApplication] delegate]; [[app navController] dismissModalViewControllerAnimated:YES]; } -(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController { AppController *app = (AppController*) [[UIApplication sharedApplication] delegate]; [[app navController] dismissModalViewControllerAnimated:YES]; } @end |
如下图所示,在cocos2d中打开的新的viewController,此时cocos2d中的动画循环事件都会自动暂停,直到从新返回cocos2d的游戏当中即可从新打开。
代码比较简单我就不做过多的解释了,仔细看看就应该明白啦。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
// // MyViewController.m // Navigation // // Created by 雨松MOMO on 12-11-18. // // #import "MyViewController.h" @interface MyViewController () @end @implementation MyViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.view.backgroundColor = [UIColor whiteColor]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; UIButton *button0 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button0.frame = CGRectMake(0, 0, 100, 50); [button0 setTitle:@"回到场景1" forState:UIControlStateNormal]; [button0 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; button0.tag = 0; [self.view addSubview:button0]; UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button1.frame = CGRectMake(0, 100, 100, 50); [button1 setTitle:@"回到场景2" forState:UIControlStateNormal]; [button1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; button1.tag = 1; [self.view addSubview:button1]; self.title = @"雨松MOMO"; //返回按钮 UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc]initWithTitle:@"按钮" style:UIBarButtonItemStylePlain target:self action:@selector(backController)]; self.navigationItem.leftBarButtonItem = leftBarItem; [leftBarItem release]; } -(void) viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } -(void)backController { UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"标题" message:@"这是一个按钮" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil]; [alertView show]; [alertView release]; } -(void)buttonPressed:(id)sender { UIButton * button = (UIButton*)sender; //根据点击按钮切换当前游戏场景 switch (button.tag) { case 0: //使用replaceScene切换场景的时候会重新调用scene方法,这样就会重新执行init方法。 //如果不像重复初始化可以将场景存在变量中。 [[CCDirector sharedDirector]replaceScene:[HelloWorldLayer scene]]; break; case 1: [[CCDirector sharedDirector]replaceScene:[HelloGameLayer scene]]; break; default: break; } //如果你紧紧只是想返回上一个场景的话,其实不用写上面replaceScene这部分代码,直接执行下面的返回操作即可。 [self.navigationController popViewControllerAnimated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end |
切换场景的特效方式都可以以这样的方法来添加,快快把游戏和软件结合起来吧。
代码下载: http://vdisk.weibo.com/s/ioMx4
- 本文固定链接: https://www.xuanyusong.com/archives/1878
- 转载请注明: 雨松MOMO 于 雨松MOMO程序研究院 发表
cannot specify -o when generating multiple output files 这个问题怎么解决,我是把unity岛成Xcode
的文件放到我的Xcode project里面了,调整过arc兼容,配置,之后爆出的错误。。。要怎么解决
每日踩踩
过奖啦 蛤蛤
求转发,求拥抱….哈哈..
期待你的大作 哈哈哈!!!
你出的unity3d的书籍名字叫什么,我想买本,能给个链接,最好有教学目录
书里面讲的是unity的基础的基本的东西,没有这种与android ios平台相结合的东西。当然非常建议momo能把这些东西也写到书里,对开发者会有很大的帮助!
这样啊,我以前做ios的,现在开始学unity
这倒是真的,如果这些能弄进去,此书不火,还有天理,还有王法?
谢谢 以后一定会的。。
书籍已经在路上,哈哈,亚马逊
支持哈
感谢支持。。
交换个友链吧!【Software MyZone】:http://www.firedragonpzy.com.cn你的已加
表情达人。。。
杀 杀 杀 杀 ! 谁能当我!!! ——-难道这就是桃园吗。。。