查找元素
import { _decorator, Component, Node, find } from 'cc';
import { TestCom } from './TestCom';
const { ccclass, property } = _decorator;
@ccclass('GameMgr')
export class GameMgr extends Component {
start(): void {
// 组件实例.start();
// this--->组件实例;
console.log(this.node);
console.log(this.node.parent);
// this.node.children.length所有孩子数目
for(var i = 0; i < this.node.children.length; i ++) {
console.log(this.node.children[i]);
}
// 查节点
var cubeNode = find("Cube/Sphere");
console.log("find", cubeNode);
// end
// 在孩子里面找
console.log(this.node.getChildByName("Sphere"));
console.log(this.node.getChildByName("Sphere")?.getChildByName("Torus"));
console.log(this.node.getChildByPath("Sphere/Torus"));
// end
// 获取节点名字
console.log(this.node.name);
// step1: 先找节点
var Torus = this.node.getChildByPath("Sphere/Torus");
// step2: 根据类型/根据类型的名字来找;
var testCom: any = Torus?.getComponent(TestCom);
console.log(testCom);
// ccclass这个注解,会帮我们做一个名字---》类型的一个映射;
testCom = Torus?.getComponent("TestCom_bycw");
console.log(testCom);
// end
// Torus节点添加一个组件实例
Torus?.addComponent(TestCom); // new TestCom --->【组件实例数组里面】 --->组件实例.node --->Torus节点
this.addComponent(TestCom); // 在组件实例所在节点上加一个新的组件实例
// end
var comSet = Torus?.getComponents("TestCom_bycw"); // 数组【类型组件】
// var comSet = Torus?.getComponents(TestCom);
console.log(comSet);
// 到孩子找
testCom = this.node.getComponentInChildren(TestCom);
console.log(testCom);
// end
testCom = this.getComponentInChildren(TestCom); // ===>this.node.getComponentInChildren(TestCom);
}
update(dt: number): void {
// 组件实例.update();
}
}最后更新于