当前位置: 首页 > news >正文

cocos creator 3.8 Node学习 3

//在Ts、js中 this指向当前的这个组件实例
//this下的一个数据成员node,指向组件实例化的这个节点
//同样也可以根据节点找到挂载的所有组件
//this.node 指向当前脚本挂载的节点//子节点与父节点的关系 
// Node.parent是一个Node,Node.children是一个Node[]
// this.node.parent //等同于
console.warn(this.node.parent.name);
// this.node.getParent()
console.warn(this.node.getParent().name);// this.node.children
this.node.children.forEach(element => {console.log("子节点")console.log(element.name);
});// 替换父节点 Node.parent = /Node.setParent(XX)
this.node.parent = this.AddBtn;
console.warn(this.node.parent.name)
// this.node.setParent(value: Node)
this.node.setParent(this.AddBtn);
console.warn(this.node.getParent().name);//添加节点 使用预制的时候@property(Node)AddBtn: Node = null!;@property(Prefab)sprite: Prefab = null!;
// addChild(child: Node)
let tempNode: Node = instantiate(this.sprite);
// addChild有可能造成内存泄露,尤其是加自己玩
this.node.addChild(tempNode);
this.node.children.forEach(element => {console.log("子节点");console.warn(element.name);
});
// siblingIndex:设置位置,等同于2.x的setIndex(index: number)
// insertChild(child: Node, siblingIndex: number)
this.node.insertChild(tempNode, 0);
this.node.children.forEach(element => {console.log("=子节点=");console.warn(element.name);
});
// getSiblingIndex()
// setSiblingIndex(index: number)//节点查找
find() //从1级节点开始查找
getChildByName(name: string)//下级节点
getChildByPath(path: string)//从下级节点开始算起,往下找
// let node = find("Canvas/Test");
// console.log(node)
// node = this.node.getChildByName("Label")
// node.getComponent(Label).string = "哈哈";
// // node = this.node.getChildByName("Label")?.getChildByName("Label"); //等同于下面这一个行代码
// node = this.node.getChildByPath("Label/Label");
// node.getComponent(Label).string = "~哈哈~";//节点移除
removeFromParent()
removeChild(child: this | Node)
removeAllChildren()
//节点判断
isChildOf(parent: this | Scene | null)
isValid
// let labelNode = this.node.getChildByPath("Label/Label");
// console.log(labelNode.isChildOf(this.node))
// 节点是否销毁
// console.log(this.node.isValid)
//组件查找
//先找节点 再找节点上挂载的组件
// 我测试的结果:挂的BoxCollider 查找Collider一样起效果
//根据类型来查找,需要引入
getComponent<T extends Component>
//根据名字来查找,不需要引入
getComponent(className: string)
getComponents<T extends Component>
getComponents(className: string)
getComponentInChildren<T extends Component>
getComponentInChildren(className: string)
getComponentsInChildren<T extends Component>
getComponentsInChildren(className: string)
//添加移除组件
addComponent<T extends Component>
addComponent(className: string)
removeComponent<T extends Component>
removeComponent(classNameOrInstance: string | Component)
//销毁节点
destroy()
destroyAllChildren()
//事件监听 常用前两
on(type: string | NodeEventType, callback: Function, target?: unknown, useCapture?: boolean)
//this.node.on(Input.EventType.TOUCH_MOVE, this.onTouch, this);
off(type: string | NodeEventType, callback: Function, target?: unknown, useCapture?: boolean)
//this.node.off(Input.EventType.TOUCH_MOVE, this.onTouch, this);
once(type: string | NodeEventType, callback: Function, target?: unknown, useCapture?: boolean)
//通常用非官方分装的EventListener.ts不用这些
emit(type: string, arg0?: any, arg1?: any, arg2?: any, arg3?: any, arg4?: any)
dispatchEvent(event: Event)
hasEventListener(type: string, callback?: Function, target?: unknown)
targetOff(target: unknown)

有时候脑袋一热就问人家,怎么样才能简简单单的组件化开发一款游戏,怎么样才能合理的把你想要的功能代码化,不知道问烦了几个家伙了,经常回复我的就是那个节点有要求,你就单独挂一个脚本,你管它那么多干啥?

有时间就学一下3.x,就是没3D的素材,至于为啥?不透露了

http://www.lryc.cn/news/489032.html

相关文章:

  • 微信小程序底部button,小米手机偶现布局错误的bug
  • 【计组】复习题
  • Apache Maven 标准文件目录布局
  • Android 功耗分析(底层篇)
  • 【Xbim+C#】创建圆盘扫掠IfcSweptDiskSolid
  • IntelliJ+SpringBoot项目实战(四)--快速上手数据库开发
  • 利用oss进行数据库和网站图片备份
  • Excel - VLOOKUP函数将指定列替换为字典值
  • 实验室管理平台:Spring Boot技术构建
  • 操作系统进程和线程——针对实习面试
  • 使用 cnpm 安装 Electron,才是正确快速的方法
  • 【人工智能】PyTorch、TensorFlow 和 Keras 全面解析与对比:深度学习框架的终极指南
  • 【第八课】Rust中的函数与方法
  • c语言学习25二维数组
  • 如何理解Lua 使用虚拟堆栈
  • 【倍数问题——同余系】
  • 「San」监听DOM变化的方法
  • 如何选择服务器
  • 嵌入式驱动面试总结
  • Uniapp 简单配置鸿蒙
  • 线程池的实现与应用
  • 基于Java Springboot单位考勤系统
  • 近源渗透|HID ATTACK从0到1
  • 【linux】插入新硬盘如何配置:格式化、分区、自动挂载(Ubuntu)
  • YOLOv8-ultralytics-8.2.103部分代码阅读笔记-block.py
  • 代码随想录算法训练营第五十三天|Day53 图论
  • LeetCode:203.移除链表元素
  • 知识见闻 - 数学: 均方根 Root Mean Square
  • 机器硬件调优
  • 如何更改手机GPS定位