Game
Class
Extends EventTarget
Module: cc
cc.game is the singleton object for game related functions.
Index
Properties
EVENT_HIDE
String
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.EVENT_SHOW
String
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.EVENT_GAME_INITED
String
Event triggered after game inited, at this point all engine objects and game scripts are loadedEVENT_RENDERER_INITED
String
Event triggered after renderer inited, at this point you will be able to use the render contextCONFIG_KEY
Object
Key of configframe
Object
The outer frame of the game canvas, parent of cc.container.container
HTMLDivElement
The container of game canvas, equals to cc.container.canvas
HTMLCanvasElement
The canvas of the game, equals to cc._canvas.config
Object
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.
- debugMode
Methods
onStart
Callback when the scripts of engine have been load.setFrameRate
Set frameRate of game.step
Run the game frame by frame.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.resume
Resume the game from pause. This will resume: game logic execution, rendering process, event manager, background music and all audio effects.isPaused
Check whether the game is paused.restart
Restart game.end
End game, it will close the game windowprepare
Prepare game.run
Run game with configuration object and onStart function.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.removePersistRootNode
Remove a persistent root node.isPersistRootNode
Check whether the node is a persistent root node.on
Register an callback of a specific event type on the EventTarget.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.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.once
Register an callback of a specific event type on the EventTarget, the callback will remove itself after the first time it is triggered.dispatchEvent
Dispatches an event into the event flow. The event target is the EventTarget object upon which the dispatchEvent() method is called.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.
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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:46 |
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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:63 |
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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:75 |
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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:82 |
CONFIG_KEY
Key of config
meta | description |
---|---|
Type | Object |
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:96 |
frame
The outer frame of the game canvas, parent of cc.container.
meta | description |
---|---|
Type | Object |
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:134 |
container
The container of game canvas, equals to cc.container.
meta | description |
---|---|
Type | HTMLDivElement |
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:141 |
canvas
The canvas of the game, equals to cc._canvas.
meta | description |
---|---|
Type | HTMLCanvasElement |
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:148 |
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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:156 |
Methods
onStart
Callback when the scripts of engine have been load.
meta | description |
---|---|
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:218 |
setFrameRate
Set frameRate of game.
meta | description |
---|---|
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:229 |
Parameters
frameRate
Number
step
Run the game frame by frame.
meta | description |
---|---|
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:246 |
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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:255 |
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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:275 |
isPaused
Check whether the game is paused.
meta | description |
---|---|
Returns | Boolean |
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:292 |
restart
Restart game.
meta | description |
---|---|
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:302 |
end
End game, it will close the game window
meta | description |
---|---|
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:329 |
prepare
Prepare game.
meta | description |
---|---|
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:339 |
Parameters
cb
Function
run
Run game with configuration object and onStart function.
meta | description |
---|---|
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:439 |
Parameters
config
Object | Function Pass configuration object or onStart functiononStart
Function 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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:463 |
Parameters
node
Node The node to be made persistent
removePersistRootNode
Remove a persistent root node.
meta | description |
---|---|
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:499 |
Parameters
node
Node 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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/CCGame.js:515 |
Parameters
node
Node The node to be checked
on
Register an callback of a specific event type on the EventTarget.
meta | description |
---|---|
Returns | Function |
Defined in | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:217 |
Parameters
type
String A string representing the event type to listen for.callback
Function The callback that will be invoked when the event is dispatched.The callback is ignored if it is a duplicate (the callbacks are unique).
event
Event event
target
Object The target (this object) to invoke the callback, can be nulluseCapture
Boolean When set to true, the capture argument prevents callbackfrom 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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:274 |
Parameters
type
String A string representing the event type being removed.callback
Function The callback to remove.target
Object The target (this object) to invoke the callback, if it's not given, only callback without target will be removeduseCapture
Boolean 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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:329 |
Parameters
target
Object 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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:351 |
Parameters
type
String A string representing the event type to listen for.callback
Function The callback that will be invoked when the event is dispatched.The callback is ignored if it is a duplicate (the callbacks are unique).
event
Event event
target
Object The target (this object) to invoke the callback, can be nulluseCapture
Boolean When set to true, the capture argument prevents callbackfrom 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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:396 |
Parameters
event
Event 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 | https:/github.com/cocos-creator/engine/blob/master/cocos2d/core/event/event-target.js:410 |
Parameters
message
String the message to senddetail
Any whatever argument the message needs