iphone - Cocos2d 与 UIKit View 混合 - 巨大的 FPS 下降

标签 iphone ios cocos2d-iphone core-animation

我有一个用 Cocos2d 编写的游戏。在它之上,我展示了一些 UIKit 对象。

然后我向其中添加了一个 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-game 的初始化发生在 viewDidAppear: 方法中,这就是我从 Controller 调用它的原因。

当以这种方式添加 View 时,我注意到我在那里使用的动画(缩放 + 平移 + View 翻转)非常非常不稳定。在我的 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]];       
}

最佳答案

我认为您正在使用 CoreAnimation 来为您的 UIKit View 设置动画。这可能是问题所在。 Cocos2D 不会给您的 UIKit View 动画留下太多处理时间,除非您暂停 Cocos2D 或完全停止它(CCDirector stopAnimation/startAnimation)。

您可以做的是使用 CCNode 作为您正在使用的每个 UIKit 对象的包装器。 CCNode 包含 UIKit View 并更新其位置以与自己的位置同步。然后,您可以使用 Cocos2D 动画操作(即带有缓动的 MoveTo)来为节点和 UIKit View 设置动画。

关于iphone - Cocos2d 与 UIKit View 混合 - 巨大的 FPS 下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7497385/

相关文章:

iphone - 支持不同大小单元格的 iOS GridView

objective-c - 如何使用 Cocos2D 在 OS X 中将帧速率限制为 30fps

iphone - NSString 中的方法

iphone - 如何为iPhone和iPhone模拟器编译C++0x代码

ios - NSDictionary 初始化(contentsOfFile :) is deprecated so now what?

ios - 验证用户身份的地方-游戏中心

objective-c - 奇怪的错误 : CGImageProviderCreate: invalid image provider size: 55 x 36

iphone - 将数据源分配给一个 View Controller 中的两个 TableView

iphone - 用户界面控制 : Touch Up Inside work but Touch Up Outside does not

iOS 14 WidgetKit 可用内存