var DWAlert = { _alert: null, _title: null, _detailLabel: null, _cancelButton: null, _confirmButton: null, _confirmCallBack: null, _animDuration: 0.3 }; /** * 显示弹窗 * @param {string} titleString * @param {string} detailString * @param {function} confirmCallBack * @param {bool} needCancel * @param {float} animDuration */ DWAlert.show = function (titleString = "", detailString, confirmCallBack, needCancel = true, animDuration) { if (DWAlert._alert != undefined) { return; } DWAlert._animDuration = animDuration ? animDuration : DWAlert._animDuration; cc.loader.loadRes("./prefabs/DWAlert", cc.Prefab, (error, prefab) => { if (error) { console.log(error); return; } var alert = cc.instantiate(prefab); DWAlert._alert = alert; // 动画 var cbFadeOut = cc.callFunc(DWAlert.onFadeOutFinish, this); var cbFadeIn = cc.callFunc(DWAlert.onFadeInFinish, this); this.actionFadeIn = cc.sequence(cc.spawn(cc.fadeTo(DWAlert._animDuration, 255), cc.scaleTo(DWAlert._animDuration, 1.0)), cbFadeIn); this.actionFadeOut = cc.sequence(cc.spawn(cc.fadeTo(DWAlert._animDuration, 0), cc.scaleTo(DWAlert._animDuration, 2.0)), cbFadeOut); DWAlert._title = cc.find("alertBackground/titleLabel", alert); DWAlert._detailLabel = cc.find("alertBackground/detailLabel", alert); DWAlert._cancelButton = cc.find("alertBackground/cancelButton", alert); DWAlert._confirmButton = cc.find("alertBackground/confirmButton", alert); DWAlert._cancelButton.on(cc.Node.EventType.TOUCH_END, DWAlert.clickCancelButton, this); DWAlert._confirmButton.on(cc.Node.EventType.TOUCH_END, DWAlert.clickConfirmButton, this); DWAlert._alert.parent = cc.find("Canvas"); DWAlert.startFadeIn(); DWAlert.config(titleString, detailString, confirmCallBack, needCancel); }); }; DWAlert.config = function (titleString, detailString, confirmCallBack, needCancel) { DWAlert._confirmCallBack = confirmCallBack || function () {}; DWAlert._title.getComponent(cc.Label).string = titleString; if (detailString === undefined || detailString === null) { DWAlert._detailLabel.active = false; } else { DWAlert._detailLabel.active = true; DWAlert._detailLabel.getComponent(cc.Label).string = detailString; } if (needCancel) { DWAlert._cancelButton.active = true; } else { DWAlert._cancelButton.active = false; DWAlert._confirmButton.x = 0; } }; // 显示弹窗动画 DWAlert.startFadeIn = function () { DWAlert._alert.position = cc.p(0, 0); DWAlert._alert.setScale(2); DWAlert._alert.opacity = 0; DWAlert._alert.runAction(this.actionFadeIn); }; // 关闭弹窗动画 DWAlert.startFadeOut = function () { DWAlert._alert.runAction(this.actionFadeOut); }; // 显示弹窗动画完成回调 DWAlert.onFadeInFinish = function () { }; // 关闭弹窗动画完成回调 DWAlert.onFadeOutFinish = function () { DWAlert._cancelButton.off(cc.Node.EventType.TOUCH_END, DWAlert.clickCancelButton, this); DWAlert._confirmButton.off(cc.Node.EventType.TOUCH_END, DWAlert.clickConfirmButton, this); DWAlert.onDestory(); }; DWAlert.clickConfirmButton = function () { DWAlert._confirmCallBack(); DWAlert.startFadeOut(); }; DWAlert.clickCancelButton = function () { DWAlert.startFadeOut(); }; DWAlert.onDestory = function() { DWAlert._alert = null; DWAlert._title = null; DWAlert._detailLabel = null; DWAlert._cancelButton = null; DWAlert._confirmButton = null; DWAlert._confirmCallBack = null; DWAlert._animDuration = 0.3; }; module.exports = DWAlert;