Cocos Creator API

1.5.x

Cocos Creator is the game engine for the future.

Director

Extends EventTarget
Module: cc

ATTENTION: USE cc.director INSTEAD OF cc.Director.
cc.director is a singleton object which manage your game's logic flow.
Since the cc.director is a singleton, you don't need to call any constructor or create functions,
the standard way to use it is by calling:
- cc.director.methodName();

It creates and handle the main Window and manages how and when to execute the Scenes.

The cc.director is also responsible for:
- initializing the OpenGL context
- setting the OpenGL pixel format (default on is RGB565)
- setting the OpenGL buffer depth (default on is 0-bit)
- setting the color for clear screen (default one is BLACK)
- setting the projection (default one is 3D)
- setting the orientation (default one is Portrait)


The cc.director also sets the default OpenGL context:
- GL_TEXTURE_2D is enabled
- GL_VERTEX_ARRAY is enabled
- GL_COLOR_ARRAY is enabled
- GL_TEXTURE_COORD_ARRAY is enabled

cc.director also synchronizes timers with the refresh rate of the display.
Features and Limitations:
- Scheduled timers & drawing are synchronizes with the refresh rate of the display
- Only supports animation intervals of 1/60 1/30 & 1/15

Methods

convertToUI
(
  • glPoint
)
Vec2

Converts an OpenGL coordinate to a view coordinate
Useful to convert node points to window points for calls such as glScissor
Implementation can be found in CCDirectorWebGL.

name type description
glPoint Vec2

returns:

type: Vec2

getWinSize ( ) Size

Returns the size of the WebGL view in points.
It takes into account any possible rotation (device orientation) of the window.

returns:

type: Size

getWinSizeInPixels ( ) Size

Returns the size of the OpenGL view in pixels.
It takes into account any possible rotation (device orientation) of the window.
On Mac winSize and winSizeInPixels return the same value.

returns:

type: Size

getVisibleSize ( ) Size

Returns the visible size of the running scene.

returns:

type: Size

getVisibleOrigin ( ) Vec2

Returns the visible origin of the running scene.

returns:

type: Vec2

pause ( )

Pause the director's ticker, only involve the game logic execution. It won't pause the rendering process nor the event manager. If you want to pause the entier game including rendering, audio and event, please use Game.pause

runSceneImmediate
(
  • scene
  • [onBeforeLoadScene ]
  • [onLaunched ]
)

Run a scene. Replaces the running scene with a new one or enter the first scene.
The new scene will be launched immediately.

name type description
scene Scene

The need run scene.

onBeforeLoadScene optional Function

The function invoked at the scene before loading.

onLaunched optional Function

The function invoked at the scene after launch.

runScene
(
  • scene
  • [onBeforeLoadScene ]
  • [onLaunched ]
)
private

Run a scene. Replaces the running scene with a new one or enter the first scene. The new scene will be launched at the end of the current frame.

name type description
scene Scene

The need run scene.

onBeforeLoadScene optional Function

The function invoked at the scene before loading.

onLaunched optional Function

The function invoked at the scene after launch.

loadScene
(
  • sceneName
  • [onLaunched ]
)
Boolean

Loads the scene by its name.

name type description
sceneName String

The name of the scene to load.

onLaunched optional Function

callback, will be called after scene launched.

returns:

type: Boolean

if error, return false

preloadScene
(
  • sceneName
  • [onLoaded ]
)

Preloads the scene to reduces loading time. You can call this method at any time you want. After calling this method, you still need to launch the scene by cc.director.loadScene. It will be totally fine to call cc.director.loadScene at any time even if the preloading is not yet finished, the scene will be launched after loaded automatically.

name type description
sceneName String

The name of the scene to preload.

onLoaded optional Function

callback, will be called after scene loaded.

  • error Error

    null or the error object.

_loadSceneByUuid
(
  • uuid
  • [onLaunched ]
  • [onUnloaded ]
  • [dontRunScene ]
)
private

Loads the scene by its uuid.

name type description
uuid String

the uuid of the scene asset to load

onLaunched optional Function
onUnloaded optional Function
dontRunScene optional Boolean

Just download and initialize the scene but will not launch it, only take effect in the Editor.

resume ( )

Resume game logic execution after pause, if the current scene is not paused, nothing will happen.

setDepthTest
(
  • on
)

Enables or disables WebGL depth test.
Implementation can be found in CCDirectorCanvas.js/CCDirectorWebGL.js

name type description
on Boolean

setClearColor
(
  • clearColor
)

set color for clear screen.
Implementation can be found in CCDirectorCanvas.js/CCDirectorWebGL.js

name type description
clearColor Color

setProjection
(
  • projection
)

Sets an OpenGL projection.
Implementation can be found in CCDirectorCanvas.js/CCDirectorWebGL.js.

name type description
projection Number

setViewport ( )

Update the view port.
Implementation can be found in CCDirectorCanvas.js/CCDirectorWebGL.js.

getProjection ( ) Number

Sets an OpenGL projection.
Implementation can be found in CCDirectorCanvas.js/CCDirectorWebGL.js.

returns:

type: Number

setAlphaBlending
(
  • on
)

Enables/disables OpenGL alpha blending.
Implementation can be found in CCDirectorCanvas.js/CCDirectorWebGL.js.

name type description
on Boolean

isSendCleanupToScene ( ) Boolean

Returns whether or not the replaced scene will receive the cleanup message.
If the new scene is pushed, then the old scene won't receive the "cleanup" message.
If the new scene replaces the old one, the it will receive the "cleanup" message.

returns:

type: Boolean

getRunningScene ( ) Scene private

Returns current render Scene, normally you will never need to use this API. In most case, you probably want to use getScene instead.

returns:

type: Scene

getScene ( ) Scene

Returns current logic Scene.

returns:

type: Scene

examples:

// This will help you to get the Canvas node in scene
 cc.director.getScene().getChildByName('Canvas');

getAnimationInterval ( ) Number

Returns the FPS value.

returns:

type: Number

isDisplayStats ( ) Boolean

Returns whether or not to display the FPS informations.

returns:

type: Boolean

setDisplayStats
(
  • displayStats
)

Sets whether display the FPS on the bottom-left corner.

name type description
displayStats Boolean

isNextDeltaTimeZero ( ) Boolean

Returns whether next delta time equals to zero.

returns:

type: Boolean

isPaused ( ) Boolean

Returns whether or not the Director is paused.

returns:

type: Boolean

getTotalFrames ( ) Number

Returns how many frames were called since the director started.

returns:

type: Number

getScheduler ( ) Scheduler

Returns the cc.Scheduler associated with this director.

returns:

type: Scheduler

setScheduler
(
  • scheduler
)

Sets the cc.Scheduler associated with this director.

name type description
scheduler Scheduler

getActionManager ( ) ActionManager

Returns the cc.ActionManager associated with this director.

returns:

setActionManager
(
  • actionManager
)

Sets the cc.ActionManager associated with this director.

name type description
actionManager ActionManager

getCollisionManager ( ) CollisionManager

Returns the cc.CollisionManager associated with this director.

returns:

getPhysicsManager ( ) PhysicsManager

Returns the cc.PhysicsManager associated with this director.

returns:

getDeltaTime ( ) Number

Returns the delta time since last frame.

returns:

type: Number

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

Events

cc.Director.EVENT_PROJECTION_CHANGED

The event projection changed of cc.Director.

Event Payload:

examples:

cc.director.on(cc.Director.EVENT_PROJECTION_CHANGED, function(event) {
     cc.log("Projection changed.");
  });

cc.Director.EVENT_BEFORE_SCENE_LOADING

The event which will be triggered before loading a new scene.

Event Payload:

cc.Director.EVENT_AFTER_SCENE_LAUNCH

The event which will be triggered after launching a new scene.

Event Payload:

cc.Director.EVENT_BEFORE_UPDATE

The event which will be triggered at the beginning of every frame.

Event Payload:

cc.Director.EVENT_AFTER_UPDATE

The event which will be triggered after engine and components update logic.

Event Payload:

cc.Director.EVENT_BEFORE_VISIT

The event which will be triggered before visiting the rendering scene graph.

Event Payload:

cc.Director.EVENT_AFTER_VISIT

The event which will be triggered after visiting the rendering scene graph, the render queue is ready but not rendered at this point.

Event Payload:

cc.Director.EVENT_AFTER_DRAW

The event which will be triggered after the rendering process.

Event Payload:

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