Flex4中将网页的参数传递给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";
            var flashvars = {var1:"abc",var2:"def"};
            var params = {};
            params.quality = "high";
            params.bgcolor = "#ffffff";
            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" />
                <param name="flashvars" value="var1=aabb&var2=ccdd" />
                <!--[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" 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
			{
				var para:Object = FlexGlobals.topLevelApplication.parameters;
				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; 

posted @ 2010-12-06 16:14  魔豆  阅读(5179)  评论(0编辑  收藏  举报