cocos creator的cc.Button擴展以下內容:
1.點擊時響應按鈕點擊聲音
2.點擊後能屏蔽按鈕一定時間,默認0.2秒
3.爲了防止vscode報錯(不影響遊戲編譯),需要將下面這斷說明代碼複製到creator.d.ts 的 cc.Button相應代碼處
--------------------使用示例---------------------
private _btnWXLogin: cc.Button
this._btnWXLogin.AddClick(this.OnBtnWXLoginClick.bind(this)).ClickSound().ForbidTime(2);
private OnBtnWXLoginClick() {
// wechat login
}
------------------代碼如下------------------------
import { AudioManager } from "../../Managers/AudioManager";
import Log from "./Log";
import { Timer } from "../../Managers/Timer";
//擴展cc的各類組件
/******************************
1.Button 自定義擴展說明 以下複製到 creator.d.ts -> cc.Button 下防止報錯提示
IsPlayClickSound: boolean;
ClickSoundName: string;
IsCanClick: boolean;
DisableTime: number;
AddClick(callback: (event: Event.EventCustom) => void): cc.Button;
ClickSound(sound?: string = null): cc.Button;
ForbidTime(time: number = 0.2): cc.Button;
*****************************/
cc.Button.prototype.IsPlayClickSound = false;
cc.Button.prototype.ClickSoundName = '';
cc.Button.prototype.IsCanClick = true;
cc.Button.prototype.DisableTime = 0;
cc.Button.prototype.AddClick = function (clickEvent) {
this.node.on('click', (e) => {
if (this.DisableTime > 0 && this.IsCanClick == false) {
return;
}
if (this.IsPlayClickSound) {
AudioManager.PlaySound(this.ClickSoundName);
}
//如果屏蔽時間大於0,點擊之後屏蔽
if (this.DisableTime > 0) {
this.IsCanClick = false; //禁用按鈕 一定時間後再啓用,防連擊
Timer.scheduleOnce(() => {
//Log.Trace(Log.Key.System, 're enable button!')
if (this) {
this.IsCanClick = true
}
}, this.DisableTime)
}
clickEvent(e)
})
return this
}
cc.Button.prototype.ClickSound = function (sound: string = null) {
this.IsPlayClickSound = true;
this.ClickSoundName = sound ? sound : 'S_Button'
return this
}
cc.Button.prototype.ForbidTime = function (time: number = 0.4) {
this.DisableTime = time;
return this
}