Director Class
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
Index
Properties
EVENT_PROJECTION_CHANGEDStringThe event projection changed of cc.Director.EVENT_BEFORE_SCENE_LOADINGStringThe event which will be triggered before loading a new scene.EVENT_BEFORE_SCENE_LAUNCHStringThe event which will be triggered before launching a new scene.EVENT_AFTER_SCENE_LAUNCHStringThe event which will be triggered after launching a new scene.EVENT_BEFORE_UPDATEStringThe event which will be triggered at the beginning of every frame.EVENT_AFTER_UPDATEStringThe event which will be triggered after engine and components update logic.EVENT_BEFORE_VISITStringThe event which will be triggered before visiting the rendering scene graph.EVENT_AFTER_VISITStringThe event which will be triggered after visiting the rendering scene graph,...EVENT_AFTER_DRAWStringThe event which will be triggered after the rendering process.PROJECTION_2DNumberConstant for 2D projection (orthogonal projection)PROJECTION_3DNumberConstant for 3D projection with a fovy=60, znear=0.5f and zfar=1500.PROJECTION_CUSTOMNumberConstant for custom projection, if cc.Director's projection set to it, it calls "updateProjection" on the projection delegate.PROJECTION_DEFAULTNumberConstant for default projection of cc.Director, default projection is 2D projection
Methods
convertToUIConverts an OpenGL coordinate to a view coordinate...getWinSizeReturns the size of the WebGL view in points....getWinSizeInPixelsOn Mac winSize and winSizeInPixels return the same value.getVisibleSizeReturns the visible size of the running scene.getVisibleOriginReturns the visible origin of the running scene.pausePause the director's ticker, only involve the game logic execution.runSceneImmediateRun a scene.runSceneRun a scene.loadSceneLoads the scene by its name.preloadScenePreloads the scene to reduces loading time._loadSceneByUuidLoads the scene by its uuid.resumeResume game logic execution after pause, if the current scene is not paused, nothing will happen.setDepthTestEnables or disables WebGL depth test....setClearColorSet color for clear screen....setProjectionSets an OpenGL projection....setViewportUpdate the view port....getProjectionSets an OpenGL projection....setAlphaBlendingEnables/disables OpenGL alpha blending....isSendCleanupToSceneReturns whether or not the replaced scene will receive the cleanup message....getRunningSceneReturns current render Scene, normally you will never need to use this API.getSceneReturns current logic Scene.getAnimationIntervalReturns the FPS value.isDisplayStatsReturns whether or not to display the FPS informations.setDisplayStatsSets whether display the FPS on the bottom-left corner.isNextDeltaTimeZeroReturns whether next delta time equals to zero.isPausedReturns whether or not the Director is paused.getTotalFramesReturns how many frames were called since the director started.getSchedulerReturns the cc.Scheduler associated with this director.setSchedulerSets the cc.Scheduler associated with this director.getActionManagerReturns the cc.ActionManager associated with this director.setActionManagerSets the cc.ActionManager associated with this director.getCollisionManagerReturns the cc.CollisionManager associated with this director.getPhysicsManagerReturns the cc.PhysicsManager associated with this director.getDeltaTimeReturns the delta time since last frame.onRegister an callback of a specific event type on the EventTarget.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).onceRegister an callback of a specific event type on the EventTarget,...dispatchEventDispatches an event into the event flow.emitSend an event to this object directly, this method will not propagate the event to any other objects.
Events
cc.Director.EVENT_PROJECTION_CHANGEDThe event projection changed of cc.Director.cc.Director.EVENT_BEFORE_SCENE_LOADINGThe event which will be triggered before loading a new scene.cc.Director.EVENT_AFTER_SCENE_LAUNCHThe event which will be triggered after launching a new scene.cc.Director.EVENT_BEFORE_UPDATEThe event which will be triggered at the beginning of every frame.cc.Director.EVENT_AFTER_UPDATEThe event which will be triggered after engine and components update logic.cc.Director.EVENT_BEFORE_VISITThe event which will be triggered before visiting the rendering scene graph.cc.Director.EVENT_AFTER_VISITThe event which will be triggered after visiting the rendering scene graph,...cc.Director.EVENT_AFTER_DRAWThe event which will be triggered after the rendering process.
Details
Properties
EVENT_PROJECTION_CHANGED
The event projection changed of cc.Director.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/CCDirector.js:1276 |
EVENT_BEFORE_SCENE_LOADING
The event which will be triggered before loading a new scene.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/CCDirector.js:1292 |
EVENT_BEFORE_SCENE_LAUNCH
The event which will be triggered before launching a new scene.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/CCDirector.js:1308 |
EVENT_AFTER_SCENE_LAUNCH
The event which will be triggered after launching a new scene.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/CCDirector.js:1324 |
EVENT_BEFORE_UPDATE
The event which will be triggered at the beginning of every frame.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/CCDirector.js:1339 |
EVENT_AFTER_UPDATE
The event which will be triggered after engine and components update logic.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/CCDirector.js:1354 |
EVENT_BEFORE_VISIT
The event which will be triggered before visiting the rendering scene graph.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/CCDirector.js:1369 |
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.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/CCDirector.js:1387 |
EVENT_AFTER_DRAW
The event which will be triggered after the rendering process.
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/CCDirector.js:1405 |
PROJECTION_2D
Constant for 2D projection (orthogonal projection)
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCDirector.js:1568 |
PROJECTION_3D
Constant for 3D projection with a fovy=60, znear=0.5f and zfar=1500.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCDirector.js:1577 |
PROJECTION_CUSTOM
Constant for custom projection, if cc.Director's projection set to it, it calls "updateProjection" on the projection delegate.
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCDirector.js:1586 |
PROJECTION_DEFAULT
Constant for default projection of cc.Director, default projection is 2D projection
| meta | description |
|---|---|
| Type | Number |
| Defined in | cocos2d/core/CCDirector.js:1595 |
Methods
convertToUI
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.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/CCDirector.js:272 |
Parameters
glPointVec2
getWinSize
Returns the size of the WebGL view in points.
It takes into account any possible rotation (device orientation) of the window.
| meta | description |
|---|---|
| Returns | Size |
| Defined in | cocos2d/core/CCDirector.js:337 |
getWinSizeInPixels
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.
(The pixel here refers to the resource resolution. If you want to get the physics resolution of device, you need to use cc.view.getFrameSize())
| meta | description |
|---|---|
| Returns | Size |
| Defined in | cocos2d/core/CCDirector.js:349 |
getVisibleSize
Returns the visible size of the running scene.
| meta | description |
|---|---|
| Returns | Size |
| Defined in | cocos2d/core/CCDirector.js:371 |
getVisibleOrigin
Returns the visible origin of the running scene.
| meta | description |
|---|---|
| Returns | Vec2 |
| Defined in | cocos2d/core/CCDirector.js:379 |
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
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCDirector.js:395 |
runSceneImmediate
Run a scene. Replaces the running scene with a new one or enter the first scene.
The new scene will be launched immediately.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCDirector.js:539 |
Parameters
sceneScene The need run scene.onBeforeLoadSceneFunction The function invoked at the scene before loading.onLaunchedFunction The function invoked at the scene after launch.
runScene
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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCDirector.js:648 |
Parameters
sceneScene The need run scene.onBeforeLoadSceneFunction The function invoked at the scene before loading.onLaunchedFunction The function invoked at the scene after launch.
loadScene
Loads the scene by its name.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/CCDirector.js:705 |
Parameters
sceneNameString The name of the scene to load.onLaunchedFunction callback, will be called after scene launched.
preloadScene
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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCDirector.js:753 |
Parameters
sceneNameString The name of the scene to preload.onLoadedFunction callback, will be called after scene loaded.errorError null or the error object.
_loadSceneByUuid
Loads the scene by its uuid.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCDirector.js:788 |
Parameters
uuidString the uuid of the scene asset to loadonLaunchedFunctiononUnloadedFunctiondontRunSceneBoolean 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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCDirector.js:851 |
setDepthTest
Enables or disables WebGL depth test.
Implementation can be found in CCDirectorCanvas.js/CCDirectorWebGL.js
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCDirector.js:882 |
Parameters
onBoolean
setClearColor
Set color for clear screen.
(Implementation can be found in CCDirectorCanvas.js/CCDirectorWebGL.js)
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCDirector.js:892 |
Parameters
clearColorColor
setProjection
Sets an OpenGL projection.
Implementation can be found in CCDirectorCanvas.js/CCDirectorWebGL.js.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCDirector.js:978 |
Parameters
projectionNumber
setViewport
Update the view port.
Implementation can be found in CCDirectorCanvas.js/CCDirectorWebGL.js.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCDirector.js:988 |
getProjection
Sets an OpenGL projection.
Implementation can be found in CCDirectorCanvas.js/CCDirectorWebGL.js.
| meta | description |
|---|---|
| Returns | Number |
| Defined in | cocos2d/core/CCDirector.js:1008 |
setAlphaBlending
Enables/disables OpenGL alpha blending.
Implementation can be found in CCDirectorCanvas.js/CCDirectorWebGL.js.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCDirector.js:1018 |
Parameters
onBoolean
isSendCleanupToScene
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.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/CCDirector.js:1028 |
getRunningScene
Returns current render Scene, normally you will never need to use this API.
In most case, you probably want to use getScene instead.
| meta | description |
|---|---|
| Returns | Scene |
| Defined in | cocos2d/core/CCDirector.js:1044 |
getScene
Returns current logic Scene.
| meta | description |
|---|---|
| Returns | Scene |
| Defined in | cocos2d/core/CCDirector.js:1057 |
Examples
// This will help you to get the Canvas node in scene
cc.director.getScene().getChildByName('Canvas');
getAnimationInterval
Returns the FPS value.
| meta | description |
|---|---|
| Returns | Number |
| Defined in | cocos2d/core/CCDirector.js:1070 |
isDisplayStats
Returns whether or not to display the FPS informations.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/CCDirector.js:1080 |
setDisplayStats
Sets whether display the FPS on the bottom-left corner.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCDirector.js:1090 |
Parameters
displayStatsBoolean
isNextDeltaTimeZero
Returns whether next delta time equals to zero.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/CCDirector.js:1103 |
isPaused
Returns whether or not the Director is paused.
| meta | description |
|---|---|
| Returns | Boolean |
| Defined in | cocos2d/core/CCDirector.js:1113 |
getTotalFrames
Returns how many frames were called since the director started.
| meta | description |
|---|---|
| Returns | Number |
| Defined in | cocos2d/core/CCDirector.js:1123 |
getScheduler
Returns the cc.Scheduler associated with this director.
| meta | description |
|---|---|
| Returns | Scheduler |
| Defined in | cocos2d/core/CCDirector.js:1177 |
setScheduler
Sets the cc.Scheduler associated with this director.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCDirector.js:1187 |
Parameters
schedulerScheduler
getActionManager
Returns the cc.ActionManager associated with this director.
| meta | description |
|---|---|
| Returns | ActionManager |
| Defined in | cocos2d/core/CCDirector.js:1199 |
setActionManager
Sets the cc.ActionManager associated with this director.
| meta | description |
|---|---|
| Defined in | cocos2d/core/CCDirector.js:1208 |
Parameters
actionManagerActionManager
getCollisionManager
Returns the cc.CollisionManager associated with this director.
| meta | description |
|---|---|
| Returns | CollisionManager |
| Defined in | cocos2d/core/CCDirector.js:1234 |
getPhysicsManager
Returns the cc.PhysicsManager associated with this director.
| meta | description |
|---|---|
| Returns | PhysicsManager |
| Defined in | cocos2d/core/CCDirector.js:1243 |
getDeltaTime
Returns the delta time since last frame.
| meta | description |
|---|---|
| Returns | Number |
| Defined in | cocos2d/core/CCDirector.js:1252 |
on
Register an callback of a specific event type on the EventTarget.
| meta | description |
|---|---|
| Returns | Function |
| Defined in | cocos2d/core/event/event-target.js:218 |
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).eventEvent event
targetObject The target (this object) to invoke the callback, can be nulluseCaptureBoolean When set to true, the capture argument prevents callbackfrom 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.on(cc.Node.EventType.TOUCH_END, function (event) {
cc.log("this is callback");
}, node);
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:275 |
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 removeduseCaptureBoolean 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 touch end event listener
node.off(cc.Node.EventType.TOUCH_END, touchEnd, node);
// remove all touch end event listeners
node.off(cc.Node.EventType.TOUCH_END);
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:330 |
Parameters
targetObject The target to be searched for all related listeners
once
Register an callback of a specific event type on the EventTarget, the callback will remove itself after the first time it is triggered.
| meta | description |
|---|---|
| Defined in | cocos2d/core/event/event-target.js:352 |
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).eventEvent event
targetObject The target (this object) to invoke the callback, can be nulluseCaptureBoolean When set to true, the capture argument prevents callbackfrom 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
Dispatches an event into the event flow. The event target is the EventTarget object upon which the dispatchEvent() method is called.
| meta | description |
|---|---|
| Defined in | cocos2d/core/event/event-target.js:397 |
Parameters
eventEvent The Event object that is dispatched into the event flow
emit
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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/event/event-target.js:411 |
Parameters
messageString the message to senddetailAny whatever argument the message needs
Events
cc.Director.EVENT_PROJECTION_CHANGED Event
Module: cc
The event projection changed of cc.Director.
Index
Details
cc.Director.EVENT_BEFORE_SCENE_LOADING Event
Module: cc
The event which will be triggered before loading a new scene.
Index
Details
cc.Director.EVENT_AFTER_SCENE_LAUNCH Event
Module: cc
The event which will be triggered after launching a new scene.
Index
Details
cc.Director.EVENT_BEFORE_UPDATE Event
Module: cc
The event which will be triggered at the beginning of every frame.
Index
Details
cc.Director.EVENT_AFTER_UPDATE Event
Module: cc
The event which will be triggered after engine and components update logic.
Index
Details
cc.Director.EVENT_BEFORE_VISIT Event
Module: cc
The event which will be triggered before visiting the rendering scene graph.
Index
Details
cc.Director.EVENT_AFTER_VISIT Event
Module: cc
The event which will be triggered after visiting the rendering scene graph, the render queue is ready but not rendered at this point.
Index
Details
cc.Director.EVENT_AFTER_DRAW Event
Module: cc
The event which will be triggered after the rendering process.