Timer.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import Tool from "./Tool";
  2. const {ccclass, property} = cc._decorator;
  3. @ccclass
  4. export default /**比赛计时器 */
  5. class Timer extends cc.Component {
  6. public static EventType_ZERO:string = 'zero';
  7. private label:cc.Label;
  8. public time:number;
  9. private counting:boolean;
  10. /**
  11. * 初始化
  12. * @param second 倒计时秒数
  13. */
  14. public init(second:number,label:cc.Label):void{
  15. this.label = label;
  16. this.label.node.color = cc.Color.GREEN;
  17. this.time = second;
  18. this.label.string = Tool.timeFormat(this.time);
  19. }
  20. /**开始计时 */
  21. public startCountTime():void{
  22. this.counting = true;
  23. }
  24. /**停止计时 */
  25. public stopCountTime():void{
  26. this.counting = false;
  27. }
  28. /**更新时间 */
  29. public update(dt:number):void{
  30. if(this.counting&&this.time>0){
  31. let nextTime = this.time - dt;
  32. if(nextTime>0){
  33. this.time = nextTime;
  34. }else{
  35. this.time = 0;
  36. }
  37. if(this.time<10){
  38. this.label.node.color = cc.Color.RED;
  39. }
  40. this.label.string = Tool.timeFormat(this.time);
  41. if(this.time==0){
  42. this.counting = false;
  43. this.node.emit(Timer.EventType_ZERO);
  44. }
  45. }
  46. }
  47. /**显示开始倒计时动画 */
  48. public showStartCountDownAnimation(parent:cc.Node,callback:Function):void{
  49. for(let i=3;i>=0;i--){
  50. this.scheduleOnce(()=>{
  51. let node = new cc.Node();
  52. node.addComponent(cc.Sprite).spriteFrame = cc.loader.getRes('texture/font_'+(i==0?'fight':i),cc.SpriteFrame);
  53. node.setScale(4);
  54. node.setPosition(0,250);
  55. parent.addChild(node);
  56. node.runAction(cc.sequence(cc.scaleBy(0.6,0.8).easing(cc.easeBackInOut()),cc.callFunc(()=>{
  57. node.destroy();
  58. if(i==1){
  59. cc.audioEngine.playEffect(cc.loader.getRes('audio/fight',cc.AudioClip),false);
  60. }
  61. if(i==0&&callback)callback();
  62. },this)));
  63. },(3-i)*1);
  64. }
  65. }
  66. }