cocos creator基礎-(五)cc.Component使用


一、組件入口函數

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

二、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 取消所有的定時器操作;
注意,如果節點或組件沒有激活是不會調用的;

var my_item = require("my_item");

// 返回了一個構造函數,然后繼承了cc.Component
// 代碼組件也有cc.Component組件的方法;
// cc.Component, 固定的入口函數
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
        // },
        // ...
        // 基本數據類型, 數,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);
        // Canvas<game_scene> Canvas
        console.log(this.name, this.node.name); // 組件實例所掛載的節點的名稱<組件名稱>, 節點.name 直接為名稱;
    },

    // 組件在第一次update調用之前調用
    start: function() {
        console.log("start");
        // 添加組件,系統組件cc.Sprite, cc.Label等, "組件代碼的名字"
        // 返回,返回掛上的組件實例
        var com_inst = this.addComponent("my_item");
        com_inst = this.node.addComponent("my_item");
        // end 

        // 查找組件實例
        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);
        // end 

        // 刪除組件
        // this.destroy(); // 刪除當前的組件實例,觸發onDisable, onDestroy的調用
        // end 

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

        // schedule(函數, 多長時間掉一次, 次數(永遠), 隔多少秒以后開始執行shedule)
        // 5秒鍾以后,每隔1秒,我們調用6 + 1次函數;
        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); // 次數 6 + 1 = 7;  cc.macro.REPEAT_FOREVER 永遠
        // end 


        // 取消所有的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");
    },

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

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

    // 組件實例銷毀的時候調用
    onDestroy: function() {
        console.log("onDestroy");
    },
});

 


免責聲明!

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



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