Вопрос

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. The timeline has 1 frame and the stage has 4 tlf text areas with instance names HH MM SS and 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

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();                                           
}
Это было полезно?

Решение

TLF TextFields are extremely expensive and have significant overhead over regular TextFields.

As a result where possible you should always use regular dynamic TextFields.

Другие советы

You are adding one enter frame listener per FRAME, these build up and cause lags. 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.

If this won't fix the issue either, try static var flag:Boolean with the first example. (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.)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top