kingBook

导航

CocosCreator循环引用Bug

Level.ts

import Enemy1Character from "./Enemy1Character";

const{ccclass,property}=cc._decorator;

@ccclass
export default class Level extends cc.Component{
	private _enemy1Prefab:cc.Node;
	
	protected onLoad():void{
		let inst=cc.instantiate(this._enemy1Prefab);
		let enemy1Character=inst.getComponent(Enemy1Character);
	}
	
}

BaseEnemyCharacter.ts

import Level from "./Level";
const{ccclass,property}=cc._decorator;

@ccclass
export default abstract class BaseEnemyCharacter extends cc.Component{
	//@property({type:Level,visible:true}) //会导致转换后的js生成require("./Level")出现循环引用错误:load script [./Enemy1Character] failed : TypeError: Object prototype may only be an Object or null: undefined
	protected _level:Level=null;
	
	protected test(val:Level):void{
		this._level=this.node.parent.getComponent(Level); //会导致转换后的js生成require("./Level")出现循环引用错误:load script [./Enemy1Character] failed : TypeError: Object prototype may only be an Object or null: undefined
	}
	
}

Enemy1Character.ts

import BaseEnemyCharacter from "./BaseEnemyCharacter";

const{ccclass,property}=cc._decorator;

@ccclass
export default class Enemy1Character extends BaseEnemyCharacter{
    
}

Enemy1Character 不继承 BaseEnemyCharacter 时

import Level from "./Level";

const{ccclass,property}=cc._decorator;

@ccclass
export default class Enemy1Character extends cc.Component{
    @property({type:Level,visible:true}) //无法序列化显示(循环引用导致)
	protected _level:Level=null;
	
	protected test(val:Level):void{
		this._level=this.node.parent.getComponent(Level);//不继承可以通过编译
	}
}

posted on 2020-08-26 21:39  kingBook  阅读(749)  评论(0编辑  收藏  举报