根据上面的文章,属性-编辑-模块-针对以下程序优化,将模块优化。
也就是设定为某个模块只有某程序可以加载,这时编译器会根据实际情况优化模块大小,主要是模块引用的一些资源和类。
我在模块TestClass中嵌入一张174,282的图。
TestClass1
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
public class TestClass1 extends Sprite
{
[Embed('001.jpg')]
public static var TU:Class;
public function TestClass1()
{
var btu:Bitmap = new TU();
addChild(btu);
trace("TestClass1");
}
}
}
1.没有公用资源或类
Test1
package
{
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.utils.getDefinitionByName;
public class Test1 extends Sprite
{
public function Test1()
{
loader.load(new URLRequest("TestClass1.swf"), lc);
addChild(loader);
}
}
}
这时Test1直接加载TestClass1,双方没有共同引用的资源和类。
大小:
Test1:1,054
TestClass1:169,848。
2.有共用资源或类
Test1
package
{
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.utils.getDefinitionByName;
public class Test1 extends Sprite
{
public function Test1()
{
var tc1:TestClass1 = new TestClass1;
addChild(tc1);
loader.load(new URLRequest("TestClass1.swf"), lc);
addChild(loader);
}
}
}
如果主项目也用到模块用到的资源或类,模块中会优化掉这些资源和类,即使这些资源本身嵌在模块中。也就是实际存放在主项目中。
Test1 :170,340
TestClass1:446
3.Preloader方式
AS3工程 Custom Preloader 動態加載 Application
如果采用编译选项的preloader方式,则无论如何都无法优化模块。
重新设Test1为不可运行文件,也就是去掉可运行文件的属性,不会生成SWF。FB中设为可运行文件后无法去掉,只能删掉文件重新拷回来。
这时模块设定的针对的优化SWF是Preloader,实际是在第二帧Test1中加载模块。
Preloader作为第一帧,Test1作为第二帧。都在Preloader.swf中。
Preloader加载Test1,Test1加载TestClass1。
结果生成的Preloader和Test1都很大,说明资源重复嵌入了:
Preloader:171,686
TestClass:169,858
Preloader:
package
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.utils.getDefinitionByName;
public class Preloader extends MovieClip
{
/**
* PreloaderApp Constructor.
*/
public function Preloader()
{
this.addEventListener(Event.ADDED_TO_STAGE, handleToStage);
}
/**
* Handle ADDED_TO_STAGE event.
* @param event
*/
private function handleToStage(event:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, handleToStage);
//stage setting
stage.showDefaultContextMenu = false;
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
createChildren();
addEventListener(Event.ENTER_FRAME, loadApplication);
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, handleProgress);
this.loaderInfo.addEventListener(Event.COMPLETE, handleComplete);
}
private var _preLoadingText:TextField;
/**
* createChildren
*/
private function createChildren():void
{
_preLoadingText = new TextField();
_preLoadingText.text = "Loading...";
_preLoadingText.textColor = 0x000000;
_preLoadingText.x = (stage.stageWidth / 2) - (_preLoadingText.width / 2);
_preLoadingText.y = (stage.stageHeight / 2) - (_preLoadingText.height / 2);
_preLoadingText.autoSize = TextFieldAutoSize.CENTER;
addChild(_preLoadingText);
}
/**
* Handle progress.
* @param event
*/
private function handleProgress(event:ProgressEvent):void
{
var percent:int = Math.floor(event.bytesLoaded / event.bytesTotal * 100);
_preLoadingText.text = "Loading..." + percent + "%";
trace(percent);
}
/**
* Handle load complete.
* @param event
*/
private function handleComplete(event:Event):void
{
this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, handleProgress);
this.loaderInfo.removeEventListener(Event.COMPLETE, handleComplete);
}
/**
* Load main application.
* @param e
*/
private function loadApplication(event:Event):void
{
if (currentFrame == totalFrames)
{
removeEventListener(Event.ENTER_FRAME, loadApplication);
stop();
var cls:Class = getDefinitionByName("TestClass1") as Class;
addChild(new cls() as DisplayObject);
removeChild(_preLoadingText);
_preLoadingText = null;
}
}
}
}