Cocos 2d应用程序结构

时间:2011-07-13 11:47:45

标签: iphone cocos2d-iphone

我是cocos2d的新手,想知道有关cocos2d应用程序结构的一些事情。 以下是默认模板中appdidfinishlaunching函数中的代码。

- (void) applicationDidFinishLaunching:(UIApplication*)application
{
    // Init the window
    window = [[UIWindow 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];

    // 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:kEAGLColorFormatRGB565   // kEAGLColorFormatRGBA8
                                   depthFormat:0                        // GL_DEPTH_COMPONENT16_OES
                        ];

    // attach the openglView to the director
    [director setOpenGLView:glView];

//  // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
//  if( ! [director enableRetinaDisplay:YES] )
//      CCLOG(@"Retina Display Not supported");

    //
    // VERY IMPORTANT:
    // If the rotation is going to be controlled by a UIViewController
    // then the device orientation should be "Portrait".
    //
    // IMPORTANT:
    // By default, this template only supports Landscape orientations.
    // Edit the RootViewController.m file to edit the supported orientations.
    //
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
    [director setDeviceOrientation:kCCDeviceOrientationPortrait];
#else
    [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
#endif

    [director setAnimationInterval:1.0/60];
    [director setDisplayFPS:YES];


    // 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];


    // Removes the startup flicker
    [self removeStartupFlicker];

    // Run the intro Scene
    [[CCDirector sharedDirector] runWithScene: [HelloWorld scene]];     
}

我的问题是当我们向CCDirector添加一些孩子时,如果从未在窗口上添加CCDirector导演,这个东西是如何显示在屏幕上的。 我们有以下代码行

//使OpenGLView成为视图控制器的子节点     [viewController setView:glView];

这会将RootViewController的视图设置为glView,在此之前我们正在设置

[director setOpenGLView:glView]; 表示导演的openglView设置为glView。

我们不必将导演添加到viewcontroller,假设它可以被添加,这样无论我们向Director添加什么东西,它都会在屏幕上显示。

我的问题是,当我们从未在RootViewController上添加CCDirector时,如何在iphone屏幕上看到场景。

1 个答案:

答案 0 :(得分:1)

CCDirector将glView设置为RootViewController的视图。所有下一个绘图都是使用opengl进行的。在glView中。所以像CCLayer,CCNode,CCSprite等cocos2d类与UIView或UIViewController没什么关系