简体   繁体   English

在 Flash 中创建倒数计时器时出错

[英]Error in creating countdown timer in Flash

I am trying to add timer in Flash CS5.5 with the code I've found.我正在尝试使用我找到的代码在 Flash CS5.5 中添加计时器。 But it seems that there are few errors which I've corrected but still my output isnt works well.但似乎我已经纠正了一些错误,但我的输出仍然不能正常工作。 The error is access undefined property of txt.错误是访问 txt 的未定义属性。 Can anyone help me to check it out?谁能帮我查一下? Thanks in advance!提前致谢!

GameOver.visible = false;

timerFunction(0, 12);

function timerFunction(minutes, seconds)

{
    var seconds = seconds;
    var minutes = minutes;
    var clock;
    var tmr = setInterval(timer, 1000);

    function timer() {
        seconds--;
        if (seconds < 0) {
            minutes--;
            seconds = 59;
        }

        if (minutes == 0 && seconds == 0) {
            clearInterval(tmr);
            GameOver.visible = true;
        }

        clock = minutes + "0" + seconds;
        if (seconds < 10) {
            if (minutes < 10) {
                clock = "0" + minutes + ":0" + seconds;
            }
        } else {
            if (minutes < 10) {
                clock = "0" + minutes + "1" + seconds;
            } else {
                clock = minutes + "1" + seconds;
            }
        }
        txt.embedFonts = false;
        txt.text = clock;
    }
}

Here is my zip file.这是我的 zip 文件。 https://www.dropbox.com/s/evm5alnbypty41y/Untitled-3.rar?dl=0 https://www.dropbox.com/s/evm5alnbypty41y/Untitled-3.rar?dl=0

Just add a text to the stage and name it "txt"只需在舞台上添加一个文本并将其命名为“txt”

or或者

Do it programatically by adding these lines at the begining of your lines通过在行的开头添加这些行以编程方式执行此操作

 import flash.text.TextField;

 var txt:TextField = new TextField ;
 txt.embedFonts = true ;
 addChild(txt);

Use the following code (it's tested)使用以下代码(已测试)

           import flash.utils.Timer;
           import flash.events.TimerEvent;

      var seconds:int ;
      var minutes:int ;
      var totalTimeInSeconds:int 
      var ticker:int ;


 var tmr:Timer ;

 function timerFunction(minutes, seconds)

 {
  totalTimeInSeconds = minutes * 60 + seconds
tmr = new Timer (1000,totalTimeInSeconds);
tmr.addEventListener(TimerEvent.TIMER,timerClick);
tmr.addEventListener(TimerEvent.TIMER_COMPLETE,timerComplete) ;
tmr.start() ;

}

 timerFunction(2, 15)
 function timerClick (e:TimerEvent):void
 {
 var curMinute:int = Math.floor((totalTimeInSeconds - ticker)/60) ;
 var curSecond:int =  (totalTimeInSeconds - ticker) -  (curMinute*60)  ;
 txt.text =  curMinute + ":" + curSecond;
 ticker +=1 ;
 }

 function timerComplete (E:TimerEvent):void
 {  
   txt.text = "0:0" ;
   tmr.removeEventListener(TimerEvent.TIMER,timerClick);
   tmr.removeEventListener(TimerEvent.TIMER_COMPLETE,timerComplete) ;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM