| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- import Tool from "./Tool";
- const {ccclass, property} = cc._decorator;
- @ccclass
- export default /**比赛计时器 */
- class Timer extends cc.Component {
- public static EventType_ZERO:string = 'zero';
- private label:cc.Label;
- public time:number;
- private counting:boolean;
- /**
- * 初始化
- * @param second 倒计时秒数
- */
- public init(second:number,label:cc.Label):void{
- this.label = label;
- this.label.node.color = cc.Color.GREEN;
- this.time = second;
- this.label.string = Tool.timeFormat(this.time);
- }
-
- /**开始计时 */
- public startCountTime():void{
- this.counting = true;
- }
- /**停止计时 */
- public stopCountTime():void{
- this.counting = false;
- }
- /**更新时间 */
- public update(dt:number):void{
- if(this.counting&&this.time>0){
- let nextTime = this.time - dt;
- if(nextTime>0){
- this.time = nextTime;
- }else{
- this.time = 0;
- }
- if(this.time<10){
- this.label.node.color = cc.Color.RED;
- }
- this.label.string = Tool.timeFormat(this.time);
- if(this.time==0){
- this.counting = false;
- this.node.emit(Timer.EventType_ZERO);
- }
- }
- }
- /**显示开始倒计时动画 */
- public showStartCountDownAnimation(parent:cc.Node,callback:Function):void{
- for(let i=3;i>=0;i--){
- this.scheduleOnce(()=>{
- let node = new cc.Node();
- node.addComponent(cc.Sprite).spriteFrame = cc.loader.getRes('texture/font_'+(i==0?'fight':i),cc.SpriteFrame);
- node.setScale(4);
- node.setPosition(0,250);
- parent.addChild(node);
- node.runAction(cc.sequence(cc.scaleBy(0.6,0.8).easing(cc.easeBackInOut()),cc.callFunc(()=>{
- node.destroy();
- if(i==1){
- cc.audioEngine.playEffect(cc.loader.getRes('audio/fight',cc.AudioClip),false);
- }
- if(i==0&&callback)callback();
- },this)));
- },(3-i)*1);
- }
- }
- }
|