Game
Class
Extends EventTarget
Module: cc
An object to boot the game.
Index
Properties
EVENT_HIDE
String
Event triggered when game hide to background.EVENT_SHOW
String
Event triggered when game back to foregroundEVENT_RESTART
String
Event triggered when game restartEVENT_GAME_INITED
String
Event triggered after game inited, at this point all engine objects and game scripts are loadedEVENT_ENGINE_INITED
String
Event triggered after engine inited, at this point you will be able to use all engine classes.RENDER_TYPE_CANVAS
Number
Web Canvas 2d API as renderer backendRENDER_TYPE_WEBGL
Number
WebGL API as renderer backendRENDER_TYPE_OPENGL
Number
OpenGL API as renderer backendframe
Object
The outer frame of the game canvas, parent of game container.container
HTMLDivElement
The container of game canvas.canvas
HTMLCanvasElement
The canvas of the game.renderType
Number
The renderer backend of the game.config
Object
1.
Methods
onStart
Callback when the scripts of engine have been load.setFrameRate
Set frame rate of game.getFrameRate
Get frame rate set for the game, it doesn't represent the real frame rate.step
Run the game frame by frame.pause
Pause the game main loop.resume
Resume the game from pause.isPaused
Check whether the game is paused.restart
Restart game.end
End game, it will close the game windowon
Register an callback of a specific event type on the game object.once
Register an callback of a specific event type on the game object,...prepare
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....removePersistRootNode
Remove a persistent root node.isPersistRootNode
Check whether the node is a persistent root node.hasEventListener
Checks whether the EventTarget object has any callback registered for a specific type of event.off
Removes the listeners previously registered with the same type, callback, target and or useCapture,...targetOff
Removes all callbacks previously registered with the same target (passed as parameter).emit
Trigger an event directly with the event name and necessary arguments.dispatchEvent
Send an event with the event object.
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:48 |
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:65 |
EVENT_RESTART
Event triggered when game restart
meta | description |
---|---|
Type | String |
Defined in | cocos2d/core/CCGame.js:78 |
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:87 |
EVENT_ENGINE_INITED
Event triggered after engine inited, at this point you will be able to use all engine classes. It was defined as EVENT_RENDERER_INITED in cocos creator v1.x and renamed in v2.0
meta | description |
---|---|
Type | String |
Defined in | cocos2d/core/CCGame.js:95 |
RENDER_TYPE_CANVAS
Web Canvas 2d API as renderer backend
meta | description |
---|---|
Type | Number |
Defined in | cocos2d/core/CCGame.js:106 |
RENDER_TYPE_WEBGL
WebGL API as renderer backend
meta | description |
---|---|
Type | Number |
Defined in | cocos2d/core/CCGame.js:113 |
RENDER_TYPE_OPENGL
OpenGL API as renderer backend
meta | description |
---|---|
Type | Number |
Defined in | cocos2d/core/CCGame.js:120 |
frame
The outer frame of the game canvas, parent of game container.
meta | description |
---|---|
Type | Object |
Defined in | cocos2d/core/CCGame.js:147 |
container
The container of game canvas.
meta | description |
---|---|
Type | HTMLDivElement |
Defined in | cocos2d/core/CCGame.js:154 |
canvas
The canvas of the game.
meta | description |
---|---|
Type | HTMLCanvasElement |
Defined in | cocos2d/core/CCGame.js:161 |
renderType
The renderer backend of the game.
meta | description |
---|---|
Type | Number |
Defined in | cocos2d/core/CCGame.js:169 |
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:177 |
Methods
onStart
Callback when the scripts of engine have been load.
meta | description |
---|---|
Defined in | cocos2d/core/CCGame.js:239 |
setFrameRate
Set frame rate of game.
meta | description |
---|---|
Defined in | cocos2d/core/CCGame.js:250 |
Parameters
frameRate
Number
getFrameRate
Get frame rate set for the game, it doesn't represent the real frame rate.
meta | description |
---|---|
Returns | Number |
Defined in | cocos2d/core/CCGame.js:267 |
step
Run the game frame by frame.
meta | description |
---|---|
Defined in | cocos2d/core/CCGame.js:277 |
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:286 |
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:306 |
isPaused
Check whether the game is paused.
meta | description |
---|---|
Returns | Boolean |
Defined in | cocos2d/core/CCGame.js:324 |
restart
Restart game.
meta | description |
---|---|
Defined in | cocos2d/core/CCGame.js:334 |
end
End game, it will close the game window
meta | description |
---|---|
Defined in | cocos2d/core/CCGame.js:364 |
on
Register an callback of a specific event type on the game object.
This type of event should be triggered via emit
.
meta | description |
---|---|
Returns | Function |
Defined in | cocos2d/core/CCGame.js:414 |
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).
arg1
Any arg1arg2
Any arg2arg3
Any arg3arg4
Any arg4arg5
Any arg5
target
Object The target (this object) to invoke the callback, can be null
once
Register an callback of a specific event type on the game object, the callback will remove itself after the first time it is triggered.
meta | description |
---|---|
Defined in | cocos2d/core/CCGame.js:445 |
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).
arg1
Any arg1arg2
Any arg2arg3
Any arg3arg4
Any arg4arg5
Any arg5
target
Object The target (this object) to invoke the callback, can be null
prepare
Prepare game.
meta | description |
---|---|
Defined in | cocos2d/core/CCGame.js:474 |
Parameters
cb
Function
run
Run game with configuration object and onStart function.
meta | description |
---|---|
Defined in | cocos2d/core/CCGame.js:501 |
Parameters
config
Object 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 | cocos2d/core/CCGame.js:515 |
Parameters
node
Node The node to be made persistent
removePersistRootNode
Remove a persistent root node.
meta | description |
---|---|
Defined in | cocos2d/core/CCGame.js:551 |
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 | cocos2d/core/CCGame.js:565 |
Parameters
node
Node The node to be checked
hasEventListener
Checks whether the EventTarget object has any callback registered for a specific type of event.
meta | description |
---|---|
Returns | Boolean |
Defined in | cocos2d/core/event/event-target.js:68 |
Parameters
type
String The type of event.
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:116 |
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 removed
Examples
// register fire eventListener
var callback = eventTarget.on('fire', function () {
cc.log("fire in the hole");
}, target);
// remove fire event listener
eventTarget.off('fire', callback, target);
// remove all fire event listeners
eventTarget.off('fire');
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:159 |
Parameters
target
Object The target to be searched for all related listeners
emit
Trigger an event directly with the event name and necessary arguments.
meta | description |
---|---|
Defined in | cocos2d/core/event/event-target.js:215 |
Parameters
type
String event typearg1
Any First argumentarg2
Any Second argumentarg3
Any Third argumentarg4
Any Fourth argumentarg5
Any Fifth argument
Examples
eventTarget.emit('fire', event);
eventTarget.emit('fire', message, emitter);
dispatchEvent
Send an event with the event object.
meta | description |
---|---|
Defined in | cocos2d/core/event/event-target.js:235 |
Parameters
event
Event