Cocos Creator API

1.4.x

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

Button

Extends Component
Module: cc

Button has 4 Transition types When Button state changed: If Transition type is Button.Transition.NONE, Button will do nothing If Transition type is Button.Transition.COLOR, Button will change target's color If Transition type is Button.Transition.SPRITE, Button will change target Sprite's sprite If Transition type is Button.Transition.SCALE, Button will change target node's scale

Button will trigger 5 events: Button.EVENT_TOUCH_DOWN Button.EVENT_TOUCH_UP Button.EVENT_HOVER_IN Button.EVENT_HOVER_MOVE Button.EVENT_HOVER_OUT

examples:

// Add an event to the button.
button.node.on(cc.Node.EventType.TOUCH_START, function (event) {
     cc.log("This is a callback after the trigger event");
});

// You could also add a click event
//Note: In this way, you can't get the touch event info, so use it wisely.
button.node.on('click', function (event) {
   //The event is a custom event, you could get the Button component via event.detail
})

Properties

interactable Boolean

Whether the Button is disabled. If true, the Button will trigger event and do transition.

enableAutoGrayEffect Boolean

When this flag is true, Button target sprite will turn gray when interactable is false.

transition Button.Transition

Transition type

normalColor Color

Normal state color.

pressedColor Color

Pressed state color

hoverColor Color

Hover state color

disabledColor Color

Disabled state color

duration Number

Color and Scale transition duration

zoomScale Number

When user press the button, the button will zoom to a scale. The final scale of the button equals (button original scale * zoomScale)

normalSprite SpriteFrame

Normal state sprite

pressedSprite SpriteFrame

Pressed state sprite

hoverSprite SpriteFrame

Hover state sprite

disabledSprite SpriteFrame

Disabled state sprite

target Node

Transition target. When Button state changed: If Transition type is Button.Transition.NONE, Button will do nothing If Transition type is Button.Transition.COLOR, Button will change target's color If Transition type is Button.Transition.SPRITE, Button will change target Sprite's sprite

clickEvents Component.EventHandler[]

If Button is clicked, it will trigger event's handler

__eventTargets Array private

Inherited from Component:

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

node Node

Inherited from Component:

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

examples:

cc.log(comp.node);

uuid String readOnly

Inherited from Component:

The uuid for editor.

examples:

cc.log(comp.uuid);

_enabled Boolean private

Inherited from Component:

enabled Boolean

Inherited from Component:

indicates whether this component is enabled or not.

examples:

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

enabledInHierarchy Boolean readOnly

Inherited from Component:

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

examples:

cc.log(comp.enabledInHierarchy);

_isOnLoadCalled Boolean readOnly

Inherited from Component:

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 ( )

Inherited from Component:

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

lateUpdate ( )

Inherited from Component:

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

__preload ( ) private

Inher