Cocos Creator API

1.4.x

Cocos Creator is a highly customizable game development tool that utilizes the power of cocos2d-x.

Component

Module: cc

Base class for everything attached to Node(Entity).

NOTE: Not allowed to use construction parameters for Component's subclasses, because Component is created by the engine.

Properties

__eventTargets Array private

Register all related EventTargets, all event callbacks will be removed in _onPreDestroy

node Node

The node this component is attached to. A component is always attached to a node.

examples:

cc.log(comp.node);

uuid String readOnly

The uuid for editor.

examples:

cc.log(comp.uuid);

_enabled Boolean private

enabled Boolean

indicates whether this component is enabled or not.

examples:

comp.enabled = true;
cc.log(comp.enabled);

enabledInHierarchy Boolean readOnly

indicates whether this component is enabled and its node is also active in the hierarchy.

examples:

cc.log(comp.enabledInHierarchy);

_isOnLoadCalled Boolean readOnly

TODO

examples:

cc.log(_isOnLoadCalled);

_name String private

_objFlags Number private

name String

The name of the object.

examples:

obj.name = "New Obj";

isValid Boolean readOnly

Indicates whether the object is not yet destroyed.

examples:

cc.log(obj.isValid);

There are no properties that match your current filter settings. You can change your filter settings in the index section on this page. index

Methods

update ( )

Update is called every frame, if the Component is enabled.

lateUpdate ( )

LateUpdate is called every frame, if the Component is enabled.

__preload ( ) private

__preload is called before every onLoad. It is used to initialize the builtin components internally, to avoid checking whether onLoad is called before every public method calls. This method should be removed if script priority is supported.

onLoad ( )

When attaching to an active node or its node first activated.

start ( )

Called before all scripts' update if the Component is enabled the first time.

onEnable ( )

Called when this component becomes enabled and its node is active.

onDisable ( )

Called when this component becomes disabled or its node becomes inactive.

onDestroy ( )

Called when this component will be destroyed.

onFocusInEditor ( )

onLostFocusInEditor ( )

resetInEditor ( )

Called to initialize the component or node’s properties when adding the component the first time or when the Reset command is used. This function is only called in editor.

addComponent
(
  • typeOrTypename
)
Component

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

name type description
typeOrTypename Function | String

the constructor or the class name of the component to add

returns:

type: Component

the newly added component

examples:

var sprite = node.addComponent(cc.Sprite);
var test = node.addComponent("Test");

getComponent
(
  • typeOrClassName
)
Component

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.

name type description
typeOrClassName Function | String

returns:

type: Component

examples:

// get sprite component.
var sprite = node.getComponent(cc.Sprite);
// get custom test calss.
var test = node.getComponent("Test");