I have been doing a lot of work in trying to minimize memory and file size in my flash/actionscript projects, because of the structure of actionscript 3 most of this is a manual process. One of the first things you can do is when adding an event listener, use the optional parameters:
_btn.addEventListener(MouseEvent.CLICK, btnClick, false, 0, true);
The last optional parameter in the addEventListener function is useWeakReference, which by default is set to false, according to the ActionScript 3.0 Documentation, this parameter “Determines whether the reference to the listener is strong or weak. A strong reference (the default) prevents your listener from being garbage-collected. A weak reference does not.”
Another standard that I have imemented is to use the REMOVED_FROM_STAGE event in every class that I write. In this class I remove any display objects that I have added and remove all event listeners that those display objects have, because just removing an object does not do this and hose event listeners will still be there taken up memory and can hinder performance.
我一直在试图减少内存和文件大小在我的flash/ActionScript项目,由于ActionScript 3的这方面最结构,是一个手动过程。第一件事你可以做一个是当添加一个事件监听器,使用可选的参数:
_btn.addEventListener(MouseEvent.CLICK, btnClick, false, 0, true);
最后在addEventListener函数的可选参数useWeakReference,默认设置为false,根据ActionScript 3.0文档,此参数:“确定是否对侦听参考的强弱。强引用(默认值)可以防止垃圾回收收取。弱引用没有。”
另一个我imemented标准是在每个类使用我写的REMOVED_FROM_STAGE事件。在这个类中删除任何显示对象,我增加和删除所有事件监听器,这些显示对象的,因为就删除一个对象不这样做,事件事件监听器依然占用内存,可阻碍性能对象。
package
{
import flash.display.*;
import flash.events.*;
public class MyClass extends Sprite
{
private var _btn:Sprite;
//-----------------------------------
// CONSTRUCTOR
//-----------------------------------
public function MyClass():void
{
addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
}
//-----------------------------------
// INIT
//-----------------------------------
private function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
addEventListener(Event.REMOVED_FROM_STAGE, dispose, false, 0, true);
_btn = new Sprite();
_btn.addEventListener(MouseEvent.MOUSE_OVER, btnOver, false, 0, true);
_btn.addEventListener(MouseEvent.MOUSE_OUT, btnOut, false, 0, true);
_btn.addEventListener(MouseEvent.CLICK, btnClick, false, 0, true);
_btn.buttonMode = true;
addChild(_btn);
}
//-----------------------------------
// DISPOSE
//-----------------------------------
private function dispose(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
if(_btn)
{
_btn.removeEventListener(MouseEvent.MOUSE_OVER, btnOver);
_btn.removeEventListener(MouseEvent.MOUSE_OUT, btnOut);
_btn.removeEventListener(MouseEvent.CLICK, btnClick);
try
{
removeChild(_btn);
}
catch(e:Error){trace("error: MyClass: dispose: removeChild(_btn): " + e)}
_btn = null;
}
}
}
}