actionscript 3 runtime variables

code :: setting them with php

the key to creating dynamic systems on the web is the ability to send and receive data from one component to the next. flash and flex are no different. there are a lot of cases where you need to send these variables while the application is running, but there are an equal amount of times where the application needs data at run time. passing variables to flash historically has been rather easy, you just query-string data right into the src tag of your flash movie:

src="test.swf?passed=hello"

by doing this flash would create a new variable (in this case named passed) when the flash movie is loaded. but with the advent of OOP coding in AS3, this technique has become depreciated. 

in actionscript 3 you need to use the flashvars parameter to send data to your flash movie. the technique is the same for both flash and flex, but once the variable is passed flash and flex has a different syntax to access these vars. here is a simple php script that displays a text box, for data input then creates a swf wrapper that embeds your variable into the swf. ( look for <?php echo($getit); ?> )

<html>
<head>
<title>flex runtime vars</title>
<?php
$getit = $_REQUEST["what"];
if(!$getit){
?>
</head><body bgcolor="#000000">
<form method="GET" action="index.php">
<input type="text" id="what" name="what" style="COLOR: #cccccc;BACKGROUND-COLOR:#000000" /><br />
<input type="submit" value="submit" style='COLOR: #cccccc; BACKGROUND-COLOR: #282828' />
</form>
<?php
} else {
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="deeplinking/deeplinking.css" />
<script src="AC_OETags.js" language="javascript"></script>
<script src="deeplinking/deeplinking.js" language="javascript"></script>
<style>
body { margin: 0px; overflow:hidden }
</style>
<script language="JavaScript" type="text/javascript">
var requiredMajorVersion = 9;
var requiredMinorVersion = 0;
var requiredRevision = 28;
</script>
<body scroll="no" bgcolor="#000000">
<script language="JavaScript" type="text/javascript">
<!--
var hasProductInstall = DetectFlashVer(6, 0, 65);
var hasRequestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
if ( hasProductInstall && !hasRequestedVersion ) {
	var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
	var MMredirectURL = window.location;
    document.title = document.title.slice(0, 47) + " - Flash Player Installation";
    var MMdoctitle = document.title;
	AC_FL_RunContent(
		"src", "playerProductInstall",
		"FlashVars", "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+MMdoctitle+"",
		"width", "100%",
		"height", "100%",
		"align", "middle",
		"id", "test",
		"quality", "high",
		"bgcolor", "#869ca7",
		"name", "test",
		"allowScriptAccess","sameDomain",
		"type", "application/x-shockwave-flash",
		"pluginspage", "http://www.adobe.com/go/getflashplayer"
	);
} else if (hasRequestedVersion) {
	AC_FL_RunContent(
			"src", "test",
			"width", "100%",
			"height", "100%",
			"align", "middle",
			"id", "test",
			"quality", "high",
			"bgcolor", "#869ca7",
			"name", "test",
			"allowScriptAccess","sameDomain",
			"type", "application/x-shockwave-flash",
			"flashvars",'passed=<?php echo($getit); ?>',
			"pluginspage", "http://www.adobe.com/go/getflashplayer"
	);
  } else {
    var alternateContent = 'Alternate HTML content should be placed here. '
  	+ 'This content requires the Adobe Flash Player. '
   	+ '<a href=http://www.adobe.com/go/getflash/>Get Flash</a>';
    document.write(alternateContent);
  }
// -->
</script>
<noscript>
  	<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="test" width="100%" height="100%"
			codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
			<param name="movie" value="test.swf" />
			<param name="quality" value="high" />
			<param name="bgcolor" value="#869ca7" />
			<param name="flashvars" value="passed=<?php echo($getit); ?>" />
			<param name="allowScriptAccess" value="sameDomain" />
			<embed src="test.swf" quality="high" bgcolor="#869ca7"
				width="100%" height="100%" name="test" align="middle"
				flashvars="passed=<?php echo($getit); ?>"
				play="true"
				loop="false"
				quality="high"
				allowScriptAccess="sameDomain"
				type="application/x-shockwave-flash"
				pluginspage="http://www.adobe.com/go/getflashplayer">
			</embed>
	</object>
</noscript>
<?php
}
?>
</body>
</html>

to access the "passed" var is flex use:
Application.application.parameters.passed;

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
	xmlns:mx="http://www.adobe.com/2006/mxml" 
	layout="absolute" 
	backgroundGradientAlphas="[1.0, 1.0]" 
	backgroundGradientColors="[#000000, #252525]"
	creationComplete="init()">
<mx:Script>
	<![CDATA[
		public var passedvar:String;
		private function init():void { 
			//passed is the name of the flash var
			passedvar = Application.application.parameters.passed;
			thething.htmlText = passedvar.toString();
		}
	]]>
</mx:Script>
	<mx:Label id="thething" text="Label" color="#FFFFFF" fontSize="23" horizontalCenter="0" verticalCenter="0" />
</mx:Application>

to access the "passed" var is flash use:
LoaderInfo(this.root.loaderInfo).parameters;

var tf:TextField = new TextField();
tf.textColor = 0xFFFFFF;
addChild(tf);
try {
    var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
	//passed is the name of the flash var
	tf.appendText("passed var: " + paramObj['passed'].toString());
} catch (error:Error) {
    tf.appendText("error: " + error.toString());
}