Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Cocos2D Scene Management

    • 0
    • 8
    • 2
    • 1
    • 1
    • 0
    • 0
    • 0
    • 379
    Comment on it

    One of the building blocks of a cocos2d app is the scene.Now,the question arises,What scene is?A scene is specially a node that contains any other nodes visible and is responsible for everything's running on the screen, including actions.Scenes can be transformed manually or by using actions.Your app can have many scenes, but only one of them is active at a given time.

    Now,As we are aware about how important scene is ! as it is the building block of a cocos2d app,its management plays vital role in a cocos2d app.The director, CCDirector, is responsible of scene management. If you take a look at your AppDelegate.m file, at the end of the applicationDidFinishLaunching method you'll find:

    [[CCDirector sharedDirector] runWithScene: [GamePlayScene scene]];
    

    We're just telling the game to start, running the first scene of the game, in this case a GamePlayScene with Hello World.

    alt text

    There's always just one scene running at a time. You can change the current scene using replaceScene:

    [[CCDirector sharedDirector] replaceScene: [GameOverScene scene]];
    

    Don't use runWithScene to change current scene, as it's to be used just for the first scene. Everytime you replace scene, the previous scene will be deleted and any running actions stopped. To retrieve a reference to the current scene running, use runningScene:

    CCScene* currentScene = [[CCDirector sharedDirector] runningScene];
    

    There are such cases, where you may want to add a scene but still keep the previous scene on memory, for example if you pause the game and show the options screen.

    In this case, instead of replacing the scene, you can push another scene on the scene stack and suspend the previous scene using pushScene:

    [[CCDirector sharedDirector] pushScene: [OptionScene scene]];
    

    And then, when you're done, with this,just pop the new scene in order to return to the previous scene:

    [[CCDirector sharedDirector] popScene];
    

    Avoid pushing too many scenes as you would shortly run out of memory. Use it only when strictly required, in any other case just replace the scene using:

    [[CCDirector sharedDirector] replaceScene: [GameOverScene scene]];
    

    You can also pause your game current scene in this way:

    [[CCDirector sharedDirector] pause];
    

    This will pause all running timers and actions, and frame rate will be lowered to 4FPS to save CPU. The scene will still be drawn. Then, resume the scene using resume:

    [[CCDirector sharedDirector] resume];
    

    To check if the game is currently paused, check the isPaused property:

    gamePaused = [[CCDirector sharedDirector] isPaused];
    

    Have a happy coding..

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: