Cocos Creator 节点

 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

猜你喜欢

转载自www.cnblogs.com/allyh/p/9977559.html