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

<channel>
	<title>webroo.org &#187; Flash</title>
	<atom:link href="http://webroo.org/category/flash/feed/" rel="self" type="application/rss+xml" />
	<link>http://webroo.org</link>
	<description>Matt Sweetman :: Flex &#38; Actionscript Development</description>
	<pubDate>Sun, 17 Jan 2010 18:24:02 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
	<language>en</language>
			<item>
		<title>HTML 5 Canvas vs Flash</title>
		<link>http://webroo.org/2010/01/17/html-5-canvas-vs-flash/</link>
		<comments>http://webroo.org/2010/01/17/html-5-canvas-vs-flash/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 16:11:26 +0000</pubDate>
		<dc:creator>Matt Sweetman</dc:creator>
		
		<category><![CDATA[Actionscript]]></category>

		<category><![CDATA[Flash]]></category>

		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://webroo.org/?p=136</guid>
		<description><![CDATA[Over the holidays I gave myself a break from Actionscript and took a look at the HTML 5 Canvas object. It&#8217;s been a while since I programmed any javascript, and AJAX technologies seem to be the popular choice for web apps these days (goodbye Flex!). I decided to port over an old Actionscript 1 experiment [...]]]></description>
			<content:encoded><![CDATA[<p>Over the holidays I gave myself a break from Actionscript and took a look at the HTML 5 Canvas object. It&#8217;s been a while since I programmed any javascript, and AJAX technologies seem to be the popular choice for web apps these days (goodbye Flex!). I decided to port over an old Actionscript 1 experiment to see how the two technologies performed alongside each other, the results can be seen below.</p>
<p><img src="http://webroo.org/images/snow1.jpg" width="130" height="130" alt="Snowfall image" /><a href="http://webroo.org/media/snowfall/canvassnowfall.html">Canvas Snowfall</a></p>
<p><a href="http://webroo.org/media/snowfall/flashsnowfall.html">Flash Snowfall</a></p>
<p>You&#8217;ll need a modern browser to view the canvas version (eg: Firefox/Chrome/Opera/etc). View the source of the Canvas version for the javascript.</p>
<p>As you can see they look pretty much identical, the differences primarily lie in performance. The Flash version can handle more than twice the number of snowflakes before the frame rate drops. This may be down to the a fundamental difference between Canvas and Flash: Canvas is a rasterised bitmap object, whereas Flash is a vector animation tool. When you draw shapes in Canvas you&#8217;re drawing directly onto a bitmap, the shape can no longer be moved or scaled and is &#8216;baked&#8217; into the image. In some ways it&#8217;s similar to the BitmapData object in Flash. Because of this animation is far easier to accomplish in Flash and I had to adapt parts of the original code to work with the Canvas concept.</p>
<p><span id="more-136"></span>Javascript and AS1 share a lot in similarity as programming languages so porting the code was relatively easy. The Canvas object also has a similar API to the Graphics object in Flash. I&#8217;ll admit Javascript is not my favourite language, by a long shot, the lack of strict typing is difficult to return to after years of AS3, and in more complex scripts nested closures can get messy. It is however fast to program, which can come in very handy when prototyping and experimenting with ideas.</p>
<p>It may be worth investigating <a href="http://processingjs.org/">Processing</a>, a framework for the Canvas object written by John Resig, the guy behind JQuery.</p>
]]></content:encoded>
			<wfw:commentRss>http://webroo.org/2010/01/17/html-5-canvas-vs-flash/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Overlapping hit areas for sibling sprites</title>
		<link>http://webroo.org/2009/12/01/overlapping-hit-areas-for-sibling-sprites/</link>
		<comments>http://webroo.org/2009/12/01/overlapping-hit-areas-for-sibling-sprites/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 14:50:27 +0000</pubDate>
		<dc:creator>Matt Sweetman</dc:creator>
		
		<category><![CDATA[Actionscript]]></category>

		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://webroo.org/?p=124</guid>
		<description><![CDATA[I have a recurring problem with buttons. And when I mean buttons I mean anything that has a mouse handler and inherits from InteractiveObject, which includes Sprite and SimpleButton. The problem is when two or more of these objects are siblings (i.e. in the same container, not nested) and they overlap, then they block each [...]]]></description>
			<content:encoded><![CDATA[<p>I have a recurring problem with buttons. And when I mean buttons I mean anything that has a mouse handler and inherits from <span style="font-family: courier new">InteractiveObject</span>, which includes <span style="font-family: courier new">Sprite</span> and <span style="font-family: courier new">SimpleButton</span>. The problem is when two or more of these objects are siblings (i.e. in the same container, not nested) and they overlap, then they block each other. The top-most one will always receive mouse interaction and anything beneath is blocked.</p>
<p><img src="http://webroo.org/images/layers.jpg" alt="Blocked hit area diagram" /></p>
<p>What I would like is both Sprites to dispatch mouse events when the user interacts. Sometimes you can get round it by nesting buttons and using ROLL_OVER and MOUSE_OVER events, but this isn&#8217;t always practical, especially if there are a lot of overlapping buttons. So I&#8217;ve come up with another solution.</p>
<p>The idea is to create a &#8216;fake&#8217; hit area using the Rectangle object. Rectangle has a method called <span style="font-family: courier new">containsPoint</span>, which you pass a <span style="font-family: courier new">Point</span> object and it returns true or false depending on whether the coordinates fall inside the specified dimensions. So if you pass the mouse coordinates you can determine whether the mouse is inside the &#8216;fake&#8217; hit area. Also, because Rectangle isn&#8217;t a display object it can&#8217;t block mouse interaction, and so you can have multiple overlapping hit areas that all trigger when clicked.</p>
<p><span id="more-124"></span>The <span style="font-family: courier new">containsPoint</span> method has to be continually polled, so it needs to be called everytime the mouse moves. I&#8217;ve written some code that wraps this functionality up for ease of use. Take a look at the class below to see how the insides work.</p>
<p>Download: <a href="http://webroo.org/media/source/HitArea.as">HitArea.as</a></p>
<p>In the example below I&#8217;ve set up two hit areas and overlap them by 100px. The hit areas are invisible, so they won&#8217;t show up on the stage, but when you click on the overlapping section the CLICK event will fire twice, once for each hit area. There are also <span style="font-family: courier new">HitArea.ROLL_OVER</span> and <span style="font-family: courier new">HitArea.ROLL_OUT</span> events, which can be useful.</p>
<div class="codesnip-container" >
<div class="codesnip">hitArea1 = <span class="kw3">new</span> <span class="kw3">HitArea</span><span class="br0">&#40;</span><span class="nu0">200</span>, <span class="nu0">200</span><span class="br0">&#41;</span>; <span class="co1">// Dimensions are passed in the constructor</span><br />
addChild<span class="br0">&#40;</span>hitArea1<span class="br0">&#41;</span>;<br />
hitArea1.<span class="me1">addEventListener</span><span class="br0">&#40;</span><span class="kw3">HitArea</span>.<span class="me1">CLICK</span>, hitAreaClick<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
hitArea2 = <span class="kw3">new</span> <span class="kw3">HitArea</span><span class="br0">&#40;</span><span class="nu0">200</span>, <span class="nu0">200</span><span class="br0">&#41;</span>;<br />
addChild<span class="br0">&#40;</span>hitArea2<span class="br0">&#41;</span>;<br />
hitArea2.<span class="me1">x</span> = <span class="nu0">100</span>; <span class="co1">// overlap the first hit area by 100 px</span><br />
hitArea2.<span class="me1">addEventListener</span><span class="br0">&#40;</span><span class="kw3">HitArea</span>.<span class="me1">CLICK</span>, hitAreaClick<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span class="kw3">private</span> <span class="kw1">function</span> hitAreaClick<span class="br0">&#40;</span>event:Event<span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// will fire twice, even on overlapping area</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">trace</span><span class="br0">&#40;</span><span class="st0">&#8220;click: &#8220;</span> + event.<span class="me1">target</span>.<span class="kw3">name</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p>Unfortunately there are two downsides to this method: firstly it&#8217;s slightly more CPU intensive (it needs to constantly run code whenever the mouse moves). Secondly you can&#8217;t use the hand cursor with the hit area. This is the most problematic from a user-experience point of view. Unfortunately to get a hand cursor you need to set <span style="font-family: courier new">buttonMode=true</span> on the Sprite, and the moment you do that it turns it into a real button and we come back to the original problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://webroo.org/2009/12/01/overlapping-hit-areas-for-sibling-sprites/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Auto size button for Flash components</title>
		<link>http://webroo.org/2009/04/19/auto-size-button-for-flash-components/</link>
		<comments>http://webroo.org/2009/04/19/auto-size-button-for-flash-components/#comments</comments>
		<pubDate>Sun, 19 Apr 2009 10:50:37 +0000</pubDate>
		<dc:creator>Matt Sweetman</dc:creator>
		
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://webroo.org/?p=56</guid>
		<description><![CDATA[I&#8217;ve recently been working outside of Flex in standard Flash &#038; Actionscript, so I thought I&#8217;d take the opportunity to try the CS3/4 components. They&#8217;re much leaner than their bigger brother&#8217;s, and as a consequence don&#8217;t offer anywhere near the functionality, but their simplicity means they&#8217;re fast and easy to work with.
One of the tweaks [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently been working outside of Flex in standard Flash &#038; Actionscript, so I thought I&#8217;d take the opportunity to try the CS3/4 components. They&#8217;re much leaner than their bigger brother&#8217;s, and as a consequence don&#8217;t offer anywhere near the functionality, but their simplicity means they&#8217;re fast and easy to work with.</p>
<p>One of the tweaks I had to make was creating an auto-sizing Button:</p>

<object	type="application/x-shockwave-flash"
			data="/media/swf/autosizebutton.swf"
			width="380"
			height="95">
	<param name="movie" value="/media/swf/autosizebutton.swf" />
</object>
<p>If you type some text into the box you can see the difference. The standard Button doesn&#8217;t change it&#8217;s width based on the label length, which is crucial to any web project if it&#8217;s multi-lingual. So here&#8217;s my solution, with both an example and the code.</p>
<p><span id="more-56"></span><br />
<strong>Code</strong></p>
<p>In order to add the auto-sizing functionality I needed to subclass Button and override the layout routine. I also added a property that allows you to turn the auto-sizing on of off, the default is on.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">package</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> fl.<span class="me1">controls</span>.<span class="me1">Button</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> fl.<span class="me1">core</span>.<span class="me1">InvalidationType</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">text</span>.<span class="me1">TextFieldAutoSize</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">class</span> AutoSizeButton <span class="kw3">extends</span> Button<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw1">function</span> AutoSizeButton<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">super</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">protected</span> <span class="kw1">var</span> _autoSize:<span class="kw3">Boolean</span> = <span class="kw3">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * Whether auto sizing is turned on. Default is true.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw1">function</span> <span class="kw3">get</span> <span class="kw3">autoSize</span><span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">Boolean</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">return</span> _autoSize;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw1">function</span> <span class="kw3">set</span> <span class="kw3">autoSize</span><span class="br0">&#40;</span>value:<span class="kw3">Boolean</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _autoSize = value;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">textField</span>.<span class="kw3">autoSize</span> = _autoSize ? TextFieldAutoSize.<span class="kw3">LEFT</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : TextFieldAutoSize.<span class="me1">NONE</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; invalidate<span class="br0">&#40;</span>InvalidationType.<span class="kw3">SIZE</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">override</span> <span class="kw3">protected</span> <span class="kw1">function</span> drawLayout<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">if</span> <span class="br0">&#40;</span><span class="kw3">autoSize</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Set the component width by</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// calculating text &amp; padding widths</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">var</span> txtPad:Number =<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Number<span class="br0">&#40;</span>getStyleValue<span class="br0">&#40;</span><span class="st0">&#8220;textPadding&#8221;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">_width</span> = <span class="br0">&#40;</span><span class="kw3">textField</span>.<span class="kw3">textWidth</span> + <span class="nu0">4</span><span class="br0">&#41;</span> + <span class="br0">&#40;</span><span class="nu0">2</span> * txtPad<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">super</span>.<span class="me1">drawLayout</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Basically all this class is doing is intercepting the layout drawing routine and pre-calculating the entire component width. Then it calls super.drawLayout() which ensures the text field sizes to the width. The calculation is taken directly from LabelButton, a super class of Button.</p>
<p><strong>Issues</strong></p>
<p>There are a few issues: Firstly it won&#8217;t work with an icon in the button, and secondly it doesn&#8217;t auto-size the height. Also you will need to instantiate it through Actionscript, like so:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw1">var</span> myButton:AutoSizeButton = <span class="kw3">new</span> AutoSizeButton<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
myButton.<span class="me1">label</span> = <span class="st0">&#8220;My first button&#8221;</span>;<br />
addChild<span class="br0">&#40;</span>myButton<span class="br0">&#41;</span>;</div>
</div>
<p>Download source code here: <a href="http://webroo.org/media/source/AutoSizeButton.as">AutoSizeButton.as</a></p>
]]></content:encoded>
			<wfw:commentRss>http://webroo.org/2009/04/19/auto-size-button-for-flash-components/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
