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) {

    // },
});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM