序言
上一篇写了关于CCGame.js的解读,当中用到了需要cc.director的函数,想必cc.director在引擎中也是举足轻重的,那么这一篇我们就来看看CCDirector.js的源码吧。
话不多说,地址附上。
https://github.com/cocos-creator/engine/blob/v2.4.5/cocos2d/core/CCDirector.js
源码解析
// cc.EventTarget对象引入
const EventTarget = require('./event/event-target');
// 组件管理类引入
const ComponentScheduler = require('./component-scheduler');
// 节点激活/反激活管理类
const NodeActivator = require('./node-activator');
// cc.obj
const Obj = require('./platform/CCObject');
// cc.game对象引入
const game = require('./CCGame');
// cc.renderer 对象(提供渲染层接口等)导入
const renderer = require('./renderer');
// 事件管理器
const eventManager = require('./event-manager');
// 负责触发回调函数,定时器的类
const Scheduler = require('./CCScheduler');
//----------------------------------------------------------------------------------------------------------------------
/**
* !#en
* <p>
* ATTENTION: USE cc.director INSTEAD OF cc.Director.<br/>
* cc.director is a singleton object which manage your game's logic flow.<br/>
* Since the cc.director is a singleton, you don't need to call any constructor or create functions,<br/>
* the standard way to use it is by calling:<br/>
* - cc.director.methodName(); <br/>
*
* It creates and handle the main Window and manages how and when to execute the Scenes.<br/>
* <br/>
* The cc.director is also responsible for:<br/>
* - initializing the OpenGL context<br/>
* - setting the OpenGL pixel format (default on is RGB565)<br/>
* - setting the OpenGL buffer depth (default on is 0-bit)<br/>
* - setting the color for clear screen (default one is BLACK)<br/>
* - setting the projection (default one is 3D)<br/>
* - setting the orientation (default one is Portrait)<br/>
* <br/>
* <br/>
* The cc.director also sets the default OpenGL context:<br/>
* - GL_TEXTURE_2D is enabled<br/>
* - GL_VERTEX_ARRAY is enabled<br/>
* - GL_COLOR_ARRAY is enabled<br/>
* - GL_TEXTURE_COORD_ARRAY is enabled<br/>
* </p>
* <p>
* cc.director also synchronizes timers with the refresh rate of the display.<br/>
* Features and Limitations:<br/>
* - Scheduled timers & drawing are synchronizes with the refresh rate of the display<br/>
* - Only supports animation intervals of 1/60 1/30 & 1/15<br/>
* </p>
*
* !#zh
* <p>
* 注意:用 cc.director 代替 cc.Director。<br/>
* cc.director 一个管理你的游戏的逻辑流程的单例对象。<br/>
* 由于 cc.director 是一个单例,你不需要调用任何构造函数或创建函数,<br/>
* 使用它的标准方法是通过调用:<br/>
* - cc.director.methodName();
* <br/>
* 它创建和处理主窗口并且管理什么时候执行场景。<br/>
* <br/>
* cc.director 还负责:<br/>
* - 初始化 OpenGL 环境。<br/>
* - 设置OpenGL像素格式。(默认是 RGB565)<br/>
* - 设置OpenGL缓冲区深度 (默认是 0-bit)<br/>
* - 设置空白场景的颜色 (默认是 黑色)<br/>
* - 设置投影 (默认是 3D)<br/>
* - 设置方向 (默认是 Portrait)<br/>
* <br/>
* cc.director 设置了 OpenGL 默认环境 <br/>
* - GL_TEXTURE_2D 启用。<br/>
* - GL_VERTEX_ARRAY 启用。<br/>
* - GL_COLOR_ARRAY 启用。<br/>
* - GL_TEXTURE_COORD_ARRAY 启用。<br/>
* </p>
* <p>
* cc.director 也同步定时器与显示器的刷新速率。
* <br/>
* 特点和局限性: <br/>
* - 将计时器 & 渲染与显示器的刷新频率同步。<br/>
* - 只支持动画的间隔 1/60 1/30 & 1/15。<br/>
* </p>
*
* @class Director
* @extends EventTarget
*/
cc.Director = function () {
// 将EventTarget的原型链复制生成的单例中
EventTarget.call(this);
// paused?
this._paused = false;
// purge?
this._purgeDirectorInNextLoop = false;
// 默认为cc.Sie(0, 0),暂无任何作用
this._winSizeInPoints = null;
// scenes
this._scene = null;
// 当前loading的场景名:string类型
this._loadingScene = '';
// FPS
// director 启动以来游戏运行的总帧数
this._totalFrames = 0;
// 最后更新时间
this._lastUpdate = 0;
// 上一帧的增量时间
this._deltaTime = 0.0;
// 开始时间,默认为本类实例化时
this._startTime = 0.0;
// ParticleSystem max step delta time
this._maxParticleDeltaTime = 0.0;
// Scheduler for user registration update
this._scheduler = null;
// Scheduler for life-cycle methods in component
this._compScheduler = null;
// Node activator
this._nodeActivator = null;
// Action manager
this._actionManager = null;
var self = this;
// 在触发 游戏进入前台运行时的事件 时,更新 最后更新时间
game.on(game.EVENT_SHOW, function () {
self._lastUpdate = performance.now();
});
// 监听一次 引擎初始化完成事件 时,调用 cc.director.init 事件
game.once(game.EVENT_ENGINE_INITED, this.init, this);
};
// 原型链
cc.Director.prototype = {
constructor: cc.Director,
init: function () {
// 设置各个初始值
this._totalFrames = 0;
this._lastUpdate = performance.now();
this._startTime = this._lastUpdate;
this._paused = false;
this._purgeDirectorInNextLoop = false;
this._winSizeInPoints = cc.size(0, 0);
this._scheduler = new Scheduler();
// 是否存cc.ActionManager类
if (cc.ActionManager) {
this._actionManager = new cc.ActionManager();
// 使用指定的优先级为指定的对象设置 update 定时器
this._scheduler.scheduleUpdate(this._actionManager, Scheduler.PRIORITY_SYSTEM, false);
} else {
this._actionManager = null;
}
// 初始化其他模块
this.sharedInit();
return true;
},
/*
* Manage all init process shared between the web engine and jsb engine.
* All platform independent init process should be occupied here.
*/
sharedInit: function () {
// 生成单例
this._compScheduler = new ComponentScheduler();
// 生成单例
this._nodeActivator = new NodeActivator();
// Event manager
if (eventManager) {
// 激活事件管理器
eventManager.setEnabled(true);
}
// Animation manager
if (cc.AnimationManager) {
this._animationManager = new cc.AnimationManager();
// 使用指定的优先级为指定的对象设置 update 定时器
this._scheduler.scheduleUpdate(this._animationManager, Scheduler.PRIORITY_SYSTEM, false);
}
else {
this._animationManager = null;
}
// collision manager
if (cc.CollisionManager) {
this._collisionManager = new cc.CollisionManager();
// 使用指定的优先级为指定的对象设置 update 定时器
this._scheduler.scheduleUpdate(this._collisionManager, Scheduler.PRIORITY_SYSTEM, false);
}
else {
this._collisionManager = null;
}
// physics manager
if (cc.PhysicsManager) {
this._physicsManager = new cc.PhysicsManager();
// 使用指定的优先级为指定的对象设置 update 定时器
this._scheduler.scheduleUpdate(this._physicsManager, Scheduler.PRIORITY_SYSTEM, false);
}
else {
this._physicsManager = null;
}
// physics 3d manager
if (cc.Physics3DManager && (CC_PHYSICS_BUILTIN || CC_PHYSICS_CANNON)) {
this._physics3DManager = new cc.Physics3DManager();
// 使用指定的优先级为指定的对象设置 update 定时器
this._scheduler.scheduleUpdate(this._physics3DManager, Scheduler.PRIORITY_SYSTEM, false);
} else {
this._physics3DManager = null;
}
// WidgetManager
if (cc._widgetManager) {
// 初始化对齐组件
cc._widgetManager