The coco2d game which is UIView is launched from viewcontroller as such
coco2dgame=[[coco2d_view alloc] initWithFrame:CGRectMake(0,0,320,480)];
[self.view addSubview:coco2dgame];
when it ends
[coco2dgame.director end];
[coco2dgame removeFromSuperview];
coco2dgame=nil;
when I want to relaunch I call again
coco2dgame=[[coco2d_view alloc] initWithFrame:CGRectMake(0,0,320,480)];
[self.view addSubview:coco2dgame];
but I am getting errors
OpenGL error 0x0502 in -[CCSprite draw] 532
OpenGL error 0x0502 in -[CCSprite draw] 532
Your code setup seems to be a little whacky. You are loading cocos2d on a UIView, but retaining the director (a ViewController) on that view. Launching and ending the cocos2d engine within a ViewController structure can be a bit tricky, but this is what I use in my game:
Step 1: Modify the standard code in AppDelegate.m from the template. You need to comment out all of the lines dealing with the _director ivar, and change the root view controller from _director to your ViewController. The game will now launch into that ViewController within the navController created in the cocos2d template code instead of the _director.
Step 2: The ViewController that you’re launching cocos2d from needs to have a method that is called in -init that creates and, this is important, retains the CCGLView that the director uses as its view, like so:
Keeping the CCGLView around is important to preventing some OpenGL errors, as cocos2d seems to have an issue recreating it after it is destroyed. Like I said, this method is called only once, in the -init method of the ViewController you are launching cocos2d from.
Step 3: Create a method in your ViewController to setup the director and push it onto the navigation controller’s stack, like so:
Most of that code is actually copied from the commented out code in AppDelegate where the original director would have been setup. Notice, you set the director’s view to the CCGLView you create and retain in your ViewController’s -init. In this method, you have the newly created director push your startup scene.
Step 4: Inside the game layer, whenever you want to return to your ViewController that the game (cocos2d) is launched from, implement this code:
That should allow you to move freely from a view controller, to the director in cocos2d (also a sub-class of UIViewController), and back without any issues. Hopefully that explains it in sufficient detail for you, let me know how it goes!