<?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>mandarin.no &#187; AS3</title>
	<atom:link href="http://mandarin.no/category/as3/feed/" rel="self" type="application/rss+xml" />
	<link>http://mandarin.no</link>
	<description></description>
	<lastBuildDate>Sun, 21 Mar 2010 20:33:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>AS3 snippet #4: Returning Booleans</title>
		<link>http://mandarin.no/as3/as3-snippet-4-returning-booleans/</link>
		<comments>http://mandarin.no/as3/as3-snippet-4-returning-booleans/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 20:33:26 +0000</pubDate>
		<dc:creator>Thomas Viktil</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[boolean]]></category>
		<category><![CDATA[return]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://mandarin.no/?p=113</guid>
		<description><![CDATA[When returning a Boolean you can do the validation inside the return statement. Usually I would do something like this: function doTest():Boolean {     var returnValue:Boolean = false;     if (someValue &#62; otherValue) { returnValue = true; }     return returnValue; } Instead, you could shorten it down to only one line of code like this: [...]]]></description>
			<content:encoded><![CDATA[<p>When returning a Boolean you can do the validation inside the return statement. Usually I would do something like this:</p>
<pre>function doTest():Boolean
{
    var returnValue:Boolean = false;
    if (someValue &gt; otherValue) { returnValue = true; }
    return returnValue;
}</pre>
<p>Instead, you could shorten it down to only one line of code like this:</p>
<pre>function doTest():Boolean
{
    return (someValue &gt; otherValue);
}</pre>
<p>doTest will return true if someValue is greater than otherValue. Pretty neat huh?</p>
]]></content:encoded>
			<wfw:commentRss>http://mandarin.no/as3/as3-snippet-4-returning-booleans/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Garbage collecting in AIR</title>
		<link>http://mandarin.no/as3/garbage-collecting-in-air/</link>
		<comments>http://mandarin.no/as3/garbage-collecting-in-air/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 15:06:57 +0000</pubDate>
		<dc:creator>Thomas Viktil</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[air]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[adobe]]></category>
		<category><![CDATA[garbage collector]]></category>
		<category><![CDATA[memory usage]]></category>
		<category><![CDATA[system.gc]]></category>

		<guid isPermaLink="false">http://mandarin.no/?p=110</guid>
		<description><![CDATA[At a recent project I had to make a video loop indefinitely and most importanly: seemless. I tried all kinds of different methods, including using FLV, F4V, M4V, FLVplayback component, the Video class, embedded video and a few other. The only solution that worked was (in my opinion) the most outdated one. I setup a [...]]]></description>
			<content:encoded><![CDATA[<p>At a recent project I had to make a video loop indefinitely and most importanly: seemless. I tried all kinds of different methods, including using FLV, F4V, M4V, FLVplayback component, the Video class, embedded video and a few other. The only solution that worked was (in my opinion) the most outdated one.</p>
<p>I setup a new SWF file in Flash Professional and imported the video to stage. I chose to create keyframes for each frame of the video, set the JPG compression rate to 100 and then exported the video as a SWF. This was the only method I could find that would make the video loop seamlessly. It solved one problem but introduced another.</p>
<p>As the application ran I monitored the memory usage and soon discovered that the application ate up more and more memory for each time the video looped. To solve this problem I forced the garbage collector to run each time the video completed a loop. This kept the memory usage stable and the application has now been running for about a month.</p>
<p>This is how you run the garbage collector:</p>
<blockquote>
<pre>System.gc()</pre>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://mandarin.no/as3/garbage-collecting-in-air/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AS3 Snippet #3: Remove all childs</title>
		<link>http://mandarin.no/as3/as3-snippet-3-remove-all-childs/</link>
		<comments>http://mandarin.no/as3/as3-snippet-3-remove-all-childs/#comments</comments>
		<pubDate>Sat, 02 May 2009 02:05:30 +0000</pubDate>
		<dc:creator>Thomas Viktil</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[child]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[event]]></category>
		<category><![CDATA[eventhandler]]></category>
		<category><![CDATA[parent]]></category>
		<category><![CDATA[remove]]></category>
		<category><![CDATA[removeChild]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://mandarin.no/?p=79</guid>
		<description><![CDATA[I code snippet I tend to use quite often. And one that I tend to forget how I wrote the last time i used it. while (this.numChildren &#62; 0) {     removeChild(this.getChildAt(0)); } Update: And when you want to remove a child as a part of an event handler, do this: event.target.parent.removeChild(event.target); Let&#8217;s say [...]]]></description>
			<content:encoded><![CDATA[<p>I code snippet I tend to use quite often. And one that I tend to forget how I wrote the last time i used it.</p>
<pre>while (this.numChildren &gt; 0)
{
    removeChild(this.getChildAt(0));
}</pre>
<p><strong>Update:</strong></p>
<p>And when you want to remove a child as a part of an event handler, do this:</p>
<pre>event.target.parent.removeChild(event.target);</pre>
<p>Let&#8217;s say you want to delete the button you just clicked. The button dispatches an event, and this line of code goes in the event handler. event.target is the button (or what ever object you clicked). It does look at bit strange, because you have to tell the button that it&#8217;s parent should delete the button. The button can&#8217;t delete itself.</p>
<p><strong>Note:</strong></p>
<p>If you&#8217;re working with Flex, use <span style="color: #333333;"><strong>this.removeAllChildren();</strong></span> Thank you Bjørnar <img src='http://mandarin.no/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://mandarin.no/as3/as3-snippet-3-remove-all-childs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tips: Reading variables from Arduino using Flash, Flex or AIR</title>
		<link>http://mandarin.no/as3/tips-reading-variables-from-arduino-using-flash-flex-or-air/</link>
		<comments>http://mandarin.no/as3/tips-reading-variables-from-arduino-using-flash-flex-or-air/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 16:24:07 +0000</pubDate>
		<dc:creator>Thomas Viktil</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[air]]></category>
		<category><![CDATA[baud]]></category>
		<category><![CDATA[configure]]></category>
		<category><![CDATA[physical computing]]></category>
		<category><![CDATA[socket]]></category>
		<category><![CDATA[socket server]]></category>

		<guid isPermaLink="false">http://mandarin.no/?p=69</guid>
		<description><![CDATA[If you have experienced getting only gibberish from the Arduino when tracing it&#8217;s output using Flash, Flex or AIR, you might have misconfigured the socket server. I spent a whole day trying to figure out why I only got square boxes, a couple of random numbers and an occasional &#8216;f&#8217; now and then. I read [...]]]></description>
			<content:encoded><![CDATA[<p>If you have experienced getting only gibberish from the Arduino when tracing it&#8217;s output using Flash, Flex or AIR, you might have misconfigured the socket server.</p>
<p>I spent a whole day trying to figure out why I only got square boxes, a couple of random numbers and an occasional &#8216;f&#8217; now and then. I read somewhere that the configuration file for the socket server had to match the settings of the serial drivers. I didn&#8217;t pay much attention to it because I took it for granted that it was configured using a set of common settings. It turned out that I was wrong.</p>
<p>My drivers were set to 9600 baud, while my socket server ran at 19200 baud. Setting the socket server to 9600 baud solved all of my problems! Yes, all of them.</p>
]]></content:encoded>
			<wfw:commentRss>http://mandarin.no/as3/tips-reading-variables-from-arduino-using-flash-flex-or-air/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActionScript 3: Events</title>
		<link>http://mandarin.no/as3/actionscript-3-events/</link>
		<comments>http://mandarin.no/as3/actionscript-3-events/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 13:54:02 +0000</pubDate>
		<dc:creator>Thomas Viktil</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://mandarin.no/?p=33</guid>
		<description><![CDATA[A couple of days ago I encountered a problem with Events. I clicked and object, but another returned the event. Despite adding mouseEnabled = false to the objects I didn&#8217;t care about, I still couldn&#8217;t figure it out. Events is nothing new to me, but this was beginning to get on my nerves. After a [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of days ago I encountered a problem with Events. I clicked and object, but another returned the event. Despite adding mouseEnabled = false to the objects I didn&#8217;t care about, I still couldn&#8217;t figure it out. Events is nothing new to me, but this was beginning to get on my nerves.</p>
<p>After a bit of reading I stumbled across an article on <a href="http://www.adobe.com/devnet/">Adobe&#8217;s Developer Connection</a><font style="position: absolute;overflow: hidden;height: 0;width: 0"><a href="http://sikongroup.com/rentacar/">&#1082;&#1086;&#1083;&#1080; &#1087;&#1086;&#1076; &#1085;&#1072;&#1077;&#1084;</a></font>, written by Trevor McCauley (<a href="http://www.senocular.com/">Senocular</a>), about events in ActionScript 3 (AS3). It&#8217;s basic stuff, and a bit old, but gives some background info that really helped me better understand the anatomy of the event dispatchers and how the entire event thingamybob works under the hood.</p>
<p>It might even be of help to you too. Read it if you like: <a href="http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html">Introduction to event handling in ActionScript 3.0</a></p>
]]></content:encoded>
			<wfw:commentRss>http://mandarin.no/as3/actionscript-3-events/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Actionscript: Accessing MovieClips from an externally loaded SWF</title>
		<link>http://mandarin.no/as3/actionscript-accessing-movieclips-from-an-externally-loaded-swf/</link>
		<comments>http://mandarin.no/as3/actionscript-accessing-movieclips-from-an-externally-loaded-swf/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 22:51:39 +0000</pubDate>
		<dc:creator>Thomas Viktil</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[assets]]></category>
		<category><![CDATA[big spaceship]]></category>
		<category><![CDATA[bulkloader]]></category>
		<category><![CDATA[document class]]></category>
		<category><![CDATA[external]]></category>
		<category><![CDATA[getdefinitionbyname]]></category>
		<category><![CDATA[load]]></category>
		<category><![CDATA[swf]]></category>

		<guid isPermaLink="false">http://mandarin.no/?p=26</guid>
		<description><![CDATA[For the time I&#8217;m working on a website and so far I&#8217;ve only spent some time doing research around all the ideas I have. One of them was to put all MovieClips in one externally loaded SWF. Loading the SWF is easy, but how do you access the individual MovieClips? Easy as 1-2-3! For loading [...]]]></description>
			<content:encoded><![CDATA[<p>For the time I&#8217;m working on a website and so far I&#8217;ve only spent some time doing research around all the ideas I have. One of them was to put all MovieClips in one externally loaded SWF. Loading the SWF is easy, but how do you access the individual MovieClips? Easy as 1-2-3!</p>
<p>For loading the assets I use <a href="http://code.google.com/p/bulk-loader/">BulkLoader</a>; so far it seems like a pretty good alternative to writing a bunch of code myself. I like.</p>
<p>In the assets.swf I have only stuffed the library with MovieClips, and the ones I&#8217;d like to access from the outside are set to linked. Nothing is placed on stage. The document root to the SWF is set to a custom class. It is within this class the trick lays. And the trick is to set up a public function that returns any MovieClip to the parent SWF.</p>
<p>The parent SWF loads assets.swf and puts it in a variable. The public function in the assets document class is now accessible by dot syntax. By calling this function we can easily add MovieClips from the assets library to any MovieClip in the parent SWF.</p>
<p>Here&#8217;s the document class for assets.swf:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">getDefinitionByName</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Assets <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Assets<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getAsset<span style="color: #66cc66;">&#40;</span>s:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #66cc66;">*</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> a:<span style="color: #000000; font-weight: bold;">Class</span> = getDefinitionByName<span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">&#41;</span> as <span style="color: #000000; font-weight: bold;">Class</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> a<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>What this does is first of all import <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#getDefinitionByName()">getDefinitionByName</a> from the flash.utils package. This is a function that will return any object based on the string you give it. Second, and last, is setting up a generic public function that will look for and return an object with the name you provide it with.</p>
<p>In the parent SWF you would set up an eventHandler that puts the loaded SWF in a variable, and then call getAsset(). Like this (note that I&#8217;m using BulkLoader):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> mclibLoaded<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> lib:<span style="color: #0066CC;">MovieClip</span> = loader.<span style="color: #006600;">getContent</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mclib&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">var</span> p1:<span style="color: #0066CC;">MovieClip</span> = lib.<span style="color: #006600;">getAsset</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Page2&quot;</span><span style="color: #66cc66;">&#41;</span>;
	keeper.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>p1<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>loader is the name of the BulkLoader object, and keeper is a MovieClip on stage.</p>
<p>Rather easy, right?</p>
<p>If you&#8217;re interested in knowing more about <a href="http://code.google.com/p/bulk-loader/">BulkLoader</a>, take a look at the <a href="http://code.google.com/p/bulk-loader/w/list">documentation</a>.<br />
The generic function I&#8217;ve written here was taken from <a href="http://labs.bigspaceship.com/2007/11/30/as3-linked-movieclips-from-remote-swfs/">Big Spaceship Labs</a>. They did it in a more elegant way than I initially did. Don&#8217;t thank me, thank them.</p>
]]></content:encoded>
			<wfw:commentRss>http://mandarin.no/as3/actionscript-accessing-movieclips-from-an-externally-loaded-swf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Papervision3D: workaround for onReleaseOutside</title>
		<link>http://mandarin.no/as3/papervision3d-workaround-for-onreleaseoutside/</link>
		<comments>http://mandarin.no/as3/papervision3d-workaround-for-onreleaseoutside/#comments</comments>
		<pubDate>Tue, 10 Jun 2008 23:29:20 +0000</pubDate>
		<dc:creator>Thomas Viktil</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[papervision3d]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[blendmode]]></category>
		<category><![CDATA[button]]></category>
		<category><![CDATA[event]]></category>
		<category><![CDATA[fill]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[mouseevent]]></category>
		<category><![CDATA[multiply]]></category>
		<category><![CDATA[onrelease]]></category>
		<category><![CDATA[onreleaseoutside]]></category>
		<category><![CDATA[pv3d]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[white]]></category>

		<guid isPermaLink="false">http://mandarin.no/?p=19</guid>
		<description><![CDATA[I just stumbled across a very simple way of achieving an onReleaseOutside event when working with Papervision3D. Now, why would I bring up Papervision3D in such a setting? If you, like me, have included a Papervision3D scene on a stage where other 2D objects (like MovieClips and other Flash components) are scattered about, you have [...]]]></description>
			<content:encoded><![CDATA[<p>I just stumbled across a very simple way of achieving an onReleaseOutside event when working with Papervision3D.</p>
<p>Now, why would I bring up Papervision3D in such a setting? If you, like me, have included a Papervision3D scene on a stage where other 2D objects (like MovieClips and other Flash components) are scattered about, you have probably seen a few unexplainable events happening.</p>
<p>I&#8217;ve only done a few tests with PV3D and Flex, but it seems to be easy to limit the size of the 3D scene to the boundaries of a component. But when adding a PV3D scene in a Flash project (creating an instance of a PV3D class and adding it to stage using addChild()) other rules seems to apply.</p>
<p>Once I tried to limit the size of the Papervision3D (PV3D) scene to just a portion of the stage, and center it. But the bloody scene kept taking over the entire stage of the parent class. The result was that coordinate 0,0 i the PV3D class was aligned with 0,0 in the parent class. Way out of the area where it was supposed to be displayed. I managed to solve it somehow&#8230;</p>
<p>Today, working on a different project, I was facing a different challenge. I have a PV3D class added to stage. This class has a few public functions which I intend to access from the parent class using buttons and whatnot. These buttons and whatnot are added on a level (z-index) above the PV3D class, making them visible at all time. You could say the buttons are the GUI, and the PV3D class shows the content.</p>
<p>Listening for MouseEvent.MOUSE_DOWN on the button is easy, but adding a function for when releasing the mouse button outside of the button wasn&#8217;t that easy. I did a bit of googling and found some answers here and there. <a href="http://www.dgrigg.com/post.cfm/11/08/2006/Flex-2-AS-3-onReleaseOutside">Derrick Grigg</a> wrote a post about the issue. Some people dicussed it at <a href="http://www.actionscript.org/forums/archive/index.php3/t-123474.html">ActionScript.org</a>, where <a href="http://www.tink.ws/blog/">Tink</a> gave a simple solution. And Senocular wrote about it over at <a href="http://www.kirupa.com/forum/showthread.php?p=1948182#post1948182">Kirupa</a>, confirming what everyone else has written; add an eventListener to stage which listens for MOUSE_UP events.</p>
<p>That&#8217;s a clever solution, and an easy one too. At MOUSE_DOWN you add an eventListener to stage which listens for MOUSE_UP events. When that event fires, it calls a handler which removes the eventListener from stage. Simple, and it works. Except for when you have a PV3D object covering the stage.</p>
<p>Even if the PV3D object is empty and therefore seems quite transparent, one would think that the stage is clickable. But it isn&#8217;t. The eventListener on stage will never fire because of the PV3D object. The solution must be to add an object above the PV3D object and make that one listen for MOUSE_UP events. But wait a second! That would make the PV3D object unclickable. We must make the listener object appear temporary.</p>
<p>We could use addChildAt and removeChildAt to dynamically place the object above the PV3D object, but underneath the button, and then remove it when not needed. But that would require us to keep track of where we place various objects and making sure we remove the right ones and so on. Sounds like hassle.</p>
<p>I decided to place a permanent MovieClip on a layer between the PV3D object and the button. Yes, the exact opposite of what I&#8217;ve just wrtten. It&#8217;s permanently stuck to stage, but appears temporary. This object is empty for most of the time but filled when needed. Filling the MovieClip is easily done by accessing the <strong>graphics</strong> property of the MovieClip. When I click the button I run a Fill() command to fill the object with a white color. When the button releases I simply run a clear() command to remove the fill. This way I can turn the object on or off.</p>
<p>The object also has the eventListener permanently attached making it unnecessary to add and remove the eventListener for each click and release.</p>
<p>The object has no alpha property set to 0, but yet it is transparent. I read somewhere that someone had found out that using <strong>blendMode</strong> was faster and less CPU intensive that using alpha = 0. I know from experience that semitransparent objects can reduce performance (and in this project, performance is an issue), so I chose to stick with blendMode. When I attach the object to stage, I set <strong>blendMode</strong> to <strong>multiply</strong>. Multiplying the color white to any other color makes it invisible. <a href="http://en.wikipedia.org/wiki/Blend_modes#Multiply">Wikipedia</a> hasn&#8217;t the best explanation of multiply, but you&#8217;ll see that the formula is <span style="color: #0000ff;">result color = top color * bottom color / 255</span>. Since the bottom color remains unchanged when multiplying with white, white would represent the value 0. And multiplying anything with 0 results in 0.</p>
<p>So there you have it. Filling and clearing a MovieClip to turn it on and off.</p>
<p>As a final note I&#8217;d like to add one thing. If one would like to do something with the button (like changing it&#8217;s state) one would probably fall for the temptation of using the event object returned by the MouseEvent to refer to the button (event.currentTarget.something). Keep in mind that there are 2 separate objects calling this handler, making the event object refer to 2 different objects. So, as in my case, where I wanted to do things with the button, I had to directly refer to the button&#8217;s instance, and not use the event object.</p>
<p>Here are a few lines of code from my project:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> someclass <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _square:Sprite = <span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _btn:ButtonClass = <span style="color: #000000; font-weight: bold;">new</span> ButtonClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> someclass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		addChild<span style="color: #66cc66;">&#40;</span>PV3D<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Add the PV3D object</span>
&nbsp;
		_square.<span style="color: #006600;">blendMode</span> = <span style="color: #ff0000;">&quot;multiply&quot;</span>;
		_square.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_UP</span>, scrollRelease<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Listen for button release</span>
		addChild<span style="color: #66cc66;">&#40;</span>_square<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		addChild<span style="color: #66cc66;">&#40;</span>_btn<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">// This function has been added to the _btn object</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> scrollPress<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		_square.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xFFFFFF<span style="color: #66cc66;">&#41;</span>;
		_square.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">500</span>, <span style="color: #cc66cc;">250</span><span style="color: #66cc66;">&#41;</span>;
		_square.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> scrollRelease<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		_square.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">clear</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://mandarin.no/as3/papervision3d-workaround-for-onreleaseoutside/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Actionscript: Outputting a grid using only one for-loop</title>
		<link>http://mandarin.no/as3/actionscript-outputting-a-grid-using-only-one-for-loop/</link>
		<comments>http://mandarin.no/as3/actionscript-outputting-a-grid-using-only-one-for-loop/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 20:44:09 +0000</pubDate>
		<dc:creator>Thomas Viktil</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[column]]></category>
		<category><![CDATA[grid]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[modulo]]></category>
		<category><![CDATA[modulus]]></category>
		<category><![CDATA[row]]></category>

		<guid isPermaLink="false">http://mandarin.no/?p=18</guid>
		<description><![CDATA[Sometime this winter a friend of mine (Knut Urdalen) introduced me to a part of mathematics called modular arithmetic. Now that is a nifty little thing. In modular arithmetic, or modulus if you like, numbers reset themselves when reaching a given limit. Modulus is also known as &#8220;clock arithmetic&#8221;, and the clock is a very [...]]]></description>
			<content:encoded><![CDATA[<p>Sometime this winter a friend of mine (<a href="http://www.urdalen.com/" target="_self">Knut Urdalen</a>) introduced me to a part of mathematics called <a href="http://mathworld.wolfram.com/ModularArithmetic.html" target="_self">modular arithmetic</a>. Now that is a nifty little thing.</p>
<p>In modular arithmetic, or modulus if you like, numbers reset themselves when reaching a given limit. Modulus is also known as &#8220;clock arithmetic&#8221;, and the clock is a very good example for explaining modulus.</p>
<p>A 12 hour clock has a limit of 12, making the hours reset at 12 o&#8217;clock. So instead of becoming 13, it simply jumps back to 1. The same thing happens with a 24 hour clock, but at 24 hours instead of 12. This is as simple as I would&#8217;ve explained it, but <a href="http://mathworld.wolfram.com/ModularArithmetic.html" target="_self">Wolfram</a> has a more in-depth article with links to further reading. I&#8217;m sure <a href="http://www.google.com/search?hl=en&amp;q=+Modular+Arithmetic" target="_self">Google</a> has even more.</p>
<p>In ActionScript you can use modulus like this: x = y % z. Which reads like &#8220;x is y modulus to z&#8221;.</p>
<p>Following the example of the clock we would have to do a modulus of 12 on the total numbers of hours since the beginning of time to get the current time. Why? Ever since we started keeping track of time the numbers of hours have kept increasing. Time never stops. So instead of saying &#8220;oh, the time is 87654826253745 hours&#8221;, we do a modulus of 12 on 87654826253745 and get 7. And by looking out of the window we can tell if it is morning or evening.</p>
<p>Modulus can however not be used to count the number of turns the hour hand has turned at 87654826253745 o&#8217;clock. It only knows when it has reached the limit, not how many times it has turned.</p>
<p>So, as you probably have guessed already modulus is a drop dead simple way of outputting grids. And this is how I would&#8217;ve made a grid of images:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> _max:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">20</span>; <span style="color: #808080; font-style: italic;">// Total number of items</span>
<span style="color: #0066CC;">private</span> _rows:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">5</span>; <span style="color: #808080; font-style: italic;">// Items per row</span>
<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">_width</span>:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">100</span>;
<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">_height</span>:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">100</span>;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> createGrid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
       <span style="color: #000000; font-weight: bold;">var</span> ix:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span>;
        <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&amp;</span>lt;_max; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// Iterate through all items</span>
        <span style="color: #66cc66;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">var</span> col:<span style="color: #0066CC;">Number</span> = i <span style="color: #66cc66;">%</span> _rows;
                col==<span style="color: #cc66cc;">0</span> ? ++ix : <span style="color: #0066CC;">void</span>;
                x = ix <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">_width</span>;
                y = col <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">_height</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Let&#8217;s look at each of the lines;<br />
01 : define max number of items in the grid<br />
02 : define max number of items in each row<br />
03 &amp; 04 : width and height of each of the items<br />
08 : x position of row &lt;em&gt;n&lt;/em&gt; (increases for each new row)<br />
09 : loop through all items<br />
11 : col is &#8216;current item&#8217; modulus to &#8216;items per row&#8217;<br />
12 : if col has reset, increase ix by one. Here we jump to the next column<br />
13 : place item at x position &#8216;ix * _width&#8217;<br />
14 : place item at y position &#8216;col * _height&#8217;</p>
<p>And that&#8217;s about it. Simple, right?</p>
<p>If anyone of a more mathematical kind of person discovers something wrong, or would&#8217;ve explained modulus in a better way, do arrest me. I&#8217;m still learning.</p>
]]></content:encoded>
			<wfw:commentRss>http://mandarin.no/as3/actionscript-outputting-a-grid-using-only-one-for-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making a YouTube-esque video player in Flex</title>
		<link>http://mandarin.no/as3/making-a-youtube-esque-video-player-in-flex/</link>
		<comments>http://mandarin.no/as3/making-a-youtube-esque-video-player-in-flex/#comments</comments>
		<pubDate>Sun, 13 Apr 2008 11:44:15 +0000</pubDate>
		<dc:creator>Thomas Viktil</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[buffering]]></category>
		<category><![CDATA[custom preloader]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[FLV]]></category>
		<category><![CDATA[MetadataReceived]]></category>
		<category><![CDATA[NetConnection]]></category>
		<category><![CDATA[video]]></category>
		<category><![CDATA[VideoDisplay]]></category>

		<guid isPermaLink="false">http://mandarin.no/as3/making-a-youtube-esque-video-player-in-flex/</guid>
		<description><![CDATA[Earlier this week I finished a project for one of our clients. The client produces quite lot of video content, and wanted a video player that could be embedded in other websites. This was a rather straight forward project where I got to use a lot of things I already knew. But still I learned [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier this week I finished a project for one of our clients. The client produces quite lot of video content, and wanted a video player that could be embedded in other websites. This was a rather straight forward project where I got to use a lot of things I already knew. But still I learned a few new things.</p>
<p>One of the requirements was the file size of the finished SWF. I&#8217;d like to keep it to a minimum with no preloading before the buffering starts. It has to look good, so some graphics will have to be included, but the video is what people wants to see. One of the tricks to reduce filesize and loading time is avoiding using container classes like the canvas, hbox, vbox and the likes. The container classes are handy and useful for laying out your components. But they contain a lot of functionality I didn&#8217;t need, and whenever you add a component to a container class, it will redraw itself. So the initialization process will be slower. (Note to self; put all components inn the container before you add it to stage. That will reduce number of redraws)</p>
<p>As a general rule; if all you need to do is place the component at a particular coordinate; try avoiding using container classes and rather use the x, y properties of the component itself. It is very tempting to let the container do the positioning for you, but with a few extra lines of code, you can most likely avoid using them.</p>
<p>So I discarded the thought of using any containers at all. So how do I approach the placement of the components? Well, first of all I need to find the dimensions of the FLV, and then place the control bar accordingly.</p>
<p>When I first started making Flex apps, I read a lot of articles about developing with Flex. One of them was about the application startup order. I realized that was a subject worth paying attention to, but never did. And all of my Flex apps so far have worked fine. But this time I started to understand the importance of knowing how your Flex app builds itself up.</p>
<p>The video player reads a few FlashVars at startup, one of them being the URL to the FLV. Now, the dimensions of the FLV can vary from file to file, so sending the width and height will only cause more work to be done server side. Flex (and Flash) have a very easy to use event for retrieving the meta data of a FLV. So I decided to keep the number of FlashVars down to a minimum, and rather rely on the MetadataReceived event.</p>
<p>The MetadataReceived event doesn&#8217;t fire until the first part of the FLV has been read. So, since the layout of the video player is waiting for the dimensions of the FLV, I have would have to put this function at the earliest possible stage of the creation process. Sounds easy, but there was one thing that I didn&#8217;t think would matter but did; buffer time.</p>
<p>I was thinking about implementing some kind of dynamic buffering routine to take in account of the various bandwith&#8217;s people have. But the video&#8217;s are rarely longer than 30 seconds and on an average of about 6-7MB. So I figured it would be sufficient to just set the buffering to about 25-30% of the total playtime. And so I did.</p>
<p>Now, what happends when you set the bufferTime property of the videoDisplay component to e.g. 5 seconds, is that videoDisplay will wait until the buffer is full before triggering the MetadataReceived event. I don&#8217;t see any logical reason for this, but that&#8217;s the way it is. The solution to this is simply setting the bufferTime to 0 at initialization. The MetadataReceived will then trigger immediately, giving me all of the details on the FLV I needed. In the event handler for the Metadata-event I simply adjust the bufferTime up to the desired level. It is important to set the autoplay attribute of the VideoDisplay to false, or else the video will try to play immediately since the buffertime is 0.</p>
<p>At this time I have all of the information I need for placing the control bar. In the event handler for MetadataReceived I simply set the control bar&#8217;s y parameter to the height of the video. And voila! The layout is finished, and it all happend withing a fraction of a second.</p>
<p>To give the viewer an indication of the buffertime, I simply used a ProgressBar component. And on top of that I put an HSlider for scrubbing. In retrospect I now see that the ProgressBar is overkill for this purpose. It features a lot of functionality I don&#8217;t use, and I could have managed to achieve the same result using a Sprite which I scale in width (just like with the good old preloaders we made when AS1 and 2 was the big thing).</p>
<p>I mentioned earlier that I didn&#8217;t want any preloading of the application. But Flex throws in a preloader no matter what. In many cases this is a blessing, because you don&#8217;t have to make it yourself. But in this case, it&#8217;s a fair bit annoying. I guess it has to be there because of the creation process, which can take a few seconds -even on tiny applications. So my only option was to make it not look like the standard Flex preloader. I found a few articles on customizing the Flex preloader which helped me. Luckily, this doesn&#8217;t take more than a second in this application, so I simply used the client&#8217;s logo as a preloader. No indication of any progress at all.</p>
<p>These are the few things I learned while working on this project, which will most likely help me attack my next project in a better and more efficient way. Now, there&#8217;s one more thing I&#8217;d like to make a note of. And that&#8217;s something I didn&#8217;t learn.</p>
<p>It seems to me the VideoDisplay component doesn&#8217;t have any events for when the file is not found. I use the debugger version of the Flash Player, and that throws me a large error message saying the file was not found. It would&#8217;ve been better (in terms of usability) for me to trace this error using events instead, and then outputting a message onscreen saying &#8220;file not found&#8221;, or even showing a video where someone says &#8220;Sorry, file not found. But you might want to look at these videos&#8230;&#8221;. I could probably have used NetConnection. If there are anyone out there with an idea, or even better; an answer to this, please leave a comment. I&#8217;d love to hear about it.</p>
<p>Resources<br />
<a href="http://livedocs.adobe.com/flex/3/html/help.html?content=layoutperformance_03.html">Flex 3 &#8211; startup order</a><br />
<a href="http://www.slideshare.net/dcoletta/optimizing-flex-applications">David Colettas slideshow on Flex optimization</a>. It&#8217;s only a slideshow, but there are a few good tips to pick up.<br />
<a href="http://blog.poweredbyhamsters.com/2007/10/02/optimizing-flex-apps.aspx">Powered By Hamsters</a> has a few good tips on optimizing a Flex app.</p>
<p><a href="http://www.alagad.com/go/blog-entry/skinning-with-flex-3-css-explorer">Alagad &#8211; Skinning with Flex 3 CSS Explorer</a><br />
<a href="http://www.adobe.com/devnet/flex/articles/skins_styles.html">Adobe Devnet &#8211; Using CS3 to design styles and skins for Flex 3</a><br />
<a href="http://livedocs.adobe.com/flex/3/html/help.html?content=skinning_3.html">Flex 3 Help &#8211; Applying skins</a>. Some code examples of how to add skins to your components</p>
<p><a href="http://www.onflex.org/ted/2006/07/flex-2-preloaders-swf-png-gif-examples.php">Ted Parick &#8211; Flex custom preloaders</a><br />
<a href="http://flexiblemyself.blogspot.com/2007/10/custom-preloader.html">FLEXibleMySelf &#8211; Custom Preloader (Flex 3)</a></p>
<p>Always helpful:<br />
<a href="http://livedocs.adobe.com/flex/3/langref/index.html">Flex 3 Language Reference</a><br />
<a href="http://examples.adobe.com/flex3/consulting/styleexplorer/Flex3StyleExplorer.html">Flex 3 Style Explorer</a></p>
]]></content:encoded>
			<wfw:commentRss>http://mandarin.no/as3/making-a-youtube-esque-video-player-in-flex/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>totalTime and MetadataReceived from VideoDisplay object in Flex</title>
		<link>http://mandarin.no/as3/totaltime-and-metadatareceived-from-videodisplay-object-in-flex-2/</link>
		<comments>http://mandarin.no/as3/totaltime-and-metadatareceived-from-videodisplay-object-in-flex-2/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 16:53:15 +0000</pubDate>
		<dc:creator>Thomas Viktil</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[bufferTime]]></category>
		<category><![CDATA[MetadataReceived]]></category>
		<category><![CDATA[totalTime]]></category>
		<category><![CDATA[VideoDisplay]]></category>

		<guid isPermaLink="false">http://mandarin.no/as3/totaltime-and-metadatareceived-from-videodisplay-object-in-flex-2/</guid>
		<description><![CDATA[Interesting (and more than a bit annoying) observation; neither the property totalTime or the event MetadataEvent.METADATA_RECEIVED will trigger unless the buffer is full. I just discovered this as I tried to dynamically set the bufferTime to be 30% of the total playtime (I calculate the bufferTime using totalBytes and bytesLoaded). But as I traced values [...]]]></description>
			<content:encoded><![CDATA[<p>Interesting (and more than a bit annoying) observation; neither the property totalTime or the event MetadataEvent.METADATA_RECEIVED will trigger unless the buffer is full.</p>
<p>I just discovered this as I tried to dynamically set the bufferTime to be 30% of the total playtime (I calculate the bufferTime using totalBytes and bytesLoaded). But as I traced values like totalTime, I noticed it would stay at -1 until the video started playing (in other words; the buffer was full). Just for the sake of curiosity I set up an eventlistener listening for the duration property of the MetadataReceived event object. Interesting enough it wouldn&#8217;t trigger until the buffer was full.</p>
<p>It has been filed as a bug at <a href="http://bugs.adobe.com/jira/browse/SDK-14948" target="_blank">Adobe&#8217;s Flex Bug and Issues Management System</a>.</p>
<p>Possible workaround:
<ol>	
<li>set bufferTime to 0 at initialization (this would hopefully trigger the READY event (or MetadataReceived if you prefer)</li>
<p>	
<li>catch the totalTime value</li>
<p>	
<li>pause/stop the video</li>
<p>	
<li>increase bufferTime (or skip number 3 and go directly to number 4. It might work?)</li>
<p></ol>
<p>Technorati Tags: <a class="performancingtags" href="http://technorati.com/tag/flex" rel="tag">flex</a>, <a class="performancingtags" href="http://technorati.com/tag/as3" rel="tag">as3</a>, <a class="performancingtags" href="http://technorati.com/tag/actionscript" rel="tag">actionscript</a>, <a class="performancingtags" href="http://technorati.com/tag/meta%20data" rel="tag">meta data</a>, <a class="performancingtags" href="http://technorati.com/tag/video" rel="tag">video</a>, <a class="performancingtags" href="http://technorati.com/tag/development" rel="tag">development</a></p>
]]></content:encoded>
			<wfw:commentRss>http://mandarin.no/as3/totaltime-and-metadatareceived-from-videodisplay-object-in-flex-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
