【问题标题】:Ajusting as3 code to trigger on mouse event调整 as3 代码以触发鼠标事件
【发布时间】:2015-02-11 02:51:18
【问题描述】:

您好,我发现这个用于计时器计数器的代码非常有用,但是它会在我播放文件时启动。我需要一种将其更改为 MouseEvent.CLICK 的方法,以便它在用户按下按钮时启动,并在用户按下另一个按钮时停止。这个可以吗?

import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.globalization.DateTimeFormatter;

var timer:Timer = new Timer(100);
timer.start();
timer.addEventListener(TimerEvent.TIMER, timerTickHandler);
var timerCount:int = 0;

function timerTickHandler(Event:TimerEvent):void
{
timerCount += 100;
toTimeCode(timerCount);
}

function toTimeCode(milliseconds:int) : void {
//create a date object using the elapsed milliseconds
var time:Date = new Date(milliseconds);

//define minutes/seconds/mseconds
var hours:String = String(time.hours);
var minutes:String = String(time.minutes);
var seconds:String = String(time.seconds);
var miliseconds:String = String(Math.round(time.milliseconds)/100);

//add zero if neccecary, for example: 2:3.5 becomes 02:03.5
hours = (hours.length != 2) ? '0'+hours : hours;
minutes = (minutes.length != 2) ? '0'+minutes : minutes;
seconds = (seconds.length != 2) ? '0'+seconds : seconds;

//display elapsed time on in a textfield on stage
timer_txt.text = hours + ":" + minutes + ":" + seconds+"." + miliseconds;

}

【问题讨论】:

    标签: actionscript-3 timer mouseevent 2d-games stopwatch


    【解决方案1】:

    最简单的方法是使用timer.stop()timer.start()。这不会完全准确,因为调用stop()start() 基本上会重新启动当前延迟(100 毫秒),但如果这足够好,那么它应该可以工作。

    还请注意,计时器代码并不完全准确,因为 Timer 事件的调度会根据帧速率和脚本执行时间略有偏移。要获得准确的计时器,您需要轮询 getTimer(),但暂停和恢复的实现变得有点复杂。

    【讨论】:

      猜你喜欢
      • 2012-03-06
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-15
      • 1970-01-01
      • 2013-08-30
      相关资源
      最近更新 更多