노드와 컴포넌트
씬에 보이는 것은 노드 트리로 짓습니다. 노드는 자리와 계층을 갖고, 그리기나 움직임 같은 기능은 노드에 붙이는 컴포넌트가 맡습니다. 이 장에서는 노드 클래스들의 관계, 자식 붙이기, 컴포넌트 붙이기와 찾기, 변환, 직접 만드는 컴포넌트, 트리 돌기를 봅니다.
클래스 관계
Node 부모와 자식, 활성, 이름
└── ComponentNode 컴포넌트 목록, tick
└── TransformNode 로컬 자리, 회전, 크기, 투명도, 그리기
├── WorldNode 내용 크기, 기준점(pivot), 앵커, 터치 판정 (손으로 놓는 노드)
└── UINode 제약 변수로 자리를 푸는 노드 (제약 기반 레이아웃)
Component 노드에 붙는 기능 (Paint, Sprite, Text, Mask, UIButton ...)- 보통의 2D 물체와 화면 요소는
WorldNode로 만듭니다. 씬의 루트 노드(scene.getRoot())도WorldNode입니다. UINode는WorldNode와 이어지지 않는 다른 갈래입니다. 제약을 걸어 자리를 정하는 방식은 "UI" 장에서 봅니다.Component는 노드가 아닙니다.ComponentNode를 이어받은 노드에 붙어 그 노드의 자리에서 그리고 갱신합니다.
노드 만들고 붙이기
import { Engine, EngineConfiguration, Scene, WorldNode, Paint, Vector2, Color } from "./build/vanilla.js";
class NodeScene extends Scene {
initialize(engine) {
super.initialize(engine);
const root = this.getRoot();
const panel = new WorldNode();
panel.setName("Panel");
panel.setContentSize(Vector2.create(400, 240));
panel.setLocalPosition(Vector2.create(640, 360));
const panelPaint = panel.addComponent(Paint);
panelPaint.setColor(new Color(0.2, 0.25, 0.35, 1));
panelPaint.setRoundSize(16);
root.addChild(panel);
const badge = new WorldNode();
badge.setName("Badge");
badge.setContentSize(Vector2.create(64, 64));
badge.setLocalPosition(Vector2.create(400, 0));
const badgePaint = badge.addComponent(Paint);
badgePaint.setColor("#ffcc33");
badgePaint.setRoundSize(32);
panel.addChild(badge);
}
preDraw(graphic) {
super.preDraw(graphic);
const engine = this.getEngine();
const viewManager = engine.getViewManager();
viewManager.applyViewRect(graphic);
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new NodeScene());addChild(child)는 자식 목록 맨 뒤에 붙입니다. 다른 부모에 붙어 있던 노드면 거기서 떼어 옵니다.removeChild(child)는 떼어 내기만 하고 노드를 없애지 않습니다. 떼어 낸 노드는 다시 붙일 수 있습니다.- 노드는 생성자에서 기본값(자리 0, 0, 크기 1, 회전 0, 투명도 1, 활성)을 가집니다.
WorldNode는 내용 크기 (0, 0), 기준점과 앵커가 가운데 (0.5, 0.5) 입니다.
WorldNode 의 자리
WorldNode 의 로컬 자리는 "부모의 내용 영역 왼쪽 위에서 잰, 이 노드 기준점의 자리" 입니다. 기준점은 내용 영역 안의 비율(0 ~ 1)이라 처음 값 (0.5, 0.5) 이면 노드의 가운데가 로컬 자리에 옵니다. 위 예제에서 Badge 는 Panel 의 오른쪽 위 모서리(400, 0)에 가운데가 놓입니다.
| 메서드 | 내용 |
|---|---|
setContentSize(size), setContentSizeXY(width, height) |
내용 크기. Paint 와 Sprite 는 이 크기만큼 칠합니다 |
setPivot(pivot) |
기준점 (0 ~ 1 로 자릅니다). Pivot.topLeft, Pivot.middleCenter, Pivot.bottomRight 같은 아홉 값이 있습니다 |
setAnchor(anchor) |
앵커 (0 ~ 1). 그리기에는 쓰이지 않고 setAnchoredPosition 이 자리를 셀 때만 씁니다 |
setAnchoredPosition(position) |
부모 내용 영역 안의 앵커 점(부모 크기 x 앵커)에서 잰 자리로 로컬 자리를 정합니다 |
setRect(x, y, width, height) |
왼쪽 위가 (x, y) 에 오도록 내용 크기와 로컬 자리를 함께 정합니다 (기준점이 어디든) |
import { WorldNode, Pivot, Vector2 } from "./build/vanilla.js";
const parent = new WorldNode();
parent.setPivot(Pivot.topLeft);
parent.setContentSize(Vector2.create(800, 600));
const corner = new WorldNode();
corner.setAnchor(Pivot.bottomRight);
corner.setContentSize(Vector2.create(100, 50));
parent.addChild(corner);
corner.setAnchoredPosition(Vector2.create(-60, -35));
const localPosition = corner.getLocalPosition();
console.log(localPosition.x, localPosition.y);
const label = new WorldNode();
label.setPivot(Pivot.topLeft);
parent.addChild(label);
label.setRect(20, 20, 300, 40);
const labelPosition = label.getLocalPosition();
console.log(labelPosition.x, labelPosition.y);첫 줄은 740 565 입니다. (800 x 1 - 60, 600 x 1 - 35) 둘째 줄은 기준점이 왼쪽 위라 20 20 입니다.
변환
| 로컬 (부모 기준) | 전체 (루트 기준) | 내용 |
|---|---|---|
setLocalPosition, setLocalPositionXY, getLocalPosition |
setPosition, getPosition |
자리 |
setLocalRotation, getLocalRotation |
setRotation, getRotation |
회전 (도, 시계 방향) |
setLocalScale, setLocalScaleXY, getLocalScale |
setScale, getScale |
크기 배율 |
setLocalOpacity, getLocalOpacity |
getOpacity |
투명도 (0 ~ 1 로 자릅니다, 자식에게 곱해 내려갑니다) |
그릴 때 노드는 로컬 자리로 옮기고, 회전하고, 크기를 곱한 뒤, 기준점만큼(내용 크기 x 기준점) 되돌아가 내용 영역의 왼쪽 위를 원점으로 컴포넌트를 그립니다. 자식은 그 원점에서 다시 같은 차례를 밟습니다. 그래서 부모를 돌리고 키우면 자식도 함께 돌고 커집니다.
WorldNode 는 화면 쪽 판정도 줍니다.
getWorldCorners(): 부모들의 자리, 회전, 크기를 모두 거친 네 귀퉁이 (왼쪽 위, 오른쪽 위, 오른쪽 아래, 왼쪽 아래)getWorldBounds(): 그 네 점을 감싸는 축 정렬 사각형contains(viewPosition): 뷰 좌표의 점이 노드 안인지 (돌아간 노드도 맞게)convertViewToLocal(viewPosition): 뷰 좌표를 이 노드의 내용 영역 좌표로 (왼쪽 위가 0, 0)
활성과 그리는 차례
setActive(false)인 노드와 그 자손은 갱신(tick)도, 그리기도, 터치 판정도 받지 않습니다.isActiveInHierarchy()는 루트까지 모두 활성인지 봅니다.- 투명도가 0 인 노드는 자기와 자손을 그리지 않지만 갱신은 받습니다.
- 자식은 자식 목록 차례로 그립니다. 뒤에 있을수록 위에 보입니다.
setSiblingIndex(index)로 형제 사이의 차례를 바꾸고getSiblingIndex()로 읽습니다.
컴포넌트 붙이기와 찾기
| 메서드 | 내용 |
|---|---|
addComponent(Type) |
클래스를 받아 새로 만들어 붙이고 그 인스턴스를 돌려줍니다 |
getOrAddComponent(Type) |
있으면 그것, 없으면 새로 붙인 것 |
getComponent(Type) |
그 클래스(또는 그 하위 클래스)인 첫 컴포넌트, 없으면 null |
getComponents(Type) |
그 클래스인 컴포넌트 모두 (배열) |
hasComponent(Type) |
있는지 |
getAllComponents() |
붙은 차례대로 전부 |
removeComponent(component) |
인스턴스를 떼어 냅니다 |
컴포넌트는 require() 로 함께 있어야 할 컴포넌트를 알립니다. addComponent 는 붙이기 직전에 그 목록을 같은 노드에 getOrAddComponent 로 먼저 붙입니다. 예를 들어 UILabel 은 Text 와 RichText 를, UIScrollView 는 Mask 를 요구합니다.
붙일 때 차례는 생성자, 노드 지정(setNode), 요구 컴포넌트 붙이기, attach(node) 입니다. 떼어 낼 때는 노드를 비우고 목록에서 뺀 뒤 detach(node) 를 부릅니다.
컴포넌트 만들기
Component 를 이어받아 필요한 것만 덮어씁니다.
| 덮어쓰는 메서드 | 불리는 때 |
|---|---|
attach(node), detach(node) |
노드에 붙을 때, 떨어질 때 |
tick(timeDelta) |
노드가 활성인 동안 매 프레임. tick 을 덮어쓴 컴포넌트만 불립니다 (isTickNeeded()) |
draw(graphic) |
노드가 보이고 컴포넌트가 켜져(isEnable()) 있을 때. 원점은 노드 내용 영역의 왼쪽 위 |
require() |
붙기 직전. 함께 붙일 클래스 목록 |
isDrawnInsideContent() |
내용 영역 안에만 그리면 true 를 돌려줍니다. 그 노드의 켜진 컴포넌트가 모두 이렇게 알리면 화면 밖 거르기가 노드가 화면 밖일 때 그리기를 건너뜁니다 |
import { Engine, EngineConfiguration, Scene, WorldNode, Component, Paint, Vector2, Color } from "./build/vanilla.js";
class Spin extends Component {
#degreesPerSecond;
constructor() {
super();
this.#degreesPerSecond = 90;
}
setDegreesPerSecond(degreesPerSecond) {
this.#degreesPerSecond = degreesPerSecond;
}
tick(timeDelta) {
const node = this.getNode();
const rotation = node.getLocalRotation();
node.setLocalRotation(rotation + this.#degreesPerSecond * timeDelta);
}
}
class CrossMark extends Component {
draw(graphic) {
const node = this.getNode();
const contentSize = node.getContentSize();
graphic.setStrokeColor("#ffffff");
graphic.drawLine([Vector2.create(0, 0), Vector2.create(contentSize.x, contentSize.y)], 4);
graphic.drawLine([Vector2.create(contentSize.x, 0), Vector2.create(0, contentSize.y)], 4);
}
isDrawnInsideContent() {
return true;
}
}
class ComponentScene extends Scene {
initialize(engine) {
super.initialize(engine);
const root = this.getRoot();
const node = new WorldNode();
node.setContentSize(Vector2.create(160, 160));
node.setLocalPosition(Vector2.create(640, 360));
const paint = node.addComponent(Paint);
paint.setColor(new Color(0.8, 0.3, 0.3, 1));
node.addComponent(CrossMark);
const spin = node.addComponent(Spin);
spin.setDegreesPerSecond(45);
root.addChild(node);
const foundSpin = node.getComponent(Spin);
const hasCrossMark = node.hasComponent(CrossMark);
console.log(foundSpin === spin, hasCrossMark);
}
preDraw(graphic) {
super.preDraw(graphic);
const engine = this.getEngine();
const viewManager = engine.getViewManager();
viewManager.applyViewRect(graphic);
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new ComponentScene());컴포넌트는 붙은 차례로 그립니다. 위 예제는 Paint 로 바탕을 칠한 뒤 CrossMark 가 그 위에 선을 긋습니다.
노드 트리 돌기
| 메서드 | 내용 |
|---|---|
getParent(), isRoot(), isLeaf() |
부모, 부모가 없는지, 자식이 없는지 |
getChildren(), getChildCount(), getChild(index) |
직계 자식 |
findChildByName(name), findChild(predicate) |
직계 자식 가운데 첫 것 |
findDescendantByName(name), findChildRecursive(predicate) |
자손 전체에서 깊이 우선으로 첫 것 (findChildRecursiveByName 은 findDescendantByName 과 같습니다) |
removeChildByName(name), removeChildAt(index), removeChildren() |
직계 자식 떼어 내기 |
destroyChildrenWhere(predicate) |
조건에 맞는 직계 자식을 떼어 destroy() 를 부르고 그 수를 돌려줍니다 |
import { WorldNode, Paint } from "./build/vanilla.js";
function createNode(name) {
const node = new WorldNode();
node.setName(name);
return node;
}
function visit(node, depth, lines) {
const name = node.getName();
lines.push(`${" ".repeat(depth)}${name}`);
const children = node.getChildren();
for (const child of children) {
visit(child, depth + 1, lines);
}
}
const root = createNode("root");
const player = createNode("Player");
const weapon = createNode("Weapon");
const enemies = createNode("Enemies");
root.addChild(player);
player.addChild(weapon);
root.addChild(enemies);
for (let index = 0; index < 3; ++index) {
const enemy = createNode(`Enemy${index}`);
enemy.addComponent(Paint);
enemies.addChild(enemy);
}
const lines = [];
visit(root, 0, lines);
console.log(lines.join("\n"));
const foundWeapon = root.findDescendantByName("Weapon");
const paintedNode = root.findChildRecursive((node) => {
return node.hasComponent(Paint);
});
const removedCount = enemies.destroyChildrenWhere((node) => {
return node.getName() !== "Enemy0";
});
const paintedName = paintedNode.getName();
const enemyCount = enemies.getChildCount();
console.log(foundWeapon === weapon, paintedName, removedCount, enemyCount);주의할 점
getLocalPosition(),getLocalScale(),getPivot(),getContentSize()는 사본을 돌려줍니다. 받은 벡터를 고쳐도 노드는 그대로이므로 고친 값을 다시set합니다.getComponent(Type)은 하위 클래스도 찾습니다.Sprite는Paint를 이어받으므로getComponent(Paint)가Sprite를 돌려줄 수 있습니다.setEnable(false)는 그 컴포넌트의 그리기를 멈추지만,tick은 노드가 활성이면 계속 불립니다. 갱신까지 멈추려면 컴포넌트 안에서isEnable()을 보거나 노드를 끕니다.- 노드가 터치를 받으려면
WorldNode.setInteractable(true)와 함께 씬이 터치를 노드로 넘겨야 합니다. (TouchRecognizer, "입력" 장) - 루트 노드는 씬의
create()에서 생기고load에서 뷰 크기를 받습니다. 노드를 붙이는 일은create를 부른 뒤(super.create()다음)나load,initialize에서 합니다.
관련 클래스
Node, ComponentNode, TransformNode, WorldNode, UINode, Component, Pivot, Paint, Scene
Nodes and Components
What you see in a scene is built as a node tree. Nodes have a position and a place in the hierarchy, while features such as drawing and movement are handled by components attached to nodes. This chapter covers how the node classes relate, attaching children, adding and finding components, transforms, writing your own components, and walking the tree.
How the classes relate
Node parent and children, active state, name
└── ComponentNode component list, tick
└── TransformNode local position, rotation, scale, opacity, drawing
├── WorldNode content size, pivot, anchor, touch hit test (placed by hand)
└── UINode position solved from constraint variables (constraint based layout)
Component feature attached to a node (Paint, Sprite, Text, Mask, UIButton ...)- Ordinary 2D objects and screen elements are made with
WorldNode. The scene's root node (scene.getRoot()) is aWorldNodetoo. UINodeis a separate branch that does not connect toWorldNode. Placing nodes with constraints is covered in the "UI" chapter.Componentis not a node. It attaches to a node derived fromComponentNodeand draws and updates at that node's position.
Creating and attaching nodes
import { Engine, EngineConfiguration, Scene, WorldNode, Paint, Vector2, Color } from "./build/vanilla.js";
class NodeScene extends Scene {
initialize(engine) {
super.initialize(engine);
const root = this.getRoot();
const panel = new WorldNode();
panel.setName("Panel");
panel.setContentSize(Vector2.create(400, 240));
panel.setLocalPosition(Vector2.create(640, 360));
const panelPaint = panel.addComponent(Paint);
panelPaint.setColor(new Color(0.2, 0.25, 0.35, 1));
panelPaint.setRoundSize(16);
root.addChild(panel);
const badge = new WorldNode();
badge.setName("Badge");
badge.setContentSize(Vector2.create(64, 64));
badge.setLocalPosition(Vector2.create(400, 0));
const badgePaint = badge.addComponent(Paint);
badgePaint.setColor("#ffcc33");
badgePaint.setRoundSize(32);
panel.addChild(badge);
}
preDraw(graphic) {
super.preDraw(graphic);
const engine = this.getEngine();
const viewManager = engine.getViewManager();
viewManager.applyViewRect(graphic);
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new NodeScene());addChild(child)appends to the end of the child list. If the node was attached to another parent, it is detached from there first.removeChild(child)only detaches; it does not destroy the node. A detached node can be attached again.- A node starts with default values from its constructor (position 0, 0, scale 1, rotation 0, opacity 1, active). A
WorldNodehas content size (0, 0) and its pivot and anchor at the center (0.5, 0.5).
Positioning a WorldNode
The local position of a WorldNode is "where this node's pivot is, measured from the top left of the parent's content area". The pivot is a ratio inside the content area (0 to 1), so with the default (0.5, 0.5) the center of the node sits on its local position. In the example above, the center of Badge sits on the top right corner of Panel (400, 0).
| Method | Description |
|---|---|
setContentSize(size), setContentSizeXY(width, height) |
Content size. Paint and Sprite fill this size |
setPivot(pivot) |
Pivot (clamped to 0 to 1). There are nine values such as Pivot.topLeft, Pivot.middleCenter and Pivot.bottomRight |
setAnchor(anchor) |
Anchor (0 to 1). Not used for drawing; only setAnchoredPosition uses it to compute a position |
setAnchoredPosition(position) |
Sets the local position measured from the anchor point (parent size x anchor) inside the parent's content area |
setRect(x, y, width, height) |
Sets the content size and local position together so the top left lands on (x, y), wherever the pivot is |
import { WorldNode, Pivot, Vector2 } from "./build/vanilla.js";
const parent = new WorldNode();
parent.setPivot(Pivot.topLeft);
parent.setContentSize(Vector2.create(800, 600));
const corner = new WorldNode();
corner.setAnchor(Pivot.bottomRight);
corner.setContentSize(Vector2.create(100, 50));
parent.addChild(corner);
corner.setAnchoredPosition(Vector2.create(-60, -35));
const localPosition = corner.getLocalPosition();
console.log(localPosition.x, localPosition.y);
const label = new WorldNode();
label.setPivot(Pivot.topLeft);
parent.addChild(label);
label.setRect(20, 20, 300, 40);
const labelPosition = label.getLocalPosition();
console.log(labelPosition.x, labelPosition.y);The first line is 740 565. (800 x 1 - 60, 600 x 1 - 35) The second line is 20 20 because the pivot is the top left.
Transforms
| Local (relative to parent) | Global (relative to root) | Description |
|---|---|---|
setLocalPosition, setLocalPositionXY, getLocalPosition |
setPosition, getPosition |
Position |
setLocalRotation, getLocalRotation |
setRotation, getRotation |
Rotation (degrees, clockwise) |
setLocalScale, setLocalScaleXY, getLocalScale |
setScale, getScale |
Scale |
setLocalOpacity, getLocalOpacity |
getOpacity |
Opacity (clamped to 0 to 1, multiplied down to children) |
When drawing, a node moves to its local position, rotates, applies its scale, then moves back by the pivot (content size x pivot) and draws its components with the top left of the content area as the origin. Children repeat the same steps from that origin. So rotating and scaling a parent rotates and scales its children with it.
WorldNode also provides screen side tests.
getWorldCorners(): the four corners after all parents' position, rotation and scale (top left, top right, bottom right, bottom left)getWorldBounds(): the axis aligned rectangle around those four pointscontains(viewPosition): whether a point in view coordinates is inside the node (correct for rotated nodes too)convertViewToLocal(viewPosition): converts view coordinates to this node's content area coordinates (top left is 0, 0)
Active state and draw order
- A node with
setActive(false)and its descendants receive no updates (tick), no drawing and no touch hit tests.isActiveInHierarchy()checks that every node up to the root is active. - A node with opacity 0 draws neither itself nor its descendants, but still receives updates.
- Children are drawn in child list order. Later children appear on top. Change the order among siblings with
setSiblingIndex(index)and read it withgetSiblingIndex().
Adding and finding components
| Method | Description |
|---|---|
addComponent(Type) |
Takes a class, creates and attaches a new instance, and returns it |
getOrAddComponent(Type) |
The existing one if there is one, otherwise a newly attached one |
getComponent(Type) |
The first component of that class (or a subclass), or null |
getComponents(Type) |
Every component of that class (array) |
hasComponent(Type) |
Whether there is one |
getAllComponents() |
All of them, in attach order |
removeComponent(component) |
Detaches the instance |
A component declares the components it needs with require(). Right before attaching, addComponent first attaches that list to the same node with getOrAddComponent. For example, UILabel requires Text and RichText, and UIScrollView requires Mask.
Attaching goes constructor, node assignment (setNode), attaching required components, then attach(node). Detaching clears the node, removes it from the list, and then calls detach(node).
Writing a component
Extend Component and override only what you need.
| Method to override | When it is called |
|---|---|
attach(node), detach(node) |
When attached to a node, when detached |
tick(timeDelta) |
Every frame while the node is active. Only components that override tick are called (isTickNeeded()) |
draw(graphic) |
When the node is visible and the component is enabled (isEnable()). The origin is the top left of the node's content area |
require() |
Right before attaching. The list of classes to attach with it |
isDrawnInsideContent() |
Return true if you draw only inside the content area. When every enabled component of the node says so, off-screen culling skips drawing the node while it is off screen |
import { Engine, EngineConfiguration, Scene, WorldNode, Component, Paint, Vector2, Color } from "./build/vanilla.js";
class Spin extends Component {
#degreesPerSecond;
constructor() {
super();
this.#degreesPerSecond = 90;
}
setDegreesPerSecond(degreesPerSecond) {
this.#degreesPerSecond = degreesPerSecond;
}
tick(timeDelta) {
const node = this.getNode();
const rotation = node.getLocalRotation();
node.setLocalRotation(rotation + this.#degreesPerSecond * timeDelta);
}
}
class CrossMark extends Component {
draw(graphic) {
const node = this.getNode();
const contentSize = node.getContentSize();
graphic.setStrokeColor("#ffffff");
graphic.drawLine([Vector2.create(0, 0), Vector2.create(contentSize.x, contentSize.y)], 4);
graphic.drawLine([Vector2.create(contentSize.x, 0), Vector2.create(0, contentSize.y)], 4);
}
isDrawnInsideContent() {
return true;
}
}
class ComponentScene extends Scene {
initialize(engine) {
super.initialize(engine);
const root = this.getRoot();
const node = new WorldNode();
node.setContentSize(Vector2.create(160, 160));
node.setLocalPosition(Vector2.create(640, 360));
const paint = node.addComponent(Paint);
paint.setColor(new Color(0.8, 0.3, 0.3, 1));
node.addComponent(CrossMark);
const spin = node.addComponent(Spin);
spin.setDegreesPerSecond(45);
root.addChild(node);
const foundSpin = node.getComponent(Spin);
const hasCrossMark = node.hasComponent(CrossMark);
console.log(foundSpin === spin, hasCrossMark);
}
preDraw(graphic) {
super.preDraw(graphic);
const engine = this.getEngine();
const viewManager = engine.getViewManager();
viewManager.applyViewRect(graphic);
}
}
const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new ComponentScene());Components draw in attach order. In the example above, Paint fills the background and then CrossMark draws lines on top of it.
Walking the node tree
| Method | Description |
|---|---|
getParent(), isRoot(), isLeaf() |
Parent, whether it has no parent, whether it has no children |
getChildren(), getChildCount(), getChild(index) |
Direct children |
findChildByName(name), findChild(predicate) |
First match among direct children |
findDescendantByName(name), findChildRecursive(predicate) |
First match among all descendants, depth first (findChildRecursiveByName is the same as findDescendantByName) |
removeChildByName(name), removeChildAt(index), removeChildren() |
Detach direct children |
destroyChildrenWhere(predicate) |
Detaches the direct children that match, calls destroy() on them, and returns how many |
import { WorldNode, Paint } from "./build/vanilla.js";
function createNode(name) {
const node = new WorldNode();
node.setName(name);
return node;
}
function visit(node, depth, lines) {
const name = node.getName();
lines.push(`${" ".repeat(depth)}${name}`);
const children = node.getChildren();
for (const child of children) {
visit(child, depth + 1, lines);
}
}
const root = createNode("root");
const player = createNode("Player");
const weapon = createNode("Weapon");
const enemies = createNode("Enemies");
root.addChild(player);
player.addChild(weapon);
root.addChild(enemies);
for (let index = 0; index < 3; ++index) {
const enemy = createNode(`Enemy${index}`);
enemy.addComponent(Paint);
enemies.addChild(enemy);
}
const lines = [];
visit(root, 0, lines);
console.log(lines.join("\n"));
const foundWeapon = root.findDescendantByName("Weapon");
const paintedNode = root.findChildRecursive((node) => {
return node.hasComponent(Paint);
});
const removedCount = enemies.destroyChildrenWhere((node) => {
return node.getName() !== "Enemy0";
});
const paintedName = paintedNode.getName();
const enemyCount = enemies.getChildCount();
console.log(foundWeapon === weapon, paintedName, removedCount, enemyCount);Things to watch
getLocalPosition(),getLocalScale(),getPivot()andgetContentSize()return copies. Changing the returned vector does not change the node, sosetthe changed value back.getComponent(Type)finds subclasses too.SpriteextendsPaint, sogetComponent(Paint)may return aSprite.setEnable(false)stops that component's drawing, but itstickkeeps being called while the node is active. To stop updates too, checkisEnable()inside the component or turn the node off.- For a node to receive touches, besides
WorldNode.setInteractable(true), the scene has to pass touches to nodes. (TouchRecognizer, "Input" chapter) - The root node is created in the scene's
create()and gets the view size inload. Attach nodes aftercreatehas run (aftersuper.create()), or inloadorinitialize.
Related classes
Node, ComponentNode, TransformNode, WorldNode, UINode, Component, Pivot, Paint, Scene