Options
All
  • Public
  • Public/Protected
  • All
Menu

Class BaseNode

A base node for CCNode, it will:

  • maintain scene hierarchy and active logic
  • notifications if some properties changed
  • define some interfaces shares between CCNode
  • define machanisms for Enity Component Systems
  • define prefab and serialize functions
class

_BaseNode

extends

Object

uses

EventTarget

method

constructor

param

Hierarchy

Implements

Index

Constructors

constructor

  • newBaseNode(name?: undefined | string): BaseNode

Properties

Protected __eventTargets

__eventTargets: any[] = []

Register all related EventTargets, all event callbacks will be removed in onPreDestroy protected _eventTargets: EventTarget[] = [];

Protected _active

_active: boolean = true

Protected _activeInHierarchy

_activeInHierarchy: boolean = false

Protected _children

_children: this[] = []

Protected _components

_components: Component[] = []
default

[]

readonly

Protected _eventMask

_eventMask: number = 0

Protected _eventProcessor

_eventProcessor: NodeEventProcessor = new NodeEventProcessor(this)

Protected _id

_id: string = idGenerator.getNewId()

Protected _name

_name: string

_objFlags

_objFlags: number

Protected _parent

_parent: this | null = null

Protected _prefab

_prefab: any = null

The PrefabInfo object

type

{PrefabInfo}

Protected _registerIfAttached

_registerIfAttached: undefined | (Anonymous function) = !CC_EDITOR ? undefined : function (this: BaseNode, register) {if (EditorExtends.Node && EditorExtends.Component) {if (register) {EditorExtends.Node.add(this._id, this);for (let i = 0; i < this._components.length; i++) {const comp = this._components[i];EditorExtends.Component.add(comp._id, comp);}}else {EditorExtends.Node.remove(this._id);for (let i = 0; i < this._components.length; i++) {const comp = this._components[i];EditorExtends.Component.remove(comp._id);}}}const children = this._children;for (let i = 0, len = children.length; i < len; ++i) {const child = children[i];child._registerIfAttached!(register);}}

Protected _scene

_scene: any = NullScene

此节点属于哪个场景。

type

{cc.Scene}}

Protected _siblingIndex

_siblingIndex: number = 0

Static Protected _stackId

_stackId: number = 0

Static Protected _stacks

_stacks: Array<Array<null | BaseNode>> = [[]]

Static Protected idGenerator

idGenerator: IDGenerator = idGenerator

Accessors

Protected persistNode

  • get persistNode(): boolean
  • set persistNode(value: boolean): void

active

  • get active(): boolean
  • set active(isActive: boolean): void
  • 当前节点的自身激活状态。
    值得注意的是,一个节点的父节点如果不被激活,那么即使它自身设为激活,它仍然无法激活。
    如果你想检查节点在场景中实际的激活状态可以使用 activeInHierarchy

    property

    active

    type

    {Boolean}

    default

    true

    example
    node.active = false;

    Returns boolean

  • Parameters

    • isActive: boolean

    Returns void

activeInHierarchy

  • get activeInHierarchy(): boolean
  • 表示此节点是否在场景中激活。

    property

    activeInHierarchy

    type

    {Boolean}

    example
    cc.log("activeInHierarchy: " + node.activeInHierarchy);

    Returns boolean

children

  • get children(): this[]
  • 节点的所有子节点。

    property

    children

    type

    {Node[]}

    readonly
    example
    var children = node.children;
    for (var i = 0; i < children.length; ++i) {
        cc.log("Node: " + children[i]);
    }

    Returns this[]

components

eventProcessor

isValid

  • get isValid(): boolean
  • 表示该对象是否可用(被 destroy 后将不可用)。
    当一个对象的 destroy 调用以后,会在这一帧结束后才真正销毁。
    因此从下一帧开始 isValid 就会返回 false,而当前帧内 isValid 仍然会是 true。
    如果希望判断当前帧是否调用过 destroy,请使用 cc.isValid(obj, true),不过这往往是特殊的业务需求引起的,通常情况下不需要这样。

    property

    {Boolean} isValid

    default

    true

    readonly
    example
    import * as cc from 'cc';
    var node = new cc.Node();
    cc.log(node.isValid);    // true
    node.destroy();
    cc.log(node.isValid);    // true, still valid in this frame
    // after a frame...
    cc.log(node.isValid);    // false, destroyed in the end of last frame

    Returns boolean

name

  • get name(): string
  • set name(value: string): void

parent

  • get parent(): null | this
  • set parent(value: null | this): void

scene

  • get scene(): any

uuid

  • get uuid(): string
  • 主要用于编辑器的 uuid,在编辑器下可用于持久化存储,在项目构建之后将变成自增的 id。

    property

    uuid

    type

    {String}

    readonly
    example
    cc.log("Node Uuid: " + node.uuid);

    Returns string

Methods

Protected Optional checkMultipleComp

  • checkMultipleComp(constructor: Function): boolean

destroyImmediate

  • _destroyImmediate(): void

destruct

  • _destruct(): void
  • Clear all references in the instance.

    NOTE: this method will not clear the getter or setter functions which defined in the instance of CCObject. You can override the _destruct method if you need, for example: _destruct: function () { for (var key in this) { if (this.hasOwnProperty(key)) { switch (typeof this[key]) { case 'string': this[key] = ''; break; case 'object': case 'function': this[key] = null; break; } } }

    Returns void

Protected disableChildComps

  • _disableChildComps(): void

Protected instantiate

  • instantiate(cloned: any): any

Protected onBatchCreated

  • _onBatchCreated(): void

Protected onBatchRestored

  • _onBatchRestored(): void

Protected onHierarchyChanged

  • onHierarchyChanged(oldParent: this | null): void

Protected onHierarchyChangedBase

  • onHierarchyChangedBase(oldParent: this | null): void

Protected onPostActivated

  • onPostActivated(active: boolean): void

Protected onPreDestroy

  • _onPreDestroy(): void

Protected onPreDestroyBase

  • _onPreDestroyBase(): boolean

Protected onSetParent

  • onSetParent(oldParent: this | null, keepWorldTransform?: boolean): void

Protected Optional onSiblingIndexChanged

  • onSiblingIndexChanged(siblingIndex: number): void

removeComponent

  • removeComponent(component: Component): void

updateSiblingIndex

  • _updateSiblingIndex(): void

addChild

  • addChild(child: this): void

addComponent

  • addComponent<T>(classConstructor: Constructor<T>): T | null
  • addComponent(className: string): Component | null
  • 向节点添加一个指定类型的组件类,你还可以通过传入脚本的名称来添加组件。

    example
    var test = node.addComponent("Test");

    Type parameters

    Parameters

    Returns T | null

  • 向节点添加一个指定类型的组件类,你还可以通过传入脚本的名称来添加组件。

    example
    var test = node.addComponent("Test");

    Parameters

    • className: string

    Returns Component | null

attr

  • attr(attrs: Object): void
  • 属性配置函数。在 attrs 的所有属性将被设置为节点属性。

    example
    var attrs = { key: 0, num: 100 };
    node.attr(attrs);

    Parameters

    • attrs: Object

      Properties to be set to node

    Returns void

destroy

  • destroy(): boolean

destroyAllChildren

  • destroyAllChildren(): void
  • 销毁所有子节点,并释放所有它们对其它对象的引用。
    实际销毁操作会延迟到当前帧渲染前执行。

    example
    node.destroyAllChildren();

    Returns void

dispatchEvent

  • dispatchEvent(event: Event): void

emit

  • emit(type: string, ...args: any[]): void

getChildByName

  • getChildByName(name: string): null | this
  • 通过名称获取节点的子节点。

    example
    var child = node.getChildByName("Test Node");

    Parameters

    • name: string

      A name to find the child node.

    Returns null | this

    a CCNode object whose name equals to the input parameter

getChildByPath

  • getChildByPath(path: string): null | this
  • 通过路径获取节点的子节点。

    example
    var child = node.getChildByPath("Test Node");

    Parameters

    • path: string

      A path to find the child node.

    Returns null | this

    a CCNode object whose name equals to the input parameter

getChildByUuid

  • getChildByUuid(uuid: string): null | this
  • 通过 uuid 获取节点的子节点。

    example
    var child = node.getChildByUuid(uuid);

    Parameters

    • uuid: string

      The uuid to find the child node.

    Returns null | this

    a Node whose uuid equals to the input parameter

getComponent

  • getComponent<T>(classConstructor: Constructor<T>): T | null
  • getComponent(className: string): Component | null
  • 获取节点上指定类型的组件,如果节点有附加指定类型的组件,则返回,如果没有则为空。
    传入参数也可以是脚本的名称。

    example
    // get custom test calss.
    var test = node.getComponent("Test");

    Type parameters

    Parameters

    Returns T | null

  • 获取节点上指定类型的组件,如果节点有附加指定类型的组件,则返回,如果没有则为空。
    传入参数也可以是脚本的名称。

    example
    // get custom test calss.
    var test = node.getComponent("Test");

    Parameters

    • className: string

    Returns Component | null

getComponentInChildren

  • getComponentInChildren<T>(classConstructor: Constructor<T>): T | null
  • getComponentInChildren(className: string): Component | null

getComponents

  • getComponents<T>(classConstructor: Constructor<T>): T[]
  • getComponents(className: string): Component[]

getComponentsInChildren

  • getComponentsInChildren<T>(classConstructor: Constructor<T>): T[]
  • getComponentsInChildren(className: string): Component[]

getParent

  • getParent(): null | this

getSiblingIndex

  • getSiblingIndex(): number

hasEventListener

  • hasEventListener(type: string): boolean

insertChild

  • insertChild(child: this, siblingIndex: number): void
  • 插入子节点到指定位置

    example
    node.insertChild(child, 2);

    Parameters

    • child: this

      the child node to be inserted 要插入的子节点

    • siblingIndex: number

      the sibling index to place the child in 用于放置子节点的同级索引

    Returns void

isChildOf

  • isChildOf(parent: this | null): boolean
  • 是否是指定节点的子节点?

    example
    node.isChildOf(newNode);

    Parameters

    • parent: this | null

    Returns boolean

    True if this node is a child, deep child or identical to the given node.

off

  • off(type: string, callback?: Function, target?: Object, useCapture?: any): void

on

once

  • once(type: string, callback: Function, target?: Object, useCapture?: any): void

removeAllChildren

  • removeAllChildren(): void

removeChild

  • removeChild(child: this): void
  • 移除节点中指定的子节点。

    example
    node.removeChild(newNode);

    Parameters

    • child: this

      The child node which will be removed. 将被移除的子节点

    Returns void

removeComponent

  • removeComponent<T>(classConstructor: Constructor<T>): void
  • removeComponent(classNameOrInstance: string | Component): void
  • 删除节点上的指定组件,传入参数可以是一个组件构造函数或组件名,也可以是已经获得的组件引用。 如果你已经获得组件引用,你也可以直接调用 component.destroy()

    deprecated

    please destroy the component to remove it.

    example
    const sprite = node.getComponent(CC.Sprite);
    if (sprite) {
        node.removeComponent(sprite);
    }
    node.removeComponent('cc.SpriteComponent');

    Type parameters

    Parameters

    Returns void

  • 删除节点上的指定组件,传入参数可以是一个组件构造函数或组件名,也可以是已经获得的组件引用。 如果你已经获得组件引用,你也可以直接调用 component.destroy()

    deprecated

    please destroy the component to remove it.

    example
    const sprite = node.getComponent(CC.Sprite);
    if (sprite) {
        node.removeComponent(sprite);
    }
    node.removeComponent('cc.SpriteComponent');

    Parameters

    Returns void

removeFromParent

  • removeFromParent(): void
  • 从父节点中删除该节点。
    如果这个节点是一个孤节点,那么什么都不会发生。

    see

    cc.Node#removeFromParentAndCleanup

    example
    node.removeFromParent();

    Returns void

setParent

  • setParent(value: this | null, keepWorldTransform?: boolean): void
  • 设置该节点的父节点。

    example
    node.setParent(newNode);

    Parameters

    • value: this | null
    • Default value keepWorldTransform: boolean = false

    Returns void

setSiblingIndex

  • setSiblingIndex(index: number): void

targetOff

  • targetOff(target: string | Object): void

walk

  • walk(prefunc: function, postfunc?: undefined | function): void
  • 遍历该节点的子树里的所有节点并按规则执行回调函数。 对子树中的所有节点,包含当前节点,会执行两次回调,prefunc 会在访问它的子节点之前调用,postfunc 会在访问所有子节点之后调用。 这个函数的实现不是基于递归的,而是基于栈展开递归的方式。 请不要在 walk 过程中对任何其他的节点嵌套执行 walk。

    example
    node.walk(function (target) {
        console.log('Walked through node ' + target.name + ' for the first time');
    }, function (target) {
        console.log('Walked through node ' + target.name + ' after walked all children in its sub tree');
    });

    Parameters

    • prefunc: function

      The callback to process node when reach the node for the first time

        • (target: this): void
        • Parameters

          • target: this

          Returns void

    • Optional postfunc: undefined | function

      The callback to process node when re-visit the node after walked all children in its sub tree

    Returns void

Static deferredDestroy

  • _deferredDestroy(): void

Static Protected findChildComponent

  • findChildComponent(children: BaseNode[], constructor: any): any

Static Protected findChildComponents

  • findChildComponents(children: BaseNode[], constructor: any, components: any): void

Static Protected findComponent

Static Protected findComponents

  • findComponents(node: BaseNode, constructor: Function, components: Component[]): void

Static setScene

Generated using TypeDoc