0

我希望能够让 Cocos2d 包含在它自己的 NSWindowController 中,这样 AppDelegate 将能够管理在不同窗口中同时运行的多个不同的 Cocos2d 实例。

每次我在 NSWindowController 子类中加载 Cocos2d 时,我都会在 OpenGL 视图中看到一个空白视图。

生成的 OpenGL 视图子类化为 CCGLView

为什么不能正确渲染场景?

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

Xib 配置

这发生在带有 Xcode 版本 4.6 (4H127) 的 Cocos2d 2.0 上

4

1 回答 1

2

我不能告诉你为什么你的屏幕是白色的。如果您基于 Cocos2D Mac 模板,请检查 MainWindow.xib。Cocos2D 视图跨越整个窗口,所以只需调整它的大小并使用它。

可以告诉你的是,你运行多个 cocos2d 视图的目标是徒劳的:cocos2d 只支持一个视图。

多年来,对多视图的支持一直在路线图上,而且看起来不会很快出现。考虑到 cocos2d 对许多核心类(Director、ActionManager、Scheduler、Caches 等)的 Singleton 的依赖程度,这也是一个不小的变化。

于 2013-02-07T21:45:17.413 回答