代码如下,是读取JSON后
loadPlayerInfo是用来解析数据的,调用loadPlayerInfo报错
import Global from “./Global”;
const {ccclass, property} = cc._decorator;
@ccclass
export default class Common extends cc.Component {
public onLoad(){
this.init();
}
public init(){
cc.log("common onload");
cc.loader.loadResDir("data",this.completeCallback);
}
public completeCallback(error: Error, resource: any[], urls: string[]){
if (error) {
cc.log("error:"+error);
}else{
for (let index = 0; index < resource.length; index++) {
let element = resource[index];
cc.log("completeCallback:"+element.name);
if(element.name == "player"){
cc.log("choice"+element.name);
this.loadPlayerInfo(element);
}
//cc.director.loadScene("Game");
}
}
}
public loadPlayerInfo(element){
cc.log("completeCallback:"+element.name);
let attack : number = element.json.playerinfo.default.attack;
let defence : number = element.json.playerinfo.default.defence;
let hp : number = element.json.playerinfo.default.hp;
let hp_max : number = element.json.playerinfo.default.hp_max;
Global.PlayerInfo = [attack,defence,hp,hp_max];
}
}
报错如下
Common.ts:25
TypeError: Cannot read property ‘loadPlayerInfo’ of undefined
Common.ts:26
at Common.completeCallback (d:\wxgame_release\zombie\assets\Script\Common.ts:26:26)
at http://localhost:7456/app/engine/bin/cocos2d-js-for-preview.js:20253:29
at CCLoader. (http://localhost:7456/app/engine/bin/cocos2d-js-for-preview.js:20212:20)
at http://localhost:7456/app/engine/bin/cocos2d-js-for-preview.js:20084:37
at http://localhost:7456/app/engine/bin/cocos2d-js-for-preview.js:29351:11
请各路大神帮忙
好好看报错信息TypeError: Cannot read property ‘loadPlayerInfo’ of undefined
this指向问题