組件入口函數1: onLoad: 組件載入的時候調用, 保證了你可以獲取到場景中的其他節點,以及節點關聯的資源數據;2: start: 也就是第一次執行 update 之前觸發;3: update(dt):組件每次刷新的時候調用,距離上一次刷新的時間(會在所有畫面更新前執行);4: lateUpda ...
組件入口函數
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 取消所有的定時器操作;
註意,如果節點或組件沒有激活是不會調用的;
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"); },