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);
}
}