准备工作
- 准备好资源,并且要放在"resources"目录下
- 准备好url
动态加载
loadCallBack(err: any, res:any) {
if (err) {
cc.log('Error url [' + err + ']');
return;
}
console.log(res);
this.res = res;
this.setupDatas();
}
load() {
var url = "111.png";
var loadCallBack = this.loadCallBack.bind(this);
// specify the type to load sub asset from texture's url
cc.loader.loadRes(url, cc.SpriteFrame, loadCallBack);
}
setupDatas()
{
var node = new cc.Node("nodeName");
node.setPosition(0, 0);
var component = node.addComponent(cc.Sprite);
component.spriteFrame = this.res;
this.node.addChild(node);
}
网友评论