views:

334

answers:

3

Hi, I use the following code to add cocos2d scene to a viewcontroller

- (void)viewDidLoad {
    self.view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];


    if( ! [CCDirector setDirectorType:CCDirectorTypeDisplayLink] )
        [CCDirector setDirectorType:CCDirectorTypeDefault];

    [[CCDirector sharedDirector] setPixelFormat:kPixelFormatRGBA8888];

    [CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];


    [[CCDirector sharedDirector] setDeviceOrientation:kCCDeviceOrientationPortrait];
    [[CCDirector sharedDirector] setAnimationInterval:1.0/60];

    [[CCDirector sharedDirector] attachInView:self.view];
    ///adding HelloWorld scene to the view...
    [[CCDirector sharedDirector] runWithScene: [HelloWorld scene]];
    [super viewDidLoad];
}

And now i need to set the alpha value of self.view....so i did it..

-(void)displaySharePage
{
    self.view.alpha=0;

}

But it crashed......don't know why....i got the message..

'A Director was alloced. setDirectorType must be the first call to Director'

can anyone help.....advance thanks..

+1  A: 

From the error, it looks like one of two things is happening:

  1. viewDidLoad is being called more than once. You can check this by adding a log statement or breakpoint at the beginning of the method. This will help you find the root cause. You have to make sure that the director code is only called once. One way (not necessarily the right way) is to move the [CCDirector setDirectorType:] call to your app delegate.

  2. You are calling [CCDirector setDirectorType:] somewhere else in your code. This seems unlikely, but doing a search for it would be helpful.

Colin Gislason
+2  A: 

attachInView will be deprecated. Try using setOpenGLView instead. http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_director.html#a87f9460b05b18b5c7726e1bdcbfe3eca

Matt Williamson
+1  A: 

maybe its really getting called twice?