Game Class
Extends EventTarget
Module: cc
An object to boot the game.
Index
Properties
EVENT_HIDEStringEvent triggered when game hide to background.EVENT_SHOWStringEvent triggered when game back to foregroundEVENT_RESTARTStringEvent triggered when game restartEVENT_GAME_INITEDStringEvent triggered after game inited, at this point all engine objects and game scripts are loadedEVENT_ENGINE_INITEDStringEvent triggered after engine inited, at this point you will be able to use all engine classes.RENDER_TYPE_CANVASNumberWeb Canvas 2d API as renderer backendRENDER_TYPE_WEBGLNumberWebGL API as renderer backendRENDER_TYPE_OPENGLNumberOpenGL API as renderer backendframeObjectThe outer frame of the game canvas, parent of game container.containerHTMLDivElementThe container of game canvas.canvasHTMLCanvasElementThe canvas of the game.renderTypeNumberThe renderer backend of the game.configObject1.
Methods
onStartCallback when the scripts of engine have been load.setFrameRateSet frame rate of game.getFrameRateGet frame rate set for the game, it doesn't represent the real frame rate.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 windowonRegister an callback of a specific event type on the game object.onceRegister an callback of a specific event type on the game object,...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.hasEventListenerChecks whether the EventTarget object has any callback registered for a specific type of event.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).dispatchEventSend an event with the event object.removeAllRemoves all callbacks registered in a certain event type or all callbacks registered with a certain targetemitTrigger an event directly with the event name and necessary arguments.
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_RESTART
Event triggered when game restart
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/CCGame.js:77 |
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:86 |
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:94 |
RENDER_TYPE_CANVAS
Web Canvas 2d API as renderer backend
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCGame.js:105 |
RENDER_TYPE_WEBGL
WebGL API as renderer backend
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCGame.js:112 |
RENDER_TYPE_OPENGL
OpenGL API as renderer backend
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCGame.js:119 |
frame
The outer frame of the game canvas, parent of game container.
| meta | description |
|---|---|
| Type | Object |
| Defined in | cocos2d/core/CCGame.js:146 |
container
The container of game canvas.
| meta | description |
|---|---|
| Type | HTMLDivElement |
| Defined in | cocos2d/core/CCGame.js:153 |
canvas
The canvas of the game.
| meta | description |
|---|---|
| Type | HTMLCanvasElement |
| Defined in | cocos2d/core/CCGame.js:160 |
renderType
The renderer backend of the game.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCGame.js:168 |
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:176 |
Methods
onStart
Callback when the scripts of engine have been load.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCGame.js:238 |
setFrameRate
Set frame rate of game.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCGame.js:249 |
Parameters
frameRateNumber
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:266 |
step
Run the game frame by frame.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCGame.js:276 |
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:285 |
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:305 |
isPaused
Check whether the game is paused.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/CCGame.js:323 |
restart
Restart game.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCGame.js:333 |
end
End game, it will close the game window
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCGame.js:363 |
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:417 |
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).arg1Any arg1arg2Any arg2arg3Any arg3arg4Any arg4arg5Any arg5
targetObject 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:448 |
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).arg1Any arg1arg2Any arg2arg3Any arg3arg4Any arg4arg5Any arg5
targetObject The target (this object) to invoke the callback, can be null
prepare
Prepare game.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCGame.js:477 |
Parameters
cbFunction
run
Run game with configuration object and onStart function.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCGame.js:508 |
Parameters
configObject Pass configuration object or onStart functiononStartFunction 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:522 |
Parameters
nodeNode The node to be made persistent
removePersistRootNode
Remove a persistent root node.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCGame.js:558 |
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:572 |
Parameters
nodeNode 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:69 |
Parameters
typeString 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:119 |
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
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:163 |
Parameters
targetObject The target to be searched for all related listeners
dispatchEvent
Send an event with the event object.
| meta | description |
|---|---|
| Defined in | cocos2d/core/event/event-target.js:208 |
Parameters
eventEvent
removeAll
Removes all callbacks registered in a certain event type or all callbacks registered with a certain target
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/callbacks-invoker.js:235 |
Parameters
emit
Trigger an event directly with the event name and necessary arguments.
| meta | description |
|---|---|
| Defined in | cocos2d/core/platform/callbacks-invoker.js:309 |
Parameters
keyString event typearg1Any First argumentarg2Any Second argumentarg3Any Third argumentarg4Any Fourth argumentarg5Any Fifth argument
Examples
eventTarget.emit('fire', event);
eventTarget.emit('fire', message, emitter);