每一个节点都具有node组件,在脚本中可以用this.node调出属性和方法
关于获得子节点:
this.node.childrenCount;//子节点个数
this.node.children[0];//获得子节点方式一,这里是获得第一个子节点
this.node.getChildrenByName(“子节点名称”);//获得子节点方式二,通过名称查找
cc.find(“父节点/子节点/子节点的子节点”)//获得子节点方式三,通过路径查找,cc为cocos的名称空间,适用于父节点与该节点不同的子节点的查找方式,多多一层就多加一道斜杠
this.node.childrenCount;//子节点个数
this.node.children[0];//获得子节点方式一,这里是获得第一个子节点
this.node.getChildrenByName(“子节点名称”);//获得子节点方式二,通过名称查找
cc.find(“父节点/子节点/子节点的子节点”)//获得子节点方式三,通过路径查找,cc为cocos的名称空间,适用于父节点与该节点不同的子节点的查找方式,多多一层就多加一道斜杠
对应地,关于父节点:
this.node.getParent();//获得父节点
this.node.setParent(父节点名称);//为当前节点设置一个新的父节点
this.node.removeAllChildren(子节点名称);//移除所有子节点
this.node.removeChild(子节点名称);//移除某一个子节点
this.node.removeFromParent();//把节点从该节点的父节点中移除掉
this.node.getParent();//获得父节点
this.node.setParent(父节点名称);//为当前节点设置一个新的父节点
this.node.removeAllChildren(子节点名称);//移除所有子节点
this.node.removeChild(子节点名称);//移除某一个子节点
this.node.removeFromParent();//从该节点的父节点中移除掉
注意:新版本中疑似不再有getParent和setParent方法,只有Parent属性(参见官方文档),但由于本人学习的还是旧版本,花时间看一下比较好。
this.node和node组件中可视的内容:
位置:
this.node.x;//访问x方向位置
this.node.y;//访问y方向位置
this.node.setPosition(x,y);//直接设置位置
This.node.setPosition(cc.v2(x,y));//创建cc空间提供的结构体cc.v2,这个结构体可以当作一个对象,这个对象只有x,y两个数
this.node.x;//访问x方向位置
this.node.y;//访问y方向位置
this.node.setPosition(x,y);//直接设置位置
This.node.setPosition(cc.v2(x,y));//创建cc空间提供的结构体cc.v2,这个结构体可以当作一个对象,这个对象只有x,y两个数
旋转:
this.node.rotation;//访问旋转量
this.node.rotation;//访问旋转量
缩放:
this.node.scale;//访问整体的缩放
this.node.scaleX;//访问x方向的缩放
this.node.scaleY;//访问y方向的缩放
this.node.scale;//访问整体的缩放
this.node.scaleX;//访问x方向的缩放
this.node.scaleY;//访问y方向的缩放
锚点:
this.node.anchorX;//访问x方向锚点
this.node.anchorY;//访问y方向锚点
this.node.anchorX;//访问x方向锚点
this.node.anchorY;//访问y方向锚点
颜色:
this.node.color = cc.Color.颜色;//设置颜色
this.node.color = cc.Color.颜色;//设置颜色
节点的开关:
this.node.active = true;//打开节点
this.node.active = false;//关闭节点
this.enabled = true;//打开组件自身的开关
this.enabled = true;//关闭组件自身的开关
this.node.active = true;//打开节点
this.node.active = false;//关闭节点
this.enabled = true;//打开组件自身的开关
this.enabled = true;//关闭组件自身的开关
获取组件//对node和脚本自身的操作已包含在上文中:
let 组件 = this.getCompent(cc.组件类型);
let 组件 = this.getCompent(cc.组件类型);
例:获取精灵组件
let sprite = this.getComponent(cc.Sprite);//获取单个精灵组件
sprite.enabled = true;//对获取的精灵组件进行开关等操作
let sprite = this.getComponents(cc.Sprite);//多个精灵组件时,用这种方法获得数组返回值
this.getComponentInChildren(cc.sprite);//从子物体中获取精灵组件
childrenSprite.enabled = false;//关闭从子物体中获取的精灵组件
let sprite = this.getComponent(cc.Sprite);//获取单个精灵组件
sprite.enabled = true;//对获取的精灵组件进行开关等操作
let sprite = this.getComponents(cc.Sprite);//多个精灵组件时,用这种方法获得数组返回值
this.getComponentInChildren(cc.sprite);//从子物体中获取精灵组件
childrenSprite.enabled = false;//关闭从子物体中获取的精灵组件
获取不到时,一般返回值为”null”
感谢你的阅读,再看一张碧蓝档案壁纸犒劳一下自己吧