Monday, May 11, 2009

Accessing swf vars from Flex

Ever need your memory jogged on the topic of putting swf vars into your Flex app? Here's just the thing for that:

Flash cs3 as3 file "click.swf"

ActionScript Code:
click_mc.addEventListener(MouseEvent.CLICK, reportClick);
var output:String = new String();

function reportClick(evt:MouseEvent):void
{
for(var i:int = 0; i<5; i++) {
output += "I have been clicked. i = " + i + "\n";
}
}

MXML Flex 2.0
ActionScript Code:
<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

<mx:Script>
<![CDATA[
private function updateTextField():void
{
output_txt.text += swf.content["output"];
}
]]>
</mx:Script>

<mx:SWFLoader x="10" y="10" source="click.swf" scaleContent="false" id="swf" click="updateTextField()"/>

<mx:TextArea x="10" y="518" width="550" height="211" id="output_txt"/>

</mx:Application>

Made for Flex 2 but perfectly applicable to 3, 4 and perhaps beyond... Thanks to the forums at actionscript.org for this one.

No comments: