Developers Manual > Cocos2d-JS > Features Of Cocos2d-JS > Game Entry - ccgame

Game Entry: cc.game

Summary

cc.Application and cc.AppControl in cocos2d-x have been replaced with cc.game in cocos2d-js, to simplify the code to run a game.

How to use

First include the xxx/frameworks/cocos2d-html5/CCBoot.js into index.html.

Then you can type the code to boot your game, which is usually put in main.js.

And you should config settings of project in project.json.

1
2
3
4
5
6
7
cc.game.onStart = function(){
    //load resources
    cc.LoaderScene.preload(resource_list, function () {
        cc.director.runScene(new MyScene());
    }, this);
};
cc.game.run();

The engine will read project.json while cc.game.run is called.

Delay to run

There is a requirement that all scripts have prepared but the main loop of the game will be run latter.

1
2
3
4
5
6
7
8
9
10
11
cc.game.onStart = function(){
    //load resources
    cc.LoaderScene.preload(resource_list, function () {
        cc.director.runScene(new MyScene());
    }, this);
};
cc.game.prepare();

document.getElementById("myBtn").addEventListener("click", function(){
    cc.game.run();
});

debugMode constants

Like debugMode in project.json:

1
2
3
4
5
6
7
DEBUG_MODE_NONE : 0                     //NONE
DEBUG_MODE_INFO : 1                     //Level of INFO, WARN, ERROR will be printed
DEBUG_MODE_WARN : 2                     //Level of WARN, ERROR will be printed
DEBUG_MODE_ERROR : 3                    //Level of ERROR will be printed
DEBUG_MODE_INFO_FOR_WEB_PAGE : 4        //Level of INFO, WARN, ERROR will be printed in html page
DEBUG_MODE_WARN_FOR_WEB_PAGE : 5        //Level of WARN, ERROR will be printed in html page
DEBUG_MODE_ERROR_FOR_WEB_PAGE : 6       //Level of ERROR will be printed in html page

CONFIG_KEY constants

Key of project.json:

1
2
3
4
5
6
7
8
9
engineDir : "engineDir",
dependencies : "dependencies",
debugMode : "debugMode",
showFPS : "showFPS",
frameRate : "frameRate",
id : "id",
renderMode : "renderMode",
jsList : "jsList",
classReleaseMode : "classReleaseMode"

Use `cc.game.config[cc.game.CONFIG_KEY.***] to get the config value of game.

EVENT_HIDE & EVENT_SHOW

With cocos-js3.0 we can add event listeners for entering background or foreground,such as switching browser's tab or pressing your home button on cellphone.And you can add two or more handlers to the same event.

1
2
3
4
5
6
    cc.eventManager.addCustomListener(cc.game.EVENT_HIDE, function(){
        //onEnterBackground handler
    });
    cc.eventManager.addCustomListener(cc.game.EVENT_SHOW, function(){
        //onEnterForeground handler
    });

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.