AS3获取当前的时间,而无需创建新对象 [英] AS3 get current time without creating new object

查看:116
本文介绍了AS3获取当前的时间,而无需创建新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得自午夜1970年1月1日在AS3毫秒为单位的当前数量,而不必使用new操作符?我这么问是因为我需要做的这种每秒约100倍,目前尽量减少堆分配,以保持GC低。

How can I get the current number of milliseconds since midnight January 1, 1970 in AS3 without having to use the 'new' operator? I'm asking because I need to do this about 100 times per second and currently try to reduce heap allocations to keep gc low.

推荐答案

您应该创建它一次,然后添加毫秒,因为这与创立了的getTimer通过();您还需要一个帮助变量来存储你的数据创建时间;

You should create it once and then add milliseconds, that passed since that creation with getTimer(); You will also need a helping variable to store your Data creation time;

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();在它自己,它的效率更高

If you need just to know what time has passed since last frame, without knowing the day of the week, or a year, you can just use getTimer(); on it's own, it's more efficient

这篇关于AS3获取当前的时间,而无需创建新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆