如何在不使用“新”运算符的情况下在 AS3 中获取自 1970 年 1 月 1 日午夜以来的当前毫秒数?我问是因为我需要每秒执行大约 100 次,并且目前尝试减少堆分配以保持 gc 低。
问问题
15606 次
2 回答
4
您应该创建一次,然后添加自使用 getTimer() 创建以来经过的毫秒数;您还需要一个帮助变量来存储您的数据创建时间;
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.getTimer;
public class Main extends Sprite
{
private var _myLittleDate:Date;
private var _myLittleDateCreationTime:Number;
public function Main():void
{
_myLittleDate = new Date(); //Our first and only object creation
_myLittleDateCreationTime = getTimer(); //Store it's creation time
addEventListener(Event.ENTER_FRAME, onEnterFrame); //Check function
}
private function onEnterFrame(e:Event):void
{
_myLittleDate.time += ( getTimer() - _myLittleDateCreationTime );
trace(_myLittleDate); //You get valid, refreshed data object here
_myLittleDateCreationTime = getTimer(); // Don't forget to update your initial time
//It's accurate since method .getTimer() is accurate itself
//You can even compare two Data object by creating another and tracing it right here
}
}
}
如果您只需要知道自上一帧以来经过了多少时间,而不知道星期几或一年,您可以使用 getTimer(); 自己做,效率更高
于 2013-09-21T04:40:48.687 回答
4
我使用的实用方法:
// Get time since the epoch and time since the VM was started
private static const dateTime:Number = new Date().time;
private static const dateTimestamp:uint = getTimer();
/**
* Current time in ms
*/
public static function getCurrentTime():Number
{
return dateTime + (getTimer() - dateTimestamp);
}
于 2013-11-07T14:33:02.047 回答