时间计算
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
package com.game
{
import flash.utils.*;
import flash.events.*;
import flash.text.*;
import flash.display.MovieClip;
public class GameTime extends MovieClip
{
private var lastTime:Number;
private var timepast:Number;
private var showTime:TextField;
private var hour:Number;//小时
private var min:Number;//分
private var second:Number;//秒
public function GameTime()
{
lastTime=getTimer();
showTime=new TextField();
addChild(showTime);
addEventListener(Event.ENTER_FRAME,Run);
}
public function Run(event:Event):void
{
Show();
}
private function Show():void
{
timepast=getTimer()-lastTime;//毫秒运算
hour=Math.floor(timepast/(3600*1000));
min=Math.floor(timepast/(60*1000));
second=Math.floor(timepast/1000);
if (hour>=24)
{
second=0;
hour=0;
second=0;
}
if (second>=60)
{
second-=60*min;
}
if (min>=60)
{
min-=60*hour;
}
showTime.text=String(100+hour).substr(1,2)+":"+String(min+100).substr(1,2)+":"+String(second+100).substr(1,2);
}
public function getGameTime():String
{
timepast=getTimer()-lastTime;//毫秒运算
hour=Math.floor(timepast/(3600*1000));
min=Math.floor(timepast/(60*1000));
second=Math.floor(timepast/1000);
if (hour>=24)
{
second=0;
hour=0;
second=0;
}
if (second>=60)
{
second-=60*min;
}
if (min>=60)
{
min-=60*hour;
}
return String(100 + hour).substr(1,2) + ":" + String(min + 100).substr(1,2) + ":" + String(second + 100).substr(1,2);
}
}
}
可以出现一个时间计算来使用 格式为00:00:00 可以进行测试一下。