Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > Flex > Flex 2, 3 & 4

Reply
 
Thread Tools Rate Thread Display Modes
Old 05-14-2008, 08:56 PM   #1
bcedergren
Registered User
 
Join Date: Apr 2008
Posts: 23
Default Accessing variable data from Flash swf in Flex

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:
ActionScript 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.
bcedergren is offline   Reply With Quote
Old 05-14-2008, 09:09 PM   #2
tsj4
Senior Member
 
Join Date: May 2005
Location: San Francisco
Posts: 741
Default

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.
tsj4 is offline   Reply With Quote
Old 05-14-2008, 09:42 PM   #3
bcedergren
Registered User
 
Join Date: Apr 2008
Posts: 23
Default

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 is offline   Reply With Quote
Old 05-15-2008, 02:28 PM   #4
bcedergren
Registered User
 
Join Date: Apr 2008
Posts: 23
Default

So if I have the variable outside of the function how do I access it in Flex?
bcedergren is offline   Reply With Quote
Old 05-15-2008, 02:39 PM   #5
saravanan
You have the power
 
saravanan's Avatar
 
Join Date: Jun 2006
Location: Chennai(Now @ Bangalore),India
Posts: 716
Send a message via MSN to saravanan
Default

Load swf file using SWFLoader component in flex and acess using swfloader.content.variable or function
saravanan is offline   Reply With Quote
Old 05-15-2008, 02:54 PM   #6
bcedergren
Registered User
 
Join Date: Apr 2008
Posts: 23
Default

Is this AS3 or MXML?
bcedergren is offline   Reply With Quote
Old 05-15-2008, 02:59 PM   #7
bcedergren
Registered User
 
Join Date: Apr 2008
Posts: 23
Default

If my code in Flash is:

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

How do I reference the output variable in Flex?
bcedergren is offline   Reply With Quote
Old 05-15-2008, 10:54 PM   #8
tsj4
Senior Member
 
Join Date: May 2005
Location: San Francisco
Posts: 741
Default

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.

Last edited by tsj4; 05-15-2008 at 11:34 PM.
tsj4 is offline   Reply With Quote
Old 05-15-2008, 11:35 PM   #9
tsj4
Senior Member
 
Join Date: May 2005
Location: San Francisco
Posts: 741
Default

try this

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>

Last edited by tsj4; 05-15-2008 at 11:52 PM. Reason: cleaning up example
tsj4 is offline   Reply With Quote
Old 05-21-2008, 10:07 PM   #10
bcedergren
Registered User
 
Join Date: Apr 2008
Posts: 23
Default

This works great. Is it possible to send variable data back to the Flash swf?
bcedergren is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
one template, many looks? subquark ActionScript 1.0 (and below) 1166 10-29-2013 06:18 PM
Optimizing Flash Web Sites for Search Engines rockcoastmedia General Chat 19 03-29-2009 09:31 PM
Cairngorm in Flash CS3 (NOT Flex) slashripshred ActionScript 3.0 5 10-24-2008 07:05 PM
Passing variable to flash from ASP page using POST RichiLloyd ActionScript 1.0 (and below) 5 03-23-2003 05:57 PM


All times are GMT. The time now is 11:43 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.