avf*_*avf 7 iphone cocos2d-iphone ios
我有一个使用UIKit菜单的cocos2d动力游戏,所以我只将框架用于一个viewcontroller,这就是游戏本身.此外,它只有一个场景.由于cocos2d 2.0导演本身是一个UIViewController子类,所以MenuViewController当用户点击一个开始按钮时我就把它推进去:
-(void)startGameButtonPressed {
CCDirectorIOS* director = (CCDirectorIOS *) [CCDirector sharedDirector];
// Create an CCGLView with a RGB565 color buffer, and a depth buffer of 0-bits
self.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];
// attach the openglView to the director
[director setView:glView];
[director runWithScene:[GameLayer scene]];
[director setDelegate:(id <CCDirectorDelegate>) [[UIApplication sharedApplication] delegate]];
[self.navigationController pushViewController:director animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
当用户启动第一个游戏时,第一次调用该方法时,这样可以正常工作.比赛结束后,我打电话[[CCDirector sharedDirector] end].
大多数导演设置都是在appDelegate中完成的(它与默认的Cocos2d模板保持不变).我只将CCGLView保留属性作为我的保留属性MenuViewController,因为否则应用程序在[[CCDirector sharedDirector] end]被调用时崩溃并且CCGLView不保留.我认为这可能是一个cocos2d错误.在[[CCDirector sharedDirector] end]框架调用中[self setView:nil],但它仍然试图稍后访问该视图(可能在另一个线程上).
现在的问题是,在我上面的方法的第二次调用时(当用户想要从菜单开始另一个游戏时),startGameButtonPressed导演被推动但屏幕仍然是黑色的.游戏正在运行并响应,我只是没有看到任何东西.有人可以帮帮我吗?
好的,我有同样的问题,我能够"修复它".
当您设置CCGLView和[director setView:]时,即使您弹出控制器,场景仍然存在.唯一发生的事情就是场景停止了.
因此,为了使"重启"工作,你必须检查是否已经有一个正在运行的场景(即使它已停止,而不是runWithScene:你使用replaceScene:.
这是我的代码,所以你可以看到:
- (void)setupCocos2D {
CCGLView *glView = [CCGLView viewWithFrame:CGRectMake(0, 0, 320, 480)
pixelFormat:kEAGLColorFormatRGB565 //kEAGLColorFormatRGBA8
depthFormat:0 //GL_DEPTH_COMPONENT24_OES
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];
// HERE YOU CHECK TO SEE IF THERE IS A SCENE RUNNING IN THE DIRECTOR ALREADY
if(![director_ runningScene]){
[director_ setView:glView]; // SET THE DIRECTOR VIEW
if( ! [director_ enableRetinaDisplay:YES] ) // ENABLE RETINA
CCLOG(@"Retina Display Not supported");
[director_ runWithScene:[HelloWorldLayer scene]]; // RUN THE SCENE
} else {
// THERE IS A SCENE, START SINCE IT WAS STOPPED AND REPLACE TO RESTART
[director_ startAnimation];
[director_ replaceScene:[HelloWorldLayer scene]];
}
[director_ setDelegate:(id <CCDirectorDelegate>) [[UIApplication sharedApplication] delegate]];
// I DO NOT PUSH BECAUSE I ALREADY PUSHED TO THIS CONTROLLER, SO I ADD THE COCOS2D VIEW AS A SUBVIEW
[self.view addSubview:[director_ view]];
}
Run Code Online (Sandbox Code Playgroud)
希望这段代码可以帮到你,因为我花了一整天时间试图解决这个问题.它可能不是正确的方式,甚至不是最漂亮的方式,但它正在工作:)
编辑:
另外,请不要,如果你POP COCOS2D场景,你不必,[[CCDirector sharedDirector] end]因为动画将被视图dealloc /删除停止.
小智 5
我花了几天时间寻找与此相关的信息,并将与您分享我自己的经验.我也在尝试创建一个加载到UITableViewController中的游戏,当触摸一个单元格时,CCDirector会从中加载.这是一个游戏中心回合制游戏,因此设计(想想与朋友的话).到目前为止,我发现的最佳方法如下(注意我在2.0中工作 - 其中CCDirector是UIViewController子类):
在AppDelegate.h中,创建一个新的ivar来保存从模板代码创建的CCGLView.然后将didFinishLaunching中创建的CCGLView分配给新的ivar.这允许导演重复使用最初创建的视图,而不是每次重新加载CCDirector时都尝试重新创建它,这似乎会导致我的经验中出现各种奇怪的问题.
您还想在AppDelegate中创建一个名为-setupDirector的新方法,或者您可以在其中设置导演.每次重新创建CCDirector时都应调用此方法.我在下面发布了我的版本.注意我的CCGLView的ivar叫做"GLView".
- (void)setupDirector {
if (![CCDirector sharedDirector]) {
CCLOG(@"Calling setupDirector");
director_ = (CCDirectorIOS*) [CCDirector sharedDirector];
director_.wantsFullScreenLayout = YES;
// Display FSP and SPF
[director_ setDisplayStats:NO];
// set FPS at 60
[director_ setAnimationInterval:1.0/60];
// attach the openglView to the director
[director_ setView:GLView];
// for rotation and other messages
[director_ setDelegate:self];
// 2D projection
[director_ setProjection:kCCDirectorProjection2D];
// [director setProjection:kCCDirectorProjection3D];
// Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
if( ! [director_ enableRetinaDisplay:YES] )
CCLOG(@"Retina Display Not supported");
// Default texture format for PNG/BMP/TIFF/JPEG/GIF images
// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
// You can change anytime.
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
// If the 1st suffix is not found and if fallback is enabled then fallback suffixes are going to searched. If none is found, it will try with the name without suffix.
// On iPad HD : "-ipadhd", "-ipad", "-hd"
// On iPad : "-ipad", "-hd"
// On iPhone HD: "-hd"
CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils];
[sharedFileUtils setEnableFallbackSuffixes:NO]; // Default: NO. No fallback suffixes are going to be used
[sharedFileUtils setiPhoneRetinaDisplaySuffix:@"-hd"]; // Default on iPhone RetinaDisplay is "-hd"
[sharedFileUtils setiPadSuffix:@"-ipad"]; // Default on iPad is "ipad"
[sharedFileUtils setiPadRetinaDisplaySuffix:@"-ipadhd"]; // Default on iPad RetinaDisplay is "-ipadhd"
// Assume that PVR images have premultiplied alpha
[CCTexture2D PVRImagesHavePremultipliedAlpha:YES];
}
Run Code Online (Sandbox Code Playgroud)
此外,您还需要对模板加载视图控制器的方式进行一些更改.通常,cocos2D使用director_作为根视图控制器设置导航控制器.在这里,您要分配并初始化菜单视图控制器并添加THAT而不是director_:
// Create a Navigation Controller with the Director
gamesTVC_ = [[GamesTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
navController_ = [[UINavigationController alloc] initWithRootViewController:gamesTVC_];
navController_.navigationBarHidden = NO;
Run Code Online (Sandbox Code Playgroud)
didFinishLaunching中的其他所有内容都可以保持不变.现在,在startGameButtonPressed方法的menuViewController中,您将在app实例上调用新创建的setupDirector方法,该方法通过调用引用:
AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];
if ([CCDirector sharedDirector].runningScene) {
[[CCDirectorIOS sharedDirector] end];
}
[app setupDirector];
[app.navController pushViewController:app.director animated:YES];
Run Code Online (Sandbox Code Playgroud)
我包括一个检查,以确保CCDirector仍然没有运行,如果是,请结束它.在您的游戏层中,当您想要弹出视图控制器时,您只需调用它:
AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];
[app.navController popViewControllerAnimated:YES];
[[CCDirector sharedDirector] end];
Run Code Online (Sandbox Code Playgroud)
此流程应允许您自由使用导航控制器通过CCDirector推送您的游戏场景,并在您想要返回基于UIKit的主菜单时弹出该视图控制器.我希望这会有所帮助,因为我花了很多令人沮丧的时间试图为自己的游戏做到这一点.