动态创建 sprite node

准备工作

    1. 准备好资源,并且要放在"resources"目录下
    1. 准备好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);
    }


你可能感兴趣的:(动态创建 sprite node)