错误堆栈:
Uncaught TypeError: Cannot read property ‘noCache’ of nullurlAppendTimestamp @ CCLoader.js:3
cc.loader.cc.loader.loadTxt @ CCLoader.js:193
load @ Utils.js:18
require.Utils @ Utils.js:30
s @ _prelude.js:1
e @ _prelude.js:1
(anonymous function) @ _prelude.js:1
Utils.js代码:
var *
cfg *= {
moveSpeed: 10,
version:
“0.15”,
showTutorial:
true,
load:
function () {
*//cc.game.config = {
** // ‘noCache’:true
** //};
var txt =
“text=”;
var url = *
cc*.
url.raw(
‘txt/README.md’);
console.log(url);
*
cc*.
loader.loadTxt(url,
function(err,content){
console.log(
‘utils_text=’ + content+
“,”);
for(
var p
in err){
console.log(
“v:”+p+
“;”);
}
});
*
cc*.
sys.
localStorage.
setItem(
‘key’,
‘cacheText’);
this.
version = *
cc*.
sys.
os+
“,”+*
cc*.
sys.
browserType+
“,”+txt;
}
};
*
cfg*.load();
module*.exports = *
cfg*;
onLoad函数部分代码:
var utils =
require(
‘Utils’);
console.log(
'utils -> ’ + utils.version);
按照官方说法加载时,会在游戏开始时会自动 require 所有脚本,这时每个模块内部定义的代码就会被执行一次,所以之后无论又被 require 几次,返回的始终是同一份实例。
问题原因:上面的代码在加载Utils.js的时候cc.game.config还是为空,所以导致参数取不到报错。
另外还发现:
var url = *
cc*.
url.raw(
‘txt/README.md’);得到的地址为: txt/README.md
但是同样的代码,你放到onLoad里面,得到的是: res/raw-assets/txt/README.md。
是不是通过require加装的js脚步里面,不能使用一些引擎中的函数?因为这时候引擎自己还没初始化好呢。