本文基于 Cocos Creator 2.4.1 撰写。
目录:根目录下
代码code如下:
// PREDEFINE
// window may be undefined when first load engine from editor
var _global = typeof window === 'undefined' ? global : window;
//定义全局变量 获取global 或者window ,因为运行环境是非web 所以获取到的是global。大家注意
/**
* !#en
* The main namespace of Cocos2d-JS, all engine core classes, functions, properties and constants are defined in this namespace.
* !#zh
* Cocos 引擎的主要命名空间,引擎代码中所有的类,函数,属性和常量都在这个命名空间中定义。
* @module cc
* @main cc
*/
_global.cc = _global.cc || {};//定义全局的cc变量
// For internal usage
cc.internal = cc.internal || {};//给cc下定义全局变量internal
require('./predefine');//定义全局配置变量,预定义 其中CC_DEV 等宏变量都在里面
// polyfills 基于系统 类的拓展,为了处理兼容ie等浏览器 或者新增方便的用法
require('./polyfill/string');//比如string 拓展了startsWith endsWith trimLeft
require('./polyfill/misc');//数学函数拓展
require('./polyfill/array');//array拓展
require('./polyfill/object');//object拓展
require('./polyfill/array-buffer');//拓展isView方法
require('./polyfill/number');//parseFloat parseInt
if (!(CC_EDITOR && Editor.isMainProcess)) {//CC_EDITOR 在预定义文件里面有定义,这个逻辑是针对web环境?
require('./polyfill/typescript');//引入
}
require('./cocos2d/core/predefine');//基于cocos内置的拓展
// LOAD COCOS2D ENGINE CODE
if (!(CC_EDITOR && Editor.isMainProcess)) {
require('./cocos2d');//引入 ccGame 和 action 部分
}
// LOAD EXTENDS
require('./extends');
/*
拓展部分包括
core
动画模块
粒子
tilemap
web环境 模块还会多引入video webview CCNodePool 和action
*/
if (CC_EDITOR) {
if (Editor.isMainProcess) {
Editor.versions['cocos2d'] = require('./package').version;
}
}
/*
导出global 下的cc 命名空间 */
module.exports = _global.cc;