<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FlashCanon &#187; Adobe Flex</title>
	<atom:link href="http://flash.fincanon.com/archives/tag/adobe-flex/feed" rel="self" type="application/rss+xml" />
	<link>http://flash.fincanon.com</link>
	<description>Flash Platform stuff from Jason Fincanon</description>
	<lastBuildDate>Fri, 13 Jan 2012 18:22:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Making Flash and Flex talk (Part 2)</title>
		<link>http://flash.fincanon.com/archives/152</link>
		<comments>http://flash.fincanon.com/archives/152#comments</comments>
		<pubDate>Tue, 30 Sep 2008 16:14:10 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Adobe Flex]]></category>

		<guid isPermaLink="false">http://flash.fincanon.com/?p=152</guid>
		<description><![CDATA[As promised in my previous post, here's part 2 of Making Flash and Flex talk. This post is going to be a bit shorter than part 1 since a lot of the code and information is the same. You'll see in the example that the user experience is the same in both this post and [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://flash.fincanon.com/wp-content/uploads/2008/10/fftalk.jpg" alt="" title="FFTalkEvent" width="200" height="141" class="alignleft size-full wp-image-153" />As promised in my <a href="http://flash.fincanon.com/archives/151">previous post</a>, here's part 2 of Making Flash and Flex talk. This post is going to be a bit shorter than <a href="http://flash.fincanon.com/archives/151">part 1</a> since a lot of the code and information is the same. You'll see in the example that the user experience is the same in both this post and <a href="http://flash.fincanon.com/archives/151">part 1</a>. However, by using listeners and a custom event in this example, we lessen the chance for error that could be caused by calling a function directly by name in a child (parent) file.</p>
<p>This example involves a total of 4 files: FF_Talk.mxml, FFTalkEvent.as, ffTalkSwf.swf and, of course ffTalkSwf.fla.<br />
You can view the source code either <a href="/tutorials/FF_Talk/srcview/index.html" target="_blank">here</a> or by right-clicking on the example and clicking "View Source". You can also grab the zip containing these files via the download link at the end of this post.</p>
<p><strong>THE CODE:</strong><br />
Let's take a look at the <a href="/tutorials/FF_Talk/srcview/index.html" target="_blank">source</a> and break it down just a little.</p>
<p><strong>FLEX SIDE:</strong><br />
As in <a href="http://flash.fincanon.com/archives/151">part 1</a>, I start by declaring the variables <em>flashSaid</em> and <em>mySwfMc</em>. This time however, I also import a new class file named <em>FFTalkEvent</em> which will serve as the means of communication between Flex and Flash in this case. More about <em>FFTalkEvent</em> below.</p>
<div class="igBar"><span id="lactionscript-6"><a href="#" onclick="javascript:showPlainTxt('actionscript-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">Actionscript:</span>
<div id="actionscript-6">
<div class="actionscript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">import</span> com.<span style="color: #006600;">fincanon</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">FFTalkEvent</span>;</div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> flashSaid:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">""</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> mySwfMc:<span style="color: #0066CC;">MovieClip</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Also just like in <a href="http://flash.fincanon.com/archives/151">part 1</a>, the next thing in the mxml file is the <em>setSwfMc</em> function. This function still casts the content of the SWFLoader as a MovieClip but now it adds an event listener to both the loaded swf and its Flex parent (instead of sending a reference of the Flex parent into the swf).</p>
<div class="igBar"><span id="lactionscript-7"><a href="#" onclick="javascript:showPlainTxt('actionscript-7'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">Actionscript:</span>
<div id="actionscript-7">
<div class="actionscript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setSwfMc<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; mySwfMc = mySWFLoader.<span style="color: #006600;">content</span> as <span style="color: #0066CC;">MovieClip</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; mySwfMc.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>FFTalkEvent.<span style="color: #006600;">TALK_TO_FLEX</span>,listenToFlash<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>FFTalkEvent.<span style="color: #006600;">TALK_TO_FLASH</span>,mySwfMc.<span style="color: #006600;">listenToFlex</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Next are the functions <em>listenToFlash</em> and <em>talkToFlash</em> (you'll also recognize these from <a href="http://flash.fincanon.com/archives/151">part 1</a>). The big difference this time is that we're either listening for or dispatching a new <em>FFTalkEvent</em> instead of making the more "dangerous" move of calling a function directly by name. By NOT calling a function directly by name, some of the possibility for error is removed. In <a href="http://flash.fincanon.com/archives/151">part 1</a>, I was calling function in the child swf... but what if that function wasn't there? The results of that call could end up bringing the user's experience to a grinding halt.</p>
<div class="igBar"><span id="lactionscript-8"><a href="#" onclick="javascript:showPlainTxt('actionscript-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">Actionscript:</span>
<div id="actionscript-8">
<div class="actionscript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> listenToFlash<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:FFTalkEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; flashSaid = <span style="color: #0066CC;">e</span>.<span style="color: #006600;">said</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> talkToFlash<span style="color: #66cc66;">&#40;</span>stringToPass:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> FFTalkEvent<span style="color: #66cc66;">&#40;</span>FFTalkEvent.<span style="color: #006600;">TALK_TO_FLASH</span>,<span style="color: #000000; font-weight: bold;">false</span>,<span style="color: #000000; font-weight: bold;">false</span>,stringToPass<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>As you can see in the <a href="/tutorials/FF_Talk/srcview/index.html" target="_blank">source</a>, the rest of the mxml file is simply the text fields, labels, buttons, SWFLoader, etc.</p>
<p><strong>FLASH SIDE:</strong><br />
Again, as in <a href="http://flash.fincanon.com/archives/151">part 1</a>, the code in the child swf is not that far off from the code in the parent mxml. Also, the main difference between this version and the version in <a href="http://flash.fincanon.com/archives/151">Part 1</a> is the fact that we're importing <em>FFTalkEvent</em> and we're listening for or dispatching new instances of <em>FFTalkEvent</em>.</p>
<div class="igBar"><span id="lactionscript-9"><a href="#" onclick="javascript:showPlainTxt('actionscript-9'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">Actionscript:</span>
<div id="actionscript-9">
<div class="actionscript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">import</span> com.<span style="color: #006600;">fincanon</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">FFTalkEvent</span>;</div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">function</span> talkToFlex<span style="color: #66cc66;">&#40;</span>me:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> FFTalkEvent<span style="color: #66cc66;">&#40;</span>FFTalkEvent.<span style="color: #006600;">TALK_TO_FLEX</span>, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #000000; font-weight: bold;">false</span>, flashInputTxt.<span style="color: #0066CC;">text</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">function</span> listenToFlex<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:FFTalkEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; flexSaidTxt.<span style="color: #0066CC;">text</span> = <span style="color: #0066CC;">e</span>.<span style="color: #006600;">said</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">talkToFlexBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,talkToFlex<span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>FFTalkEvent:</strong><br />
<em>FFTalkEvent</em> in its current state is a simple, straight forward, bare-bones class that extends <em>Event</em>. It contains a couple of constants (<em>TALK_TO_FLEX</em> and <em>TALK_TO_FLASH</em>) and a String var named <em>said</em>. If you notice above, we call the <em>said</em> variable to get the value that was passed from Flex to Flash (or vice-versa).</p>
<div class="igBar"><span id="lactionscript-10"><a href="#" onclick="javascript:showPlainTxt('actionscript-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">Actionscript:</span>
<div id="actionscript-10">
<div class="actionscript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">package com.<span style="color: #006600;">fincanon</span>.<span style="color: #006600;">events</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FFTalkEvent <span style="color: #0066CC;">extends</span> Event<span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const TALK_TO_FLEX:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">"TalkToFlex"</span>;</div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const TALK_TO_FLASH:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">"TalkToFlash"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> said:<span style="color: #0066CC;">String</span>;</div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FFTalkEvent<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>, bubbles:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">false</span>, cancelable:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">false</span>, sentString:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">""</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">said</span> = sentString;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>, bubbles, cancelable<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>THE EXAMPLE:</strong><br />

<object width="475" height="400">
<param name="movie" value="/tutorials/FF_Talk/FF_Talk.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="window"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#FFFFFF"></param>
<embed type="application/x-shockwave-flash" width="475" height="400" src="/tutorials/FF_Talk/FF_Talk.swf" quality="high" bgcolor="#FFFFFF" wmode="window" menu="false" ></embed>
</object>
</p>
<p><strong>WHERE TO GO FROM HERE:</strong><br />
Like I said, <em>FFTalkEvent</em> in its current state is a simple, straight forward, bare-bones class. While this example is simply passing a string, you can see where it could very easily be built upon to handle much more complex tasks and I'll most likely take the time to do that in down time between projects. Whether or not I post future versions may depend on how much feedback/interest I see with this version.</p>
<p><strong>DOWNLOAD THE ZIP:</strong><br />
Source files <a href="/tutorials/FF_Talk/srcview/FF_Talk.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://flash.fincanon.com/archives/152/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Making Flash and Flex talk (Part 1)</title>
		<link>http://flash.fincanon.com/archives/151</link>
		<comments>http://flash.fincanon.com/archives/151#comments</comments>
		<pubDate>Mon, 29 Sep 2008 18:38:09 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Adobe Flex]]></category>

		<guid isPermaLink="false">http://flash.fincanon.com/?p=151</guid>
		<description><![CDATA[I recently ran into some issues making Flex talk to a loaded swf while also allowing that loaded swf to talk to its Flex parent. I had done it once before on a prior project but since it was a one-time deal, I had forgotten exactly what I did to accomplish the task. I knew [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://flash.fincanon.com/wp-content/uploads/2008/10/fftalk.jpg" alt="" title="FFTalkEvent" width="200" height="141" class="alignleft size-full wp-image-153" />I recently ran into some issues making Flex talk to a loaded swf while also allowing that loaded swf to talk to its Flex parent. I had done it once before on a prior project but since it was a one-time deal, I had forgotten exactly what I did to accomplish the task. I knew it had to do with casting the SWFLoader.content as a MovieClip, but I still needed to go digging back to the old project to make sure I had all of the correct steps. That was when problem #2 showed up... I couldn't remember which project I did this in. So, as the next logical step, me and a friend started doing some research online and found less than satisfactory results/answers to our questions.This lead me to feeling the need to write this post not only for my own future reference but also in hopes that it may show up in someone else's search results and help them in the future.</p>
<p>This example involves a total of 3 files: FF_Talk_Simple.mxml, ffTalkSimpleSwf.swf and, of course ffTalkSimpleSwf.fla.<br />
You can view the source code either <a href="/tutorials/FF_Talk_Simple/srcview/index.html" target="_blank">here</a> or by right-clicking on the example and clicking "View Source". You can also grab the zip containing these files via the download link at the end of this post.</p>
<p><strong>THE CODE:</strong><br />
Let's take a look at the <a href="/tutorials/FF_Talk_Simple/srcview/index.html" target="_blank">source</a> and break it down just a little.</p>
<p><strong>FLEX SIDE:</strong><br />
In the mxml file, I start by declaring a couple of variables (<em>flashSaid</em> and <em>mySwfMc</em>).<br />
<em>flashSaid</em> will be used to populate a dynamic text field so you can see that your string has actually made it from Flash to Flex and <em>mySwfMc</em> will be used as a reference to your loaded swf child.</p>
<div class="igBar"><span id="lactionscript-15"><a href="#" onclick="javascript:showPlainTxt('actionscript-15'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">Actionscript:</span>
<div id="actionscript-15">
<div class="actionscript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> flashSaid:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">""</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> mySwfMc:<span style="color: #0066CC;">MovieClip</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Next, is a function named <em>setSwfMc</em>. This function is called from the SWFLoader when your child swf has finished loading. Once called, <em>setSwfMc</em> does two things. First, it casts the content of the SWFLoader as a MovieClip (this is required in order to access everything inside the child swf). Next, it passes a reference to the Flex parent into the child swf (this will be covered in the Flash code below).</p>
<div class="igBar"><span id="lactionscript-16"><a href="#" onclick="javascript:showPlainTxt('actionscript-16'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">Actionscript:</span>
<div id="actionscript-16">
<div class="actionscript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setSwfMc<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; mySwfMc = mySWFLoader.<span style="color: #006600;">content</span> as <span style="color: #0066CC;">MovieClip</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; mySwfMc.<span style="color: #006600;">myFlexParent</span> = <span style="color: #0066CC;">this</span>;</div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The other two functions in the mxml file handle the communication on this end of the conversation. They are named <em>listenToFlash</em> and <em>talkToFlash</em> and you can probably guess what they do pretty quickly. <em>listenToFlash</em> will be called from the child swf and it is expecting a to recieve a string. Once called, it takes that string and assigns it as the value of the <em>flashSaid</em> variable mentioned earlier. Since that variable is [Bindable], the text field is updated and you know it worked. The other function here, <em>talkToFlash</em>, is very similar but it is sending instead of receiving. Within <em>talkToFlash</em>, we are calling a function named <em>listenToFlex</em> that resides in the child swf. Much like the <em>listenToFlash</em> functon, <em>listenToFlex</em> is expecting a string to be passed.</p>
<div class="igBar"><span id="lactionscript-17"><a href="#" onclick="javascript:showPlainTxt('actionscript-17'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">Actionscript:</span>
<div id="actionscript-17">
<div class="actionscript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> listenToFlash<span style="color: #66cc66;">&#40;</span>stringToShow:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; flashSaid = stringToShow;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> talkToFlash<span style="color: #66cc66;">&#40;</span>stringToPass:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; mySwfMc.<span style="color: #006600;">listenToFlex</span><span style="color: #66cc66;">&#40;</span>stringToPass<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>FLASH SIDE:</strong><br />
The code in the child swf is not that far off from the code in the parent mxml.<br />
If you recall from above, we passed a reference to the Flex parent into the swf and assigned it as the value of a variable named <em>myFlexParent</em>. So it stands to reason that we need to declare such a variable and that's the first thing that happens in the swf. Next, we do just as we did in the mxml file and set up the conversation functions (<em>talkToFlex</em> and <em>listenToFlex</em>). Since these do pretty much the same thing as their counterparts in the mxml, I'll save you the trouble of reading it twice (and me the trouble of typing it twice). The last thing you see in the code below is just the listener for the button to talk to flex.</p>
<div class="igBar"><span id="lactionscript-18"><a href="#" onclick="javascript:showPlainTxt('actionscript-18'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">Actionscript:</span>
<div id="actionscript-18">
<div class="actionscript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span> myFlexParent:<span style="color: #0066CC;">Object</span>;</div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">function</span> talkToFlex<span style="color: #66cc66;">&#40;</span>me:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; myFlexParent.<span style="color: #006600;">listenToFlash</span><span style="color: #66cc66;">&#40;</span>flashInputTxt.<span style="color: #0066CC;">text</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">function</span> listenToFlex<span style="color: #66cc66;">&#40;</span>stringToShow:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; flexSaidTxt.<span style="color: #0066CC;">text</span> = stringToShow;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#666666;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#000000;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">talkToFlexBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,talkToFlex<span style="color: #66cc66;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>THE EXAMPLE:</strong><br />

<object width="475" height="400">
<param name="movie" value="/tutorials/FF_Talk_Simple/FF_Talk_Simple.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="window"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#FFFFFF"></param>
<embed type="application/x-shockwave-flash" width="475" height="400" src="/tutorials/FF_Talk_Simple/FF_Talk_Simple.swf" quality="high" bgcolor="#FFFFFF" wmode="window" menu="false" ></embed>
</object>
</p>
<p><strong>UP NEXT:</strong><br />
<a href="http://flash.fincanon.com/archives/152">Part 2</a> of this topic will be very similar to this post except that I will be implementing listeners and a custom event to communicate between Flex and its loaded swf child. I'll try to have that up today or tomorrow.</p>
<p><strong>DOWNLOAD THE ZIP:</strong><br />
Source files <a href="/tutorials/FF_Talk_Simple/srcview/FF_Talk_Simple.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://flash.fincanon.com/archives/151/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

