简体   繁体   English

AS3倒数计时器会随着时间的推移经历极端的帧速率损失

[英]AS3 Countdown Timer experiences extreme framerate loss over time

I have a simple countdown timer written in AS3 that builds up CPU use and frame render time progressively over time to the point of frame rate dropping to 2-3 per second over the course of 10 minutes. 我有一个简单的倒数计时器,用AS3编写,随着时间的推移逐渐增加CPU使用和帧渲染时间,在10分钟内帧速率下降到每秒2-3个。 The timeline has 1 frame and the stage has 4 tlf text areas with instance names HH MM SS and FF. 时间轴有1帧,舞台有4个文本区域,实例名称为HH MM SS和FF。 The code updates these text areas each frame based upon a comparison with the current time and an "event time". 代码基于与当前时间和“事件时间”的比较,每帧更新这些文本区域。
I've looked in scout and the offending activity is a "Handling event 'render'" which eats up 95% of the active time 我看了一下侦察兵,犯罪活动是“处理事件'渲染'”,占用了95%的活跃时间

Code shown below 代码如下所示

import flash.events.Event;
stop();

// ***Customization Point*** Enter the event date as (Year, Month - 1, day, 24Hr Hours,     Minutes -1, Seconds - 1)
var End:Date = new Date(2013, 9, 15, 12, 0, 0);

var Now:Date;
var HH:Number;
var MM:Number;
var SS:Number;
var FF:Number;
stage.addEventListener(Event.ENTER_FRAME, tick);

function tick(e:Event = null):void
{
Now = new Date();
HH = (End.day > Now.day) ? (End.hours - Now.hours + (24 * (End.day - Now.day))) : (End.hours - Now.hours);
MM = (End.minutes == 0)? 60 - Now.minutes : End.minutes - Now.minutes;
MM = (MM == 60)? 0 : MM;
HH = (MM < 0)? HH - 1 : HH - 1;
MM = (MM < 0)? -1 * MM : MM;

SS = (End.seconds == 0)? 60 - Now.seconds : End.seconds - Now.seconds;
SS = (SS == 60)? 0 : SS;
MM = (SS < 0)? MM - 1 : MM;
SS = (SS < 0)? -1 * SS : SS;

FF = 30 - Math.round(Now.milliseconds / 33.33);

F.text = (FF < 10) ? "0" + FF.toString() : FF.toString();
S.text = (SS < 10) ? "0" + SS.toString() : SS.toString();
M.text = (MM < 10) ? "0" + MM.toString() : MM.toString();
H.text = (HH < 10) ? "0" + HH.toString() : HH.toString();                                           
}

You are adding one enter frame listener per FRAME, these build up and cause lags. 您为每个FRAME添加一个输入框架侦听器,这些构建并导致滞后。 Make it so that the listener is added once (use flag for this). 使它成为一个监听器添加一次(为此使用标志)。

var flag:Boolean; // default is false, but we don't want a value assigned HERE
if (!flag) {
    flag=true;
    stage.addEventListener(Event.ENTER_FRAME, tick);
}

Update: Maybe declaring a variable resets its value, so you can try this instead: 更新:可能声明变量重置其值,因此您可以尝试这样做:

if (!this["flag"]) {
    this["flag"]=true;
    stage.addEventListener(Event.ENTER_FRAME, tick);
}

The change eliminates the local var, and now saves the state on the MovieClip instance, so it persists. 此更改消除了本地var,现在将状态保存在MovieClip实例上,因此它仍然存在。

If this won't fix the issue either, try static var flag:Boolean with the first example. 如果这也无法解决问题,请尝试使用第一个示例的static var flag:Boolean (Personally I don't like timeline code, with it you are not in full control of code and data flow, so it can throw up for various Flash engine issues. Also I don't have tools to reproduce this issue, my Flash CS4 trial is long over.) (就我个人而言,我不喜欢时间轴代码,因为它不能完全控制代码和数据流,所以它可能会引发各种Flash引擎问题。而且我没有工具来重现这个问题,我的Flash CS4审判很久了。)

TLF TextFields are extremely expensive and have significant overhead over regular TextFields. TLF TextFields非常昂贵,并且与常规TextField相比具有显着的开销。

As a result where possible you should always use regular dynamic TextFields. 因此,您应该始终使用常规动态TextField。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM