在ActionScript 3中,使用矢量图形是一种保证对项目性能造成巨大损害的方法.
使用单一的Bitmap
所有图形通过使用.copyPixels()
通过其BitmapData
全部到位矢量图形的对象将产生一个可笑的性能提升,是人们喜欢自己开发中的Flash游戏是必不可少的.
除此之外,我不确定我应该针对并尝试优化的下一个重要事项是什么.我确实使用了很多内置的三角函数,但它们似乎并没有那么多.我知道有一些库用近似方法和类似的方法来优化数学,但到目前为止我还没有找到这些必要的库.
我应该看看还有其他重要的已知点吗?我更多地指的是我应该注意的内置事物(比如避免矢量渲染),而不是如何改进我自己的编码风格.
我发现有用的文件是:
优化Adobe Flash平台的性能
Gary Grossman的ActionScript 3.0和AVM2性能调优
Mike Chambers构建高性能iPhone应用程序
一些亮点:
限制内存使用的最简单的优化技巧之一是使用适当类型的显示对象.对于非交互式的简单形状,请使用Shape对象.对于不需要时间轴的交互式对象,请使用Sprite对象.对于使用时间轴的动画,请使用MovieClip对象.
getSize()
的基准码
getSize()
返回指定对象的内存大小.
除String外的所有基本类型在内存中使用4-8个字节.如果未分配值,则ActionScript虚拟机(AVM)将为表示64位值的 Number分配8个字节.String类型的行为不同.基准代码并确定任务的最有效对象.
通过重用对象来优化内存,并尽可能避免重新创建它们.
重用对象减少了实例化对象的需要,这可能很昂贵.它还减少了垃圾收集器运行的可能性,这可能会降低应用程序的速度.
要确保对象是垃圾回收,请删除对该对象的所有引用.内存分配而不是对象删除会触发垃圾回收.尝试通过尽可能多地重用对象来限制垃圾收集过程.此外,在可能的情况下将引用设置为null,以便垃圾收集器花费更少的处理时间来查找对象.将垃圾收集视为保险,并在可能的情况下始终明确管理对象生存期.
将对显示对象的引用设置为null不能确保对象被冻结.该对象继续消耗CPU周期,直到它被垃圾收集.
BitmapData类包含一个
dispose()
方法,虽然dispose方法从内存中删除了像素,但仍必须将引用设置为null才能完全释放它.
使用向量(尤其是大量向量)会显着增加对CPU或GPU资源的需求.使用位图是优化渲染的好方法,因为运行时需要较少的处理资源来绘制屏幕上的像素而不是渲染矢量内容.
将过滤器应用于显示对象时,运行时会在内存中创建两个位图.使用外部创作的位图有助于运行时减少CPU或GPU负载.
谨慎使用mipmapping.虽然它可以提高缩小的位图的质量,但它会对带宽,内存和速度产生影响.
TextField
对于输入文本,使用Text Engine作为只读文本对于只读文本,最好使用Flash文本引擎,它提供低内存使用和更好的渲染.对于输入文本, TextField对象是更好的选择,因为创建典型行为(例如输入处理和自动换行)需要较少的ActionScript代码.
与使用传统的回调函数相比,使用本机事件模型可能更慢并且消耗更多内存.必须在内存中创建和分配事件对象,这会导致性能下降.例如,在侦听
Event.ENTER_FRAME
事件时,会在事件处理程序的每个帧上创建一个新的事件对象.由于捕获和冒泡阶段,显示对象的性能可能特别慢,如果显示列表很复杂,这可能是昂贵的.
即使显示对象不再在显示列表中并且等待垃圾回收,它们仍然可能使用CPU密集型代码.
使用Loader类加载远程内容时,冻结的概念也很重要.
unloadAndStop()
method allows you to unload a SWF file, automatically freeze every object in the loaded SWF file, and force the garbage collector to run.
Event.ACTIVATE
and Event.DEACTIVATE
events to detect background inactivity
Event.ACTIVATE
andEvent.DEACTIVATE
events allow you to detect when the runtime gains or loses focus. As a result, code can be optimized to react to context changes.The activate and deactivate events allow you to implement a similar mechanism to the "Pause and Resume" feature sometimes found on mobile devices and Netbooks.
Detecting mouse interaction can be CPU-intensive when many interactive objects are shown onscreen, especially if they overlap. When possible, consider disabling mouse interaction, which helps your application to use less CPU processing, and as a result, reduce battery usage on mobile devices.
Timers are preferred over
Event.ENTER_FRAME
events for non-animated content that executes for a long time.A timer can behave in a similar way to an
Event.ENTER_FRAME
event, but an event can be dispatched without being tied to the frame rate. This behavior can offer some significant optimization. Consider a video player application as an example. In this case, you do not need to use a high frame rate, because only the application controls are moving.
Limit the use of tweening, which saves CPU processing, memory, and battery life helping content run faster on low-tier devices.
Vector
vs. Array
The Vector class allows faster read and write access than the Array class.
Array element access and iteration are much faster when using a Vector instance than they are when using an Array.
In strict mode the compiler can identify data type errors.
Runtime range checking (or fixed-length checking) increases reliability significantly over Arrays.
Reduce amount of code execution using
drawPath()
,drawGraphicsData()
,drawTriangles()
Fewer lines of code can provide better ActionScript execution performance.
Taking advantage of the bubbling of an event can help you to optimize ActionScript code execution time. You can register an event handler on one object, instead of multiple objects, to improve performance.
setVector()
methodWhen painting pixels, some simple optimizations can be made just by using the appropriate methods of the BitmapData class. A fast way to paint pixels is to use the
setVector()
method.
lock()
and unlock()
BitmapData
when using slow methods like setPixel()
or setPixel32()
Calling
lock()
andunlock()
prevents the screen from being updated unnecessarily. Methods that iterate over pixels, such asgetPixel()
,getPixel32()
,setPixel()
, andsetPixel32()
, are likely to be slow, especially on mobile devices. If possible, use methods that retrieve all the pixels in one call. For reading pixels, use thegetVector()
method, which is faster than thegetPixels()
method. Also, remember to use APIs that rely on Vector objects, when possible, as they are likely to run faster.
String
class methods instead of regular expressionsWhen a String class method is available, it runs faster than the equivalent regular expression and does not require the creation of another object.
apendText()
instead of the +=
operatorUsing the
appendText()
method provides performance improvements.
[]
can slow performance - store a reference in a local variableUsing the square bracket operator can slow down performance. You can avoid using it by storing your reference in a local variable.
Calling functions can be expensive. Try to reduce the number of function calls by moving code inline.
Moving the function call inline results in code that is more than four times faster.
Even if the off-stage elements are not shown onscreen and are not rendered, they still exist on the display list. The runtime continues to run internal tests on these elements to make sure that they are still off-stage and the user is not interacting with them.
alpha
propertyWhen a display object uses alpha blending, the runtime must combine the color values of every stacked display object and the background color to determine the final color. Thus, alpha blending can be more processor-intensive than drawing an opaque color. This extra computation can hurt performance on slow devices.
A higher frame rate expends more CPU cycles and energy from the battery than a lower rate.
Runtime code execution fundamentals
This feature caches a vector object, renders it as a bitmap internally, and uses that bitmap for rendering. Bitmap caching improves rendering if the cached content is not rotated, scaled, or changed on each frame. Any transformation other than translation on the x- and y-axes, rendering is not improved.
cacheAsBitmapMatrix
property when using cached bitmaps in mobile AIR apps
cacheAsBitmapMatrix
in the AIR mobile profile you can apply any two-dimensional transformation to the object without regenerating the cached bitmap. You can also change the alpha property without regenerating the cached bitmap.
BitmapData
class to create custom bitmap caching behaviorUsing only a single cached bitmap is used in memory and shared by all instances.
Event.ENTER_FRAME
in a single handlerThis technique saves CPU resources.
opaqueBackground
property to improve text rendering performanceThe bitmap caching feature allows you to cache vector content as bitmaps to improve rendering performance. This feature is helpful for complex vector content and also when used with text content that requires processing to be rendered.
Alpha transparency places an additional burden on the runtime when drawing transparent bitmap images. You can use the
opaqueBackground
property to bypass that, by specifying a color as a background.
In order to leverage GPU acceleration of Flash content with AIR for mobile platforms, Adobe recommends that you use renderMode="direct" (that is, Stage3D) rather than renderMod
真棒的答案!+1.我还想添加避免使用`MouseEvent.MOUSE_MOVE`(特别是在定位移动设备时).有一个简单的解决方法,就是立即从其处理程序中删除`MOUSE_MOVE`事件监听器,基本上只调用它一次,然后将其传递给`ENTER_FRAME`事件处理程序.