<?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; flash</title>
	<atom:link href="http://mandarin.no/tag/flash/feed/" rel="self" type="application/rss+xml" />
	<link>http://mandarin.no</link>
	<description></description>
	<lastBuildDate>Tue, 17 Aug 2010 09:35:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<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>Flash CS3: Adding custom classes to MovieClips in the library</title>
		<link>http://mandarin.no/flash/flash-cs3-adding-custom-classes-to-movieclips-in-the-library/</link>
		<comments>http://mandarin.no/flash/flash-cs3-adding-custom-classes-to-movieclips-in-the-library/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 20:19:55 +0000</pubDate>
		<dc:creator>Thomas Viktil</dc:creator>
				<category><![CDATA[flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[asset]]></category>
		<category><![CDATA[asset management]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[cs3]]></category>
		<category><![CDATA[custom class]]></category>
		<category><![CDATA[document class]]></category>
		<category><![CDATA[extending]]></category>
		<category><![CDATA[external assets]]></category>
		<category><![CDATA[external library]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[linkage]]></category>
		<category><![CDATA[movieclip]]></category>
		<category><![CDATA[package]]></category>
		<category><![CDATA[resources]]></category>
		<guid isPermaLink="false">http://mandarin.no/?p=29</guid>
		<description><![CDATA[Today I was tearing out my hair in frustration of a problem I just couldn&#8217;t seem to figure out. And the solution was just about as stupid as I had hoped it wouldn&#8217;t be. The case was as followed: I&#8217;m putting MovieClips in a SWF for use as a library in another SWF (I&#8217;ll call [...]]]></description>
			<content:encoded><![CDATA[<p>Today I was tearing out my hair in frustration of a problem I just couldn&#8217;t seem to figure out. And the solution was just about as stupid as I had hoped it wouldn&#8217;t be.</p>
<p>The case was as followed: I&#8217;m putting MovieClips in a SWF for use as a library in another SWF (I&#8217;ll call it master SWF). The library SWF has a custom document root with a function that helps me return the MovieClips I need. Some of the MovieClips in the library are extended with custom classes.</p>
<p>When I tried to return a MovieClip from the library SWF I would get an error message saying the MovieClip doesn&#8217;t exist. Even if the class has been set up correctly, the package names are fine, and everything looks right.</p>
<p>The library SWF resides in the root, together with the master SWF. This way I can keep the same document root in both SWF&#8217;s and share classes between. The custom class for the MovieClip I tried to return is in package bb.page, and the name of the class is Frontpage.</p>
<p>In Flash I set the linkage to bb.page.Frontpage, compiled, and expected a positive result. But no. I tried moving everything down to root level, thus flattening the whole directory structure. Removed the package name from the Frontpage class and gave it a try. It actually worked. But it&#8217;s not an optimal solution because I don&#8217;t want to mess up the root directory with tons of files.</p>
<p>Moving everything back in to the directories they came from, I gave a stupid idea a try; renaming the MovieClip in the library. And guess what? Renaming the MovieClip from &#8216;Frontpage&#8217; to &#8216;bb.page.Frontpage&#8217; did the trick. I haven&#8217;t bothered trying to figure out why it&#8217;s like this, but it seems like it just is.</p>
<p>So, conclusion to this;</p>
<blockquote><p><span style="text-decoration: line-through;">Make sure the name of the MovieClip is the same as the linkage class.</span></p></blockquote>
<p><span style="text-decoration: line-through;">Example: class Frontpage resides in bb.page. Linkage class is therefore &#8216;bb.page.Frontpage&#8217;. This will also have to be the name of the MovieClip.</span></p>
<p><strong><span style="color: #ff0000;">UPDATE August 12 2008<br />
</span></strong></p>
<p>While at work today I spent a few hours figuring out how to deal with this problem and came to a new conclusion; what I&#8217;ve written in this article is wrong. I haven&#8217;t managed to come to a perfect solution, but good enough for the project I&#8217;m working on. I&#8217;ll be back later to write more about this.</p>
<p>In the mean time, and if you are looking for a solution to a similar problem, try searching for keywords like as3 asset managemt, resource management, external libraries and such. I found some articles that gave me a better understanding of what I&#8217;m dealing with.</p>
]]></content:encoded>
			<wfw:commentRss>http://mandarin.no/flash/flash-cs3-adding-custom-classes-to-movieclips-in-the-library/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>AS3 Snippet #1: Copy text to clipboard</title>
		<link>http://mandarin.no/as3/as3-snippet-1-copy-text-to-clipboard/</link>
		<comments>http://mandarin.no/as3/as3-snippet-1-copy-text-to-clipboard/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 13:44:18 +0000</pubDate>
		<dc:creator>Thomas Viktil</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[clipboard]]></category>
		<category><![CDATA[copy]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>
		<guid isPermaLink="false">http://d2997367.u306.hosted.servetheworld.net/?p=3</guid>
		<description><![CDATA[A useful little snippet of code for when you want to copy text to the clipboard: System.setClipboard(source.text); Where &#8216;source&#8217; is the id of e.g. a text field.]]></description>
			<content:encoded><![CDATA[<p>A useful little snippet of code for when you want to copy text to the clipboard:</p>
<blockquote>
<pre>System.setClipboard(source.text);</pre>
</blockquote>
<p>Where &#8216;source&#8217; is the id of e.g. a text field.</p>
]]></content:encoded>
			<wfw:commentRss>http://mandarin.no/as3/as3-snippet-1-copy-text-to-clipboard/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>AS3 Snippet #2: Watch for changing properties</title>
		<link>http://mandarin.no/as3/as3-snippet-2-watch-for-changing-properties/</link>
		<comments>http://mandarin.no/as3/as3-snippet-2-watch-for-changing-properties/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 14:32:13 +0000</pubDate>
		<dc:creator>Thomas Viktil</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[changewatcher]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[property]]></category>
		<guid isPermaLink="false">http://d2997367.u306.hosted.servetheworld.net/?p=4</guid>
		<description><![CDATA[Today I stumbled across a handy utility class called ChangeWatcher, which lives inside the mx.binding.utils package. It provides you with a handy way of watching other object&#8217;s properties. Here&#8217;s how it&#8217;s used: ChangeWatcher.watch(object, &#8220;property&#8221;, handler); Object &#8211; the object which owns the property &#8220;property&#8221; &#8211; the property you want to watch handler &#8211; the function [...]]]></description>
			<content:encoded><![CDATA[<p>Today I stumbled across a handy utility class called ChangeWatcher, which lives inside the mx.binding.utils package. It provides you with a handy way of watching other object&#8217;s properties.</p>
<p>Here&#8217;s how it&#8217;s used:</p>
<blockquote><p>
ChangeWatcher.watch(object, &#8220;property&#8221;, handler);
</p></blockquote>
<p>Object &#8211; the object which owns the property<br />
&#8220;property&#8221; &#8211; the property you want to watch<br />
handler &#8211; the function name of the handler</p>
<p>Whenever the property of the object changes, the handler will respond. Quite handy, right?</p>
<p><em>edit:</em></p>
<p>Here&#8217;s an example for you:</p>
<p>Let&#8217;s say we have a class called WatchMe. Inside that class we have a public variable called ImBeingWatched. We&#8217;d like to trigger a function whenever this variable changes. To do this, we simply add one line of code:</p>
<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">binding</span>.<span style="color: #006600;">utils</span>.<span style="color: #66cc66;">*</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #66cc66;">*</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #66cc66;">*</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> WatchMe <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> ImBeingWatched:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">true</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> WatchMe<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ChangeWatcher.<span style="color: #0066CC;">watch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #ff0000;">&quot;ImBeingWatched&quot;</span>, doSomething<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> doSomething<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: #808080; font-style: italic;">// do something ...</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>
<p>As you can see, this works pretty much like using [Bindable] variables. But, in some cases this method proves quite handy.</p>
<p>Update: Oops! Typo. The property that ChangeWatcher is watching must be [Bindable] and Public for it to work.</p>
]]></content:encoded>
			<wfw:commentRss>http://mandarin.no/as3/as3-snippet-2-watch-for-changing-properties/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
