我有一个用 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/