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) = !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 {for (let i = 0; i < this._components.length; i++) {const comp = this._components[i];EditorExtends.Component.remove(comp._id);}EditorExtends.Node.remove(this._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

which scene this node belongs to.

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
  • If true, the node is an persist node which won't be destroyed during scene transition.
    If false, the node will be destroyed automatically when loading a new scene. Default is false.

    property

    _persistNode

    type

    {Boolean}

    default

    false

    Returns boolean

  • Parameters

    • value: boolean

    Returns void

active

  • get active(): boolean
  • set active(isActive: boolean): void

activeInHierarchy

  • get activeInHierarchy(): boolean
  • Indicates whether this node is active in the scene.

    property

    activeInHierarchy

    type

    {Boolean}

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

    Returns boolean

children

  • get children(): this[]
  • All children nodes.

    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
  • Indicates whether the object is not yet destroyed. (It will not be available after being destroyed)
    When an object's destroy is called, it is actually destroyed after the end of this frame. So isValid will return false from the next frame, while isValid in the current frame will still be true. If you want to determine whether the current frame has called destroy, use cc.isValid(obj, true), but this is often caused by a particular logical requirements, which is not normally required.

    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
  • The uuid for editor, will be stripped before building project.

    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
  • Adds a component class to the node. You can also add component to node by passing in the name of the script.

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

    Type parameters

    Parameters

    Returns T | null

  • Adds a component class to the node. You can also add component to node by passing in the name of the script.

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

    Parameters

    • className: string

    Returns Component | null

attr

  • attr(attrs: Object): void
  • Properties configuration function
    All properties in attrs will be set to the node,
    when the setter of the node is available,
    the property will be set via setter function.

    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
  • Destroy all children from the node, and release all their own references to other objects.
    Actual destruct operation will delayed until before rendering.

    example
    node.destroyAllChildren();

    Returns void

dispatchEvent

  • dispatchEvent(event: Event): void

emit

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

getChildByName

  • getChildByName(name: string): null | this
  • Returns a child from the container given its name.

    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
  • Returns a child from the container given its path.

    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
  • Returns a child from the container given its 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
  • Returns the component of supplied type if the node has one attached, null if it doesn't.
    You can also get component in the node by passing in the name of the script.

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

    Type parameters

    Parameters

    Returns T | null

  • Returns the component of supplied type if the node has one attached, null if it doesn't.
    You can also get component in the node by passing in the name of the script.

    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
  • Returns the component of supplied type in any of its children using depth first search.

    example
    var Test = node.getComponentInChildren("Test");

    Type parameters

    Parameters

    Returns T | null

  • Returns the component of supplied type in any of its children using depth first search.

    example
    var Test = node.getComponentInChildren("Test");

    Parameters

    • className: string

    Returns 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
  • Inserts a child to the node at a specified index.

    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
  • Is this node a child of the given node?

    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
  • Removes a child from the container.

    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
  • Removes a component identified by the given name or removes the component object given. You can also use component.destroy() if you already have the reference.

    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

  • Removes a component identified by the given name or removes the component object given. You can also use component.destroy() if you already have the reference.

    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
  • Remove itself from its parent node.
    If the node orphan, then nothing happens.

    see

    cc.Node#removeFromParentAndCleanup

    example
    node.removeFromParent();

    Returns void

setParent

  • setParent(value: this | null, keepWorldTransform?: boolean): void
  • Set parent of the node.

    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
  • Walk though the sub children tree of the current node. Each node, including the current node, in the sub tree will be visited two times, before all children and after all children. This function call is not recursive, it's based on stack. Please don't walk any other node inside the walk process.

    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