在帧或计时器上输入 - 在 AS3 中更新哪个更好?

发布于 2024-09-09 03:56:17 字数 151 浏览 0 评论 0 原文

所以我有大约数百个地图图块,每个标题都有一些可以执行某些操作的对象。它应该做的一件事是更新文本字段中的文本(之后您需要执行某些操作)。我应该如何做得更好 - 使用输入帧事件并在每个输入帧上更新它或设置一个计时器对象,该对象每秒都会更新文本字段中的文本?我想更多地从性能角度来看哪个会更好。

So I've about hundred of map tiles and each title has some object which does some stuff. And one of things it should do is to update text (a time after which you need to do something) in text field. How should i do it better - with enter frame event and on each enter frame update it or set up a timer object which on each second would update the text in text field? I wonder more from performance view which would be better.

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

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

发布评论

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

评论(3

雪花飘飘的天空 2024-09-16 03:56:17

每当您想要进行最终影响屏幕上显示内容的处理时,都应该使用帧事件。如果您要移动图像,或调整视觉内容的大小,或制作程序化动画,或任何此类性质的内容,则应该使用帧事件。原因可能相当复杂,但在最简单的层面上,它可以归结为:如果您在屏幕更新之间多次执行这种处理,那么您就会无缘无故地浪费性能,如果您不那么频繁地执行这种处理,那么您就会无缘无故地浪费性能。获得断断续续的视觉效果。使用计时器以与帧速率相同的速度执行操作似乎是一种替代方案,但您会花费一定的时间不同步。也就是说,即使您的内容为 25FPS 并且运行延迟为 40 毫秒的计时器,由于计时的微小波动,有时该计时器事件会在重绘之间触发两次,有时会被跳过。如果使用 Timer 有一些巨大的固有优势,那么这可能是值得的,但事实并非如此。

另一方面,每当您想要执行比屏幕更新频率低得多的操作时,计时器都是一个不错的选择。创建一个在 5 秒内触发的计时器事件比创建一个计数到 200 的帧侦听器更清楚。(但要小心将帧事件和计时器混合在一起 - 请参阅 此处。)

最后说一下性能:这没有任何区别你用。或者更确切地说,与渲染以及您在帧或计时器事件内执行的操作相比,无论有什么差异都是微小的。可以产生细微差别的是最大限度地减少开销 - 如果您每帧执行许多不同的操作,请使用单个事件侦听器,并让它调用所有需要触发的函数,而不是使用大量侦听器。定时器也是如此。但即便如此,实际上也不太可能影响你的表现,除非你有数百个听众。您应该始终从最简单的方式开始,如果测试显示您存在瓶颈,则稍后再担心性能。

You should use frame events any time you want to do processing that ultimately affects what shows up on the screen. If you're moving images around, or resizing visual contents, or doing programmatic animations, or anything of that nature, you should use frame events. The reasoning why can get fairly involved, but at the simplest level it boils down to this: if you do that kind of processing more than once between screen updates, you're wasting performance for no reason, and if you do it less often you get choppy visuals. Using a Timer to do things at the same speed as the frame rate might seem like an alternative, but you'll spend a certain amount of time out of sync. That is, even if your content is 25FPS and you run a timer with a delay of 40ms, because of small fluctuations in timing, sometimes that timer event will fire twice between redraws, and sometimes it will get skipped. That might be worth it if there was some huge inherent advantage to using Timer, but there isn't.

On the other hand, any time you want to do something that happens a lot less often than screen updates, Timer is a good option. It's clearer to make a timer event that fires in 5 seconds than it is to make a frame listener that counts up to 200. (But be wary of mixing frame events and timers together - see here.)

Finally a word about performance: it doesn't make any difference which you use. Or rather, whatever difference there is will be tiny compared to rendering, and what you do inside the frame or timer events. What can make a slight difference is minimizing the overhead - if you do a lot of different things every frame, use a single event listener, and have it call all the functions that need to fire, rather than using lots of listeners. And the same thing goes for Timers. But even this, realistically is unlikely to dent your performance unless you're running many hundreds of listeners. You should always start out whatever way is simplest, and worry about performance later if testing shows you have a bottleneck.

你好,陌生人 2024-09-16 03:56:17

无论如何,Timer 对象可能会侦听 ENTER_FRAME 事件以作为其计算的基础。由于它是一个新对象,它也会消耗一些内存,并且可能会消耗更多内存,具体取决于内部工作原理。仅当您创建大量 Timer 类实例时,这才会真正成为问题。

假设这是您正在创建的动态世界,您很可能已经有一个在 ENTER_FRAME 事件上调用的主游戏循环。您可以在这里循环遍历所有图块并相应地更新文本。如果您需要记录某个图块完成某件事的时间,您可能需要存储一些时间戳或其他内容。如果没有更多关于正在发生的事情的描述,很难说真的:)

A Timer object would probably listen to the ENTER_FRAME event anyway for basing its calculations. Since it is a new object it would also use up a bit of memory and possibly more depending on the inner workings. This would only really be a problem if you were creating a lot of instances of the Timer class.

Assuming this is some dynamic world you are creating, you most likely already have a main game loop that is called on an ENTER_FRAME event. It is in here you would loop through all your tiles and update the text accordingly. You might need to store some timestamps or something if you need to record how long a tile has done something.. hard to say really without more of a description of what is happening :)

冬天旳寂寞 2024-09-16 03:56:17

我不同意费诺马斯的观点。您可能希望比 fps 更频繁地更新代码,原因有很多。然而,有很多不同的方法可以做到这一点。
最简单的方法是使用 ENTER_FRAME 事件作为更新的主要函数,并在该函数内评估已经过去的时间。然后,您可以多次运行更新,或者使用时间作为传递给对象的值,以便它们进行相应更新。这样,即使 fps 下降,您的游戏也能保持流畅,并且以后不会遇到麻烦。

另外,虽然我同意您应该避免过早优化,但从一开始就编写好的代码比在游戏接近完成时重新设计一半的游戏引擎要好。

I disagree with fenomas. There are a lot of reason why you could want to update your code more often than fps. However there are a lot of different methods to do this.
The easiest one is to use the ENTER_FRAME event as the main function for your updates and inside that function evaluate the time that has passed. You can then either run your updates several times or use the time as a value you pass to your objects for them to update accordingly. That way your gameplay stays smooth even if fps drop and you won't have troubles later on.

Also, while i agree you should avoid premature optimization, a good code right from the start is better than having to rework half your game engine while the game was near completion.

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