I’m using cocos2d-js v3.0beta.
I have tried cc.loader.loadJson like below:
cc.loader.loadJson(res.Config_json, function(err, data){
if (err) {
return cc.log("load failed");
} else {
cc.log("config: " + data);// data is the json object
self.config = data;
}
});
It works in JSB, but has a delay in HTML5. In HTML5, the json file is loaded after other code need to read its data, which leads to such error of “Cannot read property 'stageCount' of null'”. If I use:
cc.loader.load(res.Config_json, function(results){
self.config = results[0];
cc.log("config.stageCount--->" + self.config.stageCount);
});
Then, it work well and has no delay in HTML5, but when compile for win32 using JSB, it report error: TypeError: self.config is null. It seems to be unable to load the JSON file using cc.loader.load in JSB.
cc.loader.load and cc.loader.loadJson act differently in HTML5 and JSB. What’s the problem? How can I deal with it? If I want to it work in both HTML5 and JSB, what can I do?