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 foreground
  • EVENT_RESTART String Event triggered when game restart
  • EVENT_GAME_INITED String Event triggered after game inited, at this point all engine objects and game scripts are loaded
  • EVENT_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 backend
  • RENDER_TYPE_WEBGL Number WebGL API as renderer backend
  • RENDER_TYPE_OPENGL Number OpenGL API as renderer backend
  • frame 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 window
  • on 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).
  • dispatchEvent Send an event with the event object.
  • removeAll Removes all callbacks registered in a certain event type or all callbacks registered with a certain target
  • emit Trigger 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:

  1. 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.
  2. showFPS
    Left bottom corner fps information will show when "showFPS" equals true, otherwise it will be hide.
  3. exposeClassName
    Expose class name to chrome debug tools, the class intantiate performance is a little bit slower when exposed.
  4. frameRate
    "frameRate" set the wanted frame rate for your game, but the real fps depends on your game implementation and the running environment.
  5. id
    "gameCanvas" sets the id of your canvas element on the web page, it's useful only on web.
  6. 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
  7. 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
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:413
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 arg1
    • arg2 Any arg2
    • arg3 Any arg3
    • arg4 Any arg4
    • arg5 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:444
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 arg1
    • arg2 Any arg2
    • arg3 Any arg3
    • arg4 Any arg4
    • arg5 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:473
Parameters
run

Run game with configuration object and onStart function.

meta description
Defined in cocos2d/core/CCGame.js:500
Parameters
  • config Object Pass configuration object or onStart function
  • onStart 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:514
Parameters
  • node Node The node to be made persistent
removePersistRootNode

Remove a persistent root node.

meta description
Defined in cocos2d/core/CCGame.js:550
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:564
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:69
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:119
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:163
Parameters
  • target Object 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
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
  • keyOrTarget String | Object The event key to be removed or the target to be removed
emit

Trigger an event directly with the event name and necessary arguments.

meta description
Defined in cocos2d/core/platform/callbacks-invoker.js:309
Parameters
  • key String event type
  • arg1 Any First argument
  • arg2 Any Second argument
  • arg3 Any Third argument
  • arg4 Any Fourth argument
  • arg5 Any Fifth argument
Examples
eventTarget.emit('fire', event);
eventTarget.emit('fire', message, emitter);

results matching ""

    No results matching ""