位置: 编程技术 - 正文
推荐整理分享UIView和Coco2d-x场景之间的相互跳转切换(转)(uiview和calayer关系),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:cocos2d与unity3d,cocos2d和unity2d,cocos2d和unity2d,cocos2d与unity3d,uiview和calayer关系,uiviewpropertyanimator,uiviewcontentmode,uiview和uiviewcontroller,内容如对您有帮助,希望把文章链接给更多的朋友!
那么就需要涉及到UIView和Cocos2dx场景之间的切换。那么要如何实现呢?
我们如果在xcode中新建一个cocos2dx项目,在ios文件夹中就可以发现,其实这个cocos2dx就是EAGLView,这是一个UIView。
然后这个view封装到RootViewController中;最后,这个controller在AppController中设置为window的root view controller.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // Add the view controller's view to the window and display. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; EAGLView *__glView = [EAGLView viewWithFrame: [window bounds] pixelFormat: kEAGLColorFormatRGBA8 depthFormat: GL_DEPTH_COMPONENT preserveBackbuffer: NO sharegroup: nil multiSampling: NO numberOfSamples:0 ]; // Use RootViewController manage EAGLView viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil]; viewController.wantsFullScreenLayout = YES; viewController.view = __glView; // Set RootViewController to window if( [[UIDevice currentDevice].systemVersion floatValue] < 6.0) { // warning: addSubView doesn't work on iOS6 [window addSubview: viewController.view]; } else { // use this method on ios6 [window setRootViewController:viewController]; } [window makeKeyAndVisible]; [[UIApplication sharedApplication] setStatusBarHidden: YES]; cocos2d::CCApplication::sharedApplication()->run(); returnYES;}好了,有了上面的了解,那么我们就有办法了。
假如现在我们的要求是:程序启动,第一个视图是UIView,然后点击button,调到cocos2dx的一个场景,然后点击button还可以返回到UIView。
运行的效果是:
点击跳到Scene跳转到第二个图片所示的视图;点击跳到View跳回到第一个图片所示的视图。
实现过程:
(1)很明显这是两个view controller控制下的两个view视图:第一个view只包含了一个button(MyViewController);
第二个view包含了一个cocos2dx视图和一个button(RootViewController)。
(2)所以问题就变成了这两个view之间跳转的问题了,下面我采用navigation controller来处理这二者的切换。
(3)先看看AppController.h
#import "MyViewController.h" @interface AppController : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate, UITextFieldDelegate,UIApplicationDelegate> @property (nonatomic, retain) UIWindow *window; @property (nonatomic, retain) UINavigationController* navController; @property (nonatomic, retain)MyViewController* myController; @end然后:AppController.mm
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; myController = [[MyViewController alloc] init]; navController = [[UINavigationController alloc] init]; [navController pushViewController:myController animated:NO]; navController.navigationBarHidden = YES; // Set RootViewController to window if( [[UIDevice currentDevice].systemVersion floatValue] < 6.0) { // warning: addSubView doesn't work on iOS6 [window addSubview:navController.view]; } else { // use this method on ios6 [window setRootViewController:navController]; } [window makeKeyAndVisible]; [[UIApplication sharedApplication] setStatusBarHidden: YES]; // cocos2d::CCApplication::sharedApplication()->run(); 注意暂时不可以run returnYES;}注意最后面:cocos2d::CCApplication::sharedApplication()->run(); 要注释掉,因为开的时候运行的是第一个view,
此时cocos2dx还没有跑起来。
(4)所以看看这两个视图controller:
①第一个图片所对应的MyViewController
- (void)viewDidLoad{ [superviewDidLoad]; // Do any additional setup after loading the view. UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(0, , , ); [button setTitle:@"跳到 Scene" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];} -(void)buttonPressed:(id)sender{ RootViewController* rootController = [[RootViewController alloc]init]; [self.navigationController pushViewController:rootController animated:YES];}②第二个图片所对应的RootViewController
- (void)viewDidLoad { [superviewDidLoad]; //添加一个button UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(0, , , ); [button setTitle:@"跳到 View" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; [self addGLView]; //添加cocos2dx视图 } -(void)buttonPressed:(id)sender{ CCDirector::sharedDirector()->end(); [self.navigationController popViewControllerAnimated:YES];} -(void)addGLView{ UIWindow *window = [UIApplication sharedApplication].keyWindow; EAGLView *__glView = [EAGLView viewWithFrame: [window bounds] pixelFormat: kEAGLColorFormatRGBA8 depthFormat: GL_DEPTH_COMPONENT preserveBackbuffer: NO sharegroup: nil multiSampling: NO numberOfSamples:0 ]; [__glView setFrame:CGRectMake(0, 0, , )]; [self.view addSubview:__glView]; cocos2d::CCApplication::sharedApplication()->run();//这个时候就run} // Override to allow orientations other than the default portrait orientation.// This method is deprecated on ios6- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { returnUIInterfaceOrientationIsPortrait( interfaceOrientation );} // For ios6, use supportedInterfaceOrientations & shouldAutorotate instead- (NSUInteger) supportedInterfaceOrientations{#ifdef __IPHONE_6_0 returnUIInterfaceOrientationMaskPortrait;#endif} - (BOOL) shouldAutorotate { returnYES;}好咯,主要的代码都已经贴出来了。
(5)下面再具体的看看其中关键的部分:
RootViewController
①我们创键了EAGLView的实例,作为subview添加到当前的view。而且设置它的rect为(0,0,,)
那么我们可以在HelloWorld 场景中:
//输出当前cocos2dx的size CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCLog("width = %f;height = %f",winSize.width,winSize.height);可以发现:输出的和所设置的是一致的。② 创建EAGLView实例的代码可以直接从项目初始的AppController.mm中直接超过了,然后,注意要:
cocos2d::CCApplication::sharedApplication()->run(); 让cocos2dx跑起来。
③注意其中的屏幕方向也要设置一下(为竖向)。
④现在看看,如何从这个view调回到原来的view呢?在button的回调方法中:
-(void)buttonPressed:(id)sender{ CCDirector::sharedDirector()->end(); [self.navigationController popViewControllerAnimated:YES];}很重要的是第一个语句,先结束掉当前的cocos2dx,然后再调回到原来的view。
好咯,大概的过程就是这个样子了,哥我为了这个过程,浪费了半天的时间,尤其是在cocos2dx场景如何跳转回到UIView的问题上。
原来我是想在cocos2dx的场景中的一个menu item 的回调方法中执行跳回view的功能,但是始终不能实现;
最后我想到不要cocos2dx覆盖整个视图,而是作为一个subview,然后添加一个button,直接让这个对应的controller去处理视图的跳转就可以了。
上面的跳转使用到navigation,如果不想添加这个容器的话,直接在controller之间跳转也是可以的。
下面也给出实现的代码,区别只是在于跳转的代码,其他都一样。
AppController.mm
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // Add the view controller's view to the window and display. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; myController = [[MyViewController alloc] initWithNibName:nil bundle:nil]; // Set RootViewController to window if( [[UIDevice currentDevice].systemVersion floatValue] < 6.0) { // warning: addSubView doesn't work on iOS6 [window addSubview: myController.view]; } else { // use this method on ios6 [window setRootViewController:myController]; } [window makeKeyAndVisible]; [[UIApplication sharedApplication] setStatusBarHidden: YES]; // cocos2d::CCApplication::sharedApplication()->run(); returnYES;}RootViewController.mm
-(void)buttonPressed:(id)sender{ CCDirector::sharedDirector()->end(); [self dismissViewControllerAnimated:YES completion:nil];}MyViewController
-(void)buttonPressed:(id)sender{ RootViewController* rootController = [[RootViewController alloc]init]; [self presentViewController:rootController animated:YES completion:nil];}下面为了有需要的同志,给出navigation实现的代码下载链接:点击打开链接下载链接:cocos2dx游戏 方块向前冲 开源 方块向前冲是我第一个游戏项目,也算是一个小小的尝试吧,在开发过程中借鉴了很多博客和开源项目,感觉自己各方面都有很大提升,现在决定把这
[置顶] Cocos2d-x《雷电大战》(3)-子弹无限发射 林炳文Evankaka原创作品。转载请注明出处
基于cocoStudio和BMfont的艺术字体制作 我们在制作游戏的过程中经常要使用各式各样的艺术字体,这些字体让我们的游戏看起来更加美观更加的萌(--!)但是很多的新手都不知道这些字体是
标签: uiview和calayer关系
本文链接地址:https://www.jiuchutong.com/biancheng/368946.html 转载请保留说明!下一篇:cocos2dx游戏 方块向前冲 开源(cocos2dx开发的游戏)
友情链接: 武汉网站建设