2011-09-21 66 views
2

我有一个用Cocos2d编写的游戏。最重要的是,我展示了一些UIKit对象。Cocos2d与UIKit视图混合 - 巨大的FPS下降

然后,我添加了一个UIViewController来处理使用UIKit对象和UIView动画编写的单独迷你游戏。我用这个添加到我的Cocos2D场景中的代码如下:

gameVC = [[[UGameViewController alloc] 
             initWithNibName:@"UGameViewController" bundle:nil] 
            retain]; 

[[[CCDirector sharedDirector] openGLView] addSubview:gameVC.view]; 
gameVC.parentClass = self; 
[gameVC viewDidAppear:YES]; 

的UIKit的游戏的初始化发生在viewDidAppear:方法,这就是为什么我把它从控制器。

当以这种方式添加视图时,我注意到我在那里使用的动画(缩放+翻译+视图翻转)非常非常不连贯。在我的iPad上运行应用程序并查看CoreAnimation工具可以显示出戏剧性的FPS下降 - 从cocos2d场景中的40-60 fps到UIKit部件上的4-8 fps。

有什么我可以做的,以便以某种方式解决它?


编辑:我CCDirector初始化是这样的:

- (void) applicationDidFinishLaunching:(UIApplication*)application 
{ 
    window = [[ShakeEnabledUIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    // Try to use CADisplayLink director 
    // if it fails (SDK < 3.1) use the default director 
    if(! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink]) 
     [CCDirector setDirectorType:kCCDirectorTypeDefault]; 


    CCDirector *director = [CCDirector sharedDirector]; 

    viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil]; 
    viewController.wantsFullScreenLayout = YES; 

    // 
    // Create the EAGLView manually 
    // 1. Create a RGB565 format. Alternative: RGBA8 
    // 2. depth format of 0 bit. Use 16 or 24 bit for 3d effects, like CCPageTurnTransition 
    // 
    // 
    EAGLView *glView = [EAGLView viewWithFrame:[window bounds] 
            pixelFormat:kEAGLColorFormatRGB565 
            depthFormat:0       
          preserveBackbuffer:NO]; 

    [director setOpenGLView:glView]; 

#if GAME_AUTOROTATION == kGameAutorotationUIViewController 
    [director setDeviceOrientation:kCCDeviceOrientationPortrait]; 
#else 
    [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft]; 
#endif 

    [director setAnimationInterval:1.0/60];  

    // make the OpenGLView a child of the view controller 
    [viewController setView:glView]; 
    // make the View Controller a child of the main window 
    [window addSubview: viewController.view]; 

    [window makeKeyAndVisible]; 

    // 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]; 

    // Run the intro Scene 
    [[CCDirector sharedDirector] runWithScene: [Main scene]];  
} 
+0

向我们展示您的CCDirector初始化plz – klefevre

+0

它来自标准cocos2d模板。编辑问题以添加该信息。 – kender

回答

1

我想你正在使用CoreAnimation动画您的UIKit意见。这可能是问题所在。除非您暂停Cocos2D或完全停止它(CCDirector stopAnimation/startAnimation),否则Cocos2D不会为您的UIKit视图动画留下太多处理时间。

你可以做的是使用CCNode作为你正在使用的每个UIKit对象的包装。 CCNode包含UIKit视图并更新其位置以与其自己的位置同步。然后,您可以使用Cocos2D动画操作(即MoveTo with easing)来为节点设置动画效果,从而实现UIKit视图。

+0

你能举一个关于如何将UIKit对象包装到CCNode的代码示例吗?让我们说一个CCNode上的UIView。谢谢。我尝试过CCUIViewWrapper,但是它总是会将包装视图渲染到其他所有视图上,忽略其他精灵的zOrder。 – SpaceDog