Actionscript 3 在闪存中创建倒计时计时器时出错
我试图用我找到的代码在Flash CS5.5中添加计时器。但似乎我已经纠正了一些错误,但我的输出仍然不正常。错误是访问txt的未定义属性。 有人能帮我查一下吗?提前谢谢Actionscript 3 在闪存中创建倒计时计时器时出错,actionscript-3,flash,timer,flash-cs5.5,Actionscript 3,Flash,Timer,Flash Cs5.5,我试图用我找到的代码在Flash CS5.5中添加计时器。但似乎我已经纠正了一些错误,但我的输出仍然不正常。错误是访问txt的未定义属性。 有人能帮我查一下吗?提前谢谢 GameOver.visible = false; timerFunction(0, 12); function timerFunction(minutes, seconds) { var seconds = seconds; var minutes = minutes; var clock;
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;
}
}
GameOver.visible=false;
时间函数(0,12);
功能时间功能(分钟,秒)
{
var秒=秒;
var分钟=分钟;
无功时钟;
var tmr=设置间隔(计时器,1000);
函数计时器(){
秒--;
如果(秒<0){
分钟--;
秒=59;
}
如果(分钟=0和秒=0){
清除间隔(tmr);
GameOver.visible=true;
}
时钟=分钟+“0”+秒;
如果(秒<10){
如果(分钟<10){
时钟=“0”+分钟+”:0”+秒;
}
}否则{
如果(分钟<10){
时钟=“0”+分钟+“1”+秒;
}否则{
时钟=分钟+“1”+秒;
}
}
txt.embedFonts=false;
txt.text=时钟;
}
}
这是我的zip文件。
只需在舞台上添加一个文本,并将其命名为“txt” 或 通过在行的开头添加这些行来编程
import flash.text.TextField;
var txt:TextField = new TextField ;
txt.embedFonts = true ;
addChild(txt);
使用以下代码(已测试)
您需要在问题中添加相关代码和错误。很高兴能下载.fla文件和源文件来查看它,但如果您不下载,您的问题将作为离题关闭。我已编辑了我的问题。谢谢@xxbbcc你能把相关代码复制到问题中吗?@Jezzamon我已经把代码复制到问题中了。谢谢你的回答。问题解决了。但是我仍然不能使计时器正常工作。1-在舞台上制作文本字段并将其命名为“txt”(确保嵌入字体)并使用我的代码我已经尝试了你的代码。不会出现错误,但不会显示分数。你知道为什么吗?我试过你的密码。不会出现错误,但不会显示分数。你知道为什么吗?我想你应该在timerComplete()函数中添加(GameOver.visible=true;),就在行(txt.text=“0:0”)之前
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) ;
}