<?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>Stroep &#187; snippet</title> <atom:link href="http://blog.stroep.nl/tag/snippet/feed/" rel="self" type="application/rss+xml" /><link>http://blog.stroep.nl</link> <description>Just a collection of random works - Mark Knol</description> <lastBuildDate>Tue, 07 Sep 2010 07:08:59 +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>Quick way to create an auto-increment index AS3</title><link>http://blog.stroep.nl/2010/08/auto-increment-as3-class/</link> <comments>http://blog.stroep.nl/2010/08/auto-increment-as3-class/#comments</comments> <pubDate>Sun, 22 Aug 2010 13:39:16 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[Code snippets]]></category> <category><![CDATA[actionscript]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[class]]></category> <category><![CDATA[snippet]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=977</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/snippet.jpg" alt="" title="snippet" width="100" height="100" class="alignleft size-full wp-image-588">Quick post here. I'd like to share this very quick way to create an automatic ID or index to your class instances. Mostly I pass an index as parameter to the class instances, or I use a public var to set the index. Using this way it is very easy to create an automatically filled index, since you have to set this up once and never worry again :)]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/snippet.jpg" alt="" title="snippet" width="100" height="100" class="alignleft size-full wp-image-588">Quick post here. I&#8217;d like to share this very quick way to create an automatic ID or index to your class instances. Mostly I pass an index as parameter to the class instance, or I use a public var to set the index. Using this way it is very easy to create an automatically filled index, since you have to set this up once and never worry again <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p><p>Take a look at this code:</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #0066CC;">package</span><br /> <span style="color: #66cc66;">&#123;</span><br /> &nbsp; <span style="color: #b1b100;">public</span> <span style="color: #000000; ">class</span> MyObject<br /> &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; ">var</span> global_index:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;<br /> &nbsp; &nbsp; <span style="color: #b1b100;">public</span> const <span style="color: #0066CC;">INDEX</span>:<span style="color: #0066CC;">int</span> = global_index ++;<br /> &nbsp; <span style="color: #66cc66;">&#125;</span><br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp;</div><p>As you see, I have created a static variable global_index, which is always the same to all MyObject classes. I also created a public constant &#8216;INDEX&#8217;, which would be unique in every instance. When the MyObject instance is created the index will be set to the global_index, and the global_index will increase by one. So that&#8217;s basically the trick to create the auto-increment index for your AS3 class.</p><div class="bbCSH" style="font-family: monospace;">update: Changed global_index to a private static + removed constructor</div> ]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2010/08/auto-increment-as3-class/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Multiple keys made easy with Dictionary</title><link>http://blog.stroep.nl/2010/07/multiple-keys-made-easy-with-dictionary/</link> <comments>http://blog.stroep.nl/2010/07/multiple-keys-made-easy-with-dictionary/#comments</comments> <pubDate>Sun, 11 Jul 2010 11:18:45 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[Code snippets]]></category> <category><![CDATA[actionscript]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[game]]></category> <category><![CDATA[snippet]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=964</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/behind.jpg" alt="Multiple keys made easy with Dictionary" width="100" height="100" class="alignleft size-full wp-image-586">Quick post here, a simple solution I found to have multiple keys enabled. Maybe most real game developers know better or more elegant solutions, but I found this useful enough to share it with you.]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/behind.jpg" alt="Multiple keys made easy with Dictionary" width="100" height="100" class="alignleft size-full wp-image-586">Quick post here, a simple solution I found to have multiple keys enabled. Maybe most real game developers know better or more elegant solutions, but I found this useful enough to share it with you.</p><p>Here we go. Add these variable to your class. This is a Dictionary, which allows you to associate a value with an object key.</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #b1b100;">private</span> <span style="color: #000000; ">var</span> currentKeys:Dictionary = <span style="color: #000000; ">new</span> Dictionary<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp;</div><p>Then, in your constructor or wherever you assign eventListeners, add these (well-known) listeners, with the callback functions:</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">KeyboardEvent</span>;</p><p><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>KeyboardEvent.<span style="color: #006600;">KEY_DOWN</span>, <span style="color: #0066CC;">onKeyDown</span><span style="color: #66cc66;">&#41;</span>;<br /> <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>KeyboardEvent.<span style="color: #006600;">KEY_UP</span>, <span style="color: #0066CC;">onKeyUp</span><span style="color: #66cc66;">&#41;</span>;</p><p><span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> <span style="color: #0066CC;">onKeyDown</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:KeyboardEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; currentKeys<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">keyCode</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000000; ">true</span>;<br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> <span style="color: #0066CC;">onKeyUp</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:KeyboardEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; currentKeys<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">keyCode</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000000; ">false</span>;<br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp;</div><p>Inside your enterframe-event or timer (were you want to detect which keys are pressed), you could these kind of detection.</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">ui</span>.<span style="color: #006600;">Keyboard</span>;<br /> <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;</p><p><span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> <span style="color: #0066CC;">onUpdate</span><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><br /> <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> currentKeys<span style="color: #66cc66;">&#91;</span> Keyboard.<span style="color: #0066CC;">SPACE</span> <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; tank.<span style="color: #006600;">shoot</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> currentKeys<span style="color: #66cc66;">&#91;</span> Keyboard.<span style="color: #0066CC;">RIGHT</span> <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; tank.<span style="color: #006600;">targetDirection</span> -= <span style="color: #cc66cc;">0.1</span>;<br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>&nbsp; &nbsp;<br /> &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> currentKeys<span style="color: #66cc66;">&#91;</span> Keyboard.<span style="color: #0066CC;">LEFT</span> <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; tank.<span style="color: #006600;">targetDirection</span> += <span style="color: #cc66cc;">0.1</span>;<br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>&nbsp; &nbsp;<br /> &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> currentKeys<span style="color: #66cc66;">&#91;</span> Keyboard.<span style="color: #0066CC;">UP</span> <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; tank.<span style="color: #006600;">speed</span> += tank.<span style="color: #006600;">acceleration</span>;<br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>&nbsp; &nbsp;<br /> &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> currentKeys<span style="color: #66cc66;">&#91;</span> Keyboard.<span style="color: #0066CC;">DOWN</span> <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; tank.<span style="color: #006600;">speed</span> /= <span style="color: #cc66cc;">1.8</span>;<br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp;</div><p>I found that if you use some 3 or 4+ combinations of keys at the same time, the keyboard events are not triggered anymore, which is a bit of annoying bug in the FlashPlayer. I mean pressing forward and left and shooting at the same time could be a problem? Well; Hope this helps, I think its very easy and a great usage of the Dictionary.</p> ]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2010/07/multiple-keys-made-easy-with-dictionary/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Update Image class &#8211; added contextMenu</title><link>http://blog.stroep.nl/2009/11/add-contextmenu-to-images/</link> <comments>http://blog.stroep.nl/2009/11/add-contextmenu-to-images/#comments</comments> <pubDate>Fri, 06 Nov 2009 17:35:13 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[Other]]></category> <category><![CDATA[actionscript]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[code]]></category> <category><![CDATA[snippet]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=812</guid> <description><![CDATA[<img src="/wp-content/uploads/image-contextmenu.jpg" alt="" class="alignleft" />I've updated my <a href="http://blog.stroep.nl/2009/06/nlstroeputilsimage/">Image class</a>. It is irritating flashsites break the right-click contextmenu. Well, I can't fix that and Adobe should look at that, but I think it would nice to add some of the browser - features to my image class. ]]></description> <content:encoded><![CDATA[<p><img src="/wp-content/uploads/image-contextmenu.jpg" alt="" class="alignleft" />I&#8217;ve updated my <a href="http://blog.stroep.nl/2009/06/nlstroeputilsimage/">Image class</a>. It is irritating flashsites break the right-click contextmenu. Well, I can&#8217;t fix that and Adobe should look at that, but I think it would nice to add some of the browser &#8211; features to my image class.</p><p>Currently I have these options added to the Image class:</p><blockquote><p> <strong>View image</strong><br /> Views image in new window/tab. I used NavigateToURL to make this happen.</p><p><strong>Save Image as..</strong><br /> Saves image on your computer. I used my ImageClass.saveLocal() to make this happen. This is only available within FP10, but I think this is no problem anymore because < a href = "http://www.adobe.com/products/player_census/flashplayer/version_penetration.html" > 93 %</a> (September 2009) has FlashPlayer 10, so most people have it, and I don&#8217;t support outdated software <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p><p><strong>Copy Image location</strong><br /> Copies the url to the clipboard, using System.setClipboard(url). It&#8217;s a pity I can&#8217;t create a &#8216;Copy Image&#8217; function, because pushing bitmapdata into the clipboard is only supported in AIR.</p><p><strong>Send Image</strong><br /> Sends the url to the mail-client, using &#8220;mailto:&#038;body=&#8221; + src. Also a pity; I can&#8217;t add the image directly into the mail.</p></blockquote><p><strong>How to enable the contextmenu:</strong></p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #0066CC;">import</span> nl.<span style="color: #006600;">stroep</span>.<span style="color: #006600;">utils</span>.<span style="color: #006600;">Image</span>;</p><p><span style="color: #000000; ">var</span> myImage:Image = <span style="color: #000000; ">new</span> Image<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;myImage.jpg&quot;</span><span style="color: #66cc66;">&#41;</span>;<br /> myImage.<span style="color: #006600;">enableContextMenu</span> = <span style="color: #000000; ">true</span>; <span style="color: #808080; font-style: italic;">// enable it!</span><br /> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>myImage<span style="color: #66cc66;">&#41;</span>;<br /> &nbsp;</div><p> I still love this class, I use it a lot. I want to encourage you to please add these kind of usability things to your apps / websites. And share them! Yes, it takes some extra time to create/add it, because this IS the finishing touch, but a lot of people will like this kind of features.</p><p><strong>Download</strong><br /> Check out the <a href="http://code.google.com/p/stroep/source/browse/trunk/nl/stroep/utils/Image.as">Image class</a> at<a href="http://code.google.com/p/stroep">googlecode</a></p> ]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2009/11/add-contextmenu-to-images/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>getBounds for TextFields = getBitmapBounds()</title><link>http://blog.stroep.nl/2009/11/getbitmapbounds/</link> <comments>http://blog.stroep.nl/2009/11/getbitmapbounds/#comments</comments> <pubDate>Thu, 05 Nov 2009 18:05:32 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[Code snippets]]></category> <category><![CDATA[actionscript]]></category> <category><![CDATA[bitmapdata]]></category> <category><![CDATA[snippet]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=799</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/snippet.jpg" alt="Code snippet" align="left" class="alignleft size-full wp-image-588" />I found out the getBounds()-function gives not exactly the right rectangle with a TextField. It always matches the border bounds, even if the borders are invisible. So i've created a more accurate function using getColorBoundsRect().]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/snippet.jpg" alt="Code snippet" align="left" class="alignleft size-full wp-image-588" />I found out the getBounds()-function gives not exactly the right rectangle with a TextField. It always matches the border bounds, even if the borders are invisible. So I&#8217;ve created a more accurate function using getColorBoundsRect().</p><p>Click on the words. Switch between getRect() and getTextFieldBounds() by clicking on the buttons below to see the difference.</p><p><object type="application/x-shockwave-flash" data="http://projects.stroep.nl/getBitmapBounds/GetBitmapBounds.swf" width="550" height="140"><param name="movie" value="http://projects.stroep.nl/getBitmapBounds/GetBitmapBounds.swf" /></object></p><p>» <a href="http://projects.stroep.nl/getBitmapBounds/GetBitmapBounds.zip"><b>Download source</b></a></p> ]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2009/11/getbitmapbounds/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Liquid menu: Better proportions with Math.sqrt</title><link>http://blog.stroep.nl/2009/09/better-proportions-with-square-root/</link> <comments>http://blog.stroep.nl/2009/09/better-proportions-with-square-root/#comments</comments> <pubDate>Tue, 01 Sep 2009 09:27:56 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[Code snippets]]></category> <category><![CDATA[actionscript]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[Math]]></category> <category><![CDATA[snippet]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=740</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/square-root.jpg" alt="Square root" title="Square root" width="100" height="100" class="alignleft size-full wp-image-751" />Found out something useful, which I'd like to share with you. It's about calculating the width of menu-items perfectly.Imaging you are creating a liquid website with a menu. If you want all items to have the same width, you could set the width of each item to totalMenuWidth / items.length. In most cases this will work out. But what is your menu is very small or the items doesn't fit in that fixed width because the text is too long?]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/square-root.jpg" alt="Square root" title="Square root" width="100" height="100" class="alignleft size-full wp-image-751" />Found out something useful, which I&#8217;d like to share with you. It&#8217;s about calculating the width of menu-items perfectly.</p><p>Imaging you are creating a liquid website with a menu. If you want all items to have the same width, you could set the width of each item to totalMenuWidth / items.length. In most cases this will work out. But what is your menu is very small or the items doesn&#8217;t fit in that fixed width because the text is too long?</p><p>Well you could hardcode the width&#8217;s to make your own perfect proportions and spacing, but if your menu is xml-driven and you/someone else adds new menu-items, you have to re-assign the hardcoded widths and you feel unhappy <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> Most hardcoded stuff is evil anyway because it is laziness. (I use it a lot <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> )</p><p>Well, I&#8217;d like to calculate the perfect scaling menu, so I don&#8217;t have to worry the menu will break. You can use the text-length of the menu-item as input for the width calculation. The more letters, the bigger the menu-item, right? So, If you multiply all menu-text-lengths and divide this to the current text-length, you&#8217;ll get a ratio which can be divided from the total menu width. I like this theory. So we need to do this in 2 steps; first get the ratios and get the total menu-text-lengths (with a loop), then calculate and apply this on each item.<br /> Pseudocode:<br /><blockquote>itemwidth = fullMenuWidth / (totalTextLength / menuitem.text.length)</p></blockquote><p>This works fine, and its actually very cool. Now we have a function which actually use some proportions to create a liquid menu. After a while I realized that the proportions weren&#8217;t really right. If the menu text have odd text-length differences (eg 5 letters vs 25 letters), it looks weird. Larger texts take too much width. I wanted the same proportions kinda like a html-table.</p><p>So the menu-items needs to have better proportions. Bigger text should be a smaller and the smaller text should be bigger. After some searching I &#8216;discovered&#8217; <a href="http://en.wikipedia.org/wiki/Square_root">Square root</a>, a.k.a. <a href="http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&#038;file=00001839.html">Math.sqrt()</a> in actionscript. With this function you could &#8216;normalize&#8217; numbers. Bigger numbers are getting smaller, smaller numbers are getting bigger. This was exactly what I needed!</p><p>You can see the result below:</p><p><object type="application/x-shockwave-flash" data="http://projects.stroep.nl/PerfectWidth/PerfectWidth.swf" width="100%" height="235"><param name="movie" value="http://projects.stroep.nl/PerfectWidth/PerfectWidth.swf" /><param name="wmode" value="transparent" /></object></p><p>You can see the differences between the menubars and you&#8217;ll agree to me that the last one is the best <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> I think this theory could be applied to a lot more things, like graphs or grids.</p><p>In case anyone is interested in the code; it can be downloaded <a href="http://projects.stroep.nl/PerfectWidth/perfect-width-src.zip">here</a> (AS3).</p> ]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2009/09/better-proportions-with-square-root/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>How to crop with AS3</title><link>http://blog.stroep.nl/2009/05/how-to-crop/</link> <comments>http://blog.stroep.nl/2009/05/how-to-crop/#comments</comments> <pubDate>Tue, 05 May 2009 20:03:41 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[Code snippets]]></category> <category><![CDATA[actionscript]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[snippet]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=510</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/crop.jpg" alt="" title="Crop" width="100" height="100" class="alignleft size-full wp-image-571" />Quick post; How to crop the bitmap of your stage with AS3<br/><br/><br/>]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/crop.jpg" alt="" title="Crop" width="100" height="100" class="alignleft size-full wp-image-571" />Quick post; How to crop the bitmap of your stage with AS3:</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #000000; ">function</span> crop<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">_x</span>:<span style="color: #0066CC;">Number</span>, <span style="color: #0066CC;">_y</span>:<span style="color: #0066CC;">Number</span>, <span style="color: #0066CC;">_width</span>:<span style="color: #0066CC;">Number</span>, <span style="color: #0066CC;">_height</span>:<span style="color: #0066CC;">Number</span>, displayObject:DisplayObject = <span style="color: #000000; ">null</span><span style="color: #66cc66;">&#41;</span>:Bitmap<br /> <span style="color: #66cc66;">&#123;</span><br /> <span style="color: #000000; ">var</span> cropArea:Rectangle = <span style="color: #000000; ">new</span> Rectangle<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #0066CC;">_width</span>, <span style="color: #0066CC;">_height</span> <span style="color: #66cc66;">&#41;</span>;<br /> <span style="color: #000000; ">var</span> croppedBitmap:Bitmap = <span style="color: #000000; ">new</span> Bitmap<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; ">new</span> BitmapData<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">_width</span>, <span style="color: #0066CC;">_height</span> <span style="color: #66cc66;">&#41;</span>, PixelSnapping.<span style="color: #006600;">ALWAYS</span>, <span style="color: #000000; ">true</span> <span style="color: #66cc66;">&#41;</span>;<br /> croppedBitmap.<span style="color: #006600;">bitmapData</span>.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span>displayObject!=<span style="color: #000000; ">null</span><span style="color: #66cc66;">&#41;</span> ? displayObject : <span style="color: #0066CC;">stage</span>, <span style="color: #000000; ">new</span> Matrix<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span>, -<span style="color: #0066CC;">_x</span>, -<span style="color: #0066CC;">_y</span><span style="color: #66cc66;">&#41;</span> , <span style="color: #000000; ">null</span>, <span style="color: #000000; ">null</span>, cropArea, <span style="color: #000000; ">true</span> <span style="color: #66cc66;">&#41;</span>;<br /> <span style="color: #b1b100;">return</span> croppedBitmap;<br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp;</div><p>Usage:</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #000000; ">var</span> myCroppedImage:Bitmap = crop<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">200</span>, <span style="color: #cc66cc;">200</span> <span style="color: #66cc66;">&#41;</span>;<br /> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span> <span style="color: #66cc66;">&#40;</span> myCroppedImage <span style="color: #66cc66;">&#41;</span>;<br /> &nbsp;</div><p>To save the capture, use the <a href="http://blog.stroep.nl/2008/09/as3-imagesaver-class-updated/">ImageSaver class<br /> </a></p><p>Update: I&#8217;ve added a downloadable <a href="http://code.google.com/p/stroep/source/browse/trunk/nl/stroep/utils/CropUtil.as">CropUtil class</a> to <a href="http://code.google.com/p/stroep/">googlecode</a>.</p> ]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2009/05/how-to-crop/feed/</wfw:commentRss> <slash:comments>11</slash:comments> </item> <item><title>Faster Math.abs()</title><link>http://blog.stroep.nl/2009/01/faster-mathabs/</link> <comments>http://blog.stroep.nl/2009/01/faster-mathabs/#comments</comments> <pubDate>Wed, 28 Jan 2009 18:52:04 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[Code snippets]]></category> <category><![CDATA[actionscript]]></category> <category><![CDATA[snippet]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=298</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/snippet.jpg" alt="" title="snippet" width="100" height="100" class="alignleft size-full wp-image-588" />Quick post :) This function returns the absolute value of a specified number. This is +/- 24x faster than the regular Math.abs().<br/><br/>]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/snippet.jpg" alt="" title="snippet" width="100" height="100" class="alignleft size-full wp-image-588" />Quick post <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> This function returns the absolute value of a specified number. This is +/- 24x faster than the regular Math.abs().</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> <span style="color: #0066CC;">abs</span><span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span><br /> <span style="color: #66cc66;">&#123;</span><br /> <span style="color: #b1b100;">return</span> value &lt; <span style="color: #cc66cc;">0</span> ? -value : value;<br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp;</div> ]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2009/01/faster-mathabs/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> </channel> </rss>