Game Class
Extends EventTarget
Module: cc
cc.game is the singleton object for game related functions.
Index
Properties
- EVENT_HIDE- StringEvent triggered when game hide to background.
- EVENT_SHOW- StringEvent triggered when game back to foreground
- EVENT_GAME_INITED- StringEvent triggered after game inited, at this point all engine objects and game scripts are loaded
- EVENT_RENDERER_INITED- StringEvent triggered after renderer inited, at this point you will be able to use the render context
- CONFIG_KEY- ObjectKey of config
- frame- ObjectThe outer frame of the game canvas, parent of cc.container.
- container- HTMLDivElementThe container of game canvas, equals to cc.container.
- canvas- HTMLCanvasElementThe canvas of the game, equals to cc._canvas.
- config- Object1.
Methods
- onStartCallback when the scripts of engine have been load.
- setFrameRateSet frameRate of game.
- stepRun the game frame by frame.
- pausePause the game main loop.
- resumeResume the game from pause.
- isPausedCheck whether the game is paused.
- restartRestart game.
- endEnd game, it will close the game window
- preparePrepare game.
- runRun game with configuration object and onStart function.
- addPersistRootNodeAdd a persistent root node to the game, the persistent node won't be destroyed during scene transition....
- removePersistRootNodeRemove a persistent root node.
- isPersistRootNodeCheck whether the node is a persistent root node.
- onRegister an callback of a specific event type on the EventTarget.
- offRemoves the listeners previously registered with the same type, callback, target and or useCapture,...
- targetOffRemoves all callbacks previously registered with the same target (passed as parameter).
- onceRegister an callback of a specific event type on the EventTarget,...
- dispatchEventDispatches an event into the event flow.
- emitSend an event to this object directly, this method will not propagate the event to any other objects.
Details
Properties
EVENT_HIDE
Event triggered when game hide to background. Please note that this event is not 100% guaranteed to be fired on Web platform, on native platforms, it corresponds to enter background event, os status bar or notification center may not trigger this event.
| meta | description | 
|---|---|
| Type | String | 
| Defined in | cocos2d/core/CCGame.js:47 | 
Examples
cc.game.on(cc.game.EVENT_HIDE, function () {
    cc.audioEngine.pauseMusic();
    cc.audioEngine.pauseAllEffects();
});
EVENT_SHOW
Event triggered when game back to foreground Please note that this event is not 100% guaranteed to be fired on Web platform, on native platforms, it corresponds to enter foreground event.
| meta | description | 
|---|---|
| Type | String | 
| Defined in | cocos2d/core/CCGame.js:64 | 
EVENT_GAME_INITED
Event triggered after game inited, at this point all engine objects and game scripts are loaded
| meta | description | 
|---|---|
| Type | String | 
| Defined in | cocos2d/core/CCGame.js:76 | 
EVENT_RENDERER_INITED
Event triggered after renderer inited, at this point you will be able to use the render context
| meta | description | 
|---|---|
| Type | String | 
| Defined in | cocos2d/core/CCGame.js:83 | 
CONFIG_KEY
Key of config
| meta | description | 
|---|---|
| Type | Object | 
| Defined in | cocos2d/core/CCGame.js:97 | 
frame
The outer frame of the game canvas, parent of cc.container.
| meta | description | 
|---|---|
| Type | Object | 
| Defined in | cocos2d/core/CCGame.js:135 | 
container
The container of game canvas, equals to cc.container.
| meta | description | 
|---|---|
| Type | HTMLDivElement | 
| Defined in | cocos2d/core/CCGame.js:142 | 
canvas
The canvas of the game, equals to cc._canvas.
| meta | description | 
|---|---|
| Type | HTMLCanvasElement | 
| Defined in | cocos2d/core/CCGame.js:149 | 
config
The current game configuration, including:
- debugMode
"debugMode" possible values :
0 - No message will be printed.
1 - cc.error, cc.assert, cc.warn, cc.log will print in console.
2 - cc.error, cc.assert, cc.warn will print in console.
3 - cc.error, cc.assert will print in console.
4 - cc.error, cc.assert, cc.warn, cc.log will print on canvas, available only on web.
5 - cc.error, cc.assert, cc.warn will print on canvas, available only on web.
6 - cc.error, cc.assert will print on canvas, available only on web.- showFPS
Left bottom corner fps information will show when "showFPS" equals true, otherwise it will be hide.- exposeClassName
Expose class name to chrome debug tools, the class intantiate performance is a little bit slower when exposed.- frameRate
"frameRate" set the wanted frame rate for your game, but the real fps depends on your game implementation and the running environment.- id
"gameCanvas" sets the id of your canvas element on the web page, it's useful only on web.- renderMode
"renderMode" sets the renderer type, only useful on web :
0 - Automatically chosen by engine
1 - Forced to use canvas renderer
2 - Forced to use WebGL renderer, but this will be ignored on mobile browsers- scenes
"scenes" include available scenes in the current bundle.
Please DO NOT modify this object directly, it won't have any effect.
| meta | description | 
|---|---|
| Type | Object | 
| Defined in | cocos2d/core/CCGame.js:157 | 
Methods
onStart
Callback when the scripts of engine have been load.
| meta | description | 
|---|---|
| Defined in | cocos2d/core/CCGame.js:219 | 
setFrameRate
Set frameRate of game.
| meta | description | 
|---|---|
| Defined in | cocos2d/core/CCGame.js:230 | 
Parameters
- frameRateNumber
step
Run the game frame by frame.
| meta | description | 
|---|---|
| Defined in | cocos2d/core/CCGame.js:247 | 
pause
Pause the game main loop. This will pause: game logic execution, rendering process, event manager, background music and all audio effects. This is different with cc.director.pause which only pause the game logic execution.
| meta | description | 
|---|---|
| Defined in | cocos2d/core/CCGame.js:256 | 
resume
Resume the game from pause. This will resume: game logic execution, rendering process, event manager, background music and all audio effects.
| meta | description | 
|---|---|
| Defined in | cocos2d/core/CCGame.js:276 | 
isPaused
Check whether the game is paused.
| meta | description | 
|---|---|
| Returns | Boolean | 
| Defined in | cocos2d/core/CCGame.js:293 | 
restart
Restart game.
| meta | description | 
|---|---|
| Defined in | cocos2d/core/CCGame.js:303 | 
end
End game, it will close the game window
| meta | description | 
|---|---|
| Defined in | cocos2d/core/CCGame.js:330 | 
prepare
Prepare game.
| meta | description | 
|---|---|
| Defined in | cocos2d/core/CCGame.js:340 | 
Parameters
- cbFunction
run
Run game with configuration object and onStart function.
| meta | description | 
|---|---|
| Defined in | cocos2d/core/CCGame.js:440 | 
Parameters
- configObject | Function Pass configuration object or onStart function
- onStartFunction function to be executed after game initialized
addPersistRootNode
Add a persistent root node to the game, the persistent node won't be destroyed during scene transition.
The target node must be placed in the root level of hierarchy, otherwise this API won't have any effect.
| meta | description | 
|---|---|
| Defined in | cocos2d/core/CCGame.js:464 | 
Parameters
- nodeNode The node to be made persistent
removePersistRootNode
Remove a persistent root node.
| meta | description | 
|---|---|
| Defined in | cocos2d/core/CCGame.js:500 | 
Parameters
- nodeNode The node to be removed from persistent node list
isPersistRootNode
Check whether the node is a persistent root node.
| meta | description | 
|---|---|
| Returns | Boolean | 
| Defined in | cocos2d/core/CCGame.js:516 | 
Parameters
- nodeNode The node to be checked
on
Register an callback of a specific event type on the EventTarget.
| meta | description | 
|---|---|
| Returns | Function | 
| Defined in | cocos2d/core/event/event-target.js:218 | 
Parameters
- typeString A string representing the event type to listen for.
- callbackFunction The callback that will be invoked when the event is dispatched.- The callback is ignored if it is a duplicate (the callbacks are unique).- eventEvent event
 
- targetObject The target (this object) to invoke the callback, can be null
- useCaptureBoolean When set to true, the capture argument prevents callback- from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked when event's eventPhase attribute value is AT_TARGET.
Examples
node.on(cc.Node.EventType.TOUCH_END, function (event) {
    cc.log("this is callback");
}, node);
off
Removes the listeners previously registered with the same type, callback, target and or useCapture, if only type is passed as parameter, all listeners registered with that type will be removed.
| meta | description | 
|---|---|
| Defined in | cocos2d/core/event/event-target.js:275 | 
Parameters
- typeString A string representing the event type being removed.
- callbackFunction The callback to remove.
- targetObject The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
- useCaptureBoolean Specifies whether the callback being removed was registered as a capturing callback or not.- If not specified, useCapture defaults to false. If a callback was registered twice, one with capture and one without, each must be removed separately. Removal of a capturing callback does not affect a non-capturing version of the same listener, and vice versa.
Examples
// register touchEnd eventListener
var touchEnd = node.on(cc.Node.EventType.TOUCH_END, function (event) {
    cc.log("this is callback");
}, node);
// remove touch end event listener
node.off(cc.Node.EventType.TOUCH_END, touchEnd, node);
// remove all touch end event listeners
node.off(cc.Node.EventType.TOUCH_END);
targetOff
Removes all callbacks previously registered with the same target (passed as parameter). This is not for removing all listeners in the current event target, and this is not for removing all listeners the target parameter have registered. It's only for removing all listeners (callback and target couple) registered on the current event target by the target parameter.
| meta | description | 
|---|---|
| Defined in | cocos2d/core/event/event-target.js:330 | 
Parameters
- targetObject The target to be searched for all related listeners
once
Register an callback of a specific event type on the EventTarget, the callback will remove itself after the first time it is triggered.
| meta | description | 
|---|---|
| Defined in | cocos2d/core/event/event-target.js:352 | 
Parameters
- typeString A string representing the event type to listen for.
- callbackFunction The callback that will be invoked when the event is dispatched.- The callback is ignored if it is a duplicate (the callbacks are unique).- eventEvent event
 
- targetObject The target (this object) to invoke the callback, can be null
- useCaptureBoolean When set to true, the capture argument prevents callback- from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked when event's eventPhase attribute value is AT_TARGET.
Examples
node.once(cc.Node.EventType.TOUCH_END, function (event) {
    cc.log("this is callback");
}, node);
dispatchEvent
Dispatches an event into the event flow. The event target is the EventTarget object upon which the dispatchEvent() method is called.
| meta | description | 
|---|---|
| Defined in | cocos2d/core/event/event-target.js:397 | 
Parameters
- eventEvent The Event object that is dispatched into the event flow
emit
Send an event to this object directly, this method will not propagate the event to any other objects. The event will be created from the supplied message, you can get the "detail" argument from event.detail.
| meta | description | 
|---|---|
| Defined in | cocos2d/core/event/event-target.js:411 | 
Parameters
- messageString the message to send
- detailAny whatever argument the message needs
