ccc版本:2.4.3
onLoad () {
//动态加载prefab
cc.resources.load("prefabs/role",cc.Prefab,(e,asset)=>{
if(!e && asset.loaded){
asset.createNode((e,p)=>{
if(!e){
this.role = p
this.tiledObjectLayar = this.node.getChildByName("对象层 1").getComponent(cc.TiledObjectGroup)
let o1 = this.tiledObjectLayar.getObject("出生地1")
this.node.addChild(this.role)
this.role.setPosition(o1.x,o1.y)
}else{
cc.log("角色加载出错")
}
})
}else{
cc.log("角色加载出错")
}
})
}
cc.resources.load()和文档里的参数不一样,callback的第二个参数是cc.Asset。所以不能用cc.instantiate()创建,我看到cc.Asset对象有个createNode()方法,我加载的资源是一个prefab类型,使用createNode()的时候,报错asset.createNode is not a function,如果这类资源没有相应的节点类型,该方法应该是空的才会报这个错,prefab应该是有节点的吧,怎么会报错呢?我只想动态加载加载prefab(非拖拽)。
createNode()方法vscode的使用提示:
!#en Create a new node using this asset in the scene. If this type of asset dont have its corresponding node type, this method should be null. !#zh 使用该资源在场景中创建一个新节点。 如果这类资源没有相应的节点类型,该方法应该是空的。
