<?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>Lee Burrows</title>
	<atom:link href="http://blog.leeburrows.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.leeburrows.com</link>
	<description>I will mostly be chatting about Actionscript 3. Mostly.</description>
	<lastBuildDate>Tue, 20 Dec 2011 01:52:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Even More Recommended ActionScript Resources</title>
		<link>http://blog.leeburrows.com/2011/12/even-more-recommended-actionscript-resources/</link>
		<comments>http://blog.leeburrows.com/2011/12/even-more-recommended-actionscript-resources/#comments</comments>
		<pubDate>Tue, 20 Dec 2011 01:52:03 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Recommended Resources]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[ND2D]]></category>
		<category><![CDATA[RobotLegs]]></category>
		<category><![CDATA[Stage3D]]></category>

		<guid isPermaLink="false">http://blog.leeburrows.com/?p=1994</guid>
		<description><![CDATA[If Adobe&#8217;s recent clusterfuck hasn&#8217;t sent you running into the arms of html5 here are some nice resources you might like&#8230; Stage3D ND2D &#8211; Fast, easy-to-use 2D framework built on top of Stage3D https://github.com/nulldesign/nd2d Marco Scabia&#8217;s articles on ADC. Best introduction to stage3D i have read (ie: the only one&#8217;s i understood!) http://www.adobe.com/devnet/author_bios/marco_scabia.html Coding Skills [...]]]></description>
			<content:encoded><![CDATA[<p>If Adobe&#8217;s recent <a title="Adobe Press Release" href="http://blogs.adobe.com/conversations/2011/11/flash-focus.html" target="_blank">clusterfuck</a> hasn&#8217;t sent you running into the arms of html5 here are some nice resources you might like&#8230;<br />
<span id="more-1994"></span></p>
<h3>Stage3D</h3>
<p>ND2D &#8211; Fast, easy-to-use 2D framework built on top of Stage3D<br />
<a title="ND2D" href="https://github.com/nulldesign/nd2d" target="_blank">https://github.com/nulldesign/nd2d</a></p>
<p>Marco Scabia&#8217;s articles on ADC. Best introduction to stage3D i have read (ie: the only one&#8217;s i understood!)<br />
<a title="ADC" href="http://www.adobe.com/devnet/author_bios/marco_scabia.html" target="_blank">http://www.adobe.com/devnet/author_bios/marco_scabia.html</a></p>
<h3>Coding Skills</h3>
<p>Lifting the lid on a ton of little known features of ActionScript and Flex (don&#8217;t let the title deceive you &#8211; only about half is Flex specific)<br />
<a title="Abobe TV" href="http://tv.adobe.com/watch/max-2011-develop/discover-the-unknown-flex-45/" target="_blank">http://tv.adobe.com/watch/max-2011-develop/discover-the-unknown-flex-45/</a></p>
<p>Need to brush up on your Maths? Who doesn&#8217;t? Here&#8217;s a huge bank of videos by the world&#8217;s greatest teacher<br />
<a title="Khan Academy" href="http://www.khanacademy.org/" target="_blank">http://www.khanacademy.org/</a></p>
<p>Using RobotLegs or thinking about it? This book tells you everything you need to know (and in my case, everything i was doing wrong!)<br />
<a title="ActionScript Developer's Guide to Robotlegs" href="http://shop.oreilly.com/product/0636920021216.do" target="_blank">http://shop.oreilly.com/product/0636920021216.do</a></p>
<h3></h3>
]]></content:encoded>
			<wfw:commentRss>http://blog.leeburrows.com/2011/12/even-more-recommended-actionscript-resources/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>P2P Flash On A Local Network Part 3</title>
		<link>http://blog.leeburrows.com/2011/11/p2p-flash-on-a-local-network-part-3/</link>
		<comments>http://blog.leeburrows.com/2011/11/p2p-flash-on-a-local-network-part-3/#comments</comments>
		<pubDate>Fri, 25 Nov 2011 10:50:40 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[P2P]]></category>
		<category><![CDATA[RTMFP]]></category>

		<guid isPermaLink="false">http://blog.leeburrows.com/?p=1950</guid>
		<description><![CDATA[Following on from part one and two, we now deal with sending messages to specific peers in a large group (where large means 15+ members). To get around the limitation of neighbours (see the end of part two) we need to create a system of our own to let each peer know about all the [...]]]></description>
			<content:encoded><![CDATA[<p>Following on from part <a title="P2P Flash On A Local Network Part 1" href="http://blog.leeburrows.com/2011/10/p2p-flash-on-a-local-network-part1/">one</a> and <a title="P2P Flash On A Local Network Part 2" href="http://blog.leeburrows.com/2011/11/p2p-flash-on-a-local-network-part-2/">two</a>, we now deal with sending messages to specific peers in a large group (where large means 15+ members).<br />
<span id="more-1950"></span></p>
<p>To get around the limitation of neighbours (see the end of part two) we need to create a system of our own to let each peer know about all the other peers in the group. Then we can use the techniques of part two to send messages to any peer.</p>
<p>Here it is in action (open some copies of this page to get the member count to 16 or above if you wish):</p>
<p>{P2PLanExample3}</p>
<p>
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_0" width="250" height="300" align="left">
      <param name="movie" value="http://blog.leeburrows.com/wp-content/uploads/2011/11/P2PLanExample3.swf" />
      <param name="align" value="left" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.leeburrows.com/wp-content/uploads/2011/11/P2PLanExample3.swf" width="250" height="300" align="left">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
 
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_1" width="250" height="300" align="right">
      <param name="movie" value="http://blog.leeburrows.com/wp-content/uploads/2011/11/P2PLanExample3.swf" />
      <param name="align" value="right" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.leeburrows.com/wp-content/uploads/2011/11/P2PLanExample3.swf" width="250" height="300" align="right">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
<br />
<br clear="all" /><br />

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_2" width="250" height="300" align="left">
      <param name="movie" value="http://blog.leeburrows.com/wp-content/uploads/2011/11/P2PLanExample3.swf" />
      <param name="align" value="left" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.leeburrows.com/wp-content/uploads/2011/11/P2PLanExample3.swf" width="250" height="300" align="left">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
 
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_3" width="250" height="300" align="right">
      <param name="movie" value="http://blog.leeburrows.com/wp-content/uploads/2011/11/P2PLanExample3.swf" />
      <param name="align" value="right" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.leeburrows.com/wp-content/uploads/2011/11/P2PLanExample3.swf" width="250" height="300" align="right">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
<br clear="all" /></p>
<h2>How It Works</h2>
<p>There are no methods, classes or events in the code that aren&#8217;t covered in part 1 or 2 so i won&#8217;t explain the actual code used (the commented source code is available at the bottom of the post). Rather, i will explain the flow of messaging that allows every peer to know the id of every other.</p>
<p>When a new peer joins the group it sends out a group post (ie: to all peers in the group) containing the peer id of the new member. All peers store the received id, so every peer now knows about the new guy. Next, each peer sends a directed message back to the new peer containing their peer id &#8211; so now the new member knows about every other peer. Bingo! Now we can send a text message to anyone in the group using the techniques of part 2.</p>
<p>When a peer leaves the group, it would be nice if it could send out a group posting letting all the peers know to remove it from their list, but unfortunately this won&#8217;t work (for me at least) if the swf is running in a browser and the browser window/tab is closed (in short, there&#8217;s no way to send out the &#8220;i am disconnecting&#8221; group post message before the NetConnection has been closed). Luckily, the solution is straight-forward.</p>
<p>When a peer disconnects, its neighbours receive a NetGroup.Neighbor.Disconnect event. So we just get the neighbour to send out a &#8220;he is disconnecting&#8221; group post message instead. Easy peasy!</p>
<p>Of course, if you&#8217;re using AIR you can listen for the application exiting event and send the &#8220;i am disconnecting&#8221; group post directly, but this way works for a swf environment too.</p>
<h2>Notes On The Code</h2>
<p>This code does result in some duplication of messaging &#8211; for instance, every neighbour of a disconnecting peer will send out the same group post which could result in up to 13 identical post messages where only 1 is needed. Improving efficiency i leave as an exercise for the reader (a few pleadings in the comments might persuade me to get off my lazy backside).</p>
<p>We listen for the NetGroup.Neighbor.Connect event rather than the NetGroup.Connect.Success before dispatching the &#8220;i have connected&#8221; message (setting a flag to stop later NetGroup.Neighbor.Connect events triggering repeated &#8220;i have connected&#8221; messages). There&#8217;s no point sending a message if there are no other peers in the group!</p>
<p>The code uses messaging objects with either a cmd property (for behind-the-scenes messages, ie: managing the peer lists as explained above) or a txt property for messages that the user is sending.</p>
<p>In response to Rackdolls wise comment in part 2, i have used a belt and braces approach to unique ids for the messaging object this time &#8211; in addition to the date in milliseconds used before, each object now also contains the senders peer id and a random number.</p>
<h2>Further Reading</h2>
<p>If you want to see exactly how p2p is working under the hood there are some good sessions from MAX on adobe tv:</p>
<p><a title="p2p on the flash platform with rtmfp" href="http://tv.adobe.com/watch/max-2009-develop/p2p-on-the-flash-platform-with-rtmfp/" target="_blank">http://tv.adobe.com/watch/max-2009-develop/p2p-on-the-flash-platform-with-rtmfp/</a></p>
<p><a title="advanced p2p with rtmfp tips and tricks" href="http://tv.adobe.com/watch/max-2011-develop/advanced-p2p-with-rtmfp-tips-and-tricks/" target="_blank">http://tv.adobe.com/watch/max-2011-develop/advanced-p2p-with-rtmfp-tips-and-tricks/</a></p>
<p>For a ton of tutorials and info try:</p>
<p><a title="FlashRealTime" href="http://www.flashrealtime.com/" target="_blank">http://www.flashrealtime.com/</a></p>
<p>And lastly, here&#8217;s the source code for this post: <a title="P2PLanExamples3.zip" href="http://blog.leeburrows.com/wp-content/uploads/2011/11/P2PLanExamples3.zip">P2PLanExamples3.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.leeburrows.com/2011/11/p2p-flash-on-a-local-network-part-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>P2P Flash On A Local Network Part 2</title>
		<link>http://blog.leeburrows.com/2011/11/p2p-flash-on-a-local-network-part-2/</link>
		<comments>http://blog.leeburrows.com/2011/11/p2p-flash-on-a-local-network-part-2/#comments</comments>
		<pubDate>Thu, 03 Nov 2011 22:43:55 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[P2P]]></category>
		<category><![CDATA[RTMFP]]></category>

		<guid isPermaLink="false">http://blog.leeburrows.com/?p=1897</guid>
		<description><![CDATA[Following on from part 1, i have adapted the code to to enable message sending to specific peers (or in p2p jargon, direct routing). I have added a label to display the instances&#8217; (truncated) ID and a dropdown list to select the recipient of the message: {P2PLanExample2} Note that i have removed the [EVENT] traces [...]]]></description>
			<content:encoded><![CDATA[<p>Following on from <a title="P2P Flash On A Local Network Part 1" href="http://blog.leeburrows.com/2011/10/p2p-flash-on-a-local-network-part1/">part 1</a>, i have adapted the code to to enable message sending to specific peers (or in p2p jargon, direct routing). I have added a label to display the instances&#8217; (truncated) ID and a dropdown list to select the recipient of the message:<br />
<span id="more-1897"></span></p>
<p>{P2PLanExample2}</p>
<p>
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_4" width="250" height="300" align="left">
      <param name="movie" value="http://blog.leeburrows.com/wp-content/uploads/2011/11/P2PLanExample2.swf" />
      <param name="align" value="left" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.leeburrows.com/wp-content/uploads/2011/11/P2PLanExample2.swf" width="250" height="300" align="left">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
 
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_5" width="250" height="300" align="right">
      <param name="movie" value="http://blog.leeburrows.com/wp-content/uploads/2011/11/P2PLanExample2.swf" />
      <param name="align" value="right" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.leeburrows.com/wp-content/uploads/2011/11/P2PLanExample2.swf" width="250" height="300" align="right">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
<br />
<br clear="all" /><br />

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_6" width="250" height="300" align="left">
      <param name="movie" value="http://blog.leeburrows.com/wp-content/uploads/2011/11/P2PLanExample2.swf" />
      <param name="align" value="left" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.leeburrows.com/wp-content/uploads/2011/11/P2PLanExample2.swf" width="250" height="300" align="left">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
 
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_7" width="250" height="300" align="right">
      <param name="movie" value="http://blog.leeburrows.com/wp-content/uploads/2011/11/P2PLanExample2.swf" />
      <param name="align" value="right" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.leeburrows.com/wp-content/uploads/2011/11/P2PLanExample2.swf" width="250" height="300" align="right">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
<br />
<br clear="all" /><br />
Note that i have removed the [EVENT] traces that exist in the part 1 example.</p>
<p>Sending a message to a specific peer is slightly more complex than the group posting behaviour in part 1; first we first need to know who the peers are (or more specifically, their peer IDs) before we can send a message to them.</p>
<p>So, in the constructor i&#8217;ve added a couple of extra UI elements; a Label to display the instances&#8217; peer ID, and a ComboBox to hold a list of connected peers (and an &#8220;ALL&#8221; option to allow a group posting as before).</p>
<p>As with group posting, we need to enable direct routing before we can use it. We set the appropriate permission on the GroupSpecifier object in the setupGroup method:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">//enable posting (to entire group)</span>
groupspec<span style="color: #000066; font-weight: bold;">.</span>postingEnabled = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #009900; font-style: italic;">//enable direct routing (to individual peer)</span>
groupspec<span style="color: #000066; font-weight: bold;">.</span>routingEnabled = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>Ok, now for maintaining a list of peers. If you take a look at the live example in part 1, you&#8217;ll see in the output that an event is being fired whenever a peer joins or leaves the group (the info.code property of the event is being displayed).</p>
<p>The relevant codes are &#8220;NetGroup.Neighbor.Connect&#8221; and &#8220;NetGroup.Neighbor.Disconnect&#8221;. By listening for these, we can maintain a list of connected peers. When these events fire i just add or remove the peer from the ComboBox list:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">//neighbour connected so add details to list</span>
<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;NetGroup.Neighbor.Connect&quot;</span><span style="color: #000066; font-weight: bold;">:</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> peer<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	peer<span style="color: #000066; font-weight: bold;">.</span>id = event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">info</span><span style="color: #000066; font-weight: bold;">.</span>peerID<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	peer<span style="color: #000066; font-weight: bold;">.</span>label = truncateString<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">info</span><span style="color: #000066; font-weight: bold;">.</span>peerID<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	peerList<span style="color: #000066; font-weight: bold;">.</span>addItem<span style="color: #000000;">&#40;</span>peer<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">//neighbour disconnected so remove details from list</span>
<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;NetGroup.Neighbor.Disconnect&quot;</span><span style="color: #000066; font-weight: bold;">:</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> len<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = peerList<span style="color: #000066; font-weight: bold;">.</span>items<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span>=<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>i<span style="color: #000066; font-weight: bold;">&lt;</span>len<span style="color: #000066; font-weight: bold;">;</span>i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>peerList<span style="color: #000066; font-weight: bold;">.</span>items<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>id==event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">info</span><span style="color: #000066; font-weight: bold;">.</span>peerID<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			peerList<span style="color: #000066; font-weight: bold;">.</span>removeItemAt<span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>The important data is the event.info.peerID property &#8211; this is our reference to the peer and allows us to direct messages to it. The peerID is a 64 character string which is why i am using a truncateString method to reduce it&#8217;s length where it&#8217;s being displayed in the UI.</p>
<p>Now we are ready to send out messages to specific peers. Here&#8217;s the revised button handler method:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> btnHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #009900; font-style: italic;">// Create an object to hold the data to send</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> obj<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">//add the data</span>
	obj<span style="color: #000066; font-weight: bold;">.</span>txt = inputText<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">//add some extra data to ensure every sent object is unique</span>
	obj<span style="color: #000066; font-weight: bold;">.</span>id = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">time</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">//update UI</span>
	outputText<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;[SENDING TO &quot;</span><span style="color: #000066; font-weight: bold;">+</span>peerList<span style="color: #000066; font-weight: bold;">.</span>selectedItem<span style="color: #000066; font-weight: bold;">.</span>label<span style="color: #000066; font-weight: bold;">+</span><span style="color: #990000;">&quot;]<span style="">\n</span>&quot;</span><span style="color: #000066; font-weight: bold;">+</span>inputText<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span><span style="color: #000066; font-weight: bold;">+</span><span style="color: #990000;">&quot;<span style="">\n</span>&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>peerList<span style="color: #000066; font-weight: bold;">.</span>selectedItem<span style="color: #000066; font-weight: bold;">.</span>label==<span style="color: #990000;">&quot;ALL&quot;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #009900; font-style: italic;">//send to everyone in group</span>
		group<span style="color: #000066; font-weight: bold;">.</span>post<span style="color: #000000;">&#40;</span>obj<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0033ff; font-weight: bold;">else</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #009900; font-style: italic;">//get group address of peer</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> destination<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = group<span style="color: #000066; font-weight: bold;">.</span>convertPeerIDToGroupAddress<span style="color: #000000;">&#40;</span><span style="color: #004993;">String</span><span style="color: #000000;">&#40;</span>peerList<span style="color: #000066; font-weight: bold;">.</span>selectedItem<span style="color: #000066; font-weight: bold;">.</span>id<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #009900; font-style: italic;">//save to message object</span>
		obj<span style="color: #000066; font-weight: bold;">.</span>destination = destination<span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #009900; font-style: italic;">//send message</span>
		group<span style="color: #000066; font-weight: bold;">.</span>sendToNearest<span style="color: #000000;">&#40;</span>obj<span style="color: #000066; font-weight: bold;">,</span> destination<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We create the object to send, update the UI and check the ComboBox selection. If it&#8217;s &#8220;ALL&#8221; we do a group post as before, otherwise we direct it to the specified peer:</p>
<p>First, we use the convertPeerIDToGroupAddress method of our NetGroup instance to create (unsurprisingly) a group address from the peer ID. Next we add this group address to our message object (we&#8217;ll need it later).</p>
<p>Lastly, we call the sendToNearest method of our NetGroup &#8211; passing our message object and the destination group address as arguments.</p>
<p>Note that this method doesn&#8217;t necessarily send the message directly to the recipient. It may only send it part of the way so we&#8217;ll need to cover that possibility in our code (which we will).</p>
<p>The last bit of code we need to add is for listening and responding to incoming (directed) messages. Again, we get a NetStatusEvent.NET_STATUS event when a directed message is received &#8211; this time with an info.code of &#8220;NetGroup.SendTo.Notify&#8221;. Here&#8217;s the code for it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">//directed message received so check if this is the final destination</span>
<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;NetGroup.SendTo.Notify&quot;</span><span style="color: #000066; font-weight: bold;">:</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">info</span><span style="color: #000066; font-weight: bold;">.</span>fromLocal==<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>
		<span style="color: #009900; font-style: italic;">//is destination so display it</span>
		outputText<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;[RECEIVED]<span style="">\n</span>&quot;</span><span style="color: #000066; font-weight: bold;">+</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">info</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">message</span><span style="color: #000066; font-weight: bold;">.</span>txt<span style="color: #000066; font-weight: bold;">+</span><span style="color: #990000;">&quot;<span style="">\n</span>&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">else</span>
		<span style="color: #009900; font-style: italic;">//not destination so re-send</span>
		group<span style="color: #000066; font-weight: bold;">.</span>sendToNearest<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">info</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">message</span><span style="color: #000066; font-weight: bold;">,</span> event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">info</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">message</span><span style="color: #000066; font-weight: bold;">.</span>destination<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>First we check to see if this is the final destination of the message. The info.fromLocal property of the event tells us this &#8211; if it&#8217;s true, this is the final destination.</p>
<p>If this is the destination we add the message text to the UI, otherwise we call sendToNearest to send the message on its next hop towards its destination. This is why we added the destination to the original message object &#8211; so we could extract it and use it as the second argument for any further sendToNearest calls.</p>
<p>And that&#8217;s it &#8211; group posting and directed routing both working on a peer-to-peer group on a local area network. Or is it&#8230;</p>
<h3>A Rather Large Gotcha</h3>
<p>Take a p2p group consisting of 1000 peers; if every peer was directly connected to every other, there would be approximately 500,000 connections across the local network &#8211; i&#8217;m no network expert but i&#8217;m guessing that&#8217;s frowned upon.</p>
<p>To avoid this, peers are directly connected to only a few neighbouring peers  - hence the info.code of &#8221;NetGroup.<strong>Neighbor</strong>.Connect&#8221; rather than &#8220;NetGroup.<strong>Peer</strong>.Connect&#8221;.  Unfortunately, there is no event for a non-neighbour dis/connecting and that&#8217;s a problem.</p>
<p>As we saw above, we use sendToNearest to bunny-hop a message to it&#8217;s destination &#8211; which is fine as long as we know the peer ID of the destination- but if it&#8217;s not a neighbour we&#8217;ll never get informed about it connecting, and can&#8217;t retrieve a peer ID to direct messages to.</p>
<p>The important number here is 14 &#8211; in a p2p group of 14 or less members, every peer is always a neighbour of every other.</p>
<p>So for 14 or less peers, the code above works just fine. For 15 and above, it&#8217;s back to the drawing board&#8230;</p>
<p>I plan to cover 15+ members in part 3, but to be honest i haven&#8217;t actually decided on a strategy yet (let alone implemented it) so that may turn out to be a hollow promise.</p>
<p>And on that bombshell, here&#8217;s the latest source: <a title="P2PLanExamples2 Source Code" href="http://blog.leeburrows.com/wp-content/uploads/2011/11/P2PLanExamples2.zip">P2PLanExamples2.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.leeburrows.com/2011/11/p2p-flash-on-a-local-network-part-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>P2P Flash On A Local Network Part 1</title>
		<link>http://blog.leeburrows.com/2011/10/p2p-flash-on-a-local-network-part1/</link>
		<comments>http://blog.leeburrows.com/2011/10/p2p-flash-on-a-local-network-part1/#comments</comments>
		<pubDate>Mon, 31 Oct 2011 20:21:42 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[P2P]]></category>
		<category><![CDATA[RTMFP]]></category>

		<guid isPermaLink="false">http://blog.leeburrows.com/?p=1824</guid>
		<description><![CDATA[Flash Player has provided an API for peer-to-peer communication since 10.1 (and in AIR since 2.0). P2P over the Internet requires some server-side technology (such as Flash Media Server) but on a local network it&#8217;s really easy; no server stuff and it can be set up in a few lines of code. Below are two [...]]]></description>
			<content:encoded><![CDATA[<p>Flash Player has provided an API for peer-to-peer communication since 10.1 (and in AIR since 2.0). P2P over the Internet requires some server-side technology (such as Flash Media Server) but on a local network it&#8217;s really easy; no server stuff and it can be set up in a few lines of code.<br />
<span id="more-1824"></span></p>
<p>Below are two instances of the same swf; anything entered in the input field will be sent to all other instances of the swf and appear in their text areas &#8211; feel free to open more copies of this page if you want to increase the number of swfs. I also trace out some basic info to the text area so you can see whats going on behind the scenes.</p>
<p><img class="alignright size-full wp-image-1832" title="fp-p2p-warning-popup" src="http://blog.leeburrows.com/wp-content/uploads/2011/10/fp-p2p-warning-popup.png" alt="Flash Player popup" width="213" height="136" />You should get an annoying Flash Player alert asking you to confirm that you are happy to allow p2p for this website before it is activated (sigh):</p>
<p>&nbsp;</p>
<p>{P2PLanExample1}</p>
<p>
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_8" width="250" height="300" align="left">
      <param name="movie" value="http://blog.leeburrows.com/wp-content/uploads/2011/10/P2PLanExample1.swf" />
      <param name="align" value="left" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.leeburrows.com/wp-content/uploads/2011/10/P2PLanExample1.swf" width="250" height="300" align="left">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
 
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_9" width="250" height="300" align="right">
      <param name="movie" value="http://blog.leeburrows.com/wp-content/uploads/2011/10/P2PLanExample1.swf" />
      <param name="align" value="right" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.leeburrows.com/wp-content/uploads/2011/10/P2PLanExample1.swf" width="250" height="300" align="right">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>
</p>
<p>&nbsp;</p>
<p>And here&#8217;s the code (using <a title="Minimal Comps" href="http://www.minimalcomps.com/" target="_blank">MinimalComps</a> for the UI):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> com<span style="color: #000066; font-weight: bold;">.</span>bit101<span style="color: #000066; font-weight: bold;">.</span>components<span style="color: #000066; font-weight: bold;">.</span>InputText<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> com<span style="color: #000066; font-weight: bold;">.</span>bit101<span style="color: #000066; font-weight: bold;">.</span>components<span style="color: #000066; font-weight: bold;">.</span>PushButton<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> com<span style="color: #000066; font-weight: bold;">.</span>bit101<span style="color: #000066; font-weight: bold;">.</span>components<span style="color: #000066; font-weight: bold;">.</span>TextArea<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">NetStatusEvent</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.net</span><span style="color: #000066; font-weight: bold;">.</span>GroupSpecifier<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.net</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">NetConnection</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.net</span><span style="color: #000066; font-weight: bold;">.</span>NetGroup<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #000000;">&#91;</span>SWF<span style="color: #000000;">&#40;</span><span style="color: #004993;">width</span>=<span style="color: #990000;">&quot;250&quot;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #004993;">height</span>=<span style="color: #990000;">&quot;300&quot;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #004993;">backgroundColor</span>=<span style="color: #990000;">&quot;#EEEEEE&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> P2PLanExample1 <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">Sprite</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> netConn<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">NetConnection</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> group<span style="color: #000066; font-weight: bold;">:</span>NetGroup<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> inputText<span style="color: #000066; font-weight: bold;">:</span>InputText<span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> outputText<span style="color: #000066; font-weight: bold;">:</span>TextArea<span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> submitButton<span style="color: #000066; font-weight: bold;">:</span>PushButton<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> P2PLanExample1<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900; font-style: italic;">//create UI (using minimalComps)</span>
			inputText = <span style="color: #0033ff; font-weight: bold;">new</span> InputText<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			submitButton = <span style="color: #0033ff; font-weight: bold;">new</span> PushButton<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">120</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;Send&quot;</span><span style="color: #000066; font-weight: bold;">,</span> btnHandler<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			submitButton<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> = <span style="color: #000000; font-weight:bold;">70</span><span style="color: #000066; font-weight: bold;">;</span>
			submitButton<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">enabled</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span>
			outputText = <span style="color: #0033ff; font-weight: bold;">new</span> TextArea<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">35</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			outputText<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> = <span style="color: #000000; font-weight:bold;">230</span><span style="color: #000066; font-weight: bold;">;</span>
			outputText<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span> = <span style="color: #000000; font-weight:bold;">255</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #009900; font-style: italic;">//setup connection object</span>
			netConn = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">NetConnection</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #009900; font-style: italic;">//listen for result of setup</span>
			netConn<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">NetStatusEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">NET_STATUS</span><span style="color: #000066; font-weight: bold;">,</span> netHandler<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #009900; font-style: italic;">//connect to LAN</span>
			netConn<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">connect</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;rtmfp:&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> btnHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900; font-style: italic;">//update UI</span>
			outputText<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;[SENDING]<span style="">\n</span>&quot;</span><span style="color: #000066; font-weight: bold;">+</span>inputText<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span><span style="color: #000066; font-weight: bold;">+</span><span style="color: #990000;">&quot;<span style="">\n</span>&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #009900; font-style: italic;">// Create an object to hold the data to send</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> obj<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #009900; font-style: italic;">//add the data</span>
			obj<span style="color: #000066; font-weight: bold;">.</span>txt = inputText<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #009900; font-style: italic;">//add some extra data to ensure every sent object is unique</span>
			obj<span style="color: #000066; font-weight: bold;">.</span>id = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">time</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #009900; font-style: italic;">//send to everyone in group</span>
			group<span style="color: #000066; font-weight: bold;">.</span>post<span style="color: #000000;">&#40;</span>obj<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> netHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">NetStatusEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900; font-style: italic;">//update UI</span>
			outputText<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;[EVENT]<span style="">\n</span>&quot;</span><span style="color: #000066; font-weight: bold;">+</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">info</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">code</span><span style="color: #000066; font-weight: bold;">+</span><span style="color: #990000;">&quot;<span style="">\n</span>&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #009900; font-style: italic;">//handle event</span>
			<span style="color: #0033ff; font-weight: bold;">switch</span><span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">info</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">code</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #009900; font-style: italic;">//connection succeeded so setup a group</span>
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;NetConnection.Connect.Success&quot;</span><span style="color: #000066; font-weight: bold;">:</span>
					setupGroup<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #009900; font-style: italic;">//group setup succeeded so enable submit</span>
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;NetGroup.Connect.Success&quot;</span><span style="color: #000066; font-weight: bold;">:</span>
					submitButton<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">enabled</span> = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #009900; font-style: italic;">//posting received so add to output</span>
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;NetGroup.Posting.Notify&quot;</span><span style="color: #000066; font-weight: bold;">:</span>
					outputText<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;[RECEIVED]<span style="">\n</span>&quot;</span><span style="color: #000066; font-weight: bold;">+</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">info</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">message</span><span style="color: #000066; font-weight: bold;">.</span>txt<span style="color: #000066; font-weight: bold;">+</span><span style="color: #990000;">&quot;<span style="">\n</span>&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> setupGroup<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900; font-style: italic;">//create a GroupSpecifier object</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> groupspec<span style="color: #000066; font-weight: bold;">:</span>GroupSpecifier = <span style="color: #0033ff; font-weight: bold;">new</span> GroupSpecifier<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;myGroup&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #009900; font-style: italic;">//enable posting (to entire group)</span>
			groupspec<span style="color: #000066; font-weight: bold;">.</span>postingEnabled = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #009900; font-style: italic;">//allow data to be exchanged on IP multicast sockets</span>
			groupspec<span style="color: #000066; font-weight: bold;">.</span>ipMulticastMemberUpdatesEnabled = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #009900; font-style: italic;">//set the IP adress and port to use</span>
			groupspec<span style="color: #000066; font-weight: bold;">.</span>addIPMulticastAddress<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;225.225.0.1:30000&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #009900; font-style: italic;">//create NetGroup with our NetConnection using GroupSpecifier details</span>
			group = <span style="color: #0033ff; font-weight: bold;">new</span> NetGroup<span style="color: #000000;">&#40;</span>netConn<span style="color: #000066; font-weight: bold;">,</span> groupspec<span style="color: #000066; font-weight: bold;">.</span>groupspecWithAuthorizations<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #009900; font-style: italic;">//listen for result of setup</span>
			group<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">NetStatusEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">NET_STATUS</span><span style="color: #000066; font-weight: bold;">,</span> netHandler<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>Setting Up</h3>
<p>In the constructor i create the UI and then set up a NetConnection object. By passing only the protocol when calling connect (<code>netConn.connect("rtmfp:");</code>), we are telling FP that we want a local network connection. RTMFP stands for Real Time Media Flow Protocol btw.</p>
<p>If this was an internet-wide p2p setup, we would use something like <code>netConn.connect("rtmfp://myServer.com/myPath");</code> to connect to a remote server (which helps set up the p2p connections &#8211; you wouldn&#8217;t want to connect to every flash player connected to the Internet would you?).</p>
<p>Once the NetConnection succeeds (or fails), a NetStatusEvent.NET_STATUS event is fired &#8211; which is picked up by the netHandler method. The <code>info.code</code> property of the NetStatusEvent can be used to determine what happened &#8211; in this case we are hoping for &#8220;NetConnection.Connect.Success&#8221; (and not &#8220;NetConnection.Connect.Failed&#8221;; a full list of possible codes is available at <a title="NetStatusEvent Lang Ref" href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/NetStatusEvent.html" target="_blank">http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/NetStatusEvent.html</a>).</p>
<p>Once the NetConnection object is ready, we set up a NetGroup in the setupGroup() method. First, we create a GroupSpecifier object and set some properties &#8211; giving the group a name of &#8220;myGroup&#8221; (you can have multiple groups), enabling posting to all members of the group, telling the group to use IP sockets and finally stating the IP address and port to utilise &#8211; 225.225.0.1:30000 is the value used in the AS3 docs and it works for me, so i suggest leaving it as it is unless you feel a need to change it.</p>
<p>Lastly, we create the NetGroup object, passing in a reference to the NetConnection and utilising a GroupSpecifier helper method to pass the groupspec data.</p>
<p>Success (or failure) of the NetGroup initialisation will be signalled by another NetStatusEvent.NET_STATUS event &#8211; this time with a code of &#8220;NetGroup.Connect.Success&#8221;. Once the NetGroup is ready, p2p communication is good to go.</p>
<h3>Send And Receive</h3>
<p>Sending data is managed here by the btnHandler method; we simply create an Object, attach some data to it (in this case, the text from the input field), and pass the object to the NetGroup post method. That&#8217;s it.</p>
<p>Incoming data (sent from the post method) fires a NetStatusEvent.NET_STATUS event with a code of &#8220;NetGroup.Posting.Notify&#8221;. The info.message property of the event is the same Object that was sent with the post method, so for this example (in netHandler method) the text data is in <code>event.info.message.txt</code>. Here we just add it to the output text area.</p>
<h3>Final Thoughts</h3>
<p>There can be a noticable lag between sending and receiving when the NetGroup is first initialised, but this usually improves greatly after a few messages have been sent.</p>
<p>Also, i should make clear that the swfs don&#8217;t need to be on the same device &#8211; it really is the whole local network &#8211; so if your phone hooks ups to your wifi when you&#8217;re at home, a swf in your browser could communicate with an AIR app running on your phone (use your phone as a remote control for a game running in the browser for example).</p>
<p>In <a title="P2P Flash On A Local Network Part 2" href="http://blog.leeburrows.com/2011/11/p2p-flash-on-a-local-network-part-2/">part 2</a>, i will extend this example to display info about members of the group and configure message sending to specific users. Until then, here’s the source code: <a title="P2PLanExamples1 Source Code" href="http://blog.leeburrows.com/wp-content/uploads/2011/10/P2PLanExamples1.zip">P2PLanExamples1.zip</a></p>
<h3>/* UPDATE */</h3>
<p>It turns out that every message object sent must be completely unique or it may get ignored (ie: mistaken for a previous message already received). For instance, sending &#8220;hello&#8221; twice, may result in only a single &#8220;hello&#8221; being received.</p>
<p>Luckily, we can easily cover this by adding a unique property to each message object to ensure no two are ever alike. I have amended the btnHandler method to include a time stamp on each message object. Job done.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">//add some extra data to ensure every sent object is unique</span>
obj<span style="color: #000066; font-weight: bold;">.</span>id = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">time</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.leeburrows.com/2011/10/p2p-flash-on-a-local-network-part1/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Happy Birthday to me</title>
		<link>http://blog.leeburrows.com/2011/09/happy-birthday/</link>
		<comments>http://blog.leeburrows.com/2011/09/happy-birthday/#comments</comments>
		<pubDate>Tue, 06 Sep 2011 21:33:42 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[Ego Trip]]></category>

		<guid isPermaLink="false">http://blog.leeburrows.com/?p=1795</guid>
		<description><![CDATA[I achieved consciousness at 10.33pm on 6th of September 2010. I am one year old today.]]></description>
			<content:encoded><![CDATA[<p>I achieved consciousness at 10.33pm on 6th of September 2010. I am one year old today.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.leeburrows.com/2011/09/happy-birthday/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>JavaScript for Flashers Part 3: ExternalInterface</title>
		<link>http://blog.leeburrows.com/2011/08/javascript-for-flashers-externalinterface/</link>
		<comments>http://blog.leeburrows.com/2011/08/javascript-for-flashers-externalinterface/#comments</comments>
		<pubDate>Mon, 22 Aug 2011 10:30:09 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[ExternalInterface]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.leeburrows.com/?p=1519</guid>
		<description><![CDATA[In parts one and two, i dealt with specific JavaScript libraries but this time i will cover general communication between Flash and JavaScript. Namely, calling Flash methods from JavaScript and JavaScript methods from Flash. How is it done? Well the clue is in the title &#8211; we use the External Interface class. This is an [...]]]></description>
			<content:encoded><![CDATA[<p>In parts <a title="JavaScript for Flashers Part 1: SWFObject" href="http://blog.leeburrows.com/2011/07/javascript-for-flashers-swfobject/">one</a> and <a title="JavaScript for Flashers Part 2: SWFAddress" href="http://blog.leeburrows.com/2011/08/javascript-for-flashers-swfaddress/">two</a>, i dealt with specific JavaScript libraries but this time i will cover general communication between Flash and JavaScript. Namely, calling Flash methods from JavaScript and JavaScript methods from Flash.<br />
<span id="more-1519"></span></p>
<p>How is it done? Well the clue is in the title &#8211; we use the External Interface class. This is an ActionScript class that can communicate with the container for the swf &#8211; in this case, the browser. Of course, browsers being what they are, it doesn&#8217;t work with everything, but the main players are all covered (at least for the desktop). See <a title="AS3 Reference - ExternalInterface" href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html" target="_blank">the docs</a> for details of compatibility.</p>
<h3>Flash To JavaScript</h3>
<p>Here&#8217;s an example of calling a JavaScript function:</p>
<p>{ExternalInterfaceExample1}</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_10" width="400" height="200">
      <param name="movie" value="http://blog.leeburrows.com/wp-content/uploads/2011/08/ExternalInterfaceExample1.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.leeburrows.com/wp-content/uploads/2011/08/ExternalInterfaceExample1.swf" width="400" height="200">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p>Clicking the button calls this method:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> clickHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">ExternalInterface</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">available</span><span style="color: #000000;">&#41;</span>
		<span style="color: #004993;">ExternalInterface</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">call</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;alert&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;Hello from Flash!!!&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>First we check that ExternalInterface will work (ie: that <code>ExternalInterface.available</code> is true) and then use the call method to trigger a JavaScript function. The first argument is the name of the JavaScript function to call and any remaining arguments are passed to the JavaScript function as its arguments. In this case i am calling the built-in JavaScript function &#8216;alert&#8217; (which displays an alert popup); alert takes a string (to display) as its argument which in this case is &#8216;Hello from Flash!!!&#8217;. Simple.</p>
<p>We are not restricted to built-in functions though &#8211; any JavaScript functions defined in the HTML container (ie: within HTML &lt;script&gt; tags or linked external .js files) can be called. This example calls a JavaScript function that&#8217;s embedded in the page:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> myFabFunction<span style="color: #000000;">&#40;</span>colour<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
       	document<span style="color: #000066; font-weight: bold;">.</span>body<span style="color: #000066; font-weight: bold;">.</span>style<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">backgroundColor</span> = colour<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>{ExternalInterfaceExample2}</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_11" width="400" height="200">
      <param name="movie" value="http://blog.leeburrows.com/wp-content/uploads/2011/08/ExternalInterfaceExample2.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.leeburrows.com/wp-content/uploads/2011/08/ExternalInterfaceExample2.swf" width="400" height="200">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p>The AS method just alternates between passing white and red colour values:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> clickHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	flag = <span style="color: #000066; font-weight: bold;">!</span>flag<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">ExternalInterface</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">available</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #004993;">ExternalInterface</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">call</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;myFabFunction&quot;</span><span style="color: #000066; font-weight: bold;">,</span> flag <span style="color: #000066; font-weight: bold;">?</span> <span style="color: #990000;">&quot;#FFFFFF&quot;</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #990000;">&quot;#FF0000&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Not a very imaginative or useful example, but it shows that we can manipulate the page DOM from Flash. With this gateway to the browser, we can call our own functions or hook into established libraries like <a title="jQuery" href="http://jquery.com/" target="_blank">jQuery</a> to do the leg work.</p>
<p><script type="text/javascript">// <![CDATA[
     function myFabFunction(colour){document.body.style.backgroundColor = colour;};
// ]]&gt;</script></p>
<h3>JavaScript To Flash</h3>
<p>Now for the reverse. Try resizing the browser window: the current date should appear in the brownish box below.</p>
<p>{ExternalInterfaceExample3}</p>

    <div id="swfobj_12">
      
    </div>

<p>This is the JavaScript code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">window<span style="color: #000066; font-weight: bold;">.</span>onresize = <span style="color: #339966; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
document<span style="color: #000066; font-weight: bold;">.</span>getElementById<span style="color: #000000;">&#40;</span><span style="color: #990000;">'swfobj_2'</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span>setFlashLabel<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This listens for a resize and then calls the method setFlashLabel, passing the current date as the argument (the swf file has an id of swfobj_2 so we use <code>document.getElementById('swfobj_2')</code> to get a reference to it. disclaimer: i&#8217;m not a JavaScript expert so there may be a better way).</p>
<p>Inside Flash we need to do 2 things. First we let ExternalInterface know that we have a method we wish to expose to JavaScript:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">ExternalInterface</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addCallback</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;setFlashLabel&quot;</span><span style="color: #000066; font-weight: bold;">,</span> setLabel<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>The first argument of addCallback is the function name that JavaScript will use (see JavaScript code snippet above) and the second is the method in Flash. Here it is setLabel which just updates the textfield with the passed argument and then centers it.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> setLabel<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	tf<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = <span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">;</span>
	tf<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">400</span><span style="color: #000066; font-weight: bold;">-</span>tf<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">0.5</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	tf<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">200</span><span style="color: #000066; font-weight: bold;">-</span>tf<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">0.5</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>There&#8217;s no reason why you shouldn&#8217;t use the same name for both arguments &#8211; i have just differentiated them so it&#8217;s easier to see which name refers to which side (and remember that the first argument is a String type and the second is a Function type).</p>
<p>And the circle is complete: ActionScript can talk to JavaScript and JavaScript can talk to ActionScript.</p>
<h3>Final Note</h3>
<p>One caveat: this blog uses SWFObject to embed the examples and I was forced to use SWFObject in dynamic mode for example 3 &#8211; JavaScript to Flash doesn&#8217;t seem to work when using static mode.</p>
<p>And that&#8217;s all there is to it. Here&#8217;s the source code: <a title="ExternalInterfaceExamples.zip" href="http://blog.leeburrows.com/wp-content/uploads/2011/08/ExternalInterfaceExamples.zip">ExternalInterfaceExamples.zip</a></p>
<p><script type="text/javascript">// <![CDATA[
     window.onresize = function(){ document.getElementById('swfobj_2').setFlashLabel(new Date()); }
// ]]&gt;</script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.leeburrows.com/2011/08/javascript-for-flashers-externalinterface/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>JavaScript for Flashers Part 2: SWFAddress</title>
		<link>http://blog.leeburrows.com/2011/08/javascript-for-flashers-swfaddress/</link>
		<comments>http://blog.leeburrows.com/2011/08/javascript-for-flashers-swfaddress/#comments</comments>
		<pubDate>Fri, 19 Aug 2011 07:35:19 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[SWFAddress]]></category>
		<category><![CDATA[SWFObject]]></category>

		<guid isPermaLink="false">http://blog.leeburrows.com/?p=1517</guid>
		<description><![CDATA[SWFAddress is a JavaScript and ActionScript library for adding deep-linking functionality to Flash websites; allowing users to navigate using the browser back and next buttons in the same way as an HTML website. It&#8217;s dead easy to use so let&#8217;s dive in&#8230; Without SWFAddress Here&#8217;s a simple example of a 3 page site: {SWFAddressExample1} Here&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>SWFAddress is a JavaScript and ActionScript library for adding deep-linking functionality to Flash websites; allowing users to navigate using the browser back and next buttons in the same way as an HTML website. It&#8217;s dead easy to use so let&#8217;s dive in&#8230;<br />
<span id="more-1517"></span></p>
<h3>Without SWFAddress</h3>
<p>Here&#8217;s a simple example of a 3 page site:</p>
<p>{SWFAddressExample1}</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_13" width="550" height="400">
      <param name="movie" value="http://blog.leeburrows.com/wp-content/uploads/2011/08/SWFAddressExample1.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.leeburrows.com/wp-content/uploads/2011/08/SWFAddressExample1.swf" width="550" height="400">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p>Here&#8217;s a quick overview of how the navigation works: when a button is clicked, this method is called:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> clickHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	showPage<span style="color: #000000;">&#40;</span><span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">name</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>which takes the button name (each buttons name property is either &#8220;1&#8243;, &#8220;2&#8243; or &#8220;3&#8243;), converts it to an integer and passes the value on to showPage:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> showPage<span style="color: #000000;">&#40;</span><span style="color: #004993;">index</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	page1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">visible</span> = <span style="color: #004993;">index</span>==<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
	page2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">visible</span> = <span style="color: #004993;">index</span>==<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
	page3<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">visible</span> = <span style="color: #004993;">index</span>==<span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This method just sets each pages&#8217; visibility depending on the value of index. Pretty simple stuff. Needless to say, this is not the best way to handle multiple views (you should at least be removing them from the display list; if not destroying and recreating&#8230; but that&#8217;s another story). The point to note from this is that clickHandler calls showPage directly. With SWFAddress we do things slightly differently.</p>
<h3>With SWFAddress</h3>
<p>First off, SWFAddress requires SWFObject to work so read <a title="JavaScript for Flashers Part 1: SWFObject" href="http://blog.leeburrows.com/2011/07/javascript-for-flashers-swfobject/">part one</a> first, if you&#8217;re not yet using it. Otherwise head over to <a title="SWFAddress" href="http://www.asual.com/swfaddress" target="_blank">asual.com/swfaddress</a> to download the latest source files (version 2.4 as i write this). Here is the same &#8216;site&#8217; with deep linking enabled thanks to SWFAddress. Open a few pages with the 3 buttons to build up some history and then try the browser back/forward buttons. Pretty neat, huh!</p>
<p>{SWFAddressExample2}</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_14" width="550" height="400">
      <param name="movie" value="http://blog.leeburrows.com/wp-content/uploads/2011/08/SWFAddressExample2.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.leeburrows.com/wp-content/uploads/2011/08/SWFAddressExample2.swf" width="550" height="400">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p>Notice that the browser address updates every time you change the &#8216;page&#8217; &#8211; adding (eg) #/1 to the end of the URL. This is the trick &#8211; changing the URL beyond the # adds new history to the browser (allowing back/next to navigate this history)  but does not cause the browser to refresh the page.</p>
<p>The first step was to add a link to the SWFAddress JavaScript library (swfaddess.js from downloaded source files) within this HTML page &#8211; and it&#8217;s important that the link to swfaddress.js is below the link to swfobject.js, eg:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;</span>head<span style="color: #000066; font-weight: bold;">&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;</span>script <span style="color: #004993;">type</span>=<span style="color: #990000;">&quot;text/javascript&quot;</span> src=<span style="color: #990000;">&quot;swfobject.js&quot;</span><span style="color: #000066; font-weight: bold;">&gt;&lt;/</span>script<span style="color: #000066; font-weight: bold;">&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;</span>script <span style="color: #004993;">type</span>=<span style="color: #990000;">&quot;text/javascript&quot;</span> src=<span style="color: #990000;">&quot;swfaddress.js&quot;</span><span style="color: #000066; font-weight: bold;">&gt;&lt;/</span>script<span style="color: #000066; font-weight: bold;">&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;/</span>head<span style="color: #000066; font-weight: bold;">&gt;</span></pre></div></div>

<p>Next, the SWFAddress ActionScript library needs including &#8211; the downloaded source files come with AS1, AS2 and AS3 libraries, but as this is the 21st century, i will only cover AS3. The library is provided as a SWC  or as two AS3 files (SWFAddress.as and SWFAddressEvent.as).</p>
<p><strong>Add library in Flex Builder/Flash Builder:</strong> Select ‘Properties’ from the ‘Project’ menu and choose ‘ActionScript Build Path’. Ensure that you are in ‘Library Path’, click on ‘Add SWC…’ and browse to the swc file you just extracted from the zip. Select it and click ‘ok’ to add it to the project.</p>
<p><strong>Add library in Flash Professional:</strong>In the properties panel, click on the ‘Edit…’ button next to ‘ActionScript Settings’. In the subsequent pop-up window, ensure you are in ‘Library path’ and click on the ‘+’ symbol to add a new path and then click on the Flash (‘f’) icon to browse to the swc file you just extracted. Double click the swc file and you will see it listed. Now click on ‘ok’ to close the pop-up window.</p>
<p>Now we are ready to code&#8230;</p>
<p>In example 1 we called showPage directly from clickHandler. This time we call showPage indirectly &#8211; using SWFAddress as the middleman. clickHandler now looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> clickHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	SWFAddress<span style="color: #000066; font-weight: bold;">.</span>setValue<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;/&quot;</span><span style="color: #000066; font-weight: bold;">+</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">name</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We call the static method setValue(), passing a string as the argument &#8211; the first character needs to be &#8220;/&#8221; and here i am adding either &#8220;1&#8243;, &#8220;2&#8243; or &#8220;3&#8243; depending on the button clicked. This will cause the browser address to update (to become &#8230;#/1 or &#8230;#/2 or &#8230;#/3 in this case). Now we listen for a browser address change (note that SWFAddress is a static class so we add listener to class rather than creating an instance of it):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">SWFAddress<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span>SWFAddressEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CHANGE</span><span style="color: #000066; font-weight: bold;">,</span> swfaddressHandler<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>and then update the page based on the result:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> swfaddressHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span>SWFAddressEvent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #009900; font-style: italic;">//should be r on end of subst but wordpress throwing a wobbly!</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> pageIndex<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">.</span>subst<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>pageIndex==<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>
		pageIndex = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
	showPage<span style="color: #000000;">&#40;</span>pageIndex<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The value property of the SWFAddressEvent contains the text beyond the # (so the first character will be &#8220;/&#8221;). In this example, we strip off the slash, convert to an integer and store the result in pageIndex. After a quick validation check we call showPage as normal. If the address in the browser doesn&#8217;t contain a # then the value returned is a single &#8220;/&#8221; &#8211; in the code above, a single slash results in pageIndex of zero; in which case revert to the default (page 1).</p>
<p>In example 1, i called showPage(1); in the constructor to display the first page. In example 2 this isn&#8217;t necessary as SWFAddress fires a CHANGE event on startup &#8211; and this is used to trigger the initial page to display. Not only does this allow us to add back/next navigation but we could also provide links to specific content rather than the default &#8216;home&#8217; page (eg: www.mycompany.com#/great-bargains).</p>
<p>Lastly i should mention that in the real world we would want to add some validation to the values coming in &#8211; after all, the user could type anything in the address bar. For this example, changing the URL to &#8220;&#8230;#/4&#8243; results in no page being displayed &#8211; not professional. not professional at all.</p>
<p>Here&#8217;s the source code (excluding SWFAddress libraries): <a title="SWFAddressExamples" href="http://blog.leeburrows.com/wp-content/uploads/2011/08/SWFAddressExamples.zip">SWFAddressExamples.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.leeburrows.com/2011/08/javascript-for-flashers-swfaddress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript for Flashers Part 1: SWFObject</title>
		<link>http://blog.leeburrows.com/2011/07/javascript-for-flashers-swfobject/</link>
		<comments>http://blog.leeburrows.com/2011/07/javascript-for-flashers-swfobject/#comments</comments>
		<pubDate>Mon, 25 Jul 2011 11:00:39 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[SWFObject]]></category>

		<guid isPermaLink="false">http://blog.leeburrows.com/?p=1513</guid>
		<description><![CDATA[As fantastic as Flash is, it can&#8217;t do everything; sometimes we need to use other technologies to get results. For Flash running in a browser, JavaScript is a useful (and sometimes essential) tool. I categorise my use of JavaScript into three areas: 1. Essential: embedding Flash in HTML (and providing alternative content), 2. Semi-essential: deep-linking [...]]]></description>
			<content:encoded><![CDATA[<p>As fantastic as Flash is, it can&#8217;t do everything; sometimes we need to use other technologies to get results. For Flash running in a browser, JavaScript is a useful (and sometimes essential) tool.</p>
<p>I categorise my use of JavaScript into three areas: 1. Essential: embedding Flash in HTML (and providing alternative content), 2. Semi-essential: deep-linking (using browser back/next buttons for Flash navigation) and 3. Optional: everything else. I will cover <a title="JavaScript for Flashers Part 2: SWFAddress" href="http://blog.leeburrows.com/2011/08/javascript-for-flashers-swfaddress/">2</a> &amp; <a title="JavaScript for Flashers Part 3: ExternalInterface" href="http://blog.leeburrows.com/2011/08/javascript-for-flashers-externalinterface/">3</a> in later posts, but first&#8230;<br />
<span id="more-1513"></span></p>
<h3>1. Embedding Flash with SWFObject</h3>
<p>Ok, you can skip over this because you already use SWFObject, right? If you&#8217;re not using it, then you&#8217;re doing it wrong (imho)! To quote the SWFObject website, &#8220;<em>SWFObject is an easy-to-use and standards-friendly method to embed Flash content, which utilizes one small JavaScript file</em>&#8221; &#8211; note the terms &#8220;easy-to-use&#8221;, &#8220;standards-friendly&#8221; and &#8220;one small&#8221;; music to my ears.</p>
<p>Even Adobe recommend and use it. Recent versions of Dreamweaver, Flash Pro and Flash Builder all use it; although not necessarily in the best way &#8211; I never use the HTML that Dreamweaver/Flash/Flash Builder generates, but create it like this&#8230;</p>
<p>First, download the latest code (version 2.2 as i&#8217;m writing) from <a title="SWFObject on Google Code" href="http://code.google.com/p/swfobject/" target="_blank">http://code.google.com/p/swfobject/</a>. Download the zip file and extract the contents for later.</p>
<p>Next, visit the online generator: <a title="Online SWFObject Generator" href="http://www.bobbyvandersluis.com/swfobject/generator/index.html" target="_blank">http://www.bobbyvandersluis.com/swfobject/generator/index.html</a>. This will generate the HTML for us (alternatively, offline AIR and HTML versions can be downloaded from the SWFObject site).</p>
<p>You&#8217;ll see this form:</p>
<p><img class="size-full wp-image-1527 alignnone" title="swfobject code generator" src="http://blog.leeburrows.com/wp-content/uploads/2011/07/swfobject1.jpg" alt="" width="550" height="564" /></p>
<p>Most of the form fields should be fairly self-explanatory but some do need explanation;</p>
<p><strong>SWFObject (.js)</strong></p>
<p>The path and filename of the swfobject.js file that you extracted from the zip file.</p>
<p><strong>Publishing method</strong></p>
<p>Either &#8216;Static&#8217; or &#8216;Dynamic&#8217;. The static method embeds both the flash and the alternative content within the HTML page. The dynamic method only includes the alternative content within the page; it is replaced with the flash content at runtime (ie: once it has been loaded into the browser).</p>
<p><strong>Detect Flash version</strong></p>
<p>The minimum version of Flash Player required to view your swf.</p>
<p><strong>Adobe Express Install</strong></p>
<p>The path and filename of the expressInstall.swf file from zip file (only required if ticked). This allows the user to download and install the latest version of Flash Player without having to visit the Adobe site (as long as they already have version 6.0.65 or later).</p>
<p><strong>Flash (.swf)</strong></p>
<p>The path and filename of the swf file.</p>
<p><strong>Dimensions</strong></p>
<p>Width and height of the swf; either in pixels or percentages.</p>
<p><strong>Flash content id</strong></p>
<p>The ID to give the flash content in the HTML.</p>
<p><strong>More</strong></p>
<p>Further options can be included by clicking on the &#8216;more&#8217; link below the content id field &#8211; this will reveal more fields for attributes (eg: alignment), parameters (eg: background colour) and FlashVars.</p>
<p><strong>HTML Template</strong></p>
<p>HTML document type.</p>
<p><strong>Character encoding</strong></p>
<p>HTML document character encoding.</p>
<p><strong>Alternative content</strong></p>
<p>HTML content to display when Flash content is not shown (eg: when user does not have Flash installed) &#8211; this content will also be indexed by search engines so it&#8217;s a great place to add some <a title="SEO" href="http://en.wikipedia.org/wiki/Search_engine_optimization" target="_blank">SEO</a> stuff.</p>
<h3><span class="Apple-style-span" style="font-size: 15px;">Generate HTML</span></h3>
<p>The only decision to make is whether to use static or dynamic publishing. For me, the only answer is static publishing &#8211; it uses standards compliant markup and will still display the flash content if JavaScript has been turned off or isn&#8217;t available.</p>
<p>All we need to do is complete the form and click on &#8216;Generate&#8217;. This will output the required HTML below the generate button; which can then be copy/pasted in to your preferred HTML editor. Finally, copy the swfobject.js file (and the expressInstall.swf file if you ticked &#8216;Adobe Express Install&#8217;) from the downloaded zip into the location(s) you specificied in the form and you&#8217;re done (there are actually 2 swfobject.js files in the zip; one minimised and one not minimised &#8211; either will do but it&#8217;s best to use the smaller one for obvious reasons).</p>
<p>As i said at the top of the post, using SWFObject is not optional; it&#8217;s essential, it&#8217;s mandatory, it&#8217;s obligatory. It&#8217;s.. well you get the picture.</p>
<p>In  <a title="JavaScript for Flashers Part 2: SWFAddress" href="http://blog.leeburrows.com/2011/08/javascript-for-flashers-swfaddress/">part 2</a>, i will cover the semi-essential stuff &#8211; deep linking using SWFAddress &#8211; and guess what! SWFAddress requires SWFObject to work &#8211; so that make SWFObject essential, mandatory, obligatory&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.leeburrows.com/2011/07/javascript-for-flashers-swfobject/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Get Pixel Colour Below Mouse Pointer</title>
		<link>http://blog.leeburrows.com/2011/06/get-pixel-colour-below-mouse-pointer/</link>
		<comments>http://blog.leeburrows.com/2011/06/get-pixel-colour-below-mouse-pointer/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 19:05:53 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[BitmapData]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://blog.leeburrows.com/?p=1490</guid>
		<description><![CDATA[I&#8217;ve noticed that this question pops up from time to time on forums, and it&#8217;s very straight-forward to do; we can use the draw() method of BitmapData to copy any visual element (in this case, the stage itself) into a BitmapData object. Then we can use the getPixel() method to extract the colour. To grab [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve noticed that this question pops up from time to time on forums, and it&#8217;s very straight-forward to do; we can use the draw() method of BitmapData to copy any visual element (in this case, the stage itself) into a BitmapData object. Then we can use the getPixel() method to extract the colour.<br />
<span id="more-1490"></span></p>
<p>To grab the colour in response to a mouse event:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">//add listener for mouse clicks</span>
<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span> eventHandler<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #339966; font-weight: bold;">function</span> eventHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #009900; font-style: italic;">//1x1 bitmapData to store snapshot</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> bmd<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">BitmapData</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">BitmapData</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">//matrix object to 'move' stage so that pixel under mouse is effectively at 0,0</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">matrix</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Matrix</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Matrix</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">//'move' stage according to mouse x,y values</span>
	<span style="color: #004993;">matrix</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">translate</span><span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">-</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">stageX</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000066; font-weight: bold;">-</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">stageY</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">//take snapshot of stage</span>
	bmd<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">draw</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">matrix</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">//get colour from snapshot data</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> pixel<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = bmd<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">getPixel</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">//trace result</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;colour = &quot;</span><span style="color: #000066; font-weight: bold;">+</span>pixel<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">16</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>First, we setup a 1 pixel by 1 pixel BitmapData object; we are only interested in grabbing one pixel so there&#8217;s no need for anything larger.</p>
<p>Next, we setup a Matrix object and apply its translate method using negative mouse position values &#8211; this effectively moves the object that will have it&#8217;s snapshot taken with the draw() method &#8211; normally draw() will start copying from 0,0 of the source object but we only have room in our BitmapData object for a single pixel &#8211; the translated Matrix allows us to start copying at a different position. Namely, the position of the mouse.</p>
<p>Now we use the draw() method to take the snapshot (if you more details on this and other methods of BitmapData, see <a title="BitmapData Basics Part 1" href="http://blog.leeburrows.com/2010/09/bitmapdata-basics-1/">BitmapData Basics Part 1</a>) and the getPixel() method to extract the colour of the single pixel. Hopefully, you can do something more interesting with the information than trace!</p>
<p>If you want to sample a specific object rather than the whole stage, just add an event listener to it (rather than the stage) and change the matrix translation to:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">matrix</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">translate</span><span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">-</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">localX</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000066; font-weight: bold;">-</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">localY</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>If you&#8217;re not responding to a mouse event, you can still get the mouse position from the display object you are targeting; eg: stage.mouseX or myMovieClip.mouseY. So the code would become (for stage version):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> getColour<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> bmd<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">BitmapData</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">BitmapData</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">matrix</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Matrix</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Matrix</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #004993;">matrix</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">translate</span><span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">-</span><span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">mouseX</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000066; font-weight: bold;">-</span><span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">mouseY</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	bmd<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">draw</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">matrix</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> pixel<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = bmd<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">getPixel</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;colour = &quot;</span><span style="color: #000066; font-weight: bold;">+</span>pixel<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">16</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Told you it was easy&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.leeburrows.com/2011/06/get-pixel-colour-below-mouse-pointer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blatant Plugging</title>
		<link>http://blog.leeburrows.com/2011/06/blatant-plugging/</link>
		<comments>http://blog.leeburrows.com/2011/06/blatant-plugging/#comments</comments>
		<pubDate>Mon, 06 Jun 2011 10:30:43 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[Ego Trip]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash Builder]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Fractals]]></category>
		<category><![CDATA[RobotLegs]]></category>
		<category><![CDATA[Spark]]></category>

		<guid isPermaLink="false">http://blog.leeburrows.com/?p=1469</guid>
		<description><![CDATA[You may have noticed that the frequency of posts on this blog has dropped off recently. Mainly because i have been busy with a client project, but also because i was determined to finally get a personal Flex project finished &#8211; i got a sharp reminder to get on with it when i received an email [...]]]></description>
			<content:encoded><![CDATA[<p>You may have noticed that the frequency of posts on this blog has dropped off recently. Mainly because i have been busy with a client project, but also because i was determined to finally get a personal Flex project finished &#8211; i got a sharp reminder to get on with it when i received an email reminding me to renew the domain name for another year.</p>
<p>Its not totally my fault &#8211; if Adobe are going to release Spark (Flex 4 SDK) and make it much better than Halo (Flex 3) then i can hardly be blamed for wanting to completely rewrite my app (and get to grips with Spark in the process). Anyway, only one year late on delivery &#8211; not bad for an IT project &#8211; it&#8217;s finally finished.<br />
<span id="more-1469"></span></p>
<p>Here&#8217;s the link (i guess the domain tells you what it is): <a title="Mandelbrot Explorer" href="http://mandelbrot-explorer.com" target="_blank">mandelbrot-explorer.com</a>. I am fascinated by fractals, so i built the app to generate desktop wallpapers &#8211; its hardly the only one out there, but all the others seem to be Java applets &#8211; i haven&#8217;t seen a Flex (or pure Flash) one so let me know if you are aware of any.</p>
<p>If you interested in the technicals, it is a Flex 4 application with a standard MVC architecture (utilising RobotLegs). I apologise for the programmer art of the app, but i am not a designer.</p>
<p>As the project was a learning exercise for me, i don&#8217;t want to release the source code &#8211; some of the code was written before i really understood the best way to do things, so i don&#8217;t want to send anybody off in the wrong direction by following what i did. However, some parts of the code can be found elsewhere in this blog:</p>
<ul>
<li>Drawing the Mandelbrot graph uses bitmap drawing methods explained in <a title="BitmapData Basics Part 1" href="http://blog.leeburrows.com/2010/09/bitmapdata-basics-1/">BitmapData Basics Part 1</a></li>
<li>Drawing the graph over multiple frames uses the threading technique as described in <a title="Saving An Image To HD With ActionScript Part 2" href="http://blog.leeburrows.com/2011/01/saving-an-image-to-hd-with-actionscript-part-2/">Saving An Image To HD With ActionScript Part 2</a></li>
<li>Save feature uses the code and encoding classes provided in <a title="Saving An Image To HD With ActionScript Part 2" href="http://blog.leeburrows.com/2011/01/saving-an-image-to-hd-with-actionscript-part-2/">Saving An Image To HD With ActionScript Part 2</a></li>
</ul>
<p>I plan to do some Flex-related posts soon(ish) but for now it&#8217;s back to the day job&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.leeburrows.com/2011/06/blatant-plugging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

