AS3 中计时器事件被阻塞并同时触发

发布于 2024-09-08 10:54:03 字数 656 浏览 0 评论 0原文

我以多种形式经历过这个问题。

public function startTimer() {
  timer = new Timer(3000);
  timer.addEventListener(TimerEvent.TIMER, timerTick, false, 0, true);
  timer.start();
}

private function timerTick(e:TimerEvent) {
  var bubble = new Bubble();
  this.addChild(bubble);
}

一段时间后,气泡将从显示屏上移除。想象一下一个水泡从屏幕底部漂浮起来,当碰到顶部时被移除。

如果 Flash 窗口闲置大约 20 分钟,则会创建并在显示屏上显示过多的Bubble 对象。屏幕上应该只有 5 个左右(因为它们被删除了),但数量太多了。

我认为由于某种原因,计时器事件被堵塞,当我们回到浏览器窗口时,所有事件都会立即触发。这是在 Mac 上使用 Firefox,但在其他浏览器中也会发生。

我尝试了很多方法,包括使用 flash.utils.getTimer() 重写计时器(即使用系统时钟),以及使用递归 TweenLite.delayedCall。

感谢您的任何提示和指示

I've experience this problem in many forms.

public function startTimer() {
  timer = new Timer(3000);
  timer.addEventListener(TimerEvent.TIMER, timerTick, false, 0, true);
  timer.start();
}

private function timerTick(e:TimerEvent) {
  var bubble = new Bubble();
  this.addChild(bubble);
}

Bubble gets removed from the display after a certain amount of time. Imagine a water bubble in the floating up from the bottom of the screen, and getting removed when hitting the top.

If the flash window is left idle for around 20 minutes, then way too many Bubble objects are created and shown on the display. There should only be around 5 on the screen (because they get removed), but there are way too many.

I think for some reason the timer events get clogged and when we come back to the browser window, all triggered at once. This is using firefox on the mac, but also happens in other browsers.

I have tried many things, including rewriting the timers using flash.utils.getTimer() (ie using the system clock), and using a recursive TweenLite.delayedCall.

Thanks for any tips and pointers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2024-09-15 10:54:03

我认为您已经遇到了 Flash Player 10.1 中的一项新功能,当内容完全不可见时(即当用户切换选项卡或将内容滚动到屏幕外时),它会将播放速度限制为 2FPS。 Adobe 工程师在此处对此进行了解释。

所以我猜测你的气泡会根据帧动画或事件移动并自行移除。因此,当这种限制发生时,它们的移动速度比它们产生的速度要慢得多。 (即使播放受到限制,计时器事件的工作原理也大致相同 - 它们发生的机会较少,但每个事件仍然会在自上一个事件后三秒过去后的第一个机会发生。)

因此,任何混合在一起的内容帧当用户更改选项卡时,基于时间的行为在 10.1 中的行为将会有所不同,这最终对于保持设备的性能和电池电量是必要的。最好的解决办法可能是将计时器更改为某种基于框架的逻辑,如下所示:

public function startTimer() {
    frameCount = 0;
    addEventListener( Event.ENTER_FRAME, onFrame, false, 0, true);
}

private function onFrame(e:Event) {
    frameCount++;
    if (frameCount > waitTimeSeconds * publishedFPS) {
        frameCount = 0;
        timerTick();
    }
}

当然,您也可以更改气泡以完全在计时器上工作,但这仅意味着当内容位于不可见的选项卡上时,每一帧都会有一大堆定时器事件来浪费循环处理。

I think you've run afoul of a new feature in Flash Player 10.1, where it throttles playback down to 2FPS when the content is completely invisible (i.e when the user switches tabs, or scrolls the content offscreen). An Adobe engineer explains this here.

So I'm guessing that that your bubbles move and remove themselves based on frame animations or events. So when this throttling occurs, they start moving much more slowly compared to the rate they are spawned. (Even with playback throttled, timer events work roughly the same - the chance for them to occur comes less frequently, but each event still occurs at the first opportunity once three seconds have passed since the last one.)

So any content that mixes together frame-based and time-based behaviors is going to behave differently in 10.1 when the user changes tabs, which is ultimately necessary to preserve performance and battery on devices. The best fix is to probably to change your timers to some sort of frame-based logic, along these lines:

public function startTimer() {
    frameCount = 0;
    addEventListener( Event.ENTER_FRAME, onFrame, false, 0, true);
}

private function onFrame(e:Event) {
    frameCount++;
    if (frameCount > waitTimeSeconds * publishedFPS) {
        frameCount = 0;
        timerTick();
    }
}

Granted, you could also change the bubbles to work entirely on timers, but that just means that when the content is on an invisible tab, every frame there will be a big stack of timer events to waste cycles processing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文