我有一个 Cocos2d 项目,我想在整个应用程序中保持不变的背景。在applicationDidFinishLaunching
其委托的方法中,我替换了以下行:
[viewController setView:glView];
和
[[viewController view] addSubview:glView];
因为我在 RootViewController 的 initWithNib 视图中添加了子视图,如果用 glView 替换视图,这些更改就会丢失。
我还将 glView 的 pixelFormat 从更改kEAGLColorFormatRGB565
为kEAGLColorFormatRGBA8
. 当我进行更改时,glView 变得透明并且我可以看穿它,但是 fps 急剧下降。如果我不进行更改,则视图不会变得透明,但我看不到 fps 的大幅下降。我说的是 fps 的显着下降,从 59.0-60.0 下降到大约 35.0-42.0。
我在上面的 addSubview 行下方使用此代码来使视图透明:
glClearColor(0, 0, 0, 0);
director.openGLView.backgroundColor = [UIColor clearColor];
director.openGLView.opaque = NO;
最后两行是罪魁祸首;将它们注释掉(两者,而不仅仅是一个)会导致 fps 大幅下降,而注释掉该glClearColor
行对 fps 没有影响。
整个 applicationDidFinishLaunching 方法如下所示:
- (void) applicationDidFinishLaunching:(UIApplication*)application {
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if(![CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeDefault];
CCDirector *director = [CCDirector sharedDirector];
// Init the View Controller
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:kEAGLColorFormatRGBA8
depthFormat:0
];
// attach the openglView to the director
[director setOpenGLView:glView];
if(![director enableRetinaDisplay:YES] )
CCLOG(@"Retina Display Not supported");
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
#else
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
#endif
[director setAnimationInterval:1.0/60];
[director setDisplayFPS:YES];
// make the OpenGLView a child of the view controller
[[viewController view] addSubview:glView];
//***make glView transparent***
glClearColor(0, 0, 0, 0);
director.openGLView.backgroundColor = [UIColor clearColor];
director.openGLView.opaque = NO;
// 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];
// Removes the startup flicker
[self removeStartupFlicker];
// Run the intro Scene
[[CCDirector sharedDirector] runWithScene:[MainMenu scene]];
}
关于为什么会发生这种情况的任何想法?如果需要,我可以提供更多代码。