<?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>Cold Constructs &#187; Experiments</title>
	<atom:link href="http://coldconstructs.com/category/experiments/feed/" rel="self" type="application/rss+xml" />
	<link>http://coldconstructs.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 30 Aug 2010 19:28:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=9536</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Pixel-perfect collision detection with 5000+ particles</title>
		<link>http://coldconstructs.com/2009/11/pixel-perfect-collision-detection-with-5000-particles/</link>
		<comments>http://coldconstructs.com/2009/11/pixel-perfect-collision-detection-with-5000-particles/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 19:39:49 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[Experiments]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[collision detection]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[particles]]></category>
		<category><![CDATA[source]]></category>

		<guid isPermaLink="false">http://coldconstructs.com/?p=172</guid>
		<description><![CDATA[And yes, it runs perfectly fine. I&#8217;ve run it at 60+ FPS with 7,000 particles, but that actually isn&#8217;t the limitation (unless your particles are crunching heavy math for eg movement). Rather it&#8217;s the size and number of sprites that we&#8217;re colliding with the particles. Click the image below to pop a lightbox with the [...]]]></description>
			<content:encoded><![CDATA[<p>And yes, it runs perfectly fine. I&#8217;ve run it at 60+ FPS with 7,000 particles, but that actually isn&#8217;t the limitation (unless your particles are crunching heavy math for eg movement). Rather it&#8217;s the size and number of sprites that we&#8217;re colliding with the particles. Click the image below to pop a lightbox with the demo:</p>
<p><a href="http://labs.coldconstructs.com/e/vonLocalCD/bin/vonLocalCD.swf?width=600&amp;height=600" rel="prettyPhoto" title="Adjust the scroll bars at the top to change the parameters."><img src="http://labs.coldconstructs.com/i/vonlocalcd.jpg" alt="vonLocal Collision Detection" class="centered" /></a></p>
<p>To squeeze all the juice out of Flash I employed a couple tricks. <strong>The first</strong> was the particles themselves&#8211;they&#8217;re blitted to a single bitmap which is used as the source image for grabbing collision data from. The particles are also drawn with the raster engine in Flash (multiple <code>setPixel32()</code> ops to give the illusion of a line&#8230; a choppy one anyway) instead of the vector renderer (<code>lineTo()</code>). <strong>The second trick</strong> was to only grab a Vector of pixels from the regions we cared about (within sprite boundaries) every so often, then to loop through the Vector and test it against our desired conditions. Also, since the particle bitmap is more sparse than our sprites as far as opaque pixels go, we test the particle bitmap first, resulting in a lot fewer passes on the first round of conditional statements.</p>
<p><span id="more-172"></span></p>
<p>So my poor man&#8217;s method of making lines is done by interpolating the position of a particle from the previous to the current frame, for a total of 3 <code>setPixel32()</code> operations per iteration. (We can use <code>setPixel()</code> for a solid performance gain, but at a different kind of cost that I&#8217;ll explain later.) To give the particles a long tail we simply decrease the alpha of the particle bitmap a little bit every frame, making older particles fade more as time goes by. This means we can skip <code>fillRect()</code> to clear the bitmap every frame, but I&#8217;m wondering if that&#8217;s faster or slower than <code>ColorTransform</code>. Haven&#8217;t tested, for shame.</p>
<p>I&#8217;ll admit that I haven&#8217;t tried this method with bitmap-based particles but those would work just as well, in theory (using blitted rendering). One of my current projects demands those trailing things so I didn&#8217;t look into it. If you try it, please comment on this post below with your findings <img src="http://coldconstructs.com/random/pint.gif"></img><br />
<em>Click on the right-side arrow to unroll this block of code.</em></p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p172code3'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1723"><td class="code" id="p172code3"><pre class="actionscript3" style="font-family:monospace;">particleMap<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lock</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> _numParticles<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	p = particles<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// get our velocity from the perlin noise map</span>
	vel = noiseMap<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">getPixel</span><span style="color: #000000;">&#40;</span>p<span style="color: #000066; font-weight: bold;">.</span>x<span style="color: #000066; font-weight: bold;">&gt;&gt;</span>3<span style="color: #000066; font-weight: bold;">,</span> p<span style="color: #000066; font-weight: bold;">.</span>y<span style="color: #000066; font-weight: bold;">&gt;&gt;</span>3<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// because noiseMap is 1/9 the size of particleMap</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> brightness<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = vel <span style="color: #000066; font-weight: bold;">/</span> 0xFFFFFF<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> speed<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = 0<span style="color: #000066; font-weight: bold;">.</span>1 <span style="color: #000066; font-weight: bold;">+</span> brightness <span style="color: #000066; font-weight: bold;">*</span> p<span style="color: #000066; font-weight: bold;">.</span>speed<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">angle</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = 360 <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span>brightness <span style="color: #000066; font-weight: bold;">*</span> p<span style="color: #000066; font-weight: bold;">.</span>wander<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">0.0175</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// roughly PI / 180 to convert to radians</span>
	p<span style="color: #000066; font-weight: bold;">.</span>vx = <a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">cos</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">angle</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> speed<span style="color: #000066; font-weight: bold;">;</span>
	p<span style="color: #000066; font-weight: bold;">.</span>vy = <a href="http://www.google.com/search?q=math%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:math.html"><span style="color: #004993;">Math</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">sin</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">angle</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> speed<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// absolute value for velocity calculations</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> pvx<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = p<span style="color: #000066; font-weight: bold;">.</span>vx <span style="color: #000066; font-weight: bold;">&lt;</span> 0 <span style="color: #000066; font-weight: bold;">?</span> <span style="color: #000066; font-weight: bold;">-</span>p<span style="color: #000066; font-weight: bold;">.</span>vx <span style="color: #000066; font-weight: bold;">:</span> p<span style="color: #000066; font-weight: bold;">.</span>vx<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// use bitwise sign flippage</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> pvy<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = p<span style="color: #000066; font-weight: bold;">.</span>vy <span style="color: #000066; font-weight: bold;">&lt;</span> 0 <span style="color: #000066; font-weight: bold;">?</span> <span style="color: #000066; font-weight: bold;">-</span>p<span style="color: #000066; font-weight: bold;">.</span>vy <span style="color: #000066; font-weight: bold;">:</span> p<span style="color: #000066; font-weight: bold;">.</span>vy<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// poor man's lineTo ENGAGED</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> nx<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>p<span style="color: #000066; font-weight: bold;">.</span>vx <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> 1<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// where we are now + half the distance to where we will be</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> ny<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span>p<span style="color: #000066; font-weight: bold;">.</span>vy <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> 1<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> mx<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = nx <span style="color: #000066; font-weight: bold;">-</span> p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// then half THAT distance for a third position...</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> my<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = ny <span style="color: #000066; font-weight: bold;">-</span> p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// actually apply velocity to pixel</span>
	p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span>= p<span style="color: #000066; font-weight: bold;">.</span>vx<span style="color: #000066; font-weight: bold;">;</span>
	p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= p<span style="color: #000066; font-weight: bold;">.</span>vy<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// bounds-check</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">&lt;</span> 0<span style="color: #000000;">&#41;</span> p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = h <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">&gt;</span> h<span style="color: #000000;">&#41;</span> p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">&lt;</span> 0<span style="color: #000000;">&#41;</span> p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = w <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">&gt;</span> w<span style="color: #000000;">&#41;</span> p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// pick our color based on speed</span>
	speed <span style="color: #000066; font-weight: bold;">*</span>= <span style="color: #000000; font-weight:bold;">60</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>speed <span style="color: #000066; font-weight: bold;">&gt;</span> 255<span style="color: #000000;">&#41;</span> speed = <span style="color: #000000; font-weight:bold;">255</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #004993;">color</span> = 255 <span style="color: #000066; font-weight: bold;">&lt;&lt;</span> 24 <span style="color: #000066; font-weight: bold;">|</span> speed <span style="color: #000066; font-weight: bold;">&lt;&lt;</span> 16 <span style="color: #000066; font-weight: bold;">|</span> speed <span style="color: #000066; font-weight: bold;">&lt;&lt;</span> 8 <span style="color: #000066; font-weight: bold;">|</span> speed<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// render the particle three times, each in different places to get a line-ish thing going</span>
	particleMap<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">setPixel32</span><span style="color: #000000;">&#40;</span>p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span> p<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">color</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	particleMap<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">setPixel32</span><span style="color: #000000;">&#40;</span>mx<span style="color: #000066; font-weight: bold;">,</span> my<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">color</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	particleMap<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">setPixel32</span><span style="color: #000000;">&#40;</span>nx<span style="color: #000066; font-weight: bold;">,</span> ny<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">color</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #009900; font-style: italic;">// and we're done. release the pixels!</span>
particleMap<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">unlock</span><span style="color: #000000;">&#40;</span>_r<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></td></tr></table></div>

<p>So that gives us some spotty-but-close-enough trails to help with our collision detection. It works like this: We create a single bitmap to use as a canvas for our particles. We render those particles using <code>setPixel32()</code> as mentioned above. <strong>Sprites will use this bitmap as a source for collision data.</strong> Every other frame or so (more on this later) the sprites will grab a section of pixels from the particle bitmap using something like <code>getVector()</code> (>= Flash 10) or <code>getPixels()</code> (< Flash 10), using their own position and dimensions for the <code>sourceRect</code> parameter.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p172code4'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1724"><td class="line_numbers"><pre>133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
</pre></td><td class="code" id="p172code4"><pre class="actionscript3" style="font-family:monospace;">shipVec = buffer<span style="color: #000066; font-weight: bold;">.</span>getVector<span style="color: #000000;">&#40;</span>_rc<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// get an array of pixels from the whole sprite</span>
_rc<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// change rect to particleBMD's coordinate space</span>
_rc<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
pixVec = _particlebmd<span style="color: #000066; font-weight: bold;">.</span>getVector<span style="color: #000000;">&#40;</span>_rc<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// grab an array of the same area of pixels that our sprite is occupying</span>
<span style="color: #6699cc; font-weight: bold;">var</span> a<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=uint%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:uint.html"><span style="color: #004993;">uint</span></a><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
buffer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lock</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i<span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> _l<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	_pval = pixVec<span style="color: #000000;">&#91;</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// first look at the particle's image for opaque pixels</span>
	a = _pval <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight:bold;">24</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>a <span style="color: #000066; font-weight: bold;">&lt;</span> 100<span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">continue</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// it found an opaque-ish pixel, so let's see if there's an opaque pixel in the ship there too</span>
	_sval = shipVec<span style="color: #000000;">&#91;</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
	a = _sval <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight:bold;">24</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>a <span style="color: #000066; font-weight: bold;">&lt;</span> 100<span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">continue</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #3f5fbf;">/* if we get this far then we have a collision because one
	 * of the ship's pixels is overlapping a particle's pixel.
	 * we're only going to collide with red-colored particles though */</span>
	_pval = _pval <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> 16 <span style="color: #000066; font-weight: bold;">&amp;</span> 0xFF<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// red channel extraction</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>_pval <span style="color: #000066; font-weight: bold;">&gt;</span> 100<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> px<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = <a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000000;">&#40;</span>i <span style="color: #000066; font-weight: bold;">%</span> w<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// translate 1d array coordinate to 2d grid coordinates</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> py<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = <a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000000;">&#40;</span>i <span style="color: #000066; font-weight: bold;">/</span> h<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		buffer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">setPixel32</span><span style="color: #000000;">&#40;</span>px<span style="color: #000066; font-weight: bold;">,</span>py<span style="color: #000066; font-weight: bold;">,</span>0xFF4e88c9<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// colliding pixels are painted on the top of our sprite in a cold blue-ish color</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
buffer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">unlock</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></td></tr></table></div>

<p>As you can see on line 133 and 136, each sprite has a block of pixels ready to examine on each tick. The sprite loops through that block and compares every pixel of its own with the pixels in the Vector it got. If there's a match (eg both pixels are opaque) then look at the particle's pixel in detail to see if it's the <del datetime="2009-10-10T20:40:55+00:00">droid</del> one we're looking for and record its findings. </p>
<p>So you can alter line 151 so that the sprites check for specific colors in the particle bitmap, like anything brighter than dark grey (which I did here, or anything with an alpha greater than <code>0.1</code> or... use your imagination I guess?).</p>
<p>Because grabbing a new Vector from the particle bitmap can be so expensive (depending on the size of the sprite), you can time limit the collection so it only samples the particle bitmap once in a short while. In the demo SWF above I sample it once every 100 milliseconds or so, which comes out to about 24 samples a second (given 25 milliseconds per frame at 60 FPS)... as opposed to 2400 samples a second. The casual human eye can't tell the difference but it yields a significant increase in performance. Most performance tricks I've learned are based are smoke and mirror stuff like this.</p>
<p>You can use <code>setPixel()</code> (no alpha) but you'll <em>have</em> to test for color--obviously for a color that isn't what the background of the particle bitmap is, so it's a little more tricky. It's also totally possible to use other sprites as particles as long as you blit them (using <code>copyPixels()</code> or <code>setVector()</code>) to a single bitmap that can be checked by other sprites that collide with them. If you don't know what blitting is, 8bitRocket <a href="http://www.8bitrocket.com/newsdisplay.aspx?newspage=13430">has a good rundown</a> (it's where I first learned about it actually).</p>
<p>And that's pretty much it. You can download the example files/source below.</p>
<p><a title="download source" href="http://labs.coldconstructs.com/e/vonLocalCD.zip"><img class="centered" src="http://coldconstructs.com/siteFiles/download_button.png" alt="download source" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://coldconstructs.com/2009/11/pixel-perfect-collision-detection-with-5000-particles/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Pixel transition</title>
		<link>http://coldconstructs.com/2009/04/pixel-transition/</link>
		<comments>http://coldconstructs.com/2009/04/pixel-transition/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 20:35:18 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[Experiments]]></category>
		<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[source]]></category>

		<guid isPermaLink="false">http://coldconstructs.com/?p=79</guid>
		<description><![CDATA[What I wanted was a fast, seamless way to transition from clickable thumbnail on a zui canvas while dynamically loading the content of whatever the thumbnail represented. I just loved the idea of loading content while zooming into a thumbnail without loading a higher-res image to take its place, then having the pixelated thumbnail disintegrate [...]]]></description>
			<content:encoded><![CDATA[<p>What I wanted was a fast, seamless way to transition from clickable thumbnail on a zui canvas while dynamically loading the content of whatever the thumbnail represented. I just loved the idea of loading content while zooming into a thumbnail without loading a higher-res image to take its place, then having the pixelated thumbnail disintegrate to reveal the actual content that got loaded during the transition. Anyway, it was the best idea that I could come up with for my application, so here we are.</p>
<p><a href="http://labs.coldconstructs.com/e/pixelTransition.swf?width=550&amp;height=400" rel="prettyPhoto" title="Click anywhere in the app to restart the effect."><img src="http://labs.coldconstructs.com/i/pixeltransition.png" alt="Pixel Transition demo" class="centered" /></a><br />
<span id="more-79"></span></p>
<p>I started by converting the thumbnail to a Vector array of Bitmaps. I planned on using TweenLite to shuffle each bitmapped pixel off the screen, and hoped I could do some fancy stuff with the Bitmaps like scale them for interesting effects. I did this by dumping the uint values of the bitmap into a ByteArray using getPixels(), then creating a new Bitmap for each value in the ByteArray using readUnsighnedInt() in a while loop. Each new Bitmap (representing a pixel of the original image) was added to that Vector array I mentioned:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p79code6'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p796"><td class="code" id="p79code6"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> pixels<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=bytearray%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:bytearray.html"><span style="color: #004993;">ByteArray</span></a> = srcBMD<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">getPixels</span><span style="color: #000000;">&#40;</span>srcBMD<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">rect</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
pixels<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">position</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// make sure we start reading from the beginning</span>
<span style="color: #6699cc; font-weight: bold;">var</span> newx<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = src<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> newy<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a> = src<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">while</span> <span style="color: #000000;">&#40;</span>pixels<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">bytesAvailable</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> splodeParticle<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=bitmap%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:bitmap.html"><span style="color: #004993;">Bitmap</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=bitmap%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:bitmap.html"><span style="color: #004993;">Bitmap</span></a><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=bitmapdata%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:bitmapdata.html"><span style="color: #004993;">BitmapData</span></a><span style="color: #000000;">&#40;</span>1<span style="color: #000066; font-weight: bold;">,</span> 1<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">,</span> pixels<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">readUnsignedInt</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span> <a href="http://www.google.com/search?q=pixelsnapping%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:pixelsnapping.html"><span style="color: #004993;">PixelSnapping</span></a><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ALWAYS</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	splodeParticle<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = newx<span style="color: #000066; font-weight: bold;">;</span>
	splodeParticle<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = newy<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000066; font-weight: bold;">++</span>newx<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>newx <span style="color: #000066; font-weight: bold;">&gt;</span> src<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span> src<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">++</span>newy<span style="color: #000066; font-weight: bold;">;</span>
		newx = src<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
	container<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>splodeParticle<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	pixArray<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>splodeParticle<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>I&#8217;d then loop through each item in the Vector to tween them off stage, revealing the loaded content below them.</p>
<p>Turns out this was a pretty dumb idea. The performance was extraordinarily sluggish during the while and for loops (naturally) and I wasn&#8217;t able to get a smooth tween because of this. Also, the ByteArray did not recreate the original image correctly &#8212; it came out rather distorted:</p>
<p><img class="centered" src="http://labs.coldconstructs.com/i/pt-err.png" alt="oops" /></p>
<p>I&#8217;m still not sure why, as this is my first time dealing with the ByteArray class, but I figured I&#8217;d cut my losses and try a different approach.</p>
<p>Since I was getting great results with blitting in other experiments, I figured I should try holding a Vector array of Points that represented the pixels I wanted to move around. The actual &#8220;moving&#8221; simply ran setPixel() on a Bitmap for each Point in the array after I ran some translation calculations to get shit moving all nice-like. Points have a lot of extra baggage that I didn&#8217;t want though, so I just used a custom class that simply stored some dimensional variables and called it good.</p>
<p>I based my classes off of Andre Michelle&#8217;s <a href="http://lab.andre-michelle.com/bitmap-particles-2">Bitmap Particle</a> experiment &#8212; he knows more math than I ever will and came up with a fucking brilliant effect there that I can only dream of achieving, so mad props to that bloke.</p>
<p>Anyway, as you can see this worked out pretty damn well. I could spend a whole week on this experiment, playing with the variables (especially the ignite var) to get different transitional effects, as well as adding more filters&#8230; but I&#8217;ll just leave this basic example for you to play with. Just be sure to show me anything particularly cool you come up with!</p>
<p><a title="download source" href="http://labs.coldconstructs.com/src/pixelTransition.rar"><img class="centered" src="http://coldconstructs.com/siteFiles/download_button.png" alt="download source" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://coldconstructs.com/2009/04/pixel-transition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pixel particle trails</title>
		<link>http://coldconstructs.com/2009/03/pixel-particle-trails/</link>
		<comments>http://coldconstructs.com/2009/03/pixel-particle-trails/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 04:13:46 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[Experiments]]></category>
		<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[source]]></category>

		<guid isPermaLink="false">http://coldconstructs.com/?p=31</guid>
		<description><![CDATA[I&#8217;ve been messing around with particles, particularly the blitted, traily kind I guess. Click on the image below to pop and watch it. Grab the source at the bottom and let me know if you do something cool with it.


The particle trails you see are actually all on one bitmap that I draw to after [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been messing around with particles, particularly the blitted, traily kind I guess. Click on the image below to pop and watch it. Grab the source at the bottom and let me know if you do something cool with it.</p>
<p><a href="http://labs.coldconstructs.com/e/pixelparticletrails.swf?width=600&amp;height=400" rel="prettyPhoto" title="Yay pretty."><img src="http://labs.coldconstructs.com/i/pixelparticletrails.png" alt="Pixel Particle Trails demo" class="centered" /></a><br />
<span id="more-31"></span></p>
<p>The particle trails you see are actually all on one bitmap that I draw to after updating each particle in sequence, per frame tick. The particles themselves are a simple class that keeps track of a few variables such as velocity and position. The class doesn&#8217;t extend anything, instead the Main class of the swf draws a line from where the particle was in the previous frame to where it is in the current frame.</p>
<p>To make them fade over time I simply did a colorTransform on the whole bitmap at the begining of the tick method. I didn&#8217;t alter any hues obviously, I just subtracted the alpha value of the bitmap a little. So the longer the pixel has been in the bitmap, the lower it&#8217;s opacity. I wish I can claim that cleverness but I actually got the idea from somewhere else&#8230; probably <a href="http://www.8bitrocket.com/"/>8bitRocket</a>.</p>
<p>This method of graphics manipulation, called blitting, is the way to get a metric fuck-ton more performance out of Flash than is possible with mere Sprites. You can see there&#8217;s 600 &#8220;particles&#8221; in the demo &#8212; had I simply extended the Sprite class and used that to render the scene, I probably would have only been allowed a few hundred sprites while keeping the framerate at around 30.</p>
<p>If you want to see some great examples of blitting, check out 8bitRocket&#8217;s posts on the subject. You can even find a blitting engine called <a href="http://code.google.com/p/pixelblitz/"/>PixelBlitz</a> &#8212; check out <a href="http://www.photonstorm.com/archives/264/pixelblitz-first-2009-update-lots-of-new-toys-and-shmup-test-1"/>this badass schmup demo</a> of his!</p>
<p><a href="http://labs.coldconstructs.com/src/pixelparticletrails.rar" title="download source" /><img src="http://coldconstructs.com/siteFiles/download_button.png" alt="download source" class="centered"/></a></p>
]]></content:encoded>
			<wfw:commentRss>http://coldconstructs.com/2009/03/pixel-particle-trails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scaling dynamic text in AS3</title>
		<link>http://coldconstructs.com/2008/06/scaling-dynamic-text-in-as3/</link>
		<comments>http://coldconstructs.com/2008/06/scaling-dynamic-text-in-as3/#comments</comments>
		<pubDate>Tue, 24 Jun 2008 22:27:56 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[Experiments]]></category>
		<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[source]]></category>

		<guid isPermaLink="false">http://coldconstructs.com/?p=29</guid>
		<description><![CDATA[Recently I&#8217;ve had the need to scale a dynamic text field which was fed by XML. Readability after scaling was not important, but it did have to look good (it&#8217;s for a full Flash site). However, scaling the MovieClip that the text field was in would cause some pretty chaotic behavior with the text &#8212; [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve had the need to scale a dynamic text field which was fed by XML. Readability after scaling was not important, but it did have to look good (it&#8217;s for a full Flash site). However, scaling the MovieClip that the text field was in would cause some pretty chaotic behavior with the text &#8212; the text would try to scale with its parent but in doing so would shift around spastically until the parent MC settled into a new scale. This was unacceptable.</p>
<p><a href="http://labs.coldconstructs.com/e/bitmapText.swf?width=550&amp;height=400" rel="prettyPhoto" title="Click the bottom buttons to see the difference between the two rendering methods."><img src="http://labs.coldconstructs.com/i/bitmapText.png" alt="Scaling dynamic text" class="centered" /></a></p>
<p>A quick google didn&#8217;t yield anything relevant, so I was left to my own devices. I remembered being able to draw pixels to a BitmapData object while writing my game in order to improve performance, so I figured I&#8217;d see if I could just copy the pixels from the text field and &#8220;bake&#8221; it into a bitmap.</p>
<p>Turns out you can. In fact, Adobe had already done this in the help files and it&#8217;s actually extremely easy. Check it out:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p29code8'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p298"><td class="code" id="p29code8"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> tf<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=textfield%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textfield.html"><span style="color: #004993;">TextField</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=textfield%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:textfield.html"><span style="color: #004993;">TextField</span></a><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
tf<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = <span style="color: #990000;">&quot;bitmap text&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> myBitmapData<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=bitmapdata%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:bitmapdata.html"><span style="color: #004993;">BitmapData</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=bitmapdata%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:bitmapdata.html"><span style="color: #004993;">BitmapData</span></a><span style="color: #000000;">&#40;</span>80<span style="color: #000066; font-weight: bold;">,</span> 20<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
myBitmapData<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">draw</span><span style="color: #000000;">&#40;</span>tf<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> bmp<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=bitmap%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:bitmap.html"><span style="color: #004993;">Bitmap</span></a> = <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=bitmap%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:bitmap.html"><span style="color: #004993;">Bitmap</span></a><span style="color: #000000;">&#40;</span>myBitmapData<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>bmp<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></td></tr></table></div>

<p>No sweat right? Anyway, since I didn&#8217;t find any help on this anywhere else (granted, I didn&#8217;t look very hard), I thought I&#8217;d share this little adventure of mine. Hope it&#8217;s helpful.</p>
<p><a href="http://labs.coldconstructs.com/src/bitmapText.fla" title="download source" /><img src="http://coldconstructs.com/siteFiles/download_button.png" alt="download source" class="centered"/></a></p>
]]></content:encoded>
			<wfw:commentRss>http://coldconstructs.com/2008/06/scaling-dynamic-text-in-as3/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>
