Nel caso venga creata un’applicazione web con Flex (o Flash Builder), è possibile che si renda necessario “passargli” dei parametri a runtime. Questo è possibile inserendo una variabile FlashVars nel codice html e poi richiamandola da Flex/Flash Builder.
Partiamo con l’esempio del codice html. Generando un progetto web, viene automaticamente creato il file html con il seguente codice:
<script type="text/javascript" src="swfobject.js"></script>
<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.parametro = "ciao";
var params = {};
params.quality = "high";
params.bgcolor = "#ffffff";
params.allowscriptaccess = "sameDomain";
params.allowfullscreen = "true";
var attributes = {};
attributes.id = "VP";
attributes.name = "VP";
attributes.align = "middle";
swfobject.embedSWF(
"VP.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>in questo caso il parametro, chiamato proprio “parametro” è definito con la riga:
flashvars.parametro = "ciao";
In alternativa, utilizzando il classico object, si avrebbe:
<noscript>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100%" height="100%" id="VP">
<param name="movie" value="VP.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="parametro=ciao" />
<!--[if !IE]>-->
<object type="application/x-shockwave-flash" data="VP.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="parametro=ciao" />
<!--<![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>
</noscript>dove il parametro è definito nelle righe:
<param name="FlashVars" value="parametro=ciao" />Bene, ora passiamo al codice da inserire in Flex/Flash Builder. Ipotizziamo di avere una casella di testo, chiamata “txt” e di volerci caricare il contenuto di “parametro”, dovremo inserire nell’applicazione il seguente codice:
<?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="init()"
<fx:Script>
<![CDATA[
private function init():void {
// inizio recupero da flashvars
var obj:Object = this.parameters;
txt.text = obj.parametro;
// fine recupero da flashvars
}
]]>
</fx:Script>
<s:TextInput x="183" y="65" name="txt" id="txt"/>
</s:Application>
enjoy