Adobe Air & Adobe Flex & ActionScript & Mobile Dev & HTML5 & RIA & User Experience
Adobe Flex 工程的 Application 為2幀的動畫,第1幀為 Pre-load,第2幀為 Application,如果想替換 Adobe Flex 原有的 Pre-loader,那么制作將非常的方便。制作一個新的 preload Component,在 Application 的 preloader 屬性中進行相關的引用就可以完成。
這次討論的不是在 AS3 工程中直接加載 SWF,而是通過 getDefinitionByName(name:String) 的方式對 Application 進行動態加載。
例子是 Unique Instance,詳細請見 Application.as 關于 Instance 的寫法。
代碼:
PreloaderApp.as
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 PreloaderApp extends MovieClip { /** * PreloaderApp Constructor. */ public function PreloaderApp() { 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 + "%"; } /** * 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("Application") as Class; addChild(new cls() as DisplayObject); removeChild(_preLoadingText); _preLoadingText = null; } } } }
Application.as
package { import flash.display.Sprite; import flash.events.Event; public class Application extends Sprite { private static var _instance:Application; /** * Unique instance. * @return */ public static function getInstance():Application { return _instance; } /** * Application Constructor. */ public function Application() { super(); addEventListener(Event.ADDED_TO_STAGE, initOnStage); } /** * Handle ADDED_TO_STAGE event. * @param event */ protected function initOnStage(event:Event):void { //remove ADDED_TO_STAGE listener. this.removeEventListener(Event.ADDED_TO_STAGE, initOnStage); //Set unique instance. if (!_instance) _instance = this; //... } } }
Related posts:
Puxiao
十二月 31st, 2009 at 8:56 上午
This is RSL.
也读过Y.Boy前阵子写的,你这样做有一个事情你就实现不了:如果你的调用的 app 或者类是单例,怎么办?你就不能通过 new cls() 获得了。
Alvin / Aedis.Ju
十二月 31st, 2009 at 9:57 上午
哈哈,請注意看,我的例子就是單例,其實Adobe Flex Application也是這么寫單例的。
Puxiao
十二月 31st, 2009 at 8:48 下午
呵呵,是,不过单例有很多写法,其中一个写法就是在构造函数的参数里使用一个外部不可能使用的类实例。
如果那样写,似乎就不支持了。
huyan
六月 29th, 2011 at 9:54 上午
你这个用以实现flex工程支持第三方插件的功能是否可以?我将动态加载的内容打包成swc文件,并且将其不和主文件放在一个目录中,我想做的是在启动程序的时候调用swc文件的类,通过你这种方法可行?如果不可行,有没有解决的办法
Alvin / Aedis.Ju
六月 30th, 2011 at 10:29 上午
@huyan
swc文件格式你可以看成是一种zip格式,可以直接解压然后去读取它的swf,所以,你可以直接把它看成读的是swf。
不放在同一个目录,你可以参考下这2篇文章,可以仔细阅读。
教程:深入理解Flash的沙箱 – Application Domains
教程:深入理解Flash的沙箱 –Security Domains
因为你是动态加载的,所以,你可以先把swc加载进来,解压相应的swf,设定相应的权限(具体可以看上述2篇文章),然后还是采用 getDefinitionByName 的方式把它实现。
eledocornelius
十二月 11th, 2011 at 2:44 上午
I’m sure the best for you and get big save to get new coupon
drapkum
一月 19th, 2012 at 9:08 上午
sell , just clicks away aabags.com suprisely
ischeap999
一月 19th, 2012 at 1:38 下午
New Colorway Fly Wade Do you realize one thing about Dwyane Wade? He could be the 1 who spending his all eight years of his career so far in Miami. Dwyane Wade may be probably the most recognizable face in South Beach. And there is certainly a fascinating factor that they even alterations the name of Miami-Dade Country to Miami to Miami-Wade Country for per week last July. He brought a brand new title to Miami, and rapped his city seven occasions within the All-Star game. With so considerably adore for A-Wade in Miami, Jordan Brand believed that it really is wise to give his very first signature shoe using the South Beach Colorway. This upcoming Fly Wade rocks a predominantly black upper with university blue, copal blue and best pink accents. The translucent copal blue sole ties the South Beach color scheme together. Should you be fond of the Jordans, you must come here and choose up some Low cost Jordan. All of the Jordan Shoes here may be the Low-cost Air Jordan Shoes, we wholesale Jordan Shoes now, you’ll find the shoe here are so appealing![url=http://www.ischeapjordans.com]cheap jordans[/url].
kinmemporeipt
一月 19th, 2012 at 6:45 下午
CasualWriter is the best place for article writing services.
[url=http://casualwriter.net/]CasualWriter[/url]