Flex4中將網頁的參數傳遞給Flash,並且在Flash中讀取這些參數,以及flash接收網頁傳過來的值

使用flashvars將參數通過網頁傳遞給Flash

使用Flash Builder 4創建Flex應用,會自動生成網頁調用Flash,會有兩種方式顯示Flash,下面針對這兩個方式看看如何添加參數。

方式一(swfobject):

<script type="text/javascript">
            <!-- For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection. --> 
            var swfVersionStr = "10.0.0";
            <!-- To use express install, set to playerProductInstall.swf, otherwise the empty string. -->
            var xiSwfUrlStr = "playerProductInstall.swf";
            <span style="color: #000000;">
<span style="white-space:pre">		</span>var flashvars = {var1:"abc",var2:"def"};
<span style="white-space:pre">	</span>    </span>            
<span style="white-space:pre">		</span>var params = {};
<span style="white-space:pre">		</span>params.quality = "high";
<span style="white-space:pre">		</span>params.bgcolor = "#ffffff";
<span style="white-space:pre">		</span>params.allowscriptaccess = "sameDomain";
            params.allowfullscreen = "true";
            var attributes = {};
            attributes.id = "test39";
            attributes.name = "test39";
            attributes.align = "middle";
            swfobject.embedSWF(
                "test39.swf", "flashContent", 
                "100%", "100%", 
                swfVersionStr, xiSwfUrlStr, 
                flashvars, params, attributes);
            <!-- JavaScript enabled so display the flashContent div in case it is not replaced with a swf object. -->
            swfobject.createCSS("#flashContent", "display:block;text-align:left;");
        </script>

方式二(html):

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100%" height="100%" id="test39">
                <param name="movie" value="demo1.swf" />
                <param name="quality" value="high" />
                <param name="bgcolor" value="#ffffff" />
                <param name="allowScriptAccess" value="sameDomain" />
                <param name="allowFullScreen" value="true" />
                <span style="color: #000000;">
<span style="white-space:pre">			</span><param name="flashvars" value="var1=aabb&var2=ccdd" />
<span style="white-space:pre">			</span></span>               
<span style="white-space:pre">			</span> <!--[if !IE]>-->
                <object type="application/x-shockwave-flash" data="demo1.swf" width="100%" height="100%">
                    <param name="quality" value="high" />
                    <param name="bgcolor" value="#ffffff" />
                    <param name="allowScriptAccess" value="sameDomain" />
                    <param name="allowFullScreen" value="true" />
                    <param name="flashvars" value="var1=aabb&var2=ccdd" />
                <!--<![endif]-->
                <!--[if gte IE 6]>-->
                    <p> 
                        Either scripts and active content are not permitted to run or Adobe Flash Player version
                        10.0.0 or greater is not installed.
                    </p>
                <!--<![endif]-->
                    <a href="http://www.adobe.com/go/getflashplayer">
                        <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash Player" />
                    </a>
                <!--[if !IE]>-->
                </object>
                <!--<![endif]-->
            </object>

在Flash中接收網頁傳遞過來的參數

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" 
<span style="white-space:pre">			</span>creationComplete="application1_creationCompleteHandler(event)" pageTitle="abc">
    <s:layout>
        <s:BasicLayout/>
    </s:layout>
    <fx:Script>
        <![CDATA[
            import mx.core.FlexGlobals;
            import mx.events.FlexEvent;
 
            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                <span style="color: #000000;">var para:Object = FlexGlobals.topLevelApplication.parameters;
</span>             text1.text = para.var1;
                text2.text = para.var2;
            }
 
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- 將非可視元素(例如服務、值對象)放在此處 -->
    </fx:Declarations>
    <s:TextInput id="text1" x="190" y="93"/>
    <s:TextInput id="text2" x="190" y="142"/>
    <s:Label x="147" y="94" text="var1:"/>
    <s:Label x="147" y="142" text="var2:"/>
</s:Application>

參數在Flex4的獲取方式跟Flex3是不一樣的:

Flex3:  var params:Object = Application.application.parameters; 

Flex4:  var params:Object = FlexGlobals.topLevelApplication.parameters;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章