frames.frame label class_name [...]  编译器指令

以下分析都从flex3源代码推出,不一定正确!希望能和有兴趣的朋友一起探讨。

Flex 3有一个编译器指令是:frames.frame label class_name [...]       
引用内容 引用内容

Specifies a SWF file frame label with a sequence of class names that are linked onto the frame.

This option lets you add asset factories that stream in after the application that then publish their interfaces with the ModuleManager class. 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.

This is an advanced option.

上面的文本摘自flex builder 3 帮助文档的  Application Development / Using the Flex Compilers / Using mxmlc, the application compiler 一节。

下面介绍一下这个指令的用法:
建立一个工程,主文件名是 FrameTest.mxml,具体代码如下:
程序代码 程序代码

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
        <![CDATA[
        
            import flash.system.ApplicationDomain;
            
            private function loadImageHandler():void
            {
                var c:Class;
                if (ApplicationDomain.currentDomain.hasDefinition("SunnyImage"))
                {
                    c = Class(ApplicationDomain.currentDomain.getDefinition("SunnyImage"));
                    sunny.source = c;
                }
                else
                {
                    // ...............
                }
                
                                
                if (ApplicationDomain.currentDomain.hasDefinition("RainyImage"))
                {
                    c = Class(ApplicationDomain.currentDomain.getDefinition("RainyImage"));
                    rainy.source = c;
                }
                else
                {
                    // ...............
                }
            
            }
            
        ]]>
    </mx:Script>
    <mx:Image id="sunny" x="28" y="28"/>
    <mx:Image id="rainy" x="130" y="28"/>
    
    <mx:Button x="67" y="99" label="Load Images" click="loadImageHandler()"/>
    
</mx:Application>


建立一个文件夹assets(与FrameTest.mxml文件在同一个文件夹里),在assets目录里面放两张图片rainy.png和sunny.png。另外建立两个类(这两个类与FrameTest.mxml文件在同一个文件夹里)
1,SunnyImage.as
程序代码 程序代码

package
{
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import mx.core.BitmapAsset;
    
    [Embed(source="assets/sunny.png")]
    
    
    // this calss is linked onto the 3nd frame of systemManager.
    public class SunnyImage extends mx.core.BitmapAsset
    {
        //this method called by systemManager when systemManager's currentFrame == 3
        public static function frame(root:DisplayObject):void
        {
            if (root is MovieClip)
            {
                trace("currentFrame: " + MovieClip(root).currentFrame);
            }
        }
        
        public function SunnyImage()
        {
            super();
        }
    
    }

}

2,RainyImage.as
程序代码 程序代码

package
{
    import flash.display.DisplayObject;
    import flash.display.MovieClip;    
    import mx.core.BitmapAsset;
    
    [Embed(source="assets/rainy.png")]
    
    
    // this calss is linked onto the 4th frame of systemManager.
    public class RainyImage extends mx.core.BitmapAsset
    {
        
        //this method called by systemManager when systemManager's currentFrame == 4
        public static function frame(root:DisplayObject):void
        {
            if (root is MovieClip)
            {
                trace("currentFrame: " + MovieClip(root).currentFrame);
            }
        }
        
        public function RainyImage()
        {
            super();
        }
    
    }

}

建立一个配置文件FrameTest-config.xml(与FrameTest.mxml文件在同一个文件夹里),内容如下:
程序代码 程序代码

<?xml version="1.0"?>  
  
   <flex-config>
    
       <frames>
            <frame>
                <label>Three</label>
                <classname>SunnyImage</classname>
            </frame>
            <frame>
                <label>Four</label>
                <classname>RainyImage</classname>
            </frame>
        </frames>
    </flex-config>

编译,你会发现在控制台输出
currentFrame: 3
currentFrame: 4

在输出的swf上点击 "Load Images"按钮,图片出现。

发散思维1:
如果你的程序里面嵌入了其他的资源,可以把资源编译进一个类,然后让这个类链接到systemManager的第二桢之后,这样你的程序的启动时间会变快。这是这个指令的一个用法,还不错吧:)

下载文件 下载这篇文章的工程源代码

此文禁止转载,但是您可以以超级链接的方式链接到这篇文章。


[本日志由 Admin 于 2008-09-23 09:49 AM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
评论: 4 | 引用: 0 | 查看次数: 2762
回复回复skylark14[2008-12-23 04:58 PM | del]
非常感谢你提供源文件!
但在你主文件里加上<mx:DropShadowFilter id="shdow_effect" angle="45" blurX="5" blurY="5" color="0" />这行,编译就通不过了。
回复回复ray[2008-11-19 05:52 AM | del]
谢谢分享 让我想到creationPolicy
回复回复ray[2008-11-03 03:22 AM | del]
发散思维1:
如果你的程序里面嵌入了其他的资源,可以把资源编译进一个类,然后让这个类链接到systemManager的第二桢之后,这样你的程序的启动时间会变快。这是这个指令的一个用法,还不错吧:)
为什么程序的启动时间会变快? preloader会加载第1帧后的所有内容吧?

[reply=Admin,2008-11-03 09:40 AM]
flex里面的preloader会在第二桢加载完成后就认为整个swf加载完毕(具体可以看 mx.preloaders.reloader类文件的 private function timerHandler(event:TimerEvent):void函数中的386行),然后执行flex初始化。
回复回复Ethan[2008-09-22 05:04 PM | del]
不错,有机会试试
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭