有感Flex的两帧构造,就特意查了下相关文章。那里是用到元标签Frame。网上的说法,是用Frame指定的类替换文档类,然后将文档类的内容放到第二帧。虽然是说放到第二帧,可还得去实例,去addchild等等。
记得,记得一定要用Flex SDK来编译,用AIR SDK编译会出现意想不到效果。Frame指定的类根本就进不去。
那如果Frame指定的类不是MovieClip会怎么样呢。。。如果是那样。那就是无法安放的青春啊。看下边具体代码。
package com.vini123 { import flash.display.Bitmap; import flash.display.Sprite; import flash.events.Event; import flash.filters.ColorMatrixFilter; [Frame(factoryClass="com.vini123.managers.SystemManager")] [SWF(width = "672" , height="420" , frameRate="30")] public class Preloader extends Sprite { [Embed(source = "../../assets/bg.jpg")] public var Bg:Class; private var bg:Bitmap; private var colorFilter:ColorMatrixFilter; public function Preloader() { addEventListener(Event.ADDED_TO_STAGE , addToStageHandler); } private function addToStageHandler(e:Event):void { removeEventListener(Event.ADDED_TO_STAGE , addToStageHandler); stage.addEventListener(Event.RESIZE , resizeHandler); bg = new Bg() as Bitmap; addChild(bg); colorFilter = new flash.filters.ColorMatrixFilter(); colorFilter.matrix = new Array( 1/2,1/2,1/2,0,0, 1/3,1/3,1/3,0,0, 1/4,1/4,1/4,0,0, 0, 0, 0,1,0); bg.filters = [colorFilter]; stage.dispatchEvent(new Event(Event.RESIZE)); } private function resizeHandler(e:Event):void { if(bg) { bg.width = stage.stageWidth; bg.height = stage.stageHeight; } } } }
package com.vini123.managers { import com.vini123.loading.Loading; 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.utils.getDefinitionByName; public class SystemManager extends MovieClip { private var loading:Loading; public function SystemManager() { stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; stop(); initialize(); } private function initialize():void { loading = new Loading(); addChild(loading); loaderInfo.addEventListener(ProgressEvent.PROGRESS , progressHandler); loaderInfo.addEventListener(Event.COMPLETE , loadCompleteHandler); } private function progressHandler(e:ProgressEvent):void { loading.run(e.bytesLoaded/e.bytesTotal); loading.x = (stage.stageWidth - loading.width) * 0.5; loading.y = (stage.stageHeight - loading.height) * 0.5; } private function loadCompleteHandler(e:Event):void { loaderInfo.removeEventListener(ProgressEvent.PROGRESS , progressHandler); loaderInfo.removeEventListener(Event.COMPLETE , loadCompleteHandler); loading.destroy(); removeChild(loading); loading = null; nextFrame(); } override public function nextFrame():void { super.nextFrame(); /** * 这个时候,让我们容易想起flash(如flash cs6)中,单个swf做loading的时候, * 将那些带有链接类的影片剪辑的属性那里,“在第1帧中导出”的勾选去掉。 * 下边的做法就有此意。 **/ var PreloaderClass:Class = getDefinitionByName("com.vini123.Preloader") as Class; var preloader:DisplayObject = new PreloaderClass() as DisplayObject; addChild(preloader); } } }
package com.vini123.loading { import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; public class Loading extends Sprite { private var textField:TextField; private var textFormat:TextFormat; public function Loading() { textFormat = new TextFormat(); textFormat.color = 0xff0000; textField = new TextField(); textField.wordWrap = false; textField.multiline = false; textField.autoSize = TextFieldAutoSize.LEFT; textField.defaultTextFormat = new TextFormat("微软雅黑" , 15 , int(Math.random() * 0xffffff) , true); addChild(textField); } public function run(value:Number):void { textField.text = "正在加载中:" + Math.round(value * 1000)/10 + "%" textField.setTextFormat(textFormat , 6 , textField.length); } public function destroy():void { this.removeChildren(); textField.text = ""; textField = null; textFormat = null; } } }
那么,最后想一想。其实不用Frame也一样可以做成这种效果。那还是关键性的地方。不要再第一帧导出。
flex元数据相关:http://blog.csdn.net/aisajiajiao/article/details/9207837