美文网首页
cocoscreator中使用Prefab作为场景架构

cocoscreator中使用Prefab作为场景架构

作者: 神大人korose | 来源:发表于2020-01-16 12:03 被阅读0次
cc.mySence = {
    //主场景
    hall:null,
    //加载界面主场景
    homeSence:null,
    //返回场景队列
    gobackList:[],
    //当前场景
    currentScene:null,
    //加载Prefab场景
    loadScene:function (url) {
        if (cc.myApi.currentScene) {
            cc.myApi.currentScene.destroy();
        }
        cc.loader.loadRes(url, cc.Prefab, function (err, prefab) {
            if (err !== null) {
                cc.error(err);
                return
            }
            var node = cc.instantiate(prefab);
            var base = { file : url };
            node.attr(base);
            node.parent = cc.director.getScene();;
            node.setPosition(0,0);
            cc.myApi.currentScene = node;
            cc.myApi.gobackList.push(url);
        });
    },
    //返回上一个Prefab场景
    backSence:function () {
        var last = cc.myApi.gobackList.pop();
        cc.myApi.loadScene(cc.myApi.gobackList.pop());
    },
    //创建Prefab
    showModal: function (url, cb) {
        cc.loader.loadRes(url, cc.Prefab, function (err, prefab) {
            if (err !== null) {
                cc.error(err);
                return
            }
            let windowSize = cc.winSize;
            var scene = cc.director.getScene();
            var node = cc.instantiate(prefab);
            var base = { file : url };
            node.attr(base);
            node.parent = scene;
            node.setPosition(windowSize.width/2,windowSize.height/2);
            if (!!cb) {
                cb(node);
            }
        });
    },
    //删除Prefab
    close: function (file) {
        var scene = cc.director.getScene();
        var children = scene.children;
        for (var i = 0; i < children.length; i++) {
            var child = children[i];
            if (child.file !== null && child.file === file) {
                //child.parent = null;
                child.destroy();
                return;
            }
        }
        cc.error("close prefab failed:" + file);
    },
}

相关文章

网友评论

      本文标题:cocoscreator中使用Prefab作为场景架构

      本文链接:https://www.haomeiwen.com/subject/qrjfzctx.html