Director 类型
继承于 EventTarget
模块: cc
注意:用 cc.director 代替 cc.Director。
cc.director 一个管理你的游戏的逻辑流程的单例对象。
由于 cc.director 是一个单例,你不需要调用任何构造函数或创建函数,
使用它的标准方法是通过调用:
- cc.director.methodName();
它创建和处理主窗口并且管理什么时候执行场景。
cc.director 还负责:
- 初始化 OpenGL 环境。
- 设置OpenGL像素格式。(默认是 RGB565)
- 设置OpenGL缓冲区深度 (默认是 0-bit)
- 设置空白场景的颜色 (默认是 黑色)
- 设置投影 (默认是 3D)
- 设置方向 (默认是 Portrait)
cc.director 设置了 OpenGL 默认环境
- GL_TEXTURE_2D 启用。
- GL_VERTEX_ARRAY 启用。
- GL_COLOR_ARRAY 启用。
- GL_TEXTURE_COORD_ARRAY 启用。
cc.director 也同步定时器与显示器的刷新速率。
特点和局限性:
- 将计时器 & 渲染与显示器的刷新频率同步。
- 只支持动画的间隔 1/60 1/30 & 1/15。
索引
属性(properties)
EVENT_PROJECTION_CHANGEDStringcc.Director 投影变化的事件。EVENT_BEFORE_SCENE_LOADINGString加载新场景之前所触发的事件。EVENT_BEFORE_SCENE_LAUNCHString运行新场景之前所触发的事件。EVENT_AFTER_SCENE_LAUNCHString运行新场景之后所触发的事件。EVENT_BEFORE_UPDATEString每个帧的开始时所触发的事件。EVENT_AFTER_UPDATEString将在引擎和组件 “update” 逻辑之后所触发的事件。EVENT_BEFORE_VISITString这个事件从 v2.0 开始被废弃,请直接使用 cc.Director.EVENT_BEFORE_DRAWEVENT_AFTER_VISITString这个事件从 v2.0 开始被废弃,请直接使用 cc.Director.EVENT_BEFORE_DRAWEVENT_BEFORE_DRAWString渲染过程之前所触发的事件。EVENT_AFTER_DRAWString渲染过程之后所触发的事件。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
方法
convertToGL将触摸点的屏幕坐标转换为 WebGL View 下的坐标。convertToUI将触摸点的 WebGL View 坐标转换为屏幕坐标。endEnd the life of director in the next framegetWinSize获取视图的大小,以点为单位。getWinSizeInPixels获取视图大小,以像素为单位(这里的像素指的是资源分辨率。pause暂停正在运行的场景,该暂停只会停止游戏逻辑执行,但是不会停止渲染和 UI 响应。runSceneImmediate立刻切换指定场景。runScene运行指定场景。loadScene通过场景名称进行加载场景。preloadScene预加载场景,你可以在任何时候调用这个方法。resume恢复暂停场景的游戏逻辑,如果当前场景没有暂停将没任何事情发生。setDepthTest启用/禁用深度测试(在 Canvas 渲染模式下不会生效)。setClearColor设置场景的默认擦除颜色。getRunningScene获取当前逻辑场景。getScene获取当前逻辑场景。getAnimationInterval获取单位帧执行时间。setAnimationIntervalSets animation interval, this doesn't control the main loop....getDeltaTime获取上一帧的增量时间。getTotalTime获取从游戏开始到现在总共经过的时间,单位为 msgetTotalFrames获取 director 启动以来游戏运行的总帧数。isPaused是否处于暂停状态。getScheduler获取和 director 相关联的 cc.Scheduler。setScheduler设置和 director 相关联的 cc.Scheduler。getActionManager获取和 director 相关联的 cc.ActionManager(动作管理器)。setActionManager设置和 director 相关联的 cc.ActionManager(动作管理器)。getCollisionManager获取和 director 相关联的 cc.CollisionManager (碰撞管理器)。getPhysicsManager返回与 director 相关联的 cc.PhysicsManager (物理管理器)。getPhysics3DManager返回与 director 相关联的 cc.Physics3DManager (物理管理器)。hasEventListener检查事件目标对象是否有为特定类型的事件注册的回调。on注册事件目标的特定事件类型回调。off删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。targetOff在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。once注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。dispatchEvent通过事件对象派发事件clear销毁记录的事件removeAll移除在特定事件类型中注册的所有回调或在某个目标中注册的所有回调。emit通过事件名发送自定义事件
事件
cc.Director.EVENT_BEFORE_SCENE_LOADING加载新场景之前所触发的事件。cc.Director.EVENT_AFTER_SCENE_LAUNCH运行新场景之后所触发的事件。cc.Director.EVENT_BEFORE_UPDATE每个帧的开始时所触发的事件。cc.Director.EVENT_AFTER_UPDATE将在引擎和组件 “update” 逻辑之后所触发的事件。cc.Director.EVENT_BEFORE_DRAW渲染过程之前所触发的事件。cc.Director.EVENT_AFTER_DRAW渲染过程之后所触发的事件。Director.EVENT_BEFORE_PHYSICS物理过程之前所触发的事件。Director.EVENT_AFTER_PHYSICS物理过程之后所触发的事件。
Details
属性(properties)
EVENT_PROJECTION_CHANGED
cc.Director 投影变化的事件。从 v2.0 开始这个事件不会再被触发
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/CCDirector.js:930 |
| 废弃(Deprecated) | since v2.0 |
EVENT_BEFORE_SCENE_LOADING
加载新场景之前所触发的事件。
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/CCDirector.js:946 |
EVENT_BEFORE_SCENE_LAUNCH
运行新场景之前所触发的事件。
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/CCDirector.js:961 |
EVENT_AFTER_SCENE_LAUNCH
运行新场景之后所触发的事件。
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/CCDirector.js:976 |
EVENT_BEFORE_UPDATE
每个帧的开始时所触发的事件。
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/CCDirector.js:990 |
EVENT_AFTER_UPDATE
将在引擎和组件 “update” 逻辑之后所触发的事件。
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/CCDirector.js:1004 |
EVENT_BEFORE_VISIT
这个事件从 v2.0 开始被废弃,请直接使用 cc.Director.EVENT_BEFORE_DRAW
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/CCDirector.js:1013 |
| 废弃(Deprecated) | since v2.0 |
EVENT_AFTER_VISIT
这个事件从 v2.0 开始被废弃,请直接使用 cc.Director.EVENT_BEFORE_DRAW
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/CCDirector.js:1023 |
| 废弃(Deprecated) | since v2.0 |
EVENT_BEFORE_DRAW
渲染过程之前所触发的事件。
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/CCDirector.js:1038 |
EVENT_AFTER_DRAW
渲染过程之后所触发的事件。
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/CCDirector.js:1052 |
PROJECTION_2D
Constant for 2D projection (orthogonal projection)
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | cocos2d/core/CCDirector.js:1063 |
| 废弃(Deprecated) | since v2.0 |
PROJECTION_3D
Constant for 3D projection with a fovy=60, znear=0.5f and zfar=1500.
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | cocos2d/core/CCDirector.js:1073 |
| 废弃(Deprecated) | since v2.0 |
PROJECTION_CUSTOM
Constant for custom projection, if cc.Director's projection set to it, it calls "updateProjection" on the projection delegate.
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | cocos2d/core/CCDirector.js:1083 |
| 废弃(Deprecated) | since v2.0 |
PROJECTION_DEFAULT
Constant for default projection of cc.Director, default projection is 2D projection
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | cocos2d/core/CCDirector.js:1093 |
| 废弃(Deprecated) | since v2.0 |
方法
convertToGL
将触摸点的屏幕坐标转换为 WebGL View 下的坐标。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/CCDirector.js:241 |
| 废弃(Deprecated) | since v2.0 |
参数列表
uiPointVec2
convertToUI
将触摸点的 WebGL View 坐标转换为屏幕坐标。
| meta | description |
|---|---|
| 返回 | Vec2 |
| 定义于 | cocos2d/core/CCDirector.js:263 |
| 废弃(Deprecated) | since v2.0 |
参数列表
glPointVec2
end
End the life of director in the next frame
| meta | description |
|---|---|
| 定义于 | cocos2d/core/CCDirector.js:292 |
getWinSize
获取视图的大小,以点为单位。
| meta | description |
|---|---|
| 返回 | Size |
| 定义于 | cocos2d/core/CCDirector.js:300 |
| 废弃(Deprecated) | since v2.0 |
getWinSizeInPixels
获取视图大小,以像素为单位(这里的像素指的是资源分辨率。 如果要获取屏幕物理分辨率,需要用 cc.view.getFrameSize())
| meta | description |
|---|---|
| 返回 | Size |
| 定义于 | cocos2d/core/CCDirector.js:313 |
| 废弃(Deprecated) | since v2.0 |
pause
暂停正在运行的场景,该暂停只会停止游戏逻辑执行,但是不会停止渲染和 UI 响应。 如果想要更彻底得暂停游戏,包含渲染,音频和事件,请使用 Game.pause。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/CCDirector.js:330 |
runSceneImmediate
立刻切换指定场景。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/CCDirector.js:415 |
参数列表
sceneScene | SceneAsset The need run scene.onBeforeLoadSceneFunction The function invoked at the scene before loading.onLaunchedFunction The function invoked at the scene after launch.
runScene
运行指定场景。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/CCDirector.js:495 |
参数列表
sceneScene | SceneAsset The need run scene.onBeforeLoadSceneFunction The function invoked at the scene before loading.onLaunchedFunction The function invoked at the scene after launch.
loadScene
通过场景名称进行加载场景。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/CCDirector.js:519 |
参数列表
sceneNameString The name of the scene to load.onLaunchedFunction callback, will be called after scene launched.
preloadScene
预加载场景,你可以在任何时候调用这个方法。
调用完后,你仍然需要通过 cc.director.loadScene 来启动场景,因为这个方法不会执行场景加载操作。
就算预加载还没完成,你也可以直接调用 cc.director.loadScene,加载完成后场景就会启动。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/CCDirector.js:561 |
参数列表
sceneNameString The name of the scene to preload.onProgressFunction callback, will be called when the load progression change.onLoadedFunction callback, will be called after scene loaded.errorError null or the error object.
resume
恢复暂停场景的游戏逻辑,如果当前场景没有暂停将没任何事情发生。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/CCDirector.js:594 |
setDepthTest
启用/禁用深度测试(在 Canvas 渲染模式下不会生效)。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/CCDirector.js:613 |
| 废弃(Deprecated) | since v2.0 |
参数列表
onBoolean
setClearColor
设置场景的默认擦除颜色。
支持全透明,但不支持透明度为中间值。要支持全透明需手工开启 cc.macro.ENABLE_TRANSPARENT_CANVAS。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/CCDirector.js:629 |
| 废弃(Deprecated) | since v2.0 |
参数列表
clearColorColor
getRunningScene
获取当前逻辑场景。
| meta | description |
|---|---|
| 返回 | Scene |
| 定义于 | cocos2d/core/CCDirector.js:647 |
| 废弃(Deprecated) | since v2.0 |
getScene
获取当前逻辑场景。
| meta | description |
|---|---|
| 返回 | Scene |
| 定义于 | cocos2d/core/CCDirector.js:659 |
示例
// This will help you to get the Canvas node in scene
cc.director.getScene().getChildByName('Canvas');
getAnimationInterval
获取单位帧执行时间。请使用 Game.setFrameRate 来控制游戏帧率。
| meta | description |
|---|---|
| 返回 | Number |
| 定义于 | cocos2d/core/CCDirector.js:672 |
| 废弃(Deprecated) | since v2.0 |
setAnimationInterval
Sets animation interval, this doesn't control the main loop. To control the game's frame rate overall, please use Game.setFrameRate
| meta | description |
|---|---|
| 定义于 | cocos2d/core/CCDirector.js:683 |
| 废弃(Deprecated) | since v2.0 |
参数列表
valueNumber The animation interval desired.
getDeltaTime
获取上一帧的增量时间。
| meta | description |
|---|---|
| 返回 | Number |
| 定义于 | cocos2d/core/CCDirector.js:694 |
getTotalTime
获取从游戏开始到现在总共经过的时间,单位为 ms
| meta | description |
|---|---|
| 返回 | Number |
| 定义于 | cocos2d/core/CCDirector.js:704 |
getTotalFrames
获取 director 启动以来游戏运行的总帧数。
| meta | description |
|---|---|
| 返回 | Number |
| 定义于 | cocos2d/core/CCDirector.js:714 |
isPaused
是否处于暂停状态。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/CCDirector.js:724 |
getScheduler
获取和 director 相关联的 cc.Scheduler。
| meta | description |
|---|---|
| 返回 | Scheduler |
| 定义于 | cocos2d/core/CCDirector.js:734 |
setScheduler
设置和 director 相关联的 cc.Scheduler。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/CCDirector.js:744 |
参数列表
schedulerScheduler
getActionManager
获取和 director 相关联的 cc.ActionManager(动作管理器)。
| meta | description |
|---|---|
| 返回 | ActionManager |
| 定义于 | cocos2d/core/CCDirector.js:756 |
setActionManager
设置和 director 相关联的 cc.ActionManager(动作管理器)。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/CCDirector.js:765 |
参数列表
actionManagerActionManager
getCollisionManager
获取和 director 相关联的 cc.CollisionManager (碰撞管理器)。
| meta | description |
|---|---|
| 返回 | CollisionManager |
| 定义于 | cocos2d/core/CCDirector.js:791 |
getPhysicsManager
返回与 director 相关联的 cc.PhysicsManager (物理管理器)。
| meta | description |
|---|---|
| 返回 | PhysicsManager |
| 定义于 | cocos2d/core/CCDirector.js:801 |
getPhysics3DManager
返回与 director 相关联的 cc.Physics3DManager (物理管理器)。
| meta | description |
|---|---|
| 返回 | Physics3DManager |
| 定义于 | cocos2d/core/CCDirector.js:811 |
hasEventListener
检查事件目标对象是否有为特定类型的事件注册的回调。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/event/event-target.js:69 |
参数列表
typeString The type of event.
on
注册事件目标的特定事件类型回调。这种类型的事件应该被 emit 触发。
| meta | description |
|---|---|
| 返回 | Function |
| 定义于 | cocos2d/core/event/event-target.js:77 |
参数列表
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
示例
eventTarget.on('fire', function () {
cc.log("fire in the hole");
}, node);
off
删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:119 |
参数列表
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
示例
// 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
在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。 这个函数无法删除当前 EventTarget 的所有事件监听器,也无法删除 target 参数所注册的所有事件监听器。 这个函数只能删除 target 参数在当前 EventTarget 上注册的所有事件监听器。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:163 |
参数列表
targetObject The target to be searched for all related listeners
once
注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:182 |
参数列表
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
示例
eventTarget.once('fire', function () {
cc.log("this is the callback and will be invoked only once");
}, node);
dispatchEvent
通过事件对象派发事件
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:208 |
参数列表
eventEvent
clear
销毁记录的事件
| meta | description |
|---|---|
| 定义于 | cocos2d/core/event/event-target.js:221 |
removeAll
移除在特定事件类型中注册的所有回调或在某个目标中注册的所有回调。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/platform/callbacks-invoker.js:235 |
参数列表
emit
通过事件名发送自定义事件
| meta | description |
|---|---|
| 定义于 | cocos2d/core/platform/callbacks-invoker.js:309 |
参数列表
keyString event typearg1Any First argumentarg2Any Second argumentarg3Any Third argumentarg4Any Fourth argumentarg5Any Fifth argument
示例
eventTarget.emit('fire', event);
eventTarget.emit('fire', message, emitter);
事件
cc.Director.EVENT_BEFORE_SCENE_LOADING Event
模块: cc
加载新场景之前所触发的事件。
索引
Details
cc.Director.EVENT_AFTER_SCENE_LAUNCH Event
模块: cc
运行新场景之后所触发的事件。
索引
Details
cc.Director.EVENT_BEFORE_UPDATE Event
模块: cc
每个帧的开始时所触发的事件。
索引
Details
cc.Director.EVENT_AFTER_UPDATE Event
模块: cc
将在引擎和组件 “update” 逻辑之后所触发的事件。
索引
Details
cc.Director.EVENT_BEFORE_DRAW Event
模块: cc
渲染过程之前所触发的事件。
索引
Details
cc.Director.EVENT_AFTER_DRAW Event
模块: cc
渲染过程之后所触发的事件。
索引
Details
Director.EVENT_BEFORE_PHYSICS Event
模块: cc
The event which will be triggered before the physics process.
物理过程之前所触发的事件。
索引
Details
Director.EVENT_AFTER_PHYSICS Event
模块: cc
The event which will be triggered after the physics process.
物理过程之后所触发的事件。