Rony
Rony

Reputation: 1229

Add cocos2d scene to UIViewController

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..

Upvotes: 4

Views: 3076

Answers (4)

Mika
Mika

Reputation: 45

    @synthesize window;

- (void)loadView {
    // Initialization code
    CC_DIRECTOR_INIT();
    CCDirector *director = [CCDirector sharedDirector];
    //landscape
    [director setDeviceOrientation:kCCDeviceOrientationPortrait];
    [director setDisplayFPS:YES];

    //turn on multi-touch
    EAGLView *cocosView = [director openGLView];
    [cocosView setMultipleTouchEnabled:YES];

    self.view = cocosView;

    //default texture formats...
    [CCTexture2D  setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];

    [[CCDirector sharedDirector] runWithScene:[MainGame scene]];
}

Upvotes: 1

Matt Williamson
Matt Williamson

Reputation: 40193

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

Upvotes: 3

Colin Gislason
Colin Gislason

Reputation: 5589

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.

Upvotes: 1

Related Questions