cc.Component

組件入口函數
1: onLoad: 組件加載的時候調用, 保證了你能夠獲取到場景中的其餘節點,以及節點關聯的資源數據;
2: start: 也就是第一次執行 update 以前觸發;
3: update(dt):組件每次刷新的時候調用,距離上一次刷新的時間(會在全部畫面更新前執行);
4: lateUpdate(dt) 刷新完後調用(會在全部畫面更新後執行);
5: onEnable: 啓用這個組件的時候調用;
6: onDisable: 禁用這個組件的時候調用;
7: onDestroy: 組件實例銷燬的時候調用;node

cc.Component屬性
1: 組件類: 全部組件的基類;
2: node: 指向這個組件實例所掛載的這個節點(cc.Node);
3: name: 這個組件實例所掛載的節點的名字<組件的名字>;
4: properties: {} 屬性列表;
(1) name: value, 數,bool, 字符串;
(2) 位置,顏色, 大小: cc.p(0, 0), cc.color(0, 0), cc.size(100, 100)
(3) 組件: {
type: 組件類型, 系統類型,也能夠require本身編寫的組件類型
default: null or []
}
(4)其餘: 打開cocos creator源碼,找到參考,而後移動到你的代碼裏面;數組

組件添加查找刪除
1: addComponent(組件的類型): 向節點上添加一個組件實例, 返回添加好的組件實例;
2: getComponent(組件類型): 查找一個爲指定類型的組件實例(若是有多個,第一個匹配);
3: getComponents(組件類型): 查找這個節點上全部這個類型的組件實例;
[inst1, inst2, inst3, ...]
4: getComponentInChildren(組件類型): 在本身與孩子節點裏面查找;
5: getComponentsInChildren (組件類型): 在本身與孩子節點裏面查找;
6: destroy(): 從節點中刪除這個組件的實例;函數

Shedule定時器操做
1: sheduleOnce(函數, time): time秒後啓動一次定時器;
2: schedule(函數, time, 次數, 多長時間後開始); 執行的次數爲(次數 + 1), cc.macro.REPEAT_FOREVER;
3: unschedule(函數); // 取消這個定時器操做;
5: unscheduleAllCallbacks 取消全部的定時器操做;
注意,若是節點或組件沒有激活是不會調用的; ui

    properties: {

        // 基本數據類型, 數,bool, 字符串, color, pos, size
        speed: 100,
        is_debug: false,
        url_str: "",
        color: cc.color(0, 0, 0, 255),
        pos: cc.p(0, 0),
        size: cc.size(100, 100),
        // end 

        // 系統的組件, cc.Sprite, cc.Button, cc.Label, ..
        sprite_item: {
            type: cc.Sprite,
            default: null, // null/[]
        },

        sprite_array: {
            type: cc.Sprite,
            default: [],
        },
        // end 

        // 組件的代碼組件
        custom_comp: {
            type: my_item,
            default: null, // null /[]
        },
        // end 
    },
    // end 

    // use this for initialization
    // 組件在加載的時候運行
    // 你能夠在onLoad裏面訪問場景的節點和數據,這個時候場景的節點和數據都已經準備好了
    // 不會發生在調用onLoad的時候,還會出現場景節點沒有出來的狀況
    onLoad: function() {
        console.log("onLoad");
        // this:指的是當前的組件實例
        // this.node --> cc.Node, 這個組件所掛的節點對象
        // 組件實例找對應的節點   組件.node來獲取;
        console.log('this.node:', this.node);
        // Canvas<game_scene> Canvas
        console.log('name:%s,node.name:', this.name, this.node.name); // 組件實例所掛載的節點的名稱<組件名稱>, 節點.name 直接爲名稱;
    },

    // 組件在第一次update調用以前調用
    start: function() {
        console.log("start");
        // 添加組件,系統組件cc.Sprite, cc.Label等, "組件代碼的名字"
        // 返回,返回掛上的組件實例
        var com_inst = this.addComponent("my_item");
        console.log('自定義組件:', com_inst.name);
        com_inst = this.node.addComponent("my_item");
    
        // 查找組件實例
        com_inst = this.node.getComponent("my_item");
        com_inst = this.getComponent("my_item"); // 返回的是第一個找到的組件
        var com_array = this.getComponents("my_item"); // 返回的是組件數組[實例1,實例2, 實例3]
        console.log(com_inst, com_array);
 
        // 刪除組件
         this.destroy(); // 刪除當前的組件實例,觸發onDisable, onDestroy的調用
        // end 

        // 啓動定時器, 節點或組件必須是激活狀態,  例如被隱藏的節點,都是沒法啓動定時器的;
        // 這裏只會觸發一次調用
        this.scheduleOnce(function() {
                console.log("scheduleOnce called");
            }.bind(this),
            5);

        // schedule(函數, 多長時間掉一次, 次數(永遠), 隔多少秒之後開始執行shedule)
        // 5秒鐘之後,每隔1秒,咱們調用6 + 1次函數(重複6次);
        this.schedule(function() {
                console.log("schedule called");
            }.bind(this),1,6,5); // 次數 6 + 1 = 7;
        // end 

        this.schedule(function() {
                console.log("schedule forerver called");
            }.bind(this),
            1,
            cc.macro.REPEAT_FOREVER,
            5); //5秒以後執行;每秒1次;  cc.macro.REPEAT_FOREVER 永遠 


        // 取消全部的shedule
        this.scheduleOnce(function() {
                console.log("cancel all schedules");
                this.unscheduleAllCallbacks();
            }.bind(this),
            30);


        // 只取消一個, unschedule(函數對象)
        var callback = function() {
            console.log("======================");
        }.bind(this);
        this.schedule(callback, 0.5); // 默認值爲永遠執行,立刻開始

        this.scheduleOnce(function() {
                // 取消了一個定時器
                this.unschedule(callback);
            }.bind(this),5);
    },

    // called every frame, uncomment this function to activate update callback
    // 每次遊戲刷新的時候調用, dt距離閃一次刷新的時間
    update: function(dt) {
        console.log("update called", dt);
    },

    // 不是特別經常使用
    lateUpdate: function(dt) {
        console.log("lateUpdate",dt);
    },

    // 組件被激活的時候調用
    onEnable: function() {
        console.log("onEnable");
    },

    // 組件被禁用的時候調用
    onDisable: function() {
        console.log("onDisable");
    },

    // 組件實例銷燬的時候調用
    onDestroy: function() {
        console.log("onDestroy");
    },
相關文章
相關標籤/搜索