Button(按钮)组件参考

    button-color

    点击属性检查器下面的按钮,然后从添加 UI 组件中选择Button,即可添加 Button 组件到节点上。

    按钮的脚本接口请参考Button API

    Button 的 Transition 用来指定当用户点击 Button 时的状态表现。目前主要有 NONE,COLOR,SPRITE 和 SCALE。

    color-transition

    button-event

    详细说明

    Button 目前只支持 Click 事件,即当用户点击并释放 Button 时才会触发相应的回调函数。

    通过脚本代码添加回调

    方法一

    这种方法添加的事件回调和使用编辑器添加的事件回调是一样的,通过代码添加,
    你需要首先构造一个 cc.Component.EventHandler 对象,然后设置好对应的 target, component, handler 和 customEventData 参数。

    方法二

    通过 button.node.on('click', ...) 的方式来添加,这是一种非常简便的方式,但是该方式有一定的局限性,在事件回调里面无法
    获得当前点击按钮的屏幕坐标点。

    1. //假设我们在一个组件的 onLoad 方法里面添加事件处理回调,在 callback 函数中进行事件处理:
    2. extends: cc.Component,
    3. properties: {
    4. button: cc.Button
    5. },
    6. onLoad: function () {
    7. },
    8. callback: function (event) {
    9. //这里的 event 是一个 EventCustom 对象,你可以通过 event.detail 获取 Button 组件
    10. var button = event.detail;
    11. //do whatever you want with button
    12. //另外,注意这种方式注册的事件,也无法传递 customEventData