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;
}
}
}
}
分享到:
相关推荐
Managing NFS and NIS
Managing Linux Systems with Webmin Managing Linux Systems with Webmin
Managing Information Security Risks
Managing Gigabytes 很经典的关于大数据处理的书籍,废了很大力气转成pdf 编辑了目录。
Rex Black's Managing the Testing Process, a compendium of real-world advice on managing software testing successfully. It is a veritable hodge-podge of sample test documents and is filled with ...
Managing Infrastructure with puppet
Managing and Mining Uncertain Data 电子书 Edited by Charu C. Aggarwal
managing statis for optimal query performance
Measuring and Managing Credit Risk
framework for managing IT-enabled
In the third edition of the classic Managing Projects with GNU make, readers will learn why this utility continues to hold its top position in project build software, despite many younger competitors...
Blink Managing Server Clusters on Intermittent Power
1 Managing for Stakeholders, 1 2 Business in the Twenty-first Century, 20 3 The Basic Framework, 47 4 Stakeholders, Purpose, and Values, 74 5 Everyday Strategies for Creating Value for Stakeholders, ...
Managing Windows with VBScript and WMI 2004.chm 共享资源,
Managing Your Biological Data with Python 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权...
Fstream: Managing Flash Sreams in the File System PPT
A+ Guide to Managing and Maintaining Your PC
PLM305-Structuring and Managing Technical Objects
Managing ABAP Development Projects
Managing Projects with GNU Make