0

I've made small game in Xcode 4.2.

After I played all game level, the high-score-input window will appear. I want to add NSTextField to here, so the player could type his name by keyboard.

I added NSTextField to OpenGlView, but I can't see it on the screen. What's the matter with this? I also try to add subview and add NSTextField, but I get the same result.

Xcode4.2, simulator is My Mac - 32bit.

    NSView *view = [[NSView alloc] initWithFrame:NSRectFromCGRect(CGRectMake(0, 0, 480, 320))];

    [[[CCDirector sharedDirector] openGLView] addSubview:view];

    [[view layer] setZPosition:1];
//        
    NSTextField *textField = [[NSTextField alloc] initWithFrame:NSRectFromCGRect(CGRectMake(0, 0, 100, 40))];
    [view addSubview:textField];
0x8badf00d
  • 6,391
  • 3
  • 35
  • 68
bTagTiger
  • 1,261
  • 5
  • 23
  • 38
  • Maybe its the zPosition issue. Have you tried to set it as the subclass of `window`? – Kjuly Dec 05 '11 at 16:17
  • 1
    Inappropriate TITLE for question you asked. – 0x8badf00d Dec 05 '11 at 16:22
  • 1
    You'll be more likely to get good answers by improving your question than by adding a bounty. – Stephen Darlington Dec 05 '11 at 16:39
  • NSView *view = [[NSView alloc] initWithFrame:NSRectFromCGRect(CGRectMake(0, 0, 480, 320))]; [[[CCDirector sharedDirector] openGLView] addSubview:view]; [[view layer] setZPosition:1]; NSTextField *textField = [[NSTextField alloc] initWithFrame:NSRectFromCGRect(CGRectMake(0, 0, 100, 40))]; [view addSubview:textField]; This is my code. Please see and help me. – bTagTiger Dec 05 '11 at 17:18

1 Answers1

5

As the documentation for NSOpenGLView states, it does not support subviews:

"An NSOpenGLView object cannot have subviews. You can, however, divide a single NSOpenGLView into multiple rendering areas using the glViewport function."

While I'm not sure whether you're using your own NSOpenGLView subclass, or whether cocos2d provides a view itself, but it will likely have the same "limitations".

The NSTextField could be in the same window, provided it's located such that it doesn't overlap the OpenGL view.

Otherwise, you could just implement that part of the UI in a separate NSWindow.

This sample project displays the dialog as a sheet, attached to the main window with the OpenGL view:

enter image description here

Project: OpenGLHighScore.zip

NSGod
  • 22,699
  • 3
  • 58
  • 66