Cocos Creator API

1.5.x

Cocos Creator is the game engine for the future.

Game

Extends EventTarget
Module: cc

cc.game is the singleton object for game related functions.

Properties

EVENT_HIDE String

Event triggered when game hide to background. Please note that this event is not 100% guaranteed to be fired.

examples:

cc.game.on(cc.game.EVENT_HIDE, function () {
    cc.audioEngine.pauseMusic();
    cc.audioEngine.pauseAllEffects();
});

EVENT_SHOW String

Event triggered when game back to foreground Please note that this event is not 100% guaranteed to be fired.

EVENT_GAME_INITED String

Event triggered after game inited, at this point all engine objects and game scripts are loaded

EVENT_RENDERER_INITED String

Event triggered after renderer inited, at this point you will be able to use the render context

CONFIG_KEY Object

Key of config

frame 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:

  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.

There are no properties that match your current filter settings. You can change your filter settings in the index section on this page. index

Methods

onStart ( )

Callback when the scripts of engine have been load.

setFrameRate
(
  • frameRate
)

Set frameRate of game.

name type description
frameRate Number

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 ( ) Boolean

Check whether the game is paused.

returns:

type: Boolean

restart ( )

Restart game.

end ( )

End game, it will close the game window

prepare
(
  • cb
)

Prepare game.

name type description
cb Function

run
(
  • [config ]
  • [onStart ]
)

Run game with configuration object and onStart function.

name type description
config optional Object | Function

Pass configuration object or onStart function

onStart optional Function

function to be executed after game initialized

addPersistRootNode
(
  • node
)

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.

name type description
node Node

The node to be made persistent

removePersistRootNode
(
  • node
)

Remove a persistent root node.

name type description
node Node

The node to be removed from persistent node list

isPersistRootNode
(
  • node
)
Boolean

Check whether the node is a persistent root node.

name type description
node Node

The node to be checked

returns:

type: Boolean

on
(
  • type
  • callback
  • [target ]
  • [useCapture =false]
)
Function

Inherited from EventTarget:

Register an callback of a specific event type on the EventTarget.

name type description
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).

target optional Object

The target to invoke the callback, can be null

useCapture optional Boolean false

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.

returns:

type: Function

Just returns the incoming callback so you can save the anonymous function easier.

examples:

node.on(cc.Node.EventType.TOUCH_END, function (event) {
    cc.log("this is callback");
}, node);

off
(
  • type
  • callback
  • [target ]
  • [useCapture =false]
)

Inherited from EventTarget:

Removes the callback previously registered with the same type, callback, target and or useCapture.

name type description
type String

A string representing the event type being removed.

callback Function

The callback to remove.

target optional Object

The target to invoke the callback, if it's not given, only callback without target will be removed

useCapture optional Boolean false

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 touchEnd eventListener
node.off(cc.Node.EventType.TOUCH_END, touchEnd, node);

targetOff
(
  • target
)

Inherited from EventTarget:

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.

name type description
target Object

The target to be searched for all related listeners

once
(
  • type
  • callback
  • [target ]
  • [useCapture =false]
)

Inherited from EventTarget:

Register an callback of a specific event type on the EventTarget, the callback will remove itself after the first time it is triggered.

name type description
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).

target optional Object

The target to invoke the callback, can be null

useCapture optional Boolean false

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
(
  • event
)

Inherited from EventTarget:

Dispatches an event into the event flow. The event target is the EventTarget object upon which the dispatchEvent() method is called.

name type description
event Event

The Event object that is dispatched into the event flow

emit
(
  • message
  • [detail ]
)

Inherited from EventTarget:

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.

name type description
message String

the message to send

detail optional Any

whatever argument the message needs

There are no methods that match your current filter settings. You can change your filter settings in the index section on this page. index