天天看點

cocos creator---節點

//節點是cocos遊戲重要的部分。一次節點的處理自然也是重中之重了。接下來整理了節點從建立到節點挂載一些過程。
、JS中節點的使用:
    a、建立:在properties中定義節點(可能包括節點的預設值和類型);
    b、挂載:在相應的方法中擷取節點擷取相應節點,挂載到父元素
    例子:精靈圖節點
    cc.Class({
        extends: cc.Component,
            //定義節點
        properties: {
            node1:{
                default: null,
                type: cc.SpriteFrame
            },
           node2:{
                default: null,
                type: cc.Sprite
            },
        },
        onLoad: function () {
            var node2 = this.node2.getComponent(cc.Sprite);// 擷取節點元素
             //JS節點擷取:cc.find('Canvas/alert').getComponent('MajiangDataBind'); 
             // 擷取UI等其他節點:this.node.getChildByName('chat_btn_send');
            // 将節點挂載
            node2.spriteFrame = this.node1;
        }
    c、節點指派:在cocos編輯器中
        c1、在左側層級管理器中建立對應節點。
        c2、選中該節點的父元素,挂載節點到JS建立的節點上.找到對應的節點,把圖檔或者預制資源拖進去。
           
cocos creator---節點
、修改節點屬性:
     this.節點.屬性名(此時節點必須在properties)
、節點擷取
    a、擷取畫布下node節點的名叫‘label’的元件:cc.find('Canvas/node').getComponent('label')
    b、擷取彈出框的節點: cc.find('Canvas/alert')
    c、擷取節點:cc.find('Canvas').getChildByName('node')
    d、擷取在properties中定義的節點:this.節點名
           

繼續閱讀