引言
cocos creator基礎(chǔ)-(五)cc.Component使用
(文末附視頻教程)
組件入口函數(shù)
1: onLoad: 組件加載的時(shí)候調(diào)用, 保證了你可以獲取到場(chǎng)景中的其他節(jié)點(diǎn),以及節(jié)點(diǎn)關(guān)聯(lián)的資源數(shù)據(jù)
2: start: 也就是第一次執(zhí)行 update 之前觸發(fā)
3: update(dt):組件每次刷新的時(shí)候調(diào)用,距離上一次刷新的時(shí)間(會(huì)在所有畫面更新前執(zhí)行)
4: lateUpdate(dt) 刷新完后調(diào)用(會(huì)在所有畫面更新后執(zhí)行);
5: onEnable: 啟用這個(gè)組件的時(shí)候調(diào)用;
6: onDisable: 禁用這個(gè)組件的時(shí)候調(diào)用;
7: onDestroy: 組件實(shí)例銷毀的時(shí)候調(diào)用;
cc.Component屬性
1: 組件類: 所有組件的基類;
2: node: 指向這個(gè)組件實(shí)例所掛載的這個(gè)節(jié)點(diǎn)(cc.Node);
3: name: 這個(gè)組件實(shí)例所掛載的節(jié)點(diǎn)的名字<組件的名字>;
4: properties: {
? ?} 屬性列表;
? ?(1) name: value, 數(shù),bool, 字符串;
? ?(2) 位置,顏色, 大小: ?cc.p(0, 0), cc.color(0, 0), cc.size(100, 100)
? ?(3) 組件: {
? ? ? ? ? type: 組件類型, 系統(tǒng)類型,也可以require自己編寫的組件類型
? ? ? ? ? default: null or []
? ? }
(4)其他: 打開cocos creator源碼,找到參考,然后移動(dòng)到你的代碼里面;
組件添加查找刪除
1: ?addComponent(組件的類型): 向節(jié)點(diǎn)上添加一個(gè)組件實(shí)例, 返回添加好的組件實(shí)例;
2: ?getComponent(組件類型): 查找一個(gè)為指定類型的組件實(shí)例(如果有多個(gè),第一個(gè)匹配);
3: getComponents(組件類型): 查找這個(gè)節(jié)點(diǎn)上所有這個(gè)類型的組件實(shí)例;
? ?[inst1, inst2, inst3, ...]
4: getComponentInChildren(組件類型): ?在自己與孩子節(jié)點(diǎn)里面查找;
5: getComponentsInChildren (組件類型): 在自己與孩子節(jié)點(diǎn)里面查找;
6: destroy(): 從節(jié)點(diǎn)中刪除這個(gè)組件的實(shí)例;
Shedule定時(shí)器操作
1: ?sheduleOnce(函數(shù), time): time秒后啟動(dòng)一次定時(shí)器;
2: schedule(函數(shù), time, 次數(shù), ?多長時(shí)間后開始); 執(zhí)行的次數(shù)為(次數(shù) + 1), cc.macro.REPEAT_FOREVER
3: unschedule(函數(shù)); // 取消這個(gè)定時(shí)器操作;
5: unscheduleAllCallbacks ?取消所有的定時(shí)器操作;
注意,如果節(jié)點(diǎn)或組件沒有激活是不會(huì)調(diào)用的;
var my_item = require("my_item");
// 返回了一個(gè)構(gòu)造函數(shù),然后繼承了cc.Component
// 代碼組件也有cc.Component組件的方法;
// cc.Component, 固定的入口函數(shù)
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
? ? ? ? // },
? ? ? ? // ...
? ? ? ? // 基本數(shù)據(jù)類型, 數(shù),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?
? ? ? ? // 系統(tǒng)的組件, 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
? ? // 組件在加載的時(shí)候運(yùn)行
? ? // 你可以在onLoad里面訪問場(chǎng)景的節(jié)點(diǎn)和數(shù)據(jù),這個(gè)時(shí)候場(chǎng)景的節(jié)點(diǎn)和數(shù)據(jù)都已經(jīng)準(zhǔn)備好了
? ? // 不會(huì)發(fā)生在調(diào)用onLoad的時(shí)候,還會(huì)出現(xiàn)場(chǎng)景節(jié)點(diǎn)沒有出來的情況
? ? onLoad: function () {
? ? ? ? console.log("onLoad");
? ? ? ? // this, 指的是當(dāng)前的組件實(shí)例
? ? ? ? // this.node --> cc.Node, 這個(gè)組件所掛的節(jié)點(diǎn)對(duì)象
? ? ? ? // 組件實(shí)例找對(duì)應(yīng)的節(jié)點(diǎn)? ?組件.node來獲取;
? ? ? ? console.log(this.node);
? ? ? ? // Canvas<game_scene> Canvas
? ? ? ? console.log(this.name, this.node.name); // 組件實(shí)例所掛載的節(jié)點(diǎn)的名稱<組件名稱>, 節(jié)點(diǎn).name 直接為名稱;
? ? },
? ? // 組件在第一次update調(diào)用之前調(diào)用
? ? start: function() {
? ? ? ? console.log("start");
? ? ? ? // 添加組件,系統(tǒng)組件cc.Sprite, cc.Label等, "組件代碼的名字"
? ? ? ? // 返回,返回掛上的組件實(shí)例
? ? ? ? var com_inst = this.addComponent("my_item");
? ? ? ? com_inst = this.node.addComponent("my_item");
? ? ? ? // end?
? ? ? ? // 查找組件實(shí)例
? ? ? ? com_inst = this.node.getComponent("my_item");
? ? ? ? com_inst = this.getComponent("my_item"); // 返回的是第一個(gè)找到的組件
? ? ? ? var com_array = this.getComponents("my_item"); // 返回的是組件數(shù)組[實(shí)例1,實(shí)例2, 實(shí)例3]
? ? ? ? console.log(com_inst, com_array);
? ? ? ? // end?
? ? ? ? // 刪除組件
? ? ? ? // this.destroy(); // 刪除當(dāng)前的組件實(shí)例,觸發(fā)onDisable, onDestroy的調(diào)用
? ? ? ? // end?
? ? ? ? // 啟動(dòng)定時(shí)器, 節(jié)點(diǎn)或組件必須是激活狀態(tài),? 例如被隱藏的節(jié)點(diǎn),都是無法啟動(dòng)定時(shí)器的;
? ? ? ? // 這里只會(huì)觸發(fā)一次調(diào)用
? ? ? ? this.scheduleOnce(function() {
? ? ? ? ? ? console.log("scheduleOnce called");
? ? ? ? }.bind(this), 5);
? ? ? ? // end?
? ? ? ? // schedule(函數(shù), 多長時(shí)間掉一次, 次數(shù)(永遠(yuǎn)), 隔多少秒以后開始執(zhí)行shedule)
? ? ? ? // 5秒鐘以后,每隔1秒,我們調(diào)用6 + 1次函數(shù);
? ? ? ? this.schedule(function() {
? ? ? ? ? ? console.log("schedule called");
? ? ? ? }.bind(this), 1, 6, 5); // 次數(shù) 6 + 1 = 7;
? ? ? ? // end?
? ? ? ? this.schedule(function() {
? ? ? ? ? ? console.log("schedule forerver called");
? ? ? ? }.bind(this), 1, cc.macro.REPEAT_FOREVER, 5); // 次數(shù) 6 + 1 = 7;? cc.macro.REPEAT_FOREVER 永遠(yuǎn)
? ? ? ? // end?
? ? ? ? // 取消所有的shedule
? ? ? ? this.scheduleOnce(function() {
? ? ? ? ? ? console.log("cancel all schedules");
? ? ? ? ? ? this.unscheduleAllCallbacks();??
? ? ? ? }.bind(this), 30);
? ? ? ? // 只取消一個(gè), unschedule(函數(shù)對(duì)象)
? ? ? ? var callback = function() {
? ? ? ? ? ? console.log("======================");
? ? ? ? }.bind(this);
? ? ? ? this.schedule(callback, 0.5); // 默認(rèn)值為永遠(yuǎn)執(zhí)行,馬上開始
? ? ? ? this.scheduleOnce(function() {
? ? ? ? ? ? // 取消了一個(gè)定時(shí)器
? ? ? ? ? ? this.unschedule(callback);
? ? ? ? }.bind(this), 5);
? ? },
? ? // called every frame, uncomment this function to activate update callback
? ? // 每次游戲刷新的時(shí)候調(diào)用, dt距離閃一次刷新的實(shí)踐
? ? update: function (dt) {
? ? ? ? // console.log("update called", dt);
? ? },
? ? // 不是特別常用
? ? lateUpdate: function(dt) {
? ? ? ? // console.log("lateUpdate");
? ? },
? ? // 組件被激活的時(shí)候調(diào)用
? ? onEnable: function() {
? ? ? ? console.log("onEnable");
? ? },
? ? // 組件被禁用的時(shí)候調(diào)用
? ? onDisable: function() {
? ? ? ? console.log("onDisable");
? ? },?
? ? // 組件實(shí)例銷毀的時(shí)候調(diào)用
? ? onDestroy: function() {
? ? ? ? console.log("onDestroy");
? ? },
});
視頻教程:
鏈接:https://pan.baidu.com/s/16Tg5cNmbs1Uou1OJZDeLGQ
提取碼:dt28
我也創(chuàng)建了個(gè)cocos creator的學(xué)習(xí)交流群歡迎大家一起來討論點(diǎn)擊鏈接加入群聊【cocos/unity交流群】