我希望能够让 Cocos2d 包含在它自己的 NSWindowController 中,这样 AppDelegate 将能够管理在不同窗口中同时运行的多个不同的 Cocos2d 实例。
每次我在 NSWindowController 子类中加载 Cocos2d 时,我都会在 OpenGL 视图中看到一个空白视图。
为什么不能正确渲染场景?
Cocos2dWindowController.h
#import <Cocoa/Cocoa.h>
#import "cocos2d.h"
@interface Cocos2dWindowController : NSWindowController <CCDirectorDelegate>{
CCGLView *glView_;
}
@property (strong) IBOutlet CCGLView *glView;
@end
Cocos2dWindowController.mm
#import "Cocos2dWindowController.h"
#import "PuzzleWorld.h"
@interface Cocos2dWindowController ()
@end
@implementation Cocos2dWindowController
@synthesize glView=glView_;
- (id)initWithWindow:(NSWindow *)window
{
self = [super initWithWindow:window];
if (self) {
// Initialization code here.
}
return self;
}
- (void)windowDidLoad
{
[super windowDidLoad];
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
//Check to see if the view is empty prior to launching
CCDirectorMac *director = (CCDirectorMac*) [CCDirector sharedDirector];
// enable FPS and SPF
[director setDisplayStats:YES];
// connect the OpenGL view with the director
[director setView:glView_];
director.delegate = self;
// EXPERIMENTAL stuff.
// 'Effects' don't work correctly when autoscale is turned on.
// Use kCCDirectorResize_NoScale if you don't want auto-scaling.
[director setResizeMode:kCCDirectorResize_AutoScale];
// Enable "moving" mouse event. Default no.
[self.window setAcceptsMouseMovedEvents:NO];
// Center main window
[self.window center];
CCScene *scene = [CCScene node];
[scene addChild:[PuzzleWorld node]];
// Run whatever scene we'd like to run here.
if(director.runningScene)
[director replaceScene:scene];
else
[director runWithScene:scene];
}
//Override the close action to kill off Cocos2d and OpenGl
- (void)close{
[super close];
//Check to see if the view is empty prior to launching
CCDirectorMac *director = (CCDirectorMac*) [CCDirector sharedDirector];
[director setDelegate:nil];
[director end];
}
@end
这发生在带有 Xcode 版本 4.6 (4H127) 的 Cocos2d 2.0 上