1 建立prefab
保存prefab的名称为testPrefab.prefab
2 代码中动态创建prefab
Laya.loader.create('prefab/testPrefab.json',Laya.Handler.create(this,this.onPrefabLoader));
onPrefabLoader(obj:any):void{
this.pref = new Laya.Prefab();
this.pref.json = obj;
console.log("load prefab:",obj);
for(let i=0;i<8;i++) {
this.matchPlayerArr[i] = this.pref.create();
this.matchPlayerArr[i].x = 150*i
this.matchPlayerArr[i].y = 454
this.touchSceneView.matchPanel.addChild( this.matchPlayerArr[i]);
}
}
这里创建了8个prefab的对象,添加到页面中。
3 代码访问prefab的子控件
let selfImg=this.matchPlayerArr[0].getChildByName("selfImg") as Laya.Image
let imgAni=this.matchPlayerArr[0].getChildByName("imgAni") as Laya.Animation