cocos creator cc.Director与资源加载策略

1: 了解creator场景切换;

2: 了解director基本的一些接口;

3: 理解资源加载的策略;


cc.Director对象

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

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

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

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

常用接口

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

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

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

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

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

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

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

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

资源加载策略

1: h5资源加载的过程:

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

2:三种资源加载策略:

  1>: h5的小游戏:采用全部提前绑定好所有的资源。编写预加载脚本preload.js,

    将要加载的资源手动关联到第一个启动的场景上面,一次性预加载所有;

  2>: 添加等待界面,预加载下一个场景,然后再进行切换,提前关联好下一个场景要的资源,每一个需要加载资源的场景都应该挂载一个preload.js脚本区关联资源;

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

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

    };

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

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

  });

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

//preload.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

        // },

        // ...

        img_array: {

            type: cc.SpriteFrame,

            default: [],

        },

        atlas_array: {

            default: [],

            type: cc.SpriteAtlas,

        },

        sound_array: {

            default: [],

            url: cc.AudioClip,

        },

        prefab_array: {

            default: [],

            type: cc.Prefab,

        },

    },

    // use this for initialization

    onLoad: function () {

    },

    // called every frame, uncomment this function to activate update callback

    // update: function (dt) {

    // },

});

//home_scene.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) {

    // },

});

我也创建了个cocos creator的学习交流群欢迎大家一起来讨论点击链接加入群聊【cocos/unity交流群】

你可能感兴趣的:(cocos creator cc.Director与资源加载策略)