cocos creator 按钮的通用脚本

creator 默认的按钮状态有四种: NONE 、 COLOR 、 SPRITE 、 SCALE

这里的SCALE默认的是点击放大的效果,但在写代码的时候可能会要求实现点击缩小

为了方便添加,我们自定义脚本,添加到按钮上即可。 这里还可以直接在脚本里添加按钮点击音效 。

/**
 * @ 按钮通用控制脚本
 * @ 使用方法:直接添加到按钮控件即可 缩放参数可以自己调整
 */

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property
    pressedScale: number = 0.8;

    @property
    transDuration: number = 0.1;

    @property
    audio:number = 0;

    initScale: number = 0;

    onLoad () {
        var that = this;
        this.initScale = this.node.scale;

        function onTouchDown (event) {
            let scaleDownAction = cc.scaleTo(that.transDuration, that.pressedScale);
            let btnScale = event.target.getComponent('ButtonScaler');
            this.stopAllActions();
            //这里可以添加音效
            this.runAction(scaleDownAction);
        }

        function onTouchUp (event) {
            let scaleUpAction = cc.scaleTo(that.transDuration, that.initScale);
            this.stopAllActions();
            this.runAction(scaleUpAction);
        }

        this.node.on('touchstart', onTouchDown, this.node);
        this.node.on('touchend', onTouchUp, this.node);
        this.node.on('touchcancel', onTouchUp, this.node);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值