1 //節點從創建到節點掛載一些過程 2 1、JS中節點使用: 3 a、創建:在properties中定義節點(可能包括節點的默認值和類型); 4 b、掛載:在相應的方法中獲取節點獲取相應節點,掛載到父元素 5 例== 6 cc.Class({ 7 extends: cc.Component, 8 //定義節點 9 properties: { 10 node1:{ 11 default: null, 12 type: cc.SpriteFrame 13 }, 14 node2:{ 15 default: null, 16 type: cc.Sprite 17 }, 18 }, 19 onLoad: function () { 20 var node2 = this.node2.getComponent(cc.Sprite);// 獲取節點元素 21 //JS節點獲取:cc.find('Canvas/alert').getComponent('MajiangDataBind'); 22 // 獲取UI等其他節點:this.node.getChildByName('chat_btn_send'); 23 // 將節點掛載 24 node2.spriteFrame = this.node1; 25 } 26 c、節點賦值:在cocos編輯器中 27 c1、在左側層級管理器中創建對應節點。 28 c2、選中該節點的父元素,掛載節點到JS創建的節點上.找到對應的節點,把節點,圖片或者預制資源拖進去。 29
1 2、修改節點屬性: 2 this.節點.屬性名(此時節點必須在properties) 3 3、節點獲取 4 a、獲取畫布下node節點的名叫‘label’的組件:cc.find('Canvas/node').getComponent('label') 5 b、獲取彈出框的節點: cc.find('Canvas/alert') 6 c、獲取節點:cc.find('Canvas').getChildByName('node') 7 d、獲取在properties中定義的節點:this.節點名
一些常規用法,見鏈接:https://blog.csdn.net/qq_14997169/article/details/77205177?locationNum=8&fps=1