PDA

View Full Version : Accessing variable data from Flash swf in Flex


bcedergren
05-14-2008, 08:56 PM
Okay, I have been all over the net trying to figure this one out. You;d think that it would be easy enough. Here is the issue:

I have an swf generated frim Flash CS3 that outputs a variable.
I need to use that variable within Flex.

I can see the output results in the Flex console but cannot capture that variable to reuse within Flex.

Here is my code:

click_mc.addEventListener(MouseEvent.CLICK, reportClick);

function reportClick(evt:MouseEvent):void
{
for(var i:int = 0; i<5; i++)
{
var output:String = new String;
output = "I have been clicked. i = " + i;
output_txt.text = output;
trace(output);
}
}


I need to use output within Flex.

tsj4
05-14-2008, 09:09 PM
The var output is only in scope of the function which it was created in and the var and value is removed when the function has finished it's job.

Define a var outside of the function and then set the value of that var to the value of output at the same time you invoke the trace statement.

bcedergren
05-14-2008, 09:42 PM
Are referring to this:

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;
output_txt.text = output;
trace(output);
}
}

How do I set up the variable within Flex?

bcedergren
05-15-2008, 02:28 PM
So if I have the variable outside of the function how do I access it in Flex?

saravanan
05-15-2008, 02:39 PM
Load swf file using SWFLoader component in flex and acess using swfloader.content.variable or function

bcedergren
05-15-2008, 02:54 PM
Is this AS3 or MXML?

bcedergren
05-15-2008, 02:59 PM
If my code in Flash is:

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;
output_txt.text = output;
trace(output);
}
}

How do I reference the output variable in Flex?

tsj4
05-15-2008, 10:54 PM
sorry for not getting back. Another issue I see is your trying to pass the value of output to a text field which was created in your flex app (output_txt). I am not sure if flash will even allow the fla to compile since you are referencing an object which was never created.

Seems you would need to include an eventlistener to the swfloader object and then check the value of output from within the flex app.

tsj4
05-15-2008, 11:35 PM
try this

Flash cs3 as3 file "click.swf"

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


<?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>

bcedergren
05-21-2008, 10:07 PM
This works great. Is it possible to send variable data back to the Flash swf?