<?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>Stories about everyday life &#187; as3</title>
	<atom:link href="http://www.hariscusto.com/tag/as3/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hariscusto.com</link>
	<description>my playground</description>
	<lastBuildDate>Wed, 14 Dec 2011 23:10:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Communication between JavaScript and Actionscript (AS3) and vice versa</title>
		<link>http://www.hariscusto.com/programming/communication-between-javascript-and-actionscript-as3-and-vice-versa/</link>
		<comments>http://www.hariscusto.com/programming/communication-between-javascript-and-actionscript-as3-and-vice-versa/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 14:52:16 +0000</pubDate>
		<dc:creator>Haris Čusto</dc:creator>
				<category><![CDATA[Action Script]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[communication]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://www.hariscusto.com/?p=155</guid>
		<description><![CDATA[I haven&#8217;t used flash for a while, and today while creating little news slider in flash which communicate with Javascript (and vice versa), i lost 30 min to find the best way to communicate between Flash and Javascript. So to save you a lot of time here is simple description. To Call Javascript function from [...]]]></description>
			<content:encoded><![CDATA[<p>I haven&#8217;t used flash for a while, and today while creating little news slider in flash which communicate with Javascript (and vice versa), i lost 30 min to find the best way to communicate between Flash and Javascript. So to save you a lot of time here is simple description.<br />
<span id="more-155"></span><br />
To Call Javascript function from Flash: </p>
<pre class="brush: csharp; title: ; notranslate">import flash.external.ExternalInterface;
...
ExternalInterface.call(&quot;my_js_function()&quot;);
</pre>
<p>To get a return value:</p>
<pre class="brush: csharp; title: ; notranslate">
var el_width:int = ExternalInterface.call(&quot;get_width()&quot;);
</pre>
<p>To pass an argument try:</p>
<pre class="brush: csharp; title: ; notranslate">
var el_width:int = ExternalInterface.call(&quot;get_width()&quot;, &quot;my_el_id&quot;);
</pre>
<p>And that&#8217;s it <img src='http://www.hariscusto.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
<p>Communication with flash from javascript is a little bit harder, but if you follow these steps it sholud work.<br />
To Communicate with Flash from Javascript you need to adjust 3 different thing HTML, JS, AS.  </p>
<p>First HTML</p>
<p>The <object> tag must have an id.  For example, <object id="myFlashMovie" .....></object></p>
<p>The <embed> tag must be inside object and it must have a name attribute &#8211; name=&#8221;myFlashMovie&#8221;</p>
<p>so you will get something like this</p>
<pre class="brush: xml; title: ; notranslate">
&lt;object height=&quot;250&quot; width=&quot;380&quot; id=&quot;myFlashMovie&quot; codebase=&quot;http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8&quot; classid=&quot;clsid:D27CDB6E-AE6D-11cf-96B8-444553540000&quot;&gt;
&lt;param value=&quot;myflash.swf&quot; name=&quot;movie&quot;&gt;
&lt;param value=&quot;high&quot; name=&quot;quality&quot;&gt;
&lt;param value=&quot;transparent&quot; name=&quot;wmode&quot;&gt;
&lt;param value=&quot;#FFFFFF&quot; name=&quot;bgcolor&quot;&gt;
&lt;param value=&quot;true&quot; name=&quot;allowFullScreen&quot;&gt;
&lt;param value=&quot;true&quot; name=&quot;swLiveConnect&quot;&gt;
&lt;param value=&quot;always&quot; name=&quot;allowScriptAccess&quot;&gt;

    &lt;embed height=&quot;250&quot; align=&quot;middle&quot; width=&quot;380&quot; src=&quot;myflash.swf&quot; quality=&quot;high&quot; pluginspage=&quot;http://www.macromedia.com/go/getflashplayer&quot; play=&quot;true&quot; loop=&quot;true&quot; scale=&quot;showall&quot; wmode=&quot;transparent&quot; devicefont=&quot;false&quot; bgcolor=&quot;#000000&quot; menu=&quot;true&quot; allowfullscreen=&quot;false&quot; allowscriptaccess=&quot;always&quot; salign=&quot;&quot; type=&quot;application/x-shockwave-flash&quot; name=&quot;myFlashMovie&quot;&gt;
&lt;/object&gt;
</pre>
<p>Now go to Flash.<br />
first import ExternalInterface </p>
<pre class="brush: csharp; title: ; notranslate">
import flash.external.ExternalInterface;
</pre>
<p>and now you can prepare function which will be contacted from JS </p>
<pre class="brush: csharp; title: ; notranslate">
ExternalInterface.addCallback(&quot;sendToActionScript&quot;, receivedFromJavaScript);

function receivedFromJavaScript()
{
  //code
}
</pre>
<p>You&#8217;ll see that <strong>ExternalInterface.addCallback</strong> accept 2 params, first is name of function that you call on flash object inside Javascript, and the second one i reference to as function inside Flash.</p>
<p>So when you call sendToActionScript from JS, receivedFromJavaScript will be triggered inside flash. </p>
<p>Well, we are finished with Html and Flash. Now we&#8217;ll step into JS</p>
<p>Because of browser differencies you need to have little JS function which returns your flash object (well, if this function don&#8217;t work for your browser than you should google how to get flash object). </p>
<pre class="brush: jscript; title: ; notranslate">
function get_flash(flash_id)
{
var flash;
	if(navigator.appName.indexOf(&quot;Microsoft&quot;) != -1)
		flash = window.flash_id;
	else
		flash = window.document.flash_id;
return flash;

}
</pre>
<p>Remeber that our flash expect that we call sendToActionScript in JS to trigger receivedFromJavaScript function in flash, so<br />
we will do something like this</p>
<pre class="brush: jscript; title: ; notranslate">
var flash = get_flash(&quot;myFlashMovie&quot;);
flash.sendToActionScript();
</pre>
<p>And finally this should trigger Flash function. </p>
<p>Do you need source for this? If you do, leave a comment and i will find a time to prepare it. Also if you have any questions feel free to ask. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.hariscusto.com/programming/communication-between-javascript-and-actionscript-as3-and-vice-versa/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

