vanilladocs
vanillapunch.com
API / base/node

base/node

계층 객체.

Hierarchy object.

Nodeextends Object

계층 객체.

  • 부모와 자식을 가질 수 있는 계층 구조 기능.
  • 활성화 여부 지정 기능.

Hierarchy object.

  • Hierarchical structure that can have a parent and children.
  • Active state setting.
예제Example계층 만들고 이름으로 찾기Build a hierarchy and find by name
import { Node } from "./build/vanilla.js";

const root = new Node();
root.setName("root");
const child = new Node();
child.setName("child");
root.addChild(child);
const grandChild = new Node();
grandChild.setName("grandChild");
child.addChild(grandChild);
child.setActive(false);
const found = root.findChildRecursiveByName("grandChild");
console.log(found === grandChild, grandChild.isActive(), grandChild.isActiveInHierarchy());
console.log(root.getChildCount(), grandChild.getParent() === child, root.isRoot(), grandChild.isLeaf());
생성자constructorconstructor()#0.0.8 부터since 0.0.8소스source

생성.

Constructor.

메서드methodsetParent(parent)#0.0.8 부터since 0.0.8소스source

부모 설정.

Sets the parent.

parentNode | null
새 부모. (null 이면 지금 부모에서 떼어 내기만 한다)New parent. (if null, only detaches from the current parent)

반환Returnsboolean 붙였거나 떼었으면 참. (자신이나 자신의 자손 밑으로는 계층이 고리가 되므로 붙이지 않고 거짓)True if attached or detached. (false without attaching under itself or its own descendant, since the hierarchy would become a cycle)

메서드methodaddChild(child)#0.0.8 부터since 0.0.8소스source

자식 추가.

Adds a child.

childNode
붙일 노드. (다른 부모에 붙어 있었으면 거기서 떼어 와 자식 목록 맨 뒤에 붙인다)Node to attach. (if it had another parent, it is detached from there and appended to the end of the child list)

반환Returnsboolean 붙였으면 참. (이 노드 자신이나 이 노드의 조상이면 붙이지 않고 거짓)True if attached. (false without attaching if it is this node itself or an ancestor of this node)

짝Counterpart: vanilla-engine NodeTree.add_child

메서드methodremoveChild(child)#0.0.8 부터since 0.0.8소스source

자식 제거.

Removes a child.

childNode
뗄 노드. (이 노드의 자식일 때만 떼어 낸다, 아니면 아무것도 하지 않는다)Node to detach. (detached only if it is a child of this node; otherwise nothing happens)

짝Counterpart: vanilla-engine NodeTree.remove_child

메서드methodremoveChildAt(childIndex)#0.0.11 부터since 0.0.11소스source

자식 제거.

Removes a child.

childIndexnumber
뗄 자식의 차례. (0 부터, 범위 밖이면 아무것도 하지 않는다)Index of the child to detach. (from 0, does nothing if out of range)

짝Counterpart: vanilla-engine NodeTree.remove_child_at

메서드methodremoveChildren()#0.0.8 부터since 0.0.8소스source

모든 자식 제거. (직계 자식 목록만 비우기 때문에 자식들이 소유한 계층 구조는 유지됨)

Removes all children. (Only clears the direct child list, so hierarchies owned by the children are kept)

짝Counterpart: vanilla-engine NodeTree.remove_children

메서드methodisRoot()#0.0.8 부터since 0.0.8소스source

부모가 없는지 여부 반환.

Returns whether it has no parent.

반환Returnsboolean 부모가 없으면 참.True if it has no parent.

짝Counterpart: vanilla-engine WorldNode.is_root

메서드methodisLeaf()#0.0.8 부터since 0.0.8소스source

자식이 없는지 여부 반환.

Returns whether it has no children.

반환Returnsboolean 자식이 없으면 참.True if it has no children.

짝Counterpart: vanilla-engine WorldNode.is_leaf

메서드methodhasParent()#0.0.11 부터since 0.0.11소스source

부모 여부 반환.

Returns whether it has a parent.

반환Returnsboolean 부모가 있으면 참.True if it has a parent.

짝Counterpart: vanilla-engine WorldNode.has_parent

메서드methodgetParent()#0.0.8 부터since 0.0.8소스source

부모 반환.

Returns the parent.

반환ReturnsNode 부모 노드. (없으면 null)Parent node. (null if none)

메서드methodgetSiblings()#0.0.11 부터since 0.0.11소스source

형제 목록 반환.

Returns the sibling list.

반환ReturnsNode[] | null 부모의 자식 목록. (자신도 들어 있는 원본 배열, 부모가 없으면 null)Child list of the parent. (the original array including this node, null if no parent)

메서드methodgetChildren()#0.0.8 부터since 0.0.8소스source

자식 목록 반환.

Returns the child list.

반환ReturnsNode[] 직계 자식 목록. (복사하지 않은 원본 배열)List of direct children. (the original array, not a copy)

메서드methodgetChildCount()#0.0.8 부터since 0.0.8소스source

자식 수 반환.

Returns the number of children.

반환Returnsnumber 직계 자식 수.Number of direct children.

짝Counterpart: vanilla-engine NodeTree.get_child_count

메서드methodgetChild(childIndex)#0.0.8 부터since 0.0.8소스source

자식 반환.

Returns a child.

childIndexnumber
자식의 차례. (0 부터)Index of the child. (from 0)

반환ReturnsNode | null 그 차례의 직계 자식. (범위 밖이면 null)Direct child at that index. (null if out of range)

짝Counterpart: vanilla-engine WorldNode.get_child

메서드methodgetChildIndex(child)#0.0.11 부터since 0.0.11소스source

자식의 위치 반환.

Returns the position of a child.

childNode
찾을 노드.Node to find.

반환Returnsnumber 직계 자식 목록에서의 차례. (자식이 아니면 -1)Index in the direct child list. (-1 if not a child)

짝Counterpart: vanilla-engine NodeTree.get_child_index

메서드methodhasChild(child)#0.0.9 부터since 0.0.9소스source

자식 포함 여부 반환.

Returns whether it contains the child.

childNode
찾을 노드.Node to find.

반환Returnsboolean 직계 자식이면 참.True if it is a direct child.

짝Counterpart: vanilla-engine WorldNode.has_child

메서드methodsetActive(active)#0.0.8 부터since 0.0.8소스source

활성화 상태 설정.

Sets the active state.

activeboolean
참이면 활성. (자식의 값은 바꾸지 않는다)Active if true. (does not change the children's values)
메서드methodisActive()#0.0.8 부터since 0.0.8소스source

활성화 상태 반환.

Returns the active state.

반환Returnsboolean 자신이 활성이면 참. (부모는 보지 않는다)True if this node itself is active. (does not look at the parents)

짝Counterpart: vanilla-engine WorldNode.is_active

메서드methodisActiveInHierarchy()#0.0.8 부터since 0.0.8소스source

현재부터 루트까지 계층 전체의 활성화 상태 반환. (루트까지 하나라도 비활성화상태면 false 반환)

Returns the active state of the whole hierarchy from here up to the root. (Returns false if any node up to the root is inactive)

반환Returnsboolean 자신과 루트까지의 모든 부모가 활성이면 참.True if this node and all parents up to the root are active.

짝Counterpart: vanilla-engine NodeTree.is_active_in_hierarchy

메서드methodsetName(name)#0.0.11 부터since 0.0.11소스source

이름 설정.

Sets the name.

namestring
노드 이름. (같은 이름을 막지 않는다)Node name. (duplicate names are not prevented)

짝Counterpart: vanilla-engine WorldNode.set_name

메서드methodgetName()#0.0.11 부터since 0.0.11소스source

이름 반환.

Returns the name.

반환Returnsstring 노드 이름. (기본 빈 글)Node name. (empty string by default)

짝Counterpart: vanilla-engine WorldNode.get_name

메서드methodfindChild(predicate)#0.0.11 부터since 0.0.11소스source

조건으로 직계 자식 찾기.

Finds a direct child by condition.

predicatefunction(Node): boolean
직계 자식을 받아 고를 것이면 참을 돌려주는 함수. (null 이면 null 을 돌려준다)Function that receives a direct child and returns true to pick it. (returns null if null)

반환ReturnsNode | null 자식 목록 차례로 처음 맞은 직계 자식. (없으면 null)First matching direct child in child list order. (null if none)

짝Counterpart: vanilla-engine NodeTree.find_child

메서드methodfindChildRecursive(predicate)#0.0.11 부터since 0.0.11소스source

조건으로 직계 자식 찾기. (깊이 우선)

Finds a direct child by condition. (depth-first)

predicatefunction(Node): boolean
자손을 받아 고를 것이면 참을 돌려주는 함수.Function that receives a descendant and returns true to pick it.

반환ReturnsNode | null 깊이 우선으로 처음 맞은 자손. (손자 아래까지 모두 본다, 없으면 null)First matching descendant in depth first order. (looks at grandchildren and below too, null if none)

짝Counterpart: vanilla-engine NodeTree.find_child_recursive

예제Example조건으로 하위 계층 전체에서 찾기Find by condition in the whole subtree
import { Node } from "./build/vanilla.js";

const root = new Node();
const child = new Node();
const grandChild = new Node();
root.addChild(child);
child.addChild(grandChild);
grandChild.setActive(false);
const inactiveNode = root.findChildRecursive((node) => {
	return !node.isActive();
});
console.log(inactiveNode === grandChild);
메서드methodfindChildByName(name)#0.0.11 부터since 0.0.11소스source

이름으로 직계 자식 찾기.

Finds a direct child by name.

namestring
찾을 이름.Name to find.

반환ReturnsNode | null 이름이 같은 첫 직계 자식. (없으면 null)First direct child with the same name. (null if none)

짝Counterpart: vanilla-engine NodeTree.find_child_by_name

메서드methodfindChildRecursiveByName(name)#0.0.11 부터since 0.0.11소스source

이름으로 하위 계층 전체에서 찾기. (깊이 우선)

Finds by name in the whole sub-hierarchy. (depth-first)

namestring
찾을 이름.Name to find.

반환ReturnsNode | null 깊이 우선으로 처음 맞은, 이름이 같은 자손. (없으면 null)First descendant with the same name in depth first order. (null if none)

짝Counterpart: vanilla-engine NodeTree.find_child_recursive_by_name

메서드methodfindDescendantByName(name)#0.4.0 부터since 0.4.0소스source

이름으로 하위 계층 전체에서 찾기. (깊이 우선, findChildRecursiveByName 과 같다, 러스트 판 find_descendant_by_name 과 같은 이름)

Finds by name in the whole sub-hierarchy. (depth-first, same as findChildRecursiveByName, same name as find_descendant_by_name in the Rust version)

namestring
찾을 이름.Name to find.

반환ReturnsNode | null 깊이 우선으로 처음 맞은, 이름이 같은 자손. (없으면 null)First descendant with the same name in depth first order. (null if none)

짝Counterpart: vanilla-engine NodeTree.find_descendant_by_name

메서드methoddestroyChildrenWhere(predicate)#0.4.0 부터since 0.4.0소스source

조건에 맞는 직계 자식 떼어 파괴. (떼어 낸 수 반환, 러스트 판 destroy_children_where 와 같은 이름)

Detaches and destroys direct children matching a condition. (Returns the number detached, same name as destroy_children_where in the Rust version)

predicatefunction(Node): boolean
직계 자식을 받아 떼어 파괴할 것이면 참을 돌려주는 함수.Function that receives a direct child and returns true to detach and destroy it.

반환Returnsnumber 떼어 파괴한 자식 수.Number of children detached and destroyed.

짝Counterpart: vanilla-engine NodeTree.destroy_children_where

예제Example조건에 맞는 자식 떼어 파괴하기Detach and destroy children that match a condition
import { Node } from "./build/vanilla.js";

const root = new Node();
for (let index = 0; index < 4; ++index) {
	const child = new Node();
	child.setName("enemy" + index);
	child.setActive(index % 2 === 0);
	root.addChild(child);
}
const destroyedCount = root.destroyChildrenWhere((child) => {
	return !child.isActive();
});
console.log(destroyedCount, root.getChildCount());
메서드methodremoveChildByName(name)#0.0.11 부터since 0.0.11소스source

이름으로 직계 자식 제거.

Removes a direct child by name.

namestring
뗄 직계 자식의 이름. (같은 이름이 여럿이면 첫 자식만 뗀다)Name of the direct child to detach. (only the first one if several share the name)

짝Counterpart: vanilla-engine NodeTree.remove_child_by_name

메서드methodsetSiblingIndex(childIndex)#0.0.11 부터since 0.0.11소스source

형제간의 위치 설정.

Sets the position among siblings.

childIndexnumber
형제 사이의 새 차례. (0 ~ 형제 수 - 1 로 자른다, 부모가 없으면 아무것도 하지 않는다)New index among siblings. (clamped to 0 ~ sibling count - 1, does nothing if no parent)

짝Counterpart: vanilla-engine NodeTree.set_sibling_index

예제Example형제 사이에서 맨 앞, 맨 뒤로 옮기기Move to the front and back among siblings
import { Node } from "./build/vanilla.js";

const root = new Node();
const first = new Node();
const second = new Node();
const third = new Node();
root.addChild(first);
root.addChild(second);
root.addChild(third);
third.setSiblingIndex(0);
first.setSiblingIndex(100);
console.log(third.getSiblingIndex(), second.getSiblingIndex(), first.getSiblingIndex());
메서드methodgetSiblingIndex()#0.0.11 부터since 0.0.11소스source

형제간의 위치 반환.

Returns the position among siblings.

반환Returnsnumber 형제 사이의 차례. (부모가 없으면 -1)Index among siblings. (-1 if no parent)

짝Counterpart: vanilla-engine NodeTree.get_sibling_index