• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

laya实现倒计时

武飞扬头像
蜂蜜柚子茶很好喝
帮助1

最近在学习Laya相关的知识,其中就涉及到了倒计时相关的部分,由于本人是新手,所以花费了很多时间去实现这个东西:

普通的倒计时

  1.  
    export default class tCount extends Laya.Script {
  2.  
        [x: string]: any;
  3.  
        nCountDown: number;
  4.  
        txt_TimeCountDown: any;
  5.  
     
  6.  
        /** @prop {name:txt_TimeCountDown, tips:"倒计时", type:Node, default:1000}*/
  7.  
        // 更多参数说明请访问: https://ldc2.layabox.com/doc/?nav=zh-as-2-4-0
  8.  
     
  9.  
        constructor() {
  10.  
            super();
  11.  
            this.txt_TimeCountDown=null;
  12.  
             
  13.  
        }
  14.  
     
  15.  
        onAwake(){
  16.  
            console.log("GameManager onAwake");
  17.  
           
  18.  
           
  19.  
        }
  20.  
       onStart(){
  21.  
        this.gameStart();
  22.  
       }
  23.  
        onEnable(): void {
  24.  
        }
  25.  
     
  26.  
        onDisable(): void {
  27.  
        }
  28.  
     
  29.  
        onSecond(){
  30.  
            this.nCountDown--;
  31.  
            this.txt_TimeCountDown.text="" this.nCountDown;
  32.  
            if(this.nCountDown<=0){
  33.  
                this.gameOver();
  34.  
                console.log("游戏结束");  
  35.  
            }
  36.  
        }
  37.  
     
  38.  
        gameStart(){
  39.  
       
  40.  
            this.nCountDown=100;
  41.  
         
  42.  
            Laya.timer.loop(1000,this,this.onSecond);
  43.  
        }
  44.  
        gameOver(){
  45.  
            Laya.timer.clear(this,this.onSecond);
  46.  
        }
  47.  
    }
  48.  
     
学新通

00:00:00格式的倒计时:

  1.  
    export default class CountDownTest extends Laya.Script {
  2.  
         /** @prop {name:label1,tips:"天数",type:Node,default=null} */
  3.  
       label1:any;
  4.  
          /** @prop {name:label2,tips:"小时",type:Node,default=null} */
  5.  
       label2:any;
  6.  
          /** @prop {name:label3,tips:"分钟",type:Node,default=null} */
  7.  
       label3:any;
  8.  
          /** @prop {name:label4,tips:"秒数",type:Node,default=null} */
  9.  
       label4:any;
  10.  
        constructor() { super();
  11.  
            this.label1=null;
  12.  
            this.label2=null;
  13.  
            this.label3=null;
  14.  
            this.label4=null;
  15.  
       
  16.  
        }
  17.  
       
  18.  
          onAwake(){
  19.  
            this.TimeCount();
  20.  
          }
  21.  
        TimeCount(){
  22.  
            var date=new Date();
  23.  
            var now =date.getTime();
  24.  
            var str="2022/7/28 23:59:59";
  25.  
            var endDate=new Date(str);
  26.  
            var end=endDate.getTime();
  27.  
            //
  28.  
            var leftTime=end-now;
  29.  
            var d,h,m,s;  
  30.  
           
  31.  
            if (leftTime>=0) {  
  32.  
               
  33.  
                d = Math.floor(leftTime/1000/60/60/24);  
  34.  
                h = Math.floor(leftTime/1000/60/60%24);  
  35.  
                m = Math.floor(leftTime/1000/60%60);  
  36.  
                s = Math.floor(leftTime/1000%60);                    
  37.  
            }  
  38.  
            this.label1.text=d "";
  39.  
            this.label2.text=h "h";
  40.  
            this.label3.text=m "m";
  41.  
            this.label4.text=s "s";
  42.  
     
  43.  
            Laya.timer.loop(1000,this,this.TimeCount);
  44.  
            // setTimeout(this.TimeCount,1000);
  45.  
        }
  46.  
        countDown(){
  47.  
     
  48.  
        }
  49.  
        onEnable(): void {
  50.  
        }
  51.  
     
  52.  
        onDisable(): void {
  53.  
        }
  54.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbecfe
系列文章
更多 icon
同类精品
更多 icon
继续加载