最近在做一个flex 的开发,需要用到从html向flex编译出来的swf文件传递一个参数。因为没有做个相关的工作,所以就找各种帖子来看看。
以下是网上提供的大多数方法。 ?1.使用Application.application.parameters,结合flashvars进行传参。2,使用LoaderInfo(this.root.loaderInfo).parameters
不过在我的开发中都是报错,之后查了一些资料,发现LoaderInfo(this.root.loaderInfo).parameters是使用在as开发成的swf中使用,不适合使用在flex项目中。Application.application.parameters的传参方法在flex4.0之后的版本已经被抛弃了,所以不能直接使用。
flex4.0之后提供的方法是使用FlexGlobals.topLevelApplication.parameters ,直接使用可能会报错,是因为需要导入import mx.core.FlexGlobals;,这样就正确了!
以下是一个小例子:
html中:
?<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 = {};
flashvars.soundUrl = "img/music.mp3" ? ? ? ? ? ? ? ?//这个就是需要传参给swf的参数
? ? ? ? ? ? var params = {};
? ? ? ? ? ? params.quality = "high";
? ? ? ? ? ? params.bgcolor = "#b9fbf9";
? ? ? ? ? ? params.allowscriptaccess = "sameDomain";
? ? ? ? ? ? params.allowfullscreen = "true";
? ? ? ? ? ? var attributes = {};
? ? ? ? ??
? ? ? ? ? ? swfobject.embedSWF(
? ? ? ? ? ? ? ? "****.swf","flashContent",?
? ? ? ? ? ? ? ? "470","90",?
? ? ? ? ? ? ? ? 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>
flex中?
var soundUrl:String =FlexGlobals.topLevelApplication.parameters.soundUrl;?
这样就可以在flex 中引用外部参数了····
ps:菜鸟的学习,欢迎大侠拍砖指点哈!