2

The coco2d game which is UIView is launched from viewcontroller as such

           coco2dgame=[[coco2d_view alloc] initWithFrame:CGRectMake(0,0,320,480)];
            [self.view addSubview:coco2dgame];

when it ends

         [coco2dgame.director end];
         [coco2dgame removeFromSuperview];
          coco2dgame=nil;

when I want to relaunch I call again

           coco2dgame=[[coco2d_view alloc] initWithFrame:CGRectMake(0,0,320,480)];
            [self.view addSubview:coco2dgame];

but I am getting errors

           OpenGL error 0x0502 in -[CCSprite draw] 532

            OpenGL error 0x0502 in -[CCSprite draw] 532
4

1 回答 1

2

您的代码设置似乎有点古怪。您正在 UIView 上加载 cocos2d,但在该视图上保留了 director(一个 ViewController)。在 ViewController 结构中启动和结束 cocos2d 引擎可能有点棘手,但这是我在游戏中使用的:

第 1 步:从模板中修改 AppDelegate.m 中的标准代码。您需要注释掉所有处理 _director ivar 的行,并将根视图控制器从 _director 更改为您的 ViewController。游戏现在将启动到 cocos2d 模板代码中创建的 navController 中的 ViewController,而不是 _director。

第 2 步:您要从中启动 cocos2d 的 ViewController 需要有一个在 -init 中调用的方法,该方法创建并且重要的是保留主管用作其视图的 CCGLView,如下所示:

AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];
myGLView = [[CCGLView viewWithFrame:[app.window bounds]
                        pixelFormat:kEAGLColorFormatRGB565  //kEAGLColorFormatRGBA8
                        depthFormat:0   //GL_DEPTH_COMPONENT24_OES
                 preserveBackbuffer:NO
                         sharegroup:nil
                      multiSampling:NO
                    numberOfSamples:0] retain];

保留 CCGLView 对于防止某些 OpenGL 错误很重要,因为 cocos2d 在销毁后重新创建它似乎存在问题。就像我说的,这个方法只被调用一次,在你启动 cocos2d 的 ViewController 的 -init 方法中。

第 3 步:在 ViewController 中创建一个方法来设置导向器并将其推送到导航控制器的堆栈中,如下所示:

AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];

CCDirectorIOS* director = (CCDirectorIOS *) [CCDirector sharedDirector];
director.wantsFullScreenLayout = YES;
[director setView:myGLView];
// Display FSP and SPF
[director setDisplayStats:NO];

// set FPS at 60
[director setAnimationInterval:1.0/60];

// for rotation and other messages
[director setDelegate:app];

// 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");

if (director.runningScene)
    [director replaceScene:[TwoPlayerBoard node]];
else
    [director pushScene:[TwoPlayerBoard node]];

[app.navController pushViewController:director animated:YES];

大多数代码实际上是从 AppDelegate 中已设置原始导演的注释掉的代码中复制的。请注意,您将导演的视图设置为您在 ViewController 的 -init 中创建和保留的 CCGLView。在这种方法中,您让新创建的导演推送您的启动场景。

第 4 步:在游戏层中,每当您想返回启动游戏 (cocos2d) 的 ViewController 时,请执行以下代码:

AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];
[app.navController popViewControllerAnimated:YES];
[[CCDirectorIOS sharedDirector] end];

这应该允许您从视图控制器自由移动到 cocos2d 中的主管(也是 UIViewController 的子类),然后返回没有任何问题。希望这可以为您详细解释它,让我知道它是怎么回事!

于 2012-07-28T22:18:27.233 回答