104 lines
2.4 KiB
JavaScript
104 lines
2.4 KiB
JavaScript
/******************************************************************
|
|
* Copyright(C) 2019 - 2020 Nx Studio
|
|
*
|
|
* 倒计时
|
|
*
|
|
* 2018.05.18
|
|
******************************************************************/
|
|
|
|
cc.Class( {
|
|
|
|
extends: cc.Component,
|
|
|
|
properties: {
|
|
endTxt:"", //倒計時結束提示
|
|
isPop:false //是否飘窗提示
|
|
},
|
|
|
|
// 编辑器特性
|
|
editor: {
|
|
|
|
// 允许当前组件在编辑器模式下运行
|
|
executeInEditMode: false,
|
|
|
|
// requireComponent 参数用来指定当前组件的依赖组件
|
|
requireComponent: cc.Label,
|
|
|
|
// 当本组件添加到节点上后,禁止同类型(含子类)的组件再添加到同一个节点,防止逻辑发生冲突
|
|
disallowMultiple: true,
|
|
},
|
|
|
|
onEnable:function(){
|
|
|
|
},
|
|
|
|
onDisable(){
|
|
this.unscheduleAllCallbacks();
|
|
},
|
|
|
|
// 改变通知
|
|
_onValueChanged: function( _secs ) {
|
|
|
|
this.unscheduleAllCallbacks();
|
|
this.secs = _secs;
|
|
|
|
// 文本
|
|
let self = this;
|
|
let text = function( _secs ) {
|
|
|
|
// 结束
|
|
let txt = "";
|
|
if( _secs <= 0 ) {
|
|
|
|
// 空提示
|
|
txt = nx.text.getKey( self.doneKey );
|
|
if( nx.dt.strNEmpty( txt ) ) {
|
|
nx.gui.setString( self, "", txt );
|
|
return;
|
|
}
|
|
else {
|
|
txt = "00:00:00";
|
|
}
|
|
} else {
|
|
txt = nx.dt.fmtTimeFromSecs( _secs, true );
|
|
}
|
|
|
|
// 格式化
|
|
if( nx.dt.strNEmpty( self.fmtText ) ) {
|
|
txt = nx.text.format( self.fmtText, txt );
|
|
}
|
|
|
|
nx.gui.setString( self, "", txt );
|
|
|
|
};
|
|
|
|
// 0
|
|
if( !nx.dt.numPositive( _secs, false ) ) {
|
|
text( 0 );
|
|
return;
|
|
}
|
|
|
|
// 倒计时
|
|
text( this.secs );
|
|
this.schedule( ()=>{
|
|
|
|
self.secs = Math.max( self.secs - 1, 0 );
|
|
text( self.secs );
|
|
|
|
// 结束
|
|
if( self.secs <= 0 ) {
|
|
|
|
self.unscheduleAllCallbacks();
|
|
|
|
// 通告
|
|
if( self.doneEvents ) {
|
|
cc.Component.EventHandler.emitEvents( self.doneEvents );
|
|
}
|
|
}
|
|
|
|
}, 1 );
|
|
|
|
}
|
|
|
|
} );
|