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:CCDirectorTypeDispl开发者_C百科ayLink] )
[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..
attachInView will be deprecated. Try using setOpenGLView
instead. http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_director.html#a87f9460b05b18b5c7726e1bdcbfe3eca
From the error, it looks like one of two things is happening:
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.You are calling
[CCDirector setDirectorType:]
somewhere else in your code. This seems unlikely, but doing a search for it would be helpful.
maybe its really getting called twice?
@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]];
}
精彩评论