creator小功能----浅谈cc.Director与 资源加载策略

游戏里面控制管理整个游戏全局对象,包括了场景切换等,为cc.Director对象;

导演对象全局只有一个cc.director,大写的为类, 小写的cc.director为全局的导演对象;

cc.director来获取导演对象实例;

游戏中各种管理对象都可以通过cc.director获取,比如物理引擎管理,Action管理, 碰撞检测管理等;

常用接口

1: getWinSize: 适配后的逻辑大小;

2: getWinSizeInPixels: 获取窗口的像素大小;

3: getScene: 获取当前的逻辑场景,场景对象下面是Canvas;

4: setDisplayStats: 是否显示左下角FPS信息;

5: getCollisionManager: 获取碰撞检测管理对象;

6: getPhysicsManager :获取物理引擎管理对象;

扫描二维码关注公众号,回复: 9781661 查看本文章

7:loadScene(scene_name):加载场景,场景的名字,系统会加载对应的场景

8:preloadScene(scene_name):预加载场景

资源加载策略

1: h5资源加载的过程:

    (1)从服务器上下载来来资源,并把资源加载到内存中,所以你在做h5游戏,你要把你当前游戏中要用到的资源先加载下来,否者的话,你在运行的时候去加载就来不及了(h5卡住);

2:三种资源加载策略:

 1>: h5的小游戏:采用全部提前绑定好所有的资源。编写预加载脚本preload.js, 将要加载的资源手动关联到第一个启动的场景上面;

 2>: 添加等待界面,预加载下一个场景,然后再进行切换,提前关联好下一个场景要的资源;

           cc.loader.onProgress = function ( completedCount, totalCount,  item ){

            console.log("completedCount:" + completedCount + ",totalCount:" + totalCount );

        };

 3>  嫌手动关联麻烦,在场景切换中加入过渡场景,代码来加载场景的资源:

            cc.loader.loadResAll("textures", function (err, assets) {         });

   代码加载资源会导致setting.js文件过大,一般尽量少在代码里面加载资源;

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...

        wait: {
            type: cc.Node,
            default: null,
        },

        progress_label: {
            type: cc.Label,
            default: null,
        },
    },

    // use this for initialization
    onLoad: function () {
        this.wait.active = false;
        this.progress_label.string = "0%";
    },

    goto_roadmap: function() {
        // 你在做场景切换的时候,如果你直接切换过去,
        // 由于下一个场景一定要先加载完它所需要的资源,那么一定会卡住一段时间;
        // 会在这里加上我们的等待界面,加入,场景加载的等待场景;
        this.wait.active = true;
        cc.loader.onProgress = function(completedCount, totalCount, item){
            console.log("completedCount:" + completedCount + ",totalCount:" + totalCount);
            var per = Math.floor(completedCount * 100 / totalCount);
            this.progress_label.string = per + "%";
        }.bind(this);

        // 预加载
        cc.director.preloadScene("roadmap_scene", function() {
            cc.loader.onProgress = null;
            cc.director.loadScene("roadmap_scene");
        });
        // end 
    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
发布了265 篇原创文章 · 获赞 20 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/ccnu027cs/article/details/104687038