在我的游戏中,我尝试加载json文件并将其保存到属性中的一个变量,并将其用于另一个函数,但失败了。 这个变量被称为未定义的变量。 我们如何解决它?
(英文论坛没有答案,请在这里提问)
cc.Class({
extends: cc.Component,
properties: {
script_List:{
type:Object,
default:[]
},
},
loadScript:function(json_scriptName){
var url = cc.url.raw (json_scriptName);
cc.loader.load(url, function(err, res)
{
var stringJson = JSON.stringify(res);
var parJson = JSON.parse(stringJson);
var indexJson = parJson.Index;
var jsonL = indexJson.length;
this.script_List = new Array(jsonL);
for(var i=0;i<jsonL;i++){
this.script_List[i] = parJson.Index[i];
}
});
},
start () {
this.loadScript('resources/m00.json');
},
update (dt) {
if(this.script_List.length>0){
this.readScript();
}
},
readScript:function(){
for(var i=0; i<this.script_List.length; i++){ //<----error in here. script_List is undefined
if(this.script_List[i].Command == 'CG'){
cc.log('command is CG');
}
}
},
});
===========================code.js
{
"Index": [
{
"CommandName": "CG",
"Commands": [
"cg",
"story_title_0.png",
"300",
"280",
"0",
"1",
"0",
"0",
"0"
]
},
{
"CommandName": "FadeIn",
"Commands": [
"cg",
"0.5"
]
},
{
"CommandName": "Delay",
"Commands": [
"1.0"
]
},
{
"CommandName": "FadeOut",
"Commands": [
"cg",
"0.5"
]
},
{
"CommandName": "Delay",
"Commands": [
"0.8"
]
},
{
"CommandName": "RemoveAllCG",
"Commands": [
"RemoveAllCG"
]
}
]
}
====================json
cocos creator 版本是1.9.0。