Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8094233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:44:13+00:00 2026-06-05T20:44:13+00:00

I have a cocos2d powered game that uses UIKit menues, so I only use

  • 0

I have a cocos2d powered game that uses UIKit menues, so I only use the framework for one viewcontroller, which is the game itself. Also, it only has one scene. Since cocos2d 2.0 the director itself is a UIViewController subclass, so I just push it in my MenuViewController when the user taps a start button:

-(void)startGameButtonPressed {

    CCDirectorIOS* director = (CCDirectorIOS *) [CCDirector sharedDirector];


    // Create an CCGLView with a RGB565 color buffer, and a depth buffer of 0-bits
    self.glView = [CCGLView viewWithFrame:CGRectMake(0, 0, 480, 320)
                              pixelFormat:kEAGLColorFormatRGB565    //kEAGLColorFormatRGBA8
                              depthFormat:0 //GL_DEPTH_COMPONENT24_OES
                       preserveBackbuffer:NO
                               sharegroup:nil
                            multiSampling:NO
                          numberOfSamples:0];

    // attach the openglView to the director
    [director setView:glView];
    [director runWithScene:[GameLayer scene]];
    [director setDelegate:(id <CCDirectorDelegate>) [[UIApplication sharedApplication] delegate]];
    [self.navigationController pushViewController:director animated:YES];
}

This works fine for the first time the method is called, when the user starts the first game. When the game is over, I call [[CCDirector sharedDirector] end].

Most of the director setup is done in the appDelegate (it’s taken unchanged from the default Cocos2d template). I only put the CCGLView as a retained property into my MenuViewController, because otherwise the app crashes when [[CCDirector sharedDirector] end] is called and the CCGLView is not retained. I think that might be a cocos2d bug. In [[CCDirector sharedDirector] end] the framework calls [self setView:nil], but it still tries to access the view later on (probably on another thread).

The problem now is that on the second call of my method above (when the user wants to start another game from the menu), startGameButtonPressed, the director gets pushed but the screen remains black. The game is running and responding, I just don’t see anything. Can someone please help me out with that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:44:15+00:00Added an answer on June 5, 2026 at 8:44 pm

    OK, I had the same problem and I was able to “fix it”.

    When you set the CCGLView and [director setView:], even if you pop the controller the scene still exists. the only thing that happens is that the scene gets stopped.

    So in order to have the “restart” working, you have to check if there is already a running scene (even if it’s stopped, and instead of runWithScene: you use replaceScene:.

    Here is my code so you can see:

    - (void)setupCocos2D {
        CCGLView *glView = [CCGLView viewWithFrame:CGRectMake(0, 0, 320, 480)
                                   pixelFormat:kEAGLColorFormatRGB565   //kEAGLColorFormatRGBA8
                                   depthFormat:0    //GL_DEPTH_COMPONENT24_OES
                            preserveBackbuffer:NO
                                    sharegroup:nil
                                 multiSampling:NO
                               numberOfSamples:0];
    
    // HERE YOU CHECK TO SEE IF THERE IS A SCENE RUNNING IN THE DIRECTOR ALREADY    
    if(![director_ runningScene]){
        [director_ setView:glView]; // SET THE DIRECTOR VIEW
        if( ! [director_ enableRetinaDisplay:YES] ) // ENABLE RETINA
            CCLOG(@"Retina Display Not supported");
    
        [director_ runWithScene:[HelloWorldLayer scene]]; // RUN THE SCENE
    
    } else {
        // THERE IS A SCENE, START SINCE IT WAS STOPPED AND REPLACE TO RESTART
        [director_ startAnimation];
        [director_ replaceScene:[HelloWorldLayer scene]];
    }      
    
    
    [director_ setDelegate:(id <CCDirectorDelegate>) [[UIApplication sharedApplication] delegate]];
    
    // I DO NOT PUSH BECAUSE I ALREADY PUSHED TO THIS CONTROLLER, SO I ADD THE COCOS2D VIEW AS A SUBVIEW
    [self.view addSubview:[director_ view]];
    
    }
    

    Hope this code will help you, because I spent a whole day trying to figure this out.
    It may not be the correct way or even the prettiest way, but it’s working 🙂

    EDIT:
    Also, please not that if you POP the COCOS2D scene, you don’t have to [[CCDirector sharedDirector] end] as the animation will be stopped when the view is dealloc/removed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text in my game which is written in cocos2d. The text
I have a usual iOS Cocos2d game. I start with a MainMenuViewController, which then
I have game application (cocos2d) in which, a ball is to be shooted from
I have a project containing cocos2d framework, sharekit, asi & json. I've noticed that
I have a game in the works in Cocos2d. I have enemies that spawn
Hi guys I am new to the cocos2D.In my game i have one scene
I have an Xcode project (for an iPhone application) that uses cocos2d. I have
My statusbar orientation is landscape, I have a cocos2d view that only supports portrait
I have a small app that uses cocos2d to run through four levels of
I have Cocos2D in my app and I use it as the game engine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.