Button
Class
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
User can get the current clicked node with 'event.target' from event object which is passed as parameter in the callback function of click event.
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 (button) {
//The event is a custom event, you could get the Button component via first argument
})
Index
Properties
interactable
Boolean
Whether the Button is disabled.enableAutoGrayEffect
Boolean
When this flag is true, Button target sprite will turn gray when interactable is false.transition
Button.Transition
Transition typenormalColor
Color
Normal state color.pressedColor
Color
Pressed state colorhoverColor
Color
Hover state colordisabledColor
Color
Disabled state colorduration
Number
Color and Scale transition durationzoomScale
Number
When user press the button, the button will zoom to a scale.normalSprite
SpriteFrame
Normal state spritepressedSprite
SpriteFrame
Pressed state spritehoverSprite
SpriteFrame
Hover state spritedisabledSprite
SpriteFrame
Disabled state spritetarget
Node
Transition target.clickEvents
Component.EventHandler[]
If Button is clicked, it will trigger event's handler__eventTargets
Array
Register all related EventTargets,...node
Node
The node this component is attached to.uuid
String
The uuid for editor._enabled
Boolean
enabled
Boolean
indicates whether this component is enabled or not.enabledInHierarchy
Boolean
indicates whether this component is enabled and its node is also active in the hierarchy._isOnLoadCalled
Number
Returns a value which used to indicate the onLoad get called or not._name
String
_objFlags
Number
name
String
The name of the object.isValid
Boolean
Indicates whether the object is not yet destroyed.
Methods
update
This is a lifecycle method.lateUpdate
This is a lifecycle method.__preload
__preload
is called before every onLoad.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
This is a lifecycle method.onDisable
This is a lifecycle method.onDestroy
This is a lifecycle method.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.addComponent
Adds a component class to the node.getComponent
Returns the component of supplied type if the node has one attached, null if it doesn't....getComponents
Returns all components of supplied Type in the node.getComponentInChildren
Returns the component of supplied type in any of its children using depth first search.getComponentsInChildren
Returns the components of supplied type in self or any of its children using depth first search._getLocalBounds
If the component's bounding box is different from the node's, you can implement this method to supplyonRestore
for undo/redo operation.schedule
Schedules a custom selector....scheduleOnce
Schedules a callback function that runs only once, with a delay of 0 or larger.unschedule
Unschedules a custom callback function.unscheduleAllCallbacks
unschedule all scheduled callback functions: custom callback functions, and the 'update' callback function....destroy
Actual object destruction will delayed until before rendering._destruct
Clear all references in the instance._onPreDestroy
Called before the object being destroyed._serialize
The customized serialization for this object._deserialize
Init this object from the custom serialized data.
Events
click
Note: This event is emitted from the node to which the component belongs.
Details
Properties
interactable
Whether the Button is disabled. If true, the Button will trigger event and do transition.
meta | description |
---|---|
Type | Boolean |
Defined in | cocos2d/core/components/CCButton.js:161 |
enableAutoGrayEffect
When this flag is true, Button target sprite will turn gray when interactable is false.
meta | description |
---|---|
Type | Boolean |
Defined in | cocos2d/core/components/CCButton.js:192 |
transition
Transition type
meta | description |
---|---|
Type | Button.Transition |
Defined in | cocos2d/core/components/CCButton.js:205 |
normalColor
Normal state color.
meta | description |
---|---|
Type | Color |
Defined in | cocos2d/core/components/CCButton.js:224 |
pressedColor
Pressed state color
meta | description |
---|---|
Type | Color |
Defined in | cocos2d/core/components/CCButton.js:241 |
hoverColor
Hover state color
meta | description |
---|---|
Type | Color |
Defined in | cocos2d/core/components/CCButton.js:259 |
disabledColor
Disabled state color
meta | description |
---|---|
Type | Color |
Defined in | cocos2d/core/components/CCButton.js:277 |
duration
Color and Scale transition duration
meta | description |
---|---|
Type | Number |
Defined in | cocos2d/core/components/CCButton.js:294 |
zoomScale
When user press the button, the button will zoom to a scale. The final scale of the button equals (button original scale * zoomScale)
meta | description |
---|---|
Type | Number |
Defined in | cocos2d/core/components/CCButton.js:305 |
normalSprite
Normal state sprite
meta | description |
---|---|
Type | SpriteFrame |
Defined in | cocos2d/core/components/CCButton.js:317 |
pressedSprite
Pressed state sprite
meta | description |
---|---|
Type | SpriteFrame |
Defined in | cocos2d/core/components/CCButton.js:332 |
hoverSprite
Hover state sprite
meta | description |
---|---|
Type | SpriteFrame |
Defined in | cocos2d/core/components/CCButton.js:348 |
disabledSprite
Disabled state sprite
meta | description |
---|---|
Type | SpriteFrame |
Defined in | cocos2d/core/components/CCButton.js:364 |
target
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
meta | description |
---|---|
Type | Node |
Defined in | cocos2d/core/components/CCButton.js:379 |
clickEvents
If Button is clicked, it will trigger event's handler
meta | description |
---|---|
Type | Component.EventHandler[] |
Defined in | cocos2d/core/components/CCButton.js:406 |
__eventTargets
Register all related EventTargets, all event callbacks will be removed in _onPreDestroy
meta | description |
---|---|
Type | Array |
Defined in | cocos2d/core/components/CCComponent.js:61 |
node
The node this component is attached to. A component is always attached to a node.
meta | description |
---|---|
Type | Node |
Defined in | cocos2d/core/components/CCComponent.js:75 |
Examples
cc.log(comp.node);
uuid
The uuid for editor.
meta | description |
---|---|
Type | String |
Defined in | cocos2d/core/components/CCComponent.js:106 |
Examples
cc.log(comp.uuid);
_enabled
meta | description |
---|---|
Type | Boolean |
Defined in | cocos2d/core/components/CCComponent.js:147 |
enabled
indicates whether this component is enabled or not.
meta | description |
---|---|
Type | Boolean |
Defined in | cocos2d/core/components/CCComponent.js:154 |
Examples
comp.enabled = true;
cc.log(comp.enabled);
enabledInHierarchy
indicates whether this component is enabled and its node is also active in the hierarchy.
meta | description |
---|---|
Type | Boolean |
Defined in | cocos2d/core/components/CCComponent.js:186 |
Examples
cc.log(comp.enabledInHierarchy);
_isOnLoadCalled
Returns a value which used to indicate the onLoad get called or not.
meta | description |
---|---|
Type | Number |
Defined in | cocos2d/core/components/CCComponent.js:202 |
Examples
cc.log(this._isOnLoadCalled > 0);
_name
meta | description |
---|---|
Type | String |
Defined in | cocos2d/core/platform/CCObject.js:76 |
_objFlags
meta | description |
---|---|
Type | Number |
Defined in | cocos2d/core/platform/CCObject.js:83 |
name
The name of the object.
meta | description |
---|---|
Type | String |
Defined in | cocos2d/core/platform/CCObject.js:243 |
Examples
obj.name = "New Obj";
isValid
Indicates whether the object is not yet destroyed. (It will not be available after being destroyed)
When an object'sdestroy
is called, it is actually destroyed after the end of this frame. SoisValid
will return false from the next frame, whileisValid
in the current frame will still be true. If you want to determine whether the current frame has calleddestroy
, usecc.isValid(obj, true)
, but this is often caused by a particular logical requirements, which is not normally required.
meta | description |
---|---|
Type | Boolean |
Defined in | cocos2d/core/platform/CCObject.js:261 |
Examples
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
Methods
update
Update is called every frame, if the Component is enabled.
This is a lifecycle method. It may not be implemented in the super class. You can only call its super class method inside it. It should not be called manually elsewhere.
meta | description |
---|---|
Defined in | cocos2d/core/components/CCComponent.js:223 |
Parameters
dt
Number the delta time in seconds it took to complete the last frame
lateUpdate
LateUpdate is called every frame, if the Component is enabled.
This is a lifecycle method. It may not be implemented in the super class. You can only call its super class method inside it. It should not be called manually elsewhere.
meta | description |
---|---|
Defined in | cocos2d/core/components/CCComponent.js:234 |
__preload
__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.
meta | description |
---|---|
Defined in | cocos2d/core/components/CCComponent.js:244 |
onLoad
When attaching to an active node or its node first activated.
onLoad is always called before any start functions, this allows you to order initialization of scripts.
This is a lifecycle method. It may not be implemented in the super class. You can only call its super class method inside it. It should not be called manually elsewhere.
meta | description |
---|---|
Defined in | cocos2d/core/components/CCComponent.js:255 |
start
Called before all scripts' update if the Component is enabled the first time.
Usually used to initialize some logic which need to be called after all components' onload
methods called.
This is a lifecycle method. It may not be implemented in the super class. You can only call its super class method inside it. It should not be called manually elsewhere.
meta | description |
---|---|
Defined in | cocos2d/core/components/CCComponent.js:268 |
onEnable
Called when this component becomes enabled and its node is active.
This is a lifecycle method. It may not be implemented in the super class. You can only call its super class method inside it. It should not be called manually elsewhere.
meta | description |
---|---|
Defined in | cocos2d/core/components/CCComponent.js:281 |
onDisable
Called when this component becomes disabled or its node becomes inactive.
This is a lifecycle method. It may not be implemented in the super class. You can only call its super class method inside it. It should not be called manually elsewhere.
meta | description |
---|---|
Defined in | cocos2d/core/components/CCComponent.js:291 |
onDestroy
Called when this component will be destroyed.
This is a lifecycle method. It may not be implemented in the super class. You can only call its super class method inside it. It should not be called manually elsewhere.
meta | description |
---|---|
Defined in | cocos2d/core/components/CCComponent.js:301 |
onFocusInEditor
meta | description |
---|---|
Defined in | cocos2d/core/components/CCComponent.js:311 |
onLostFocusInEditor
meta | description |
---|---|
Defined in | cocos2d/core/components/CCComponent.js:316 |
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.
meta | description |
---|---|
Defined in | cocos2d/core/components/CCComponent.js:321 |
addComponent
Adds a component class to the node. You can also add component to node by passing in the name of the script.
meta | description |
---|---|
Returns | Component |
Defined in | cocos2d/core/components/CCComponent.js:331 |
Parameters
Examples
var sprite = node.addComponent(cc.Sprite);
var test = node.addComponent("Test");
getComponent
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.
meta | description |
---|---|
Returns | Component |
Defined in | cocos2d/core/components/CCComponent.js:349 |
Parameters
Examples
// get sprite component.
var sprite = node.getComponent(cc.Sprite);
// get custom test calss.
var test = node.getComponent("Test");
getComponents
Returns all components of supplied Type in the node.
meta | description |
---|---|
Returns | Component[] |
Defined in | cocos2d/core/components/CCComponent.js:373 |
Parameters
Examples
var sprites = node.getComponents(cc.Sprite);
var tests = node.getComponents("Test");
getComponentInChildren
Returns the component of supplied type in any of its children using depth first search.
meta | description |
---|---|
Returns | Component |
Defined in | cocos2d/core/components/CCComponent.js:391 |
Parameters
Examples
var sprite = node.getComponentInChildren(cc.Sprite);
var Test = node.getComponentInChildren("Test");
getComponentsInChildren
Returns the components of supplied type in self or any of its children using depth first search.
meta | description |
---|---|
Returns | Component[] |
Defined in | cocos2d/core/components/CCComponent.js:409 |
Parameters
Examples
var sprites = node.getComponentsInChildren(cc.Sprite);
var tests = node.getComponentsInChildren("Test");
_getLocalBounds
If the component's bounding box is different from the node's, you can implement this method to supply a custom axis aligned bounding box (AABB), so the editor's scene view can perform hit test properly.
meta | description |
---|---|
Defined in | cocos2d/core/components/CCComponent.js:429 |
Parameters
out_rect
Rect the Rect to receive the bounding box
onRestore
onRestore is called after the user clicks the Reset item in the Inspector's context menu or performs
an undo operation on this component.
If the component contains the "internal state", short for "temporary member variables which not included
in its CCClass properties", then you may need to implement this function.
The editor will call the getset accessors of your component to record/restore the component's state
for undo/redo operation. However, in extreme cases, it may not works well. Then you should implement
this function to manually synchronize your component's "internal states" with its public properties.
Once you implement this function, all the getset accessors of your component will not be called when
the user performs an undo/redo operation. Which means that only the properties with default value
will be recorded or restored by editor.
Similarly, the editor may failed to reset your component correctly in extreme cases. Then if you need
to support the reset menu, you should manually synchronize your component's "internal states" with its
properties in this function. Once you implement this function, all the getset accessors of your component
will not be called during reset operation. Which means that only the properties with default value
will be reset by editor.
This function is only called in editor mode.
meta | description |
---|---|
Defined in | cocos2d/core/components/CCComponent.js:442 |
schedule
Schedules a custom selector.
If the selector is already scheduled, then the interval parameter will be updated without scheduling it again.
meta | description |
---|---|
Defined in | cocos2d/core/components/CCComponent.js:536 |
Parameters
callback
function The callback functioninterval
Number Tick interval in seconds. 0 means tick every frame.repeat
Number The selector will be executed (repeat + 1) times, you can use cc.macro.REPEAT_FOREVER for tick infinitely.delay
Number The amount of time that the first tick will wait before execution.
Examples
var timeCallback = function (dt) {
cc.log("time: " + dt);
}
this.schedule(timeCallback, 1);
scheduleOnce
Schedules a callback function that runs only once, with a delay of 0 or larger.
meta | description |
---|---|
Defined in | cocos2d/core/components/CCComponent.js:573 |
Parameters
callback
function A function wrapped as a selectordelay
Number The amount of time that the first tick will wait before execution.
Examples
var timeCallback = function (dt) {
cc.log("time: " + dt);
}
this.scheduleOnce(timeCallback, 2);
unschedule
Unschedules a custom callback function.
meta | description |
---|---|
Defined in | cocos2d/core/components/CCComponent.js:590 |
Parameters
callback_fn
function A function wrapped as a selector
Examples
this.unschedule(_callback);
unscheduleAllCallbacks
unschedule all scheduled callback functions: custom callback functions, and the 'update' callback function.
Actions are not affected by this method.
meta | description |
---|---|
Defined in | cocos2d/core/components/CCComponent.js:606 |
Examples
this.unscheduleAllCallbacks();
destroy
Destroy this Object, and release all its own references to other objects.
Actual object destruction will delayed until before rendering.
From the next frame, this object is not usable any more.
You can use cc.isValid(obj) to check whether the object is destroyed before accessing it.
meta | description |
---|---|
Returns | Boolean |
Defined in | cocos2d/core/platform/CCObject.js:296 |
Examples
obj.destroy();
_destruct
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; } } }
meta | description |
---|---|
Defined in | cocos2d/core/platform/CCObject.js:430 |
_onPreDestroy
Called before the object being destroyed.
meta | description |
---|---|
Defined in | cocos2d/core/platform/CCObject.js:463 |
_serialize
The customized serialization for this object. (Editor Only)
meta | description |
---|---|
Returns | object |
Defined in | cocos2d/core/platform/CCObject.js:488 |
Parameters
exporting
Boolean
_deserialize
Init this object from the custom serialized data.
meta | description |
---|---|
Defined in | cocos2d/core/platform/CCObject.js:498 |
Parameters
data
Object the serialized json datactx
_Deserializer
Events
click
Event
Module: cc
Note: This event is emitted from the node to which the component belongs.