Building monolithic Flex SWFs that still startup quickly
- 博客分类:
- Flex 关于游戏开发
Monolithic Flex SWFs that still startup quickly? Doesn't that sound weird? Of course it does, but actually it works just fine. Thanks to the way the Flash Player downloads multi-frame SWF files it's possible to defer the loading of "heavy" parts of your Flex application making the startup time pretty fast - under the hood the Flash Player still streams in the remaining data (BWT, with "streaming" I'm not referring to live video streaming or anything video releated, there's an ongoing discussion about this somewhere else) p>Although this behaviour is common knowledge to most Flash developers it's probably not to the typical Flex developer as all frame- and timeline-based business is properly hidden by the Flex framework. By default, a Flex application actually is a 2 frame SWF file. Frame 1 contains the bootstrapping code to create the Flex application and the preloader and frame 2 contains all classes of the application itself. After frame 1 is fully loaded the preloader is displayed as long as frame 2 is downloading (this results in the typical loading screen of a Flex application). Once the whole SWF is downloaded into the Flash Player, the Application class gets instantiated and displayed. Now, the typical situation is that this 2 frame SWF file gets bigger and bigger as your application gets more and more complex, thus the download size and therefore the application startup time increases. A typical refactoring is to partition the application into multiple smaller SWF files ("Modules") and load these Modules SWF files on demand at runtime - which introduces other issues like class linking optimizations etc. Interestingly (and this is what this post is all about), it's possible to create Flex SWF files made up of more than these 2 frames. A nice side effect of this approach is that the SWF continues to download after the Application class has been created. This means the user can already interact with the Flex application while behind the curtain additional classes and assets gets pulled in. Or as noted in the Flex Developers Guide PDF on page 201: "The advantage to doing this is that the application starts faster than it would have if the assets had been included in the code, but does not require moving the assets to an external SWF file". There's not too much documentation available on how to implement this in detail, however. The best available material on what really happens are this blog post by Roger Gonzalez (former Adobe employee who worked on the Flex compiler) and a Powerpoint presentation on Modules by Alex Harui (Adobe employee, Flex Team). On the last two pages of Alex' great presentation he mentions what would be possible if Modules (i.e. compilation units) would be added behind frame 2 in a Flex SWF file. Adding those additional frames to a SWF file can be achieved by using the mxmlc -frame compiler argument specifying a label/identifier for the frame and one or more classes that should be linked to the frame. At runtime this ensures that the class/classes do not get loaded before the previous frame is completely loaded - and tada: there you have your streaming Flex application :) Here are the implementation details for a quick and dirty test case (Btw, while not necessary I added support for the Module API so this plays nicely with the mx.modules.ModuleManager class): First, I created a simple plain Flex 2 Application. Next, a class called TestFactory which implements mx.core.IFlexModuleFactory was created. This class will later be used to create instances of a Module called Test. The TestFactory class also implements a public static function frame(value:Object):void which is needed later. To link the TestFactory class onto an additional frame "outside" the default 2 frame section you'll have to add something like At runtime when frame 3 is hit, the static frame() method on the TestFactory class is called (this is not documented anywhere) passing in an instance of the active SystemManager implementation. Inside this method you can then register the TestFactory on the ModuleManager: The neat thing is that this really gets streamed in while the SWF is downloading but after the creationComplete Event has been dispatched - in other words: your application is already up and running while in the background the SWF still streams in. I added another class on frame 4 which makes the SWF pretty heavy (the class on frame 4 simply embeds a 8 MB mp3 file) to see if the Application really gets started before everything is in place - and it worked exactly as expected. Very nice! So in the end you may ask why to put everything into one monolithic SWF file at all when it seems much more elegant to load additional Modules as external SWF files. Of course, this may reduce total download size as it only loads a Module SWF when explicitely requested. This is especially true if your application is really consisting of many "mini apps" and chances are high that not every SWF needs to be loaded at all. If that's the case you should probably stay with Module SWFs. On the other hand, if you're application is not that partitioned and you just need better startup performance then the approach described above may be the perfect match. Dirk.
-frame test TestFactory
to the mxmlc arguments (either in Flex Builder or on the command line). This creates the 3 frame SWF file.
ModuleManager.getModule("published://myTest").publish(new TestFactory());
Note: Although in the documentation it says to use an URL starting with publish:// it actually has to be published://. After the Module is published you can call
ModuleManager.getModule("published://myTest");
from withinh the application to get a reference to the TestFactory instance and call create() on it to create an instance of whatever is created by the factory class.
发表评论
-
test
2011-02-12 15:35 6761123 -
禁止Flash CSX联网验证序列号的方法
2010-09-07 13:30 1002打开 C:\WINDOWS\system32\drivers\ ... -
游戏的成长性和竞技性的分析
2010-02-09 11:07 7871. 成长性分析 1). 成长性概述 成长性,最早 ... -
提高Flex应用程序性能的10大秘笈
2010-02-01 10:12 901[新闻资讯] 提高Flex程序性能的10个秘诀 F ... -
在Panel中的title中添加导航栏
2010-01-06 15:20 1043在Panel的title中添加容器 作者: admin | 发 ... -
设置PopUp窗口的蒙板样式
2010-01-06 15:11 2101modalTransparency类型: Number CSS ... -
AS代码优化
2009-12-14 17:58 834AS代码优化 不得不承认 ... -
关于项目开发过程中遇到的问题
2009-12-12 17:49 1102最近在做一个游戏编辑器,好久不写后台了,上手遇到了很多问题,总 ... -
关于Flex中remoteObject的属性chanelSet的设置问题
2009-11-26 11:08 1830在Flex项目开发过程中,我使用remoteObject与后台 ... -
11.20 《第二家园》上线内测
2009-11-20 17:35 1186http://game.d2home.com/ydkj/cli ... -
自定义Button 实现Flex Button 描边效果
2009-11-19 17:44 2176package { import fla ... -
flash AS3与javascript相互通信(例子)
2009-11-11 13:27 1532AS3与JavaScript之间的通讯用ExternalI ... -
如何加快FLEX的编译速度
2009-11-06 21:20 1768我们在开发过程中随着项目的不断壮大,经常会碰到编译速度过慢 ...
相关推荐
本文将深入探讨两种主要的内核架构:单体式内核(Monolithic Kernel)与微内核(Microkernel),并对比分析它们的特点、优势与劣势。 ### 单体式内核(Monolithic Kernel) 单体式内核是较为传统的内核设计方式,...
With lots of examples and practical advice, this book takes a holistic view of the topics that system architects and administrators must consider when building, managing, and evolving microservice ...
文中提及的标题为“Monolithic 14 Bit A/D Converter”,意味着我们讨论的是一个14位的单片模数转换器(ADC),并且描述中提到的“high accurate 14bit ADC converter”强调了该转换器的高精度特性。 【知识点解析...
Razavi_Monolithic_Phase-Locked_Loops_and_Clock_Recovery_Circuits 第二部分
"MVVM in Delphi: Architecting and Building Model View ViewModel Applications" 2016 | ISBN-10: 148422213X | 143 pages | PDF, EPUB | 23 MB Dive into the world of MVVM, learn how to build modern ...
### Gigahertz CMOS Monolithic Frequency Synthesizer #### 概述 本文介绍了一种采用标准0.25微米CMOS技术实现的千兆赫兹单片频率合成器。该合成器仅从一个2.5伏特的单一电源消耗55毫瓦,并在整个感兴趣的频率...
One of the biggest challenges developers face is how to convert legacy and monolithic Delphi applications to the MVVM architecture. This book takes you on a step-by-step journey and teaches you how to...
Recent practice in distributed systems has shifted from building and maintaining monolithic applications to breaking monoliths into microservices, but the standardization and best practices for ...
This book will be a reliable resource, and one that will help you to develop your skills and teach you techniques for building reliable microservices in PHP. The book begins with an introduction to ...
Microservices helps in decomposing applications into small services and move away from a single monolithic artifact. It helps in building systems that are scalable, flexible, and high resilient. ...
This means that developers now are faced with the challenge of building build applications that are native to the cloud. For this, they need to be aware of the environment, tools, and resources they'...
A Monolithic CMOS-MEMS 3-Axis Accelerometer With a Low-Noise, Low-Power Dual-Chopper Amplifier A Monolithic CMOS-MEMS 3-Axis Accelerometer With a Low-Noise, Low-Power Dual-Chopper Amplifier
Razavi_Monolithic_Phase-Locked_Loops_and_Clock_Rec