{"_id":"54679c8c98b6840800268982","is_link":false,"project":"5463e333447e0a100024a087","version":{"_id":"5463e333447e0a100024a08a","project":"5463e333447e0a100024a087","__v":6,"createdAt":"2014-11-12T22:46:11.486Z","releaseDate":"2014-11-12T22:46:11.486Z","categories":["5463e333447e0a100024a08b","54645c365871e90800f50151","547b1c5a4366a708001b3b50","547b1cdf4366a708001b3b5a","54e3bef8464a9c3700f7ca67","54ec651d7ab3172d00c01ab7"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"user":"54639d06447e0a1000249f7f","__v":25,"category":{"_id":"5463e333447e0a100024a08b","project":"5463e333447e0a100024a087","pages":["5463e334447e0a100024a08d","54646a285871e90800f50174","54667e7af2b6390800dfd82a","54679c8c98b6840800268982","54679d8498b6840800268987","546a2ed0394c861a00c3a413","546b6a4862515a14007ebbba","547b1c2e7eec0f080099e1dd","55d33d90b9ef4435006dec70","56570c764d1c720d001f51bb"],"version":"5463e333447e0a100024a08a","__v":10,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-11-12T22:46:11.951Z","from_sync":false,"order":1,"slug":"documentation","title":"Documentation"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2014-11-15T18:33:48.295Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"try":true,"basic_auth":false,"auth":"never","params":[],"url":""},"isReference":false,"order":3,"body":"As mentioned in the section *Cocos Basics* scenes are ordered on a stack which is LIFO (Last In First Out). Take a look at the diagram below to help understand this concept\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/97NdSEySOi20mUENPSVD_2000px-Data_stack.svg.png\",\n        \"2000px-Data_stack.svg.png\",\n        \"2000\",\n        \"1437\",\n        \"#0404fc\",\n        \"\"\n      ],\n      \"caption\": \"Push and Pop Stack\"\n    }\n  ]\n}\n[/block]\nScene can be created either in the same file as an existing scene or a new code file. It is recommended to abstract as much as you can from a scene so it is easier to manage and debug. The following topics will be covered in this section:\n[block:html]\n{\n  \"html\": \"<ul>\\n  <li><a href=\\\"#create-scene\\\"><i>Creating a new scene</i></a></li>\\n  <li><a href=\\\"#push-scene\\\"><i>Pushing a scene onto the stack</i></a> - which halts the execution of the current scene then runs and displays the new scene</li>\\n  <li><a href=\\\"#pop-scene\\\"><i>Popping a scene off the stack</i></a> - removes the current scene then runs and displays the scene below (only if a scene exists)</li>\\n  <li><a href=\\\"#replace-scene\\\"><i>Replacing a scene</i></a> - replaces the current scene with the new scene essentially popping the current scene off the stack and pushing the new scene onto the stack</li>\\n</ul>\"\n}\n[/block]\n##**Creating a new scene** \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"#ifndef __NEW_SCENE_H__\\n#define __NEW_SCENE_H__\\n\\n#include \\\"cocos2d.h\\\"\\n\\nclass NewScene : public cocos2d::Layer\\n{\\npublic:\\n    // there's no 'id' in cpp, so we recommend returning the class instance pointer\\n    static cocos2d::Scene* createScene( );\\n\\n    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone\\n    virtual bool init( );\\n    \\n    // a selector callback\\n    void menuCloseCallback( cocos2d::Ref* pSender );\\n    \\n    // implement the \\\"static create()\\\" method manually\\n    CREATE_FUNC( NewScene );\\n    \\n};\\n\\n#endif // __NEW_SCENE_H__\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Header\"\n    },\n    {\n      \"code\": \"#include \\\"NewScene.h\\\"\\n\\nUSING_NS_CC;\\n\\nScene* NewScene::createScene( )\\n{\\n    // 'scene' is an autorelease object\\n    auto scene = Scene::create( );\\n    \\n    // 'layer' is an autorelease object\\n    auto layer = NewScene::create( );\\n\\n    // add layer as a child to scene\\n    scene->addChild( layer );\\n\\n    // return the scene\\n    return scene;\\n}\\n\\n// on \\\"init\\\" you need to initialize your instance\\nbool NewScene::init( )\\n{\\n    //////////////////////////////\\n    // 1. super init first\\n    if ( !Layer::init( ) )\\n    {\\n        return false;\\n    }\\n    \\n    return true;\\n}\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++ Implementation\"\n    },\n    {\n      \"code\": \"var NewSceneLayer = cc.Layer.extend(\\n{\\n    ctor:function( )\\n  \\t{\\n        //////////////////////////////\\n        // 1. super init first\\n        this._super( );\\n\\n        return true;\\n    }\\n} );\\n\\nvar NewScene = cc.Scene.extend(\\n{\\n    onEnter:function( )\\n  \\t{\\n        this._super( );\\n\\n        var layer = new NewSceneLayer( );\\n        this.addChild( layer );\\n    }\\n} );\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\nNote: *When adding a new file in Cocos2d-JS update the project.json to reflect this addition.* \n\n##**Running a scene** - should only be used once to run a scene at the start of the game (Cocos runs a scene itself by default)\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Scene *scene = NewScene::createScene( );\\n    \\nDirector::getInstance( )->runWithScene( scene );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++\"\n    },\n    {\n      \"code\": \"var scene = new NewScene( );\\n\\ncc.director.runScene( scene );\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\n##**Pushing a scene onto the stack** \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Scene *scene = NewScene::createScene( );\\n    \\nDirector::getInstance( )->pushScene( scene );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++\"\n    },\n    {\n      \"code\": \"var scene = new NewScene( );\\n\\ncc.director.pushScene( scene );\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\n##**Popping a scene off the stack** \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Director::getInstance( )->popScene( );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++\"\n    },\n    {\n      \"code\": \"cc.director.popScene( );\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]\n##**Replacing a scene** \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"auto scene = NewScene::createScene( );\\n    \\nDirector::getInstance()->replaceScene( scene );\",\n      \"language\": \"cplusplus\",\n      \"name\": \"Cocos2d-x C++\"\n    },\n    {\n      \"code\": \"var scene = new NewScene( );\\n\\ncc.director.runScene( scene );\",\n      \"language\": \"javascript\",\n      \"name\": \"Cocos2d-JS\"\n    }\n  ]\n}\n[/block]","excerpt":"This page will cover how to use scenes.","slug":"scenes","type":"basic","title":"Scenes"}

Scenes

This page will cover how to use scenes.

As mentioned in the section *Cocos Basics* scenes are ordered on a stack which is LIFO (Last In First Out). Take a look at the diagram below to help understand this concept [block:image] { "images": [ { "image": [ "https://files.readme.io/97NdSEySOi20mUENPSVD_2000px-Data_stack.svg.png", "2000px-Data_stack.svg.png", "2000", "1437", "#0404fc", "" ], "caption": "Push and Pop Stack" } ] } [/block] Scene can be created either in the same file as an existing scene or a new code file. It is recommended to abstract as much as you can from a scene so it is easier to manage and debug. The following topics will be covered in this section: [block:html] { "html": "<ul>\n <li><a href=\"#create-scene\"><i>Creating a new scene</i></a></li>\n <li><a href=\"#push-scene\"><i>Pushing a scene onto the stack</i></a> - which halts the execution of the current scene then runs and displays the new scene</li>\n <li><a href=\"#pop-scene\"><i>Popping a scene off the stack</i></a> - removes the current scene then runs and displays the scene below (only if a scene exists)</li>\n <li><a href=\"#replace-scene\"><i>Replacing a scene</i></a> - replaces the current scene with the new scene essentially popping the current scene off the stack and pushing the new scene onto the stack</li>\n</ul>" } [/block] ##**Creating a new scene** [block:code] { "codes": [ { "code": "#ifndef __NEW_SCENE_H__\n#define __NEW_SCENE_H__\n\n#include \"cocos2d.h\"\n\nclass NewScene : public cocos2d::Layer\n{\npublic:\n // there's no 'id' in cpp, so we recommend returning the class instance pointer\n static cocos2d::Scene* createScene( );\n\n // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone\n virtual bool init( );\n \n // a selector callback\n void menuCloseCallback( cocos2d::Ref* pSender );\n \n // implement the \"static create()\" method manually\n CREATE_FUNC( NewScene );\n \n};\n\n#endif // __NEW_SCENE_H__", "language": "cplusplus", "name": "Cocos2d-x C++ Header" }, { "code": "#include \"NewScene.h\"\n\nUSING_NS_CC;\n\nScene* NewScene::createScene( )\n{\n // 'scene' is an autorelease object\n auto scene = Scene::create( );\n \n // 'layer' is an autorelease object\n auto layer = NewScene::create( );\n\n // add layer as a child to scene\n scene->addChild( layer );\n\n // return the scene\n return scene;\n}\n\n// on \"init\" you need to initialize your instance\nbool NewScene::init( )\n{\n //////////////////////////////\n // 1. super init first\n if ( !Layer::init( ) )\n {\n return false;\n }\n \n return true;\n}", "language": "cplusplus", "name": "Cocos2d-x C++ Implementation" }, { "code": "var NewSceneLayer = cc.Layer.extend(\n{\n ctor:function( )\n \t{\n //////////////////////////////\n // 1. super init first\n this._super( );\n\n return true;\n }\n} );\n\nvar NewScene = cc.Scene.extend(\n{\n onEnter:function( )\n \t{\n this._super( );\n\n var layer = new NewSceneLayer( );\n this.addChild( layer );\n }\n} );", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] Note: *When adding a new file in Cocos2d-JS update the project.json to reflect this addition.* ##**Running a scene** - should only be used once to run a scene at the start of the game (Cocos runs a scene itself by default) [block:code] { "codes": [ { "code": "Scene *scene = NewScene::createScene( );\n \nDirector::getInstance( )->runWithScene( scene );", "language": "cplusplus", "name": "Cocos2d-x C++" }, { "code": "var scene = new NewScene( );\n\ncc.director.runScene( scene );", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] ##**Pushing a scene onto the stack** [block:code] { "codes": [ { "code": "Scene *scene = NewScene::createScene( );\n \nDirector::getInstance( )->pushScene( scene );", "language": "cplusplus", "name": "Cocos2d-x C++" }, { "code": "var scene = new NewScene( );\n\ncc.director.pushScene( scene );", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] ##**Popping a scene off the stack** [block:code] { "codes": [ { "code": "Director::getInstance( )->popScene( );", "language": "cplusplus", "name": "Cocos2d-x C++" }, { "code": "cc.director.popScene( );", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block] ##**Replacing a scene** [block:code] { "codes": [ { "code": "auto scene = NewScene::createScene( );\n \nDirector::getInstance()->replaceScene( scene );", "language": "cplusplus", "name": "Cocos2d-x C++" }, { "code": "var scene = new NewScene( );\n\ncc.director.runScene( scene );", "language": "javascript", "name": "Cocos2d-JS" } ] } [/block]