<?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>mat janson blanchet &#187; Tutorials</title>
	<atom:link href="http://jansensan.net/category/tutorials/feed" rel="self" type="application/rss+xml" />
	<link>http://jansensan.net</link>
	<description></description>
	<lastBuildDate>Fri, 18 May 2012 12:06:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>How to fully fit an Away3D plane in the viewport</title>
		<link>http://jansensan.net/how-to-fully-fit-away3d-plane-in-viewport</link>
		<comments>http://jansensan.net/how-to-fully-fit-away3d-plane-in-viewport#comments</comments>
		<pubDate>Mon, 06 Feb 2012 00:50:12 +0000</pubDate>
		<dc:creator>mat janson blanchet</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Away3D]]></category>
		<category><![CDATA[Stage3D]]></category>

		<guid isPermaLink="false">http://jansensan.net/?p=756</guid>
		<description><![CDATA[There has been a lot of people who played with Away3D and built awesome things. A lot of said awesome things were built by people who know, or know someone that knows 3D modeling, which is why the quality has been so awesome. However, it may not always be necessary to build a full 3D [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone" title="Escher - Relativity (Detail)" src="http://jansensan.net/images/blog/post0029-escher-header.jpg" alt="Escher - Relativity (Detail)" width="640" height="220" /></p>
<p>There has been a lot of people who played with <a title="Away3D" href="http://away3d.com/" target="_blank">Away3D</a> and built awesome things. A lot of said awesome things were built by people who know, or know someone that knows 3D modeling, which is why the quality has been so awesome. However, it may not always be necessary to build a full 3D world, sometimes a project may simply require some 3D elements over a 2D background. But how do you place an image behind all sorts of 3D objects if Away3D's <code><a title="Away3D View3D source" href="https://github.com/away3d/away3d-core-fp11/blob/master/src/away3d/containers/View3D.as" target="_blank">View3D</a></code> has a black background? I will try and explain the process in this article. Warning: matrices and trigonometry ahead!</p>
<p><strong>Build it and they will come</strong></p>
<p>Usually, if you want to place an image and make it fit your document dimensions, you simply stretch the image accordingly. In 3D however, it is not as simple, as the world dimensions are not necessarily measured in pixels. While working with the guys from <a title="Float4 Interactive" href="http://float4.com/" target="_blank">Float4</a>, I came to understand that meters are oftentimes used as measurement unit. With that in mind, we cannot directly use the dimensions of our document.</p>
<p>So let's say we want to place an image in the 3D world and make it fill the viewport (the visual space taken by Away3D). At first, we would need an image, and a plane onto which we would like to place the image. As <a title="Apply a texture on a cube in Away3D 4.0" href="http://jansensan.net/apply-a-texture-on-a-cube-in-away3d-4-0" target="_self">I wrote previously</a>, materials applied onto shapes need to be in dimensions that are a power of 2, so will will need to rescale our image. A <code><a title="AS3 Matrix documentation" href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/geom/Matrix.html" target="_blank">Matrix</a></code> will fulfill this need.</p>
<blockquote>
<pre class="actionscript"><span style="color: #0066CC;">private</span> const <span style="color: #0066CC;">WIDTH</span>:uint = <span style="color: #cc66cc;">640</span>;
<span style="color: #0066CC;">private</span> const <span style="color: #0066CC;">HEIGHT</span>:uint = <span style="color: #cc66cc;">640</span>;
<span style="color: #0066CC;">private</span> const MATERIAL_WIDTH:uint = <span style="color: #cc66cc;">1024</span>;
<span style="color: #0066CC;">private</span> const MATERIAL_HEIGHT:uint = <span style="color: #cc66cc;">1024</span>;
&nbsp;
<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;assets/images/escher-relativity.png&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">private</span> const EscherImageClass:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _imageData:BitmapData;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _matrix:Matrix;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _material:BitmapMaterial;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _plane:Plane;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _view3D:View3D;</pre>
</blockquote>
<p><code>WIDTH</code> and <code>HEIGHT</code> represent the dimensions of my document, and also the dimensions of the image. The <code>MATERIAL_WIDTH</code> and <code>MATERIAL_HEIGHT</code> represent the dimensions needed for the material, dimensions that are a power of two. Here I will be using an image from <a title="M. C. Escher on Wikipedia" href="http://en.wikipedia.org/wiki/M._C._Escher" target="_blank">Escher</a>, obtained from Wikipedia. There is no need for a <code><a title="AS3 Bitmap documentation" href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Bitmap.html" target="_blank">Bitmap</a></code> per se, but we will need the image's <code><a title="AS3 BitmapData documentation" href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html" target="_blank">BitmapData</a></code>, hence the <code>_imageData</code> variable.</p>
<p>Let's do this!</p>
<p>Normally, I resize the material dimensions to a value higher than what I need to display; first the material is stretched up, and then visually fitted to the original dimensions of the image. If we were to scale the material to smaller dimensions than that of the image, the plane would stretch up the material afterwards when displaying the image, resulting in possible loss of smoothness or detail.</p>
<blockquote>
<pre class="actionscript">_matrix = <span style="color: #000000; font-weight: bold;">new</span> Matrix<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
_matrix.<span style="color: #006600;">scale</span><span style="color: #66cc66;">&#40;</span>MATERIAL_WIDTH / <span style="color: #0066CC;">WIDTH</span>, MATERIAL_HEIGHT / <span style="color: #0066CC;">HEIGHT</span><span style="color: #66cc66;">&#41;</span>;</pre>
</blockquote>
<p>Let's draw an instance of the Escher image into the<code>_imageData</code>. This is where <code>_matrix</code> comes in handy. I wonder what pushed Adobe's engineers to make an architectural choice where they thought users would use a <code><a title="AS3 ColorTransform documentation" href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/geom/ColorTransform.html" target="_blank">ColorTransform</a></code>, a <code><a title="AS3 BlendMode documentation" href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BlendMode.html" target="_blank">BlendMode</a></code> or a clipping <code><a title="AS3 Rectangle documentation" href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/geom/Rectangle.html" target="_blank">Rectangle</a></code> more often than a flag to smooth the drawing. I mean, putting the flag as a last argument? Anywho, let's add a lot of <code>null</code> parameters...</p>
<blockquote>
<pre class="actionscript">_imageData = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span>MATERIAL_WIDTH, MATERIAL_HEIGHT<span style="color: #66cc66;">&#41;</span>;
_imageData.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> EscherImageClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, _matrix, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;</pre>
</blockquote>
<p>Next, create the material. As seen <a title="Apply a texture on a cube in Away3D 4.0" href="http://jansensan.net/apply-a-texture-on-a-cube-in-away3d-4-0" target="_blank">previously</a>, let's use a simple trick to assign whether or not the material is mipmapped.</p>
<blockquote>
<pre class="actionscript">_material = <span style="color: #000000; font-weight: bold;">new</span> BitmapMaterial<span style="color: #66cc66;">&#40;</span>_imageData, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
_material.<span style="color: #006600;">mipmap</span> = <span style="color: #66cc66;">&#40;</span>_imageData.<span style="color: #0066CC;">width</span> == _imageData.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;</pre>
</blockquote>
<p>Apply the material to the plane, add the plane to the <code><a title="Away3D View3D source" href="https://github.com/away3d/away3d-core-fp11/blob/master/src/away3d/containers/View3D.as" target="_blank">View3D</a></code>, and then add the view to the display list.</p>
<blockquote>
<pre class="actionscript">_plane = <span style="color: #000000; font-weight: bold;">new</span> Plane<span style="color: #66cc66;">&#40;</span>_material<span style="color: #66cc66;">&#41;</span>;
_view3D = <span style="color: #000000; font-weight: bold;">new</span> View3D<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
_view3D.<span style="color: #006600;">scene</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>_plane<span style="color: #66cc66;">&#41;</span>;
addChild<span style="color: #66cc66;">&#40;</span>_view3D<span style="color: #66cc66;">&#41;</span>;</pre>
</blockquote>
<p>Do not forget to listen to <code>Event.ENTER_FRAME</code> and call the <code><a title="Away3D View3D.render() documentation" href="http://away3d.com/livedocs/away3d-core-fp11/away3d/containers/View3D.html#render()" target="_blank">View3D.render()</a></code> method at every update.</p>
<blockquote>
<pre class="actionscript">addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, updateHandler<span style="color: #66cc66;">&#41;</span>;</pre>
</blockquote>
<p>Run it, and what do we get?...</p>
<p>Nothing.</p>
<p>Uh?</p>
<p>By default, the plane is added at (0, 0, 0), so it should be visible...</p>
<p>It is, it turns out that an API choice—which may make sense in a 3D world, but that I do not understand—puts the plane with its face up on instantiation. What this means is when it is created, we see its side, which has no thickness. Just change the <code>yUp</code> parameter to <code>false</code>, so the new plane will then face the camera upon creation.</p>
<blockquote>
<pre class="actionscript">_plane.<span style="color: #006600;">yUp</span> = <span style="color: #000000; font-weight: bold;">false</span>;</pre>
</blockquote>
<p>We should get something like this.</p>
<p><img class="alignnone" title="Plane is too close, image is cropped by viewport" src="http://jansensan.net/images/blog/post0029-escher-too-big.jpg" alt="Plane is too close, image is cropped by viewport" width="640" height="640" /></p>
<p>If we compare the image and this result, we notice that the image's edges are cropped, which means the plane is too close. Below is a diagram explaining this visually.</p>
<p><img class="alignnone" title="Diagram - too big, outside of viewport" src="http://jansensan.net/images/blog/post0029-too-big-diagram.jpg" alt="Diagram - too big, outside of viewport" width="640" height="272" /></p>
<p>Imagine a pyramid drawn from the camera at its top with an angle that is the camera's <a title="Field of view on Wikipedia" href="http://en.wikipedia.org/wiki/Field_of_view" target="_blank">field of view</a>, up until a certain point. From the camera's point of view, there is always a certain frame that is visible, that is the viewport. However, that pyramid is not infinite, actually what the camera sees is not even a pyramid. The <a title="Viewing frustum on Wikipedia" href="http://en.wikipedia.org/wiki/Viewing_frustum" target="_blank">viewing frustum</a>, what is visible within a 3D world, is an area from a certain threshold (the near plane) to another one (the far plane), that's what is visible within a 3D world. What happens with the code above is that the plane is bigger than the frustum, thus being cut.</p>
<p>Below is what we want to achieve:</p>
<p><img class="alignnone" title="Diagram - Plane fits into the viewport" src="http://jansensan.net/images/blog/post0029-viewport-diagram.jpg" alt="Diagram - Plane fits into the viewport" width="640" height="240" /></p>
<p><strong>SOH-CAH-TOA</strong></p>
<p>This is where the fun begins! How do we fit our plane into our viewport? It is possible to simply move the plane on the z axis and eventually position the plane properly to fit our need. But what if the dimensions change? We'd be back onto square one. This problem requires good ol' trigonometry, and the pythagorean theorem.</p>
<p>You may wonder what this title means. It's a mnemonic trick that's been given to me back in high school on how to remember the rules of trigonometry:</p>
<blockquote><p><strong>S</strong>ine of an angle = <strong>O</strong>pposite side / <strong>H</strong>ypothenuse</p>
<p><strong>C</strong>osine of an angle = <strong>A</strong>djacent side / <strong>H</strong>ypothenuse</p>
<p><strong>T</strong>angent of an angle = <strong>O</strong>pposite side / <strong>A</strong>djacent side</p></blockquote>
<p>Then let's consider this as a trigonometry problem, illustrated below. We have a camera, the <code><a title="Away3d Camera3D source" href="https://github.com/away3d/away3d-core-fp11/blob/master/src/away3d/cameras/Camera3D.as" target="_blank">Camera3D</a></code>, which is located at (0, 0, -500) by default. Look at the sources to confirm, it is the case with Away3D, but it may be different in other 3D engines. We have a plane with a position yet undefined, but we do want to make sure that its fits perfectly into the viewport.</p>
<p><img class="alignnone" title="Diagram - trigonometry and pythagorean theorem" src="http://jansensan.net/images/blog/post0029-z-diagram.jpg" alt="Diagram - trigonometry and pythagorean theorem" width="640" height="240" /></p>
<p>Where does that 60° angle in the illustration above come from? If we take a look at the <code><a title="Away3D Camera3D source" href="https://github.com/away3d/away3d-core-fp11/blob/master/src/away3d/cameras/Camera3D.as" target="_blank">Camera3D</a></code> constructor, we find that it uses a <code><a title="Away3D PerspectiveLens source" href="https://github.com/away3d/away3d-core-fp11/blob/master/src/away3d/cameras/lenses/PerspectiveLens.as" target="_blank">PerspectiveLens</a></code> by default. In that lens constructor, we can see the default field of view is 60, which is in degrees. It's too bad we need to look at the source to obtain this, it would have been simpler to use a getter to be able to check it, but there is none.</p>
<p>Good, we have an angle, we can calculate dimensions and positions. Let's set an arbitrary z for the plane, we'll see further that it doesn't matter.</p>
<blockquote>
<pre class="actionscript"><span style="color: #000000; font-weight: bold;">var</span> angleY:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">60</span>;
<span style="color: #000000; font-weight: bold;">var</span> cameraZ:<span style="color: #0066CC;">int</span> = _view3D.<span style="color: #0066CC;">camera</span>.<span style="color: #006600;">z</span>;
<span style="color: #000000; font-weight: bold;">var</span> planeZ:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">500</span>;
<span style="color: #000000; font-weight: bold;">var</span> distFromCamToPlane:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">abs</span><span style="color: #66cc66;">&#40;</span>cameraZ<span style="color: #66cc66;">&#41;</span> + planeZ;</pre>
</blockquote>
<p>Since the field of view is vertical, let's measure the height of our plane first. Let's not forget that the field of view covers the whole height, but in order to work with rectangle triangles, we will use only half of that angle. We know the angle, we know the distance from the camera to the plane (the adjacent side), but we do not know the opposite side, so let's use the tangent. The <code><a title="AS3 Math.tan() documentation" href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Math.html#tan()" target="_blank">Math.tan()</a></code> takes radians, so just make sure to convert those degrees to radians.</p>
<blockquote>
<pre class="actionscript"><span style="color: #000000; font-weight: bold;">var</span> planeHeight:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">tan</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">PI</span> / <span style="color: #cc66cc;">180</span><span style="color: #66cc66;">&#41;</span> * <span style="color: #66cc66;">&#40;</span>angleY * <span style="color: #cc66cc;">0.5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> * distFromCamToPlane;</pre>
</blockquote>
<p>Since this represents only half of the height, let's correct it for what we need further.</p>
<blockquote>
<pre class="actionscript">planeHeight *= <span style="color: #cc66cc;">2</span>;</pre>
</blockquote>
<p>To obtain the width of our plane, simply apply the aspect ratio of the image. In this case the image is a square, but it may very well be any kind of rectangle.</p>
<blockquote>
<pre class="actionscript"><span style="color: #000000; font-weight: bold;">var</span> aspectRatio:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">WIDTH</span> / <span style="color: #0066CC;">HEIGHT</span>;
<span style="color: #000000; font-weight: bold;">var</span> planeWidth:<span style="color: #0066CC;">int</span> = planeHeight * aspectRatio;</pre>
</blockquote>
<p>And now we are ready to create our plane with the proper dimensions and position.</p>
<blockquote>
<pre class="actionscript">_plane = <span style="color: #000000; font-weight: bold;">new</span> Plane<span style="color: #66cc66;">&#40;</span>_material, planeWidth, planeHeight<span style="color: #66cc66;">&#41;</span>;
_plane.<span style="color: #006600;">yUp</span> = <span style="color: #000000; font-weight: bold;">false</span>;
_plane.<span style="color: #006600;">z</span> = planeZ;</pre>
</blockquote>
<p>Let's take a look at the whole process at once.</p>
<blockquote>
<pre class="actionscript"><span style="color: #808080; font-style: italic;">// get the scale to apply to the image</span>
_matrix = <span style="color: #000000; font-weight: bold;">new</span> Matrix<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
_matrix.<span style="color: #006600;">scale</span><span style="color: #66cc66;">&#40;</span>MATERIAL_WIDTH / <span style="color: #0066CC;">WIDTH</span>, MATERIAL_HEIGHT / <span style="color: #0066CC;">HEIGHT</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// create the image data</span>
_imageData = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span>MATERIAL_WIDTH, MATERIAL_HEIGHT<span style="color: #66cc66;">&#41;</span>;
_imageData.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> EscherImageClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, _matrix, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// create the material</span>
_material = <span style="color: #000000; font-weight: bold;">new</span> BitmapMaterial<span style="color: #66cc66;">&#40;</span>_imageData, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
_material.<span style="color: #006600;">mipmap</span> = <span style="color: #66cc66;">&#40;</span>_imageData.<span style="color: #0066CC;">width</span> == _imageData.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// create the view</span>
_view3D = <span style="color: #000000; font-weight: bold;">new</span> View3D<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// camera values</span>
<span style="color: #808080; font-style: italic;">// see away3d.camerasCamera3D's default lens' field of view</span>
<span style="color: #808080; font-style: italic;">// in away3d.cameras.lenses.PerspectiveLens</span>
<span style="color: #808080; font-style: italic;">// it turns out that the field of view is vertical</span>
<span style="color: #000000; font-weight: bold;">var</span> angleY:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">60</span>;
<span style="color: #000000; font-weight: bold;">var</span> cameraZ:<span style="color: #0066CC;">int</span> = _view3D.<span style="color: #0066CC;">camera</span>.<span style="color: #006600;">z</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// this position is arbitrary as we will see further</span>
<span style="color: #808080; font-style: italic;">// useful to calculate the distance between the camera and the plane</span>
<span style="color: #000000; font-weight: bold;">var</span> planeZ:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">500</span>;
<span style="color: #000000; font-weight: bold;">var</span> distFromCamToPlane:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">abs</span><span style="color: #66cc66;">&#40;</span>cameraZ<span style="color: #66cc66;">&#41;</span> + planeZ;
&nbsp;
<span style="color: #808080; font-style: italic;">// since the field of view is a vertical angle, calculate the height of the plane first</span>
<span style="color: #808080; font-style: italic;">// use pythagorean theorem to calculate</span>
<span style="color: #808080; font-style: italic;">// tip for trigonometry: soh-cah-toa</span>
<span style="color: #808080; font-style: italic;">// toa: tan(angle) = oppositeSide / adjacentSide</span>
<span style="color: #000000; font-weight: bold;">var</span> planeHeight:<span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">tan</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">PI</span> / <span style="color: #cc66cc;">180</span><span style="color: #66cc66;">&#41;</span> * <span style="color: #66cc66;">&#40;</span>angleY * <span style="color: #cc66cc;">0.5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> * distFromCamToPlane;
&nbsp;
<span style="color: #808080; font-style: italic;">// and since it was for a rectangle triangle, thus half the size, double the length</span>
planeHeight *= <span style="color: #cc66cc;">2</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// useful for resizing the image</span>
<span style="color: #000000; font-weight: bold;">var</span> aspectRatio:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">WIDTH</span> / <span style="color: #0066CC;">HEIGHT</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// use the aspect ratio to calulcate the width of the plane</span>
<span style="color: #000000; font-weight: bold;">var</span> planeWidth:<span style="color: #0066CC;">int</span> = planeHeight * aspectRatio;
&nbsp;
<span style="color: #808080; font-style: italic;">// create the plane</span>
_plane = <span style="color: #000000; font-weight: bold;">new</span> Plane<span style="color: #66cc66;">&#40;</span>_material, planeWidth, planeHeight<span style="color: #66cc66;">&#41;</span>;
_plane.<span style="color: #006600;">yUp</span> = <span style="color: #000000; font-weight: bold;">false</span>;
_plane.<span style="color: #006600;">z</span> = planeZ;
&nbsp;
<span style="color: #808080; font-style: italic;">// add 3D objects to the scene</span>
_view3D.<span style="color: #006600;">scene</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>_plane<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// add the away3d view to the display list</span>
addChild<span style="color: #66cc66;">&#40;</span>_view3D<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// listen to the updates</span>
addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, updateHandler<span style="color: #66cc66;">&#41;</span>;</pre>
</blockquote>
<p>This should produce a result like <a href="http://jansensan.net/experiments/fit-away3d-plane-fully-in-viewport/" target="_blank">this</a>, where the image fits perfectly in the viewport.</p>
<p><img class="alignnone" title="Plane fits viewport perfectly" src="http://jansensan.net/images/blog/post0029-full-view.jpg" alt="Plane fits viewport perfectly" width="640" height="640" /></p>
<p>For additional fun, I added an option to click to rotate it, so that it demonstrates that it is indeed 3D.</p>
<p><strong>Sources</strong></p>
<p>As I always do in my tutorials, you can download the <a title="Tutorial sources" href="http://bit.ly/plane-sources">sources</a>. This project has been built for Flash Player 11+ with FDT. You may need to adapt the Flex SDK.</p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://jansensan.net/how-to-fully-fit-away3d-plane-in-viewport/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Apply a texture on a cube in Away3D 4.0</title>
		<link>http://jansensan.net/apply-a-texture-on-a-cube-in-away3d-4-0</link>
		<comments>http://jansensan.net/apply-a-texture-on-a-cube-in-away3d-4-0#comments</comments>
		<pubDate>Sat, 20 Aug 2011 19:02:41 +0000</pubDate>
		<dc:creator>mat janson blanchet</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Away3D]]></category>
		<category><![CDATA[Broomstick]]></category>
		<category><![CDATA[Flash Player 11 Beta]]></category>
		<category><![CDATA[Molehill]]></category>

		<guid isPermaLink="false">http://jansensan.net/?p=692</guid>
		<description><![CDATA[Now that your environment is set up to play with Away3D, let's create something! In my previous post, I concluded that you can now experiment, and so I did too. It turned out that there are some little intricacies about putting a simple image on a simple cube. Let's take a look. Create a cube [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter" title="Away3D cube with a texture applied" src="http://jansensan.net/images/blog/post0027_001.jpg" alt="Away3D cube with a texture applied" width="640" height="360" /></p>
<p>Now that your environment is <a title="Setting up FDT for Away3D and Flash Player 11 (Beta)" href="http://jansensan.net/setting-up-fdt-for-away3d-and-flash-player-11-beta" target="_blank">set up to play with Away3D</a>, let's create something! In my previous post, I concluded that you can now experiment, and so I did too. It turned out that there are some little intricacies about putting a simple image on a simple cube. Let's take a look.</p>
<p><strong>Create a cube</strong></p>
<p>This part has got to be the simplest, thanks to the nice API that the guys from Away3D built.</p>
<p>ActionScript 3.0:</p>
<blockquote>
<pre class="actionscript">package
<span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">import</span> away3d.<span style="color: #006600;">containers</span>.<span style="color: #006600;">View3D</span>;
<span style="color: #0066CC;">import</span> away3d.<span style="color: #006600;">materials</span>.<span style="color: #006600;">ColorMaterial</span>;
<span style="color: #0066CC;">import</span> away3d.<span style="color: #006600;">primitives</span>.<span style="color: #006600;">Cube</span>;
&nbsp;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageAlign</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageScaleMode</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
<span style="color: #66cc66;">&#91;</span>SWF<span style="color: #66cc66;">&#40;</span>frameRate=<span style="color: #ff0000;">&quot;60&quot;</span>, <span style="color: #0066CC;">backgroundColor</span>=<span style="color: #ff0000;">&quot;#ffffff&quot;</span>, <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;640&quot;</span>, <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;360&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> Sprite
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// + ----------------------------------------</span>
	<span style="color: #808080; font-style: italic;">//		[ VARS ]</span>
	<span style="color: #808080; font-style: italic;">// + ----------------------------------------</span>
&nbsp;
	<span style="color: #0066CC;">private</span>	<span style="color: #000000; font-weight: bold;">var</span>	_view3D		:View3D;
	<span style="color: #0066CC;">private</span>	<span style="color: #000000; font-weight: bold;">var</span>	_cube		:Cube;
&nbsp;
	<span style="color: #808080; font-style: italic;">// + ----------------------------------------</span>
	<span style="color: #808080; font-style: italic;">//		[ CONSTRUCTOR ]</span>
	<span style="color: #808080; font-style: italic;">// + ----------------------------------------</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		initStage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, addedToStageHandler<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">// + ----------------------------------------</span>
	<span style="color: #808080; font-style: italic;">//		[ PRIVATE METHODS ]</span>
	<span style="color: #808080; font-style: italic;">// + ----------------------------------------</span>
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> initStage<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: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span> = StageAlign.<span style="color: #006600;">TOP_LEFT</span>;
		<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span> = StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> init<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: #808080; font-style: italic;">// it is necessary to add a texture to the shape so it is visible</span>
		<span style="color: #000000; font-weight: bold;">var</span> material:ColorMaterial = <span style="color: #000000; font-weight: bold;">new</span> ColorMaterial<span style="color: #66cc66;">&#40;</span>0xffffff<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// I personally prefer to set vars for repetitive values</span>
		<span style="color: #000000; font-weight: bold;">var</span> cubeSize:uint = <span style="color: #cc66cc;">240</span>;
		_cube = <span style="color: #000000; font-weight: bold;">new</span> Cube<span style="color: #66cc66;">&#40;</span>material, cubeSize, cubeSize, cubeSize<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// add the 3D elements to the stage</span>
		_view3D = <span style="color: #000000; font-weight: bold;">new</span> View3D<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		_view3D.<span style="color: #006600;">scene</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>_cube<span style="color: #66cc66;">&#41;</span>;
		addChild<span style="color: #66cc66;">&#40;</span>_view3D<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// add a listener so it becomes possible to render the 3D world</span>
		addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, enterFrameHandler<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">// + ----------------------------------------</span>
	<span style="color: #808080; font-style: italic;">//		[ PRIVATE METHODS ]</span>
	<span style="color: #808080; font-style: italic;">// + ----------------------------------------</span>
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> addedToStageHandler<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, addedToStageHandler<span style="color: #66cc66;">&#41;</span>;
		init<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> enterFrameHandler<span style="color: #66cc66;">&#40;</span>event: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;">// render the 3D world</span>
		_view3D.<span style="color: #006600;">render</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>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre>
</blockquote>
<p>With this code, this is what you should get as a result:</p>
<p><img class="aligncenter" title="Away3D cube with ColorMaterial applied" src="http://jansensan.net/images/blog/post0027_002.jpg" alt="Away3D cube with ColorMaterial applied" width="640" height="360" /></p>
<p>Not exactly exciting. For one, a <code>ColorMaterial</code> is really a flat color, so the best way to make it live a bit is adding light elements, which we will not see in this post (and that's also because I have not learned that yet). Also, we only see once face of the cube, quite boring.</p>
<p>To at least view that this object is in 3D, simply update the enterFrameHandler function.</p>
<p>ActionScript 3.0:</p>
<blockquote>
<pre class="actionscript"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> enterFrameHandler<span style="color: #66cc66;">&#40;</span>event: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;">// increment the cube's rotation values to see its different sides</span>
	_cube.<span style="color: #006600;">rotationX</span>++;
	_cube.<span style="color: #006600;">rotationY</span>++;
	_cube.<span style="color: #006600;">rotationZ</span>++;
&nbsp;
	<span style="color: #808080; font-style: italic;">// render the 3D world</span>
	_view3D.<span style="color: #006600;">render</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
</blockquote>
<p><strong>Apply an image texture</strong></p>
<p>Most probably, you would like to add your own textures to your 3D elements. This is where it gets fun!</p>
<p>As suggested by a lot of users on the Away3D forums, it may be better to have a 3D artist create objects in <a title="Blender" href="http://www.blender.org/" target="_blank">Blender</a> or <a title="3ds Max" href="http://usa.autodesk.com/3ds-max/" target="_blank">3ds Max</a> (I do not know if <a title="Unity3D" href="http://unity3d.com/" target="_blank">Unity3D</a> can be used, although I hope it can). Once those objects are created, there is a way to import/load them into Away3D.</p>
<p>But in this case, since I do not know 3D software and because there is no need yet to get into complicated texture mapping, we are looking at something super simple: put an image on a cube. Most of what follows is a comprehensive summary of a <a title="Away3D Forum Need help understanding texture mapping" href="http://away3d.com/forum/viewthread/476/" target="_blank">thread</a> that followed my question on <a title="Away3D's forum" href="http://away3d.com/forum/" target="_blank">Away3D's forum</a>.</p>
<p>First things first: let's create an image. There are specifications to follow when creating an image for a <code>BitmapMaterial</code>.</p>
<p>A texture does not need to be square, however the dimensions have to be a power of two. What this means, is a texture can be 512 x 512 pixels, or 256 x 1024 pixels, etc. I worked with 2048 x 1024 pixels in this case.</p>
<p>Keep in mind that if you image is not square, it will not <a title="Wikipedia - Mipmap" href="http://en.wikipedia.org/wiki/Mipmap" target="_blank">mipmap</a>. Not fully convinced I understand what it means, as I do now know 3D vocabulary that much, but suffice to say you simply need to set <code>BitmapMaterial.mipmap</code> to <code>false</code> to make it work.</p>
<p>In the case of a cube, you need to cover all six sides. In order to do so, split your image into a 3 x 2 grid, like so:</p>
<p><img class="aligncenter" title="Dice texture" src="http://jansensan.net/images/blog/post0027_003.png" alt="Dice texture" width="640" height="320" /></p>
<p>Do you notice that each part of the image that represents a side of the cube is not square? It does not matter really, since Away3D will map it as a square onto the cube. As long as you respect the grid in your design, it will be good.</p>
<p>Rather than loading the image and in order to make things quicker, I simply <a title="Embedding Resources with AS3" href="http://www.bit-101.com/blog/?p=853" target="_blank">embedded</a> it in the code.</p>
<p>ActionScript 3.0:</p>
<blockquote>
<pre class="actionscript">package
<span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">import</span> away3d.<span style="color: #006600;">containers</span>.<span style="color: #006600;">View3D</span>;
<span style="color: #0066CC;">import</span> away3d.<span style="color: #006600;">materials</span>.<span style="color: #006600;">BitmapMaterial</span>;
<span style="color: #0066CC;">import</span> away3d.<span style="color: #006600;">primitives</span>.<span style="color: #006600;">Cube</span>;
&nbsp;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Bitmap</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageAlign</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageScaleMode</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
<span style="color: #66cc66;">&#91;</span>SWF<span style="color: #66cc66;">&#40;</span>frameRate=<span style="color: #ff0000;">&quot;60&quot;</span>, <span style="color: #0066CC;">backgroundColor</span>=<span style="color: #ff0000;">&quot;#ffffff&quot;</span>, <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;640&quot;</span>, <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;360&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> Sprite
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// + ----------------------------------------</span>
	<span style="color: #808080; font-style: italic;">//		[ CONSTANTS ]</span>
	<span style="color: #808080; font-style: italic;">// + ----------------------------------------</span>
&nbsp;
	<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;../assets/images/dice-texture-2048x1024.png&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> ImageClass:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// + ----------------------------------------</span>
	<span style="color: #808080; font-style: italic;">//		[ VARS ]</span>
	<span style="color: #808080; font-style: italic;">// + ----------------------------------------</span>
&nbsp;
	<span style="color: #0066CC;">private</span>	<span style="color: #000000; font-weight: bold;">var</span>	_view3D		:View3D;
	<span style="color: #0066CC;">private</span>	<span style="color: #000000; font-weight: bold;">var</span>	_cube		:Cube;
&nbsp;
	<span style="color: #808080; font-style: italic;">// + ----------------------------------------</span>
	<span style="color: #808080; font-style: italic;">//		[ CONSTRUCTOR ]</span>
	<span style="color: #808080; font-style: italic;">// + ----------------------------------------</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		initStage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, addedToStageHangler<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">// + ----------------------------------------</span>
	<span style="color: #808080; font-style: italic;">//		[ PRIVATE METHODS ]</span>
	<span style="color: #808080; font-style: italic;">// + ----------------------------------------</span>
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> initStage<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: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span> = StageAlign.<span style="color: #006600;">TOP_LEFT</span>;
		<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span> = StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> init<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: #808080; font-style: italic;">// create a Bitmap from the embedded image</span>
		<span style="color: #000000; font-weight: bold;">var</span> image:Bitmap = <span style="color: #000000; font-weight: bold;">new</span> ImageClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as Bitmap;
&nbsp;
		<span style="color: #808080; font-style: italic;">// create the BitmapMaterial</span>
		<span style="color: #000000; font-weight: bold;">var</span> material:BitmapMaterial = <span style="color: #000000; font-weight: bold;">new</span> BitmapMaterial<span style="color: #66cc66;">&#40;</span>image.<span style="color: #006600;">bitmapData</span><span style="color: #66cc66;">&#41;</span>;
		material.<span style="color: #006600;">smooth</span> = <span style="color: #000000; font-weight: bold;">true</span>;
		<span style="color: #808080; font-style: italic;">// little trick: you do not need to care if the image is square,</span>
		<span style="color: #808080; font-style: italic;">// simply evaluate if the sides are even</span>
		material.<span style="color: #006600;">mipmap</span> = <span style="color: #66cc66;">&#40;</span>image.<span style="color: #0066CC;">width</span> == image.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// create the cube</span>
		<span style="color: #000000; font-weight: bold;">var</span> cubeSize:uint = <span style="color: #cc66cc;">240</span>;
		_cube = <span style="color: #000000; font-weight: bold;">new</span> Cube<span style="color: #66cc66;">&#40;</span>material, cubeSize, cubeSize, cubeSize<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// add the 3D elements to the stage</span>
		_view3D = <span style="color: #000000; font-weight: bold;">new</span> View3D<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		_view3D.<span style="color: #006600;">scene</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>_cube<span style="color: #66cc66;">&#41;</span>;
		addChild<span style="color: #66cc66;">&#40;</span>_view3D<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// add a listener so it becomes possible to render the 3D world</span>
		addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, enterFrameHandler<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">// + ----------------------------------------</span>
	<span style="color: #808080; font-style: italic;">//		[ PRIVATE METHODS ]</span>
	<span style="color: #808080; font-style: italic;">// + ----------------------------------------</span>
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> addedToStageHandler<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, addedToStageHandler<span style="color: #66cc66;">&#41;</span>;
		init<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> enterFrameHandler<span style="color: #66cc66;">&#40;</span>event: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;">// increment the cube's rotation values to see its different sides</span>
		_cube.<span style="color: #006600;">rotationX</span>++;
		_cube.<span style="color: #006600;">rotationY</span>++;
		_cube.<span style="color: #006600;">rotationZ</span>++;
&nbsp;
		<span style="color: #808080; font-style: italic;">// render the 3D world</span>
		_view3D.<span style="color: #006600;">render</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>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre>
</blockquote>
<p><strong>Fix the Away3D sources</strong></p>
<p>Now, I ran into an issue when <a title="Away3D Cube Test 01" href="http://jansensan.net/experiments/away3d-tests/cube01.html" target="_blank">I originally tested this</a> (Flash Player 11+ needed, click and drag to move the cube):</p>
<p><img class="aligncenter" title="Cube texture issue" src="http://jansensan.net/images/blog/post0027_004.jpg" alt="Cube texture issue" width="640" height="160" /></p>
<p>Some sides were repeated or not applied properly. That is actually what prompted me to ask my question on the forum, as I thought I had done everything proper.</p>
<p>I actually did, but here also, there was a correction needed in the Away3D sources. Away3d forum member <a title="80prozent" href="http://away3d.com/forum/member/992/" target="_blank">80prozent</a> posted a fix.</p>
<p>Look at the function <code>Cube.buildUVs()</code> into the <code>away3d.primitives.Cube class</code> and replace this part:</p>
<p>ActionScript 3.0:</p>
<blockquote>
<pre class="actionscript"><span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i = <span style="color: #cc66cc;">0</span>; i &amp;lt;= _segmentsW; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	outer_uv = u_tile_step + u_tile_dim * <span style="color: #66cc66;">&#40;</span>i / _segmentsW<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>j = <span style="color: #cc66cc;">0</span>; j &amp;lt;= _segmentsD; j++<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		uvData<span style="color: #66cc66;">&#91;</span>uidx++<span style="color: #66cc66;">&#93;</span> = outer_uv;
		uvData<span style="color: #66cc66;">&#91;</span>uidx++<span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">1</span> - v_tile_dim * <span style="color: #66cc66;">&#40;</span>j / _segmentsD<span style="color: #66cc66;">&#41;</span>;
		uvData<span style="color: #66cc66;">&#91;</span>uidx++<span style="color: #66cc66;">&#93;</span> = outer_uv;
		uvData<span style="color: #66cc66;">&#91;</span>uidx++<span style="color: #66cc66;">&#93;</span> = v_tile_step + v_tile_dim * <span style="color: #66cc66;">&#40;</span>j / _segmentsD<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i = <span style="color: #cc66cc;">0</span>; i &amp;lt;= _segmentsH; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	outer_uv = v_tile_step + v_tile_dim * <span style="color: #66cc66;">&#40;</span>i / _segmentsW<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>j = <span style="color: #cc66cc;">0</span>; j &amp;lt;= _segmentsD; j++<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		uvData<span style="color: #66cc66;">&#91;</span>uidx++<span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#40;</span>u_tile_dim * <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>_segmentsD - j<span style="color: #66cc66;">&#41;</span> / _segmentsD<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		uvData<span style="color: #66cc66;">&#91;</span>uidx++<span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">1</span> - outer_uv;
		uvData<span style="color: #66cc66;">&#91;</span>uidx++<span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">1</span> - <span style="color: #66cc66;">&#40;</span>u_tile_dim * <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>_segmentsD - j<span style="color: #66cc66;">&#41;</span> / _segmentsD<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		uvData<span style="color: #66cc66;">&#91;</span>uidx++<span style="color: #66cc66;">&#93;</span> = v_tile_step + v_tile_dim * <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>_segmentsW - i<span style="color: #66cc66;">&#41;</span> / _segmentsW<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">target</span>.<span style="color: #006600;">updateUVData</span><span style="color: #66cc66;">&#40;</span>uvData<span style="color: #66cc66;">&#41;</span>;</pre>
</blockquote>
<p>with this part:</p>
<p>ActionScript 3.0:</p>
<blockquote>
<pre class="actionscript"><span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i = <span style="color: #cc66cc;">0</span>; i &amp;lt;= _segmentsW; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	outer_uv = u_tile_step + u_tile_dim * <span style="color: #66cc66;">&#40;</span>i / _segmentsW<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>j = <span style="color: #cc66cc;">0</span>; j &amp;lt;= _segmentsD; j++<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		uvData<span style="color: #66cc66;">&#91;</span>uidx++<span style="color: #66cc66;">&#93;</span> = outer_uv;
		uvData<span style="color: #66cc66;">&#91;</span>uidx++<span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">1</span> - v_tile_dim * <span style="color: #66cc66;">&#40;</span>j / _segmentsD<span style="color: #66cc66;">&#41;</span>;
		uvData<span style="color: #66cc66;">&#91;</span>uidx++<span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">1</span> - outer_uv;
		uvData<span style="color: #66cc66;">&#91;</span>uidx++<span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">1</span> - <span style="color: #66cc66;">&#40;</span>v_tile_step + v_tile_dim * <span style="color: #66cc66;">&#40;</span>j / _segmentsD<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>i = <span style="color: #cc66cc;">0</span>; i &amp;lt;= _segmentsH; i++<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	outer_uv = v_tile_step + v_tile_dim * <span style="color: #66cc66;">&#40;</span>i / _segmentsW<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>j = <span style="color: #cc66cc;">0</span>; j &amp;lt;= _segmentsD; j++<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		uvData<span style="color: #66cc66;">&#91;</span>uidx++<span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#40;</span>u_tile_dim * <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>_segmentsD - j<span style="color: #66cc66;">&#41;</span> / _segmentsD<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		uvData<span style="color: #66cc66;">&#91;</span>uidx++<span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">1</span> - outer_uv;
		uvData<span style="color: #66cc66;">&#91;</span>uidx++<span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">1</span> - <span style="color: #66cc66;">&#40;</span>u_tile_dim * <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>_segmentsD - j<span style="color: #66cc66;">&#41;</span> / _segmentsD<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		uvData<span style="color: #66cc66;">&#91;</span>uidx++<span style="color: #66cc66;">&#93;</span> = v_tile_step + v_tile_dim * <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>_segmentsW - i<span style="color: #66cc66;">&#41;</span> / _segmentsW<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">target</span>.<span style="color: #006600;">updateUVData</span><span style="color: #66cc66;">&#40;</span>uvData<span style="color: #66cc66;">&#41;</span>;</pre>
</blockquote>
<p>His fix corrects the way the image is applied to the cube. I guess this will be eventually fixed, but at the time of writing this post, the sources provided by Away3D still need that correction.</p>
<p>And there you go! From then you can have an awesomely decorated cube with the texture of your choice!</p>
]]></content:encoded>
			<wfw:commentRss>http://jansensan.net/apply-a-texture-on-a-cube-in-away3d-4-0/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Setting up FDT for Away3D and Flash Player 11 (Beta)</title>
		<link>http://jansensan.net/setting-up-fdt-for-away3d-and-flash-player-11-beta</link>
		<comments>http://jansensan.net/setting-up-fdt-for-away3d-and-flash-player-11-beta#comments</comments>
		<pubDate>Fri, 12 Aug 2011 22:02:28 +0000</pubDate>
		<dc:creator>mat janson blanchet</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Workflow]]></category>
		<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Away3D]]></category>
		<category><![CDATA[FDT]]></category>
		<category><![CDATA[Flash Player 11 Beta]]></category>
		<category><![CDATA[Molehill]]></category>

		<guid isPermaLink="false">http://jansensan.net/?p=655</guid>
		<description><![CDATA[Recently I had been approached to develop an interface for a huge interactive wall. The company has its own engine, but the interface runs as a Flash Player piece within that environment. I am not talking about Scaleform, which allows game developers to integrate interfaces built in AS3 into their projects, but the logic is [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter" title="FDT and Away3D" src="http://jansensan.net/images/blog/post0026_001.jpg" alt="FDT and Away3D" width="640" height="160" /></p>
<p>Recently I had been approached to develop an interface for a huge interactive wall. The company has its own engine, but the interface runs as a Flash Player piece within that environment. I am not talking about <a title="Scaleform" href="http://www.scaleform.com/" target="_blank">Scaleform</a>, which allows game developers to integrate interfaces built in AS3 into their projects, but the logic is pretty much the same.</p>
<p>So the idea was to use some simple 3D shapes and let users interact with it via a touch screen. The project did not go through, however I had done some research on the latest Flash Player beta developments and the latest alpha release of Away3D. As expected, I had to do a lot of searches in Google, I cursed at my computer once or twice, but I did learn a lot. I think it would be nice to share my discoveries.</p>
<p><strong>Setting up FDT to publish onto Flash Player 11 Beta</strong></p>
<p>I have to say a lot of people wrote blog posts already on how to do that. Although their posts explained more on how to use the incubator version of Flash Player, it's pretty easy to do a parallel. I'll just list the steps here and provide the sources to my information further down.</p>
<ol>
<li>Get the latest available Flash Player debugger, Flex SDK and playerglobal.swc (by the way, since the IDE is now called Flash Builder, why is the SDK still called "Flex SDK " rather than "Flash SDK"?)</li>
<li>Uninstall whatever version of the player you have on your computer and install the latest one you have downloaded</li>
<li>Replace the playerglobal.swc in the SDK with the separate playerglobal.swc you have downloaded</li>
<li>Set up the SDK in FDT</li>
<li>When creating a new project, make sure to add the <code>-swfversion=13</code> compile argument (I hope this will be dropped when the release is no longer in beta)</li>
<li>In your HTML page, do not forget to set <code>wmode="direct"</code> so the rendering can be done on the GPU</li>
</ol>
<p>Now these steps are really well explained <a title="Targeting Flash Player 11 (Incubator Build) in FDT 4.2" href="http://www.swfgeek.net/2011/03/07/targeting-flash-player-11incubator-build-in-fdt-4-2/" target="_blank">here</a>, although since the release of the beta version (rather than the incubator version), it is no longer necessary to remove the flex.swc.</p>
<p>Now, all this is a bit of work to get running, and just recently, <a title="Get your FDT ready for Molehill" href="http://fdt.powerflasher.com/blog/2011/04/get-your-fdt-ready-for-molehill/" target="_blank">FDT released a template</a> that sets up a lot of that stuff already. This template includes the compiler argument and an HTML template that presets the <code>wmode</code>, it even includes the <a title="M2D" href="https://github.com/egreenfield/M2D" target="_blank">M2D library</a>, which is supposed to help creating 2D elements with the <code>Stage3D</code> API to enjoy the benefits of GPU rendering. Thanks guys! I guess now I want to look into creating templates.</p>
<p><strong>Setting up Away3D</strong></p>
<p><img class="alignleft" title="Away3D sources and libs" src="http://jansensan.net/images/blog/post0026_002.jpg" alt="Away3D sources and libs" width="264" height="140" /></p>
<p>I have been reading good comments here and there about <a title="Away3D" href="http://away3d.com/" target="_blank">Away3D</a>, and the running examples I have seen were quite convincing, so I decided to look into it. Getting started is quite simple.</p>
<ol>
<li>Download the <a title="Away3D downloads" href="http://away3d.com/download/" target="_blank">Away3D 4.0 alpha release</a></li>
<li>Add the provided source files to your classpath</li>
<li>Add the provided Apparat LZMA decoder library as well</li>
</ol>
<p>Logically, this should be it and you should be able to go, but nooo! Have you ever seen a development project work properly on the first try? Especially not a project that uses both an alpha release and a beta release! After a bit of research, I found <a title="How to get started with the Molehill API and Away3D 40.html" href="http://www.disturbmedia.com/wiki/How_to_get_started_with_the_Molehill_API_and_Away3D_40.html" target="_blank">an article on DisturbMedia's wiki</a> that basically explained the situation:</p>
<blockquote><p>Most errors come from AGALMiniAssembler.as. I'm not sure why, but I found that if I split the multiple classes nested in the single file into multiple files that fixes most of the errors. At the end of the process you should have 3 additional classes: Sampler, Register and OpCode.</p></blockquote>
<p>Once the split classes are added, some errors remain in the Away3D sources, however it is possible to compile an empty project at this point.</p>
<p>Next, you would expect to be able to use some Away3D classes already, so did I. The base requirement is to add a <code>View3D</code>, which contains a container that can be used as some sort of stage. Let's try:</p>
<p>ActionScript 3.0:</p>
<blockquote>
<pre class="actionscript">package
<span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">import</span> away3d.<span style="color: #006600;">containers</span>.<span style="color: #006600;">View3D</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageAlign</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageScaleMode</span>;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> Sprite
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">private</span>	<span style="color: #000000; font-weight: bold;">var</span>	_view3D	:View3D;
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// set the stage</span>
	<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span> = StageAlign.<span style="color: #006600;">TOP_LEFT</span>;
	<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span> = StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// create the View3D</span>
	_view3D = <span style="color: #000000; font-weight: bold;">new</span> View3D<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	addChild<span style="color: #66cc66;">&#40;</span>_view3D<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//</span>
	addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, enterFrameHandler<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> enterFrameHandler<span style="color: #66cc66;">&#40;</span>event: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;">// render the View3D at every frame (or other update of your choice)</span>
	_view3D.<span style="color: #006600;">render</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre>
</blockquote>
<p>Try and run that with the explanations I have given so far, you'll most probably run into the same situation I did: an error. Why? I mean, nothing seems wrong.</p>
<p>Here again, it took me a bit of research, but it turns out that <a title="Temp Fix for Stage3D.viewPort change, Flash 11 Beta" href="http://away3d.com/forum/viewthread/267/" target="_blank">a class needed a correction</a> to handle a change made by the beta release of Flash Player. As I understand, this library was mostly written while the player was still an incubator release. Simply get that updated <code>Stage3DProxy</code> class with the one provided in the forum thread, and you're good to go.</p>
<p>From this point on, you can head to minute 2:30 of <a title="Quickstart for Molehill and Away3D" href="http://johnlindquist.com/2011/02/28/quickstart-for-molehill-and-away3d/" target="_blank">John Lindquist's tutorial</a> to see how to add an awesome cube to your project, or look into the <a title="Away3D Broomstick Examples" href="https://github.com/away3d/away3d-examples-broomstick" target="_blank">Away3D examples</a>. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://jansensan.net/setting-up-fdt-for-away3d-and-flash-player-11-beta/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Book review: Flash game development by example</title>
		<link>http://jansensan.net/book-review-flash-game-development-by-example</link>
		<comments>http://jansensan.net/book-review-flash-game-development-by-example#comments</comments>
		<pubDate>Sun, 05 Jun 2011 19:05:59 +0000</pubDate>
		<dc:creator>mat janson blanchet</dc:creator>
				<category><![CDATA[Book Review]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Workflow]]></category>
		<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Emanuele Feronato]]></category>

		<guid isPermaLink="false">http://jansensan.net/?p=576</guid>
		<description><![CDATA[Thanks to Antti Kupila, I have been given a copy of Emanuele Feronato's book Flash Game Development by Example to read and critique. Emanuele has been blogging about Flash gaming for years, and since I am looking to get into gaming, I was eager to read the book. So far I have made it only [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="aligncenter" title="Flash Game Development by Example, the book" src="http://jansensan.net/images/blog/post0024_001.jpg" alt="Flash Game Development by Example, the book" width="640" height="360" /></p>
<p>Thanks to <a href="http://www.anttikupila.com/">Antti Kupila</a>, I have been given a copy of <a href="http://www.emanueleferonato.com/">Emanuele Feronato</a>'s book Flash Game Development by Example to read and critique. Emanuele has been blogging about Flash gaming for years, and since I am looking to get into gaming, I was eager to read the book.</p>
<p>So far I have made it only through the first three chapters. I did a preliminary critique of the book on the <a href="http://www.amazon.com/Flash-Development-Example-Emanuele-Feronato/dp/1849690901/ref=sr_1_1?s=books&amp;ie=UTF8&amp;qid=1307018451&amp;sr=1-1">Amazon product page</a>:</p>
<blockquote><p>Emanuele Feronato has been blogging ([...]) good tips on Flash gaming and PHP development for a while now. When I learned he had written a book on how to get into game development, I had to try it.</p>
<p>In line with his blog, Feronato's writing is clear and concise, no fluff is added. A good thing is that he assumes the reader has already some knowledge of Flash and AS3. This book does present some basic information if you know nothing of Flash, but the intent of this book is to teach you how to do games, not how to program. This is really a good thing, for there are way too many books that think that all readers have never touched programming of their life.</p>
<p>Feronato does a good job at increasing the difficulty of concepts during each chapter. His introduction to each challenge, by splitting the concept and the coding, guides the reader properly.</p>
<p>As a web developer, jumping into gaming is a good and honest challenge. Oftentimes a web dev tends to create complex systems for data and views. Feronato brings back the joy of programming with simple and logic code structures.</p>
<p>I did get put off by the fact that his instructions make use of the Flash IDE, rather than Flash Builder, FDT or FlashDevelop. In a professional environment, it's quite inefficient to code in the Flash IDE, but I believe it would have taken too many explanations to put in a book about games. All in all, it is quite easy to transpose the instructions into code-only IDEs.</p>
<p>I would also have like some more game-oriented optimization tips (like using multiplications rather than divisions, since it is quicker), for gaming is all about being code-efficient. That said, it is a good buy for those who are serious about getting into Flash gaming.</p></blockquote>
<p>I have read the other critiques of the book on Amazon and also other critiques from real game devs like <a href="http://blog.iainlobb.com/2011/05/flash-game-development-by-example-book.html">Iain Lobb</a> and <a href="http://gamedev.rasmuswriedtlarsen.com/2011/05/31/review-flash-game-development-by-example/">Rasmus Wriedt Larsen</a> and it got me thinking.</p>
<p><strong>Critiquing the critique</strong></p>
<p>My grandad used to say: "Only a fool does not change his mind." Whereas I do not think as harshly about the book, I do want to delve deeper into my critique to either correct what I said, or even add to it.</p>
<p>Lobb mentions the coding style of Feronato, which I also noted looks a lot like PHP's style:</p>
<blockquote><p>We all write weird looking code sometimes, but I think a book needs to set a better example.</p></blockquote>
<p>I must say that it did not deter me really too much, since I am used to pick up other people's work and fix it. However, now that I think of it, a book must set an example of good coding practices. I have to agree with Lobb on his critique.</p>
<p>Another practice that really should have been present in Feronato's code is tips that make code execute faster.</p>
<p>Using <code>var array:Array = []</code> may look dirty, but it is way quicker than creating arrays with the <code>new Array()</code> declaration. His examples do not use many arrays, but I do not see why good habits should not be put forward.</p>
<p>Another tip is to try and use multiplications rather than divisions. Again, the examples do not use calculations that are processor intensive, but it is the best moment to explain to new game developers tips that will help them when they have to handle complex systems in the future.</p>
<p>But those tips are something that I knew already and that could have been used in the first three chapters of the book. Let's see when I get further in the book how it goes.</p>
<p>Both Lobb and Larsen look onto Feronato's code as merely scripts, for it doesn't push OOP concepts. As Feronato's book is really built on iterating his code and moving it forward, as many oftentimes code in real life, it would have been nice for him to explain how to transform hid code into classes etc. The speed at which Feronato moves through concepts means the reader is  expected to know how to code. And if so, the reader could also then be  expected to know some OOP. Feronato could have used such a premise to  then push the use of classes. Unknowingly, I worked exactly how Larsen describes what he sees in his book:</p>
<blockquote><p>[...] if you know how to code AS3 with OOP and good coding practises, plus  being able to extract general ways of doing something from very specific  examples [...]</p></blockquote>
<p>As I mentioned in my Amazon critique, I would have preferred it if the  author would have not used the Flash IDE to code, since it's pretty  backwards. As I was going through his examples, I actually used FDT to create a proper project structure, with assets, packages and different classes for specific tasks.</p>
<p>I did enjoy Feronato's scripting style to read, but I do now think the book he wrote should have promoted practices that would be useful in professional environments.</p>
<p>Lobb critiques Feronato's choices of games for being out of touch with current trends. He makes an interesting point:</p>
<blockquote><p>I really think that a lot of game developers around my age group (e.g.  25 and older) are stuck in the past somewhat with their ideas of what  constitutes a computer game.</p></blockquote>
<p>I think that using a lot of pixel sprites in my prototypes casts me in that group. However I must disagree with Lobb here. I am not fan of Flip and Match games, Tetris or other classic puzzlers per se. As good a developer I am, I have never tackled a real game project. Starting from scratch with basic concepts and logics actually helps me learn things properly.</p>
<p>For example, in Minesweeper, the logic of how to use and create a <a title="Wikipedia - Flood Fill" href="http://en.wikipedia.org/wiki/Flood_fill" target="_blank">flood fill</a> function is explained. Since I come from a web development, where the UI is usually the most important concern, I would not know what to call such a concept. It is a definite win for me here.</p>
<p>Another example is the Connect Four winning explanations. Feronato explains how a win is made in concepts and how to translate that in code. I know pseudo code is not something new in development, but learning to do it for gaming is, to me and maybe to other non-gaming developers, something refreshing and useful.</p>
<p>Let's take a look at what I was able to create with what I have read so far.</p>
<p><strong>Concentration, a flip and match game</strong></p>
<p>This game is pretty straightforward, we pretty much all know it. Feronato explains in simple terms how to build such a game.</p>
<p>What  is missing however is an explanation for the reader on how to reset the  game so the player can enjoy more than one game. Actually, the author  does not explain how to restart any of the games he describes. That is a  big part in gaming, it should be present in his book I believe. It  would also have been a great way to show how to use functions. So as you  can see from my prototype above, I have added that logic. Once the game  is complete, the user can retry to play.</p>
<p>Stats on how the game  progresses is also interesting for the player to see. Lobb actually  mentions this in his critique, although I had though of adding these  stats previous to reading the article.</p>
<p style="text-align: center;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="640" height="360" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="FlashVars" value="scale=2" /><param name="src" value="http://jansensan.net/experiments/game-dev-concentration/swf/concentration.swf" /><param name="flashvars" value="scale=2" /><embed type="application/x-shockwave-flash" width="640" height="360" src="http://jansensan.net/experiments/game-dev-concentration/swf/concentration.swf" flashvars="scale=2"></embed></object></p>
<p>As a personal touch, I decided to make my game shareable on Facebook's wall so that my friends could try that little prototype in their news stream. I will eventually explain how easy that is, but it is not the point of this blog post. You may try it yourself in sharing the URL http://jansensan.net/experiments/game-dev-concentration/ onto your news stream. Do not just copy the URL into your browser, it redirects to this post.</p>
<p>Once you share the above URL onto your news stream, it will then show like this:</p>
<p style="text-align: center;"><img class="alignnone" title="Flip and Match in Facebook news stream" src="http://jansensan.net/images/blog/post0024_002.jpg" alt="Flip and Match in Facebook news stream" width="431" height="164" /></p>
<p>If you ever used YouTube or Vimeo on your news stream, you know the Flash piece will expand when they click on the blue play button.</p>
<p style="text-align: center;"><img class="alignnone" title="Flip and Match in Facebook news stream" src="http://jansensan.net/images/blog/post0024_003.jpg" alt="Flip and Match in Facebook news stream" width="458" height="444" /></p>
<p>With those simple additions, people were enjoying a simple prototype and sharing it around.</p>
<p><strong>Minesweeper</strong></p>
<p>To be honest, I never understood the Minesweeper game, or rather what  is compelling to play it. Or maybe I just hate it because that is the  only game that was available on my dad's work computer at the government  when I was visiting and he was asking me to wait for him... Anyways, I  digress...</p>
<p>Feronato only explains what makes the player lose the game, however he should also have explained what is the win situation. An author cannot take for granted that the reader knows the game that is described.</p>
<p style="text-align: center;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="640" height="480" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://jansensan.net/experiments/game-dev-minesweeper/swf/minesweeper.swf" /><embed type="application/x-shockwave-flash" width="640" height="480" src="http://jansensan.net/experiments/game-dev-minesweeper/swf/minesweeper.swf"></embed></object></p>
<p>With this game, Feronato explains some recursion and the concept of the flood fill, which I believe is well worth to learn.</p>
<p>Concerning the use of functions however, the explanations lack. Functions are made to reuse code and, again, restarting a game is the perfect need to reuse code. As you can see from the prototype, you can only play once, which as I mentioned before is not a way to build a complete game.</p>
<p>Maybe I have not read enough tech books, but whenever I stumble upon an example online that uses events, the tutorial not always explains that it is best to remove the event handlers, sometimes even in Adobe's documentation! Many a time did I have to explain or correct other people's code to reduce the demand on memory. Feronato does explain to remove the event handler, I have to give him props for this.</p>
<p><strong>Connect Four</strong></p>
<p>This game is not digital by any means, and recreating it does not make it more fun. However, the logic learning with this game is quite nice. Games like <a title="Drop 7 for iOS" href="http://itunes.apple.com/us/app/drop7/id299940763?mt=8" target="_blank">Drop 7</a> seems to be a game that extrapolates on the concepts defined in Connect Four.</p>
<p style="text-align: center;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="640" height="360" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://jansensan.net/experiments/game-dev-connect-four/swf/connect-four.swf" /><embed type="application/x-shockwave-flash" width="640" height="360" src="http://jansensan.net/experiments/game-dev-connect-four/swf/connect-four.swf"></embed></object></p>
<p>The structure of the code in this game made me cringe. I believe that under no circumstance should a child tell its parent what to do. Sounds logical no? I mean not only about code, just read the previous sentence aloud and you will feel how much sense it makes.</p>
<p>Feronato actually does that here, he write something like <code>this.parent.parent</code>. It is not technically wrong, since it compiles, but I believe that conceptually it is not right. It makes it so complex for someone else to inherit a project and try to figure out such a logic. Big no-no.</p>
<p>While explaining how to check for the game completion, the author does bring forward the idea that is it not efficient to loop through all possible tiles. Good suggestion on code efficiency here.</p>
<p><strong>So, what now?</strong></p>
<p>I do see that this book has some flaws, but nothing that could not have been corrected or pushed more. I think also however that both Larsen and Lobb have dismissed a couple of things in their critique.</p>
<p>a) The audience. Larsen makes a good point that the code is quick and dirty and that the prototypes are not teaching concepts, but that is exactly what the introduction says:</p>
<blockquote><p>Who this book is for</p>
<ul>
<li>AS3 developers who want to know quick and dirty techniques to create Flash games</li>
<li>Flash animators who want to learn how to create games from their works with AS3</li>
<li>Programmers who know languages different than AS3 and want to learn AS3 to make something more interesting and fun than the old "phone book"</li>
<li>Even if you aren't a programmer, but you love Flash games, you can count on this book: you will be guided step by step with clear examples and the support of the full source code of every game</li>
</ul>
</blockquote>
<p>Now, is that audience right? That's another question, but it is clearly stated.</p>
<p>b) The types of game. Lobb did hit on that and I do what to bring it back. The title of the book clearly states that is for building classic games, mostly puzzlers.</p>
<blockquote><p>Build 9 classic Flash games and learn game development along the way</p></blockquote>
<p>Again, the question to raise is, is that a good thing to teach devs? With Stage3d (a.k.a. Molehill), P2P, game controllers on AIR and all sorts of awesome features, Flash can be so much more as a gaming platform.</p>
<p>All in all, I will go on reading the book, for I know I will learn some more things. I agree however that this book does not feel complete, but it feels like a good primer to get into game development. Hey, I may also be wrong, but once a reader knows about some concepts like AI, recursion, etc., it then becomes more possible to search for more precise concepts.</p>
]]></content:encoded>
			<wfw:commentRss>http://jansensan.net/book-review-flash-game-development-by-example/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Simple share functionality for multiple social networks</title>
		<link>http://jansensan.net/simple-share-functionality-for-multiple-social-networks</link>
		<comments>http://jansensan.net/simple-share-functionality-for-multiple-social-networks#comments</comments>
		<pubDate>Mon, 18 Apr 2011 10:29:11 +0000</pubDate>
		<dc:creator>mat janson blanchet</dc:creator>
				<category><![CDATA[Social Networks]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[LinkedIn]]></category>
		<category><![CDATA[RenRen]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[vKontakte]]></category>
		<category><![CDATA[ВКонтакте]]></category>
		<category><![CDATA[人人网]]></category>

		<guid isPermaLink="false">http://jansensan.net/?p=505</guid>
		<description><![CDATA[Whether it's a game, a website, a video or even a subsection in these projects, it seems that they all need a share button. I personally believe it is sometimes too much, to the point that it seems that the PR and media teams avoid doing their work in the hopes that the customer/user will [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;">
	<img class="aligncenter" title="Social Networks Icons" src="http://jansensan.net/images/blog/post0022_001.jpg" alt="Social Networks Icons" width="640" height="126" />
</p>
<p>Whether it's a game, a website, a video or even a subsection in these projects, it seems that they all need a share button. I personally believe it is sometimes too much, to the point that it seems that the PR and media teams avoid doing their work in the hopes that the customer/user will spread the word for them. But that may be just me complaining again.</p>
<p>Nevertheless, I will present here how to use the simple sharing functionality of some of the most prominent social networks around. Remember that Facebook and Twitter are not king and queen everywhere, you may want to target your markets properly.</p>
<p>&nbsp;</p>
<p><img title="Facebook Icon" src="http://jansensan.net/images/icon-facebook.png" alt="Facebook Icon" width="16" height="16" /><strong>&nbsp;Facebook</strong></p>
<p>The infamous social network. As a developer, I kind of like to hate this network, for its documentation is poor and quickly outdated. On the other hand, if Facebook changes its API this often, it allows them to implement new stuff quicker.</p>
<p>Facebook has a relatively simple sharing functionality, even some additional info that you can put on your site metadata to display on the user's wall. I am not taking here about the Like button, which would display number of "likes"—although these <a title="Facebook Like Button Takes Over Share Button Functionality" href="http://mashable.com/2011/02/27/facebook-like-button-takes-over-share-button-functionality/" target="_blank">two functionalities were merged</a>.</p>
<p>In order to share, you do not need Facebook connect at all, you would only need it if your application, game or site requires access to the user's data. You simply need to call a URL:</p>
<blockquote>
<p>http://www.facebook.com/sharer.php?u=http://example.com&amp;t=The Post's Title</p>
</blockquote>
<p>If we deconstruct the URL, it's pretty easy to see what is what:</p>
<div>
<ul>
<li><strong>http://www.facebook.com/sharer.php</strong>: the URL to the PHP function that will share on Facebook</li>
<li><strong>u=http://example.com</strong>:<strong>u</strong> is the variable name which represents the URL that you want to share</li>
<li><strong>t=The Post's Title</strong>: <strong>t</strong> is the variable name which represents the title of what will be shared on the wall</li>
</ul>
</div>
<p>Keep in mind however that if the site that you share has a title and description metadata, the value that you set to <strong>t</strong> will be overwritten.</p>
<p>You may also want to push it further and set yourself the title, the description and even a thumbnail for your site. For example, this is what was done for the adidas Porsche Design site:</p>
<p style="text-align: center;">
<img class="alignnone" title="adidas Porsche Design sharing on Facebook" src="http://jansensan.net/images/blog/post0022_002.jpg" alt="adidas Porsche Design sharing on Facebook" width="640" height="583" />
</p>
<p>In order to set those values, simply add the Facebook metadata tags on the page you wish to share, like so:</p>
<p>HTML:</p>
<blockquote><p><code>&lt;meta property="og:title" content="Your website title goes here"/&gt;<br />
&lt;meta property="og:description" content="Your website description goes here"/&gt;<br />
&lt;meta property="og:image" content="http://www.dummyimage.com/50x50"/&gt;</code>
</p></blockquote>
<p>You may read more in the <a title="Facebook Doc - Like Button" href="http://developers.facebook.com/docs/reference/plugins/like/" target="_blank">Facebook documentation</a>. (I can't believe I just wrote that...)</p>
<p>&nbsp;</p>
<p><img title="Twitter Icon" src="http://jansensan.net/images/icon-twitter-2.png" alt="Twitter Icon" width="16" height="16" /><strong>&nbsp;Twitter</strong></p>
<p>The way to share on Twitter is pretty similar to Facebook's, if not simpler. Here as well, you may simply use a URL with some parameters:</p>
<blockquote>
<p><!-- p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Monaco; color: #2a00ff} span.s1 {color: #000000} -->http://twitter.com/share?url=http://example.com&amp;text=This tweet has been shared via a URL.</p>
</blockquote>
<p>Here again, here is a breakdown of the URL:</p>
<div>
<ul>
<li><strong>http://twitter.com/share</strong>: the URL to the function that will share on Twitter</li>
<li><strong>url=http://example.com</strong>: <strong>url</strong> is the variable name which represents the URL that you want to share</li>
<li><strong>text=This tweet has been shared via a URL.</strong>: <strong>text</strong> is the variable name which contains the tweet</li>
</ul>
</div>
<p>&nbsp;</p>
<p><img title="LinkedIn Icon" src="http://jansensan.net/images/icon-linkedin.png" alt="LinkedIn Icon" width="16" height="16" /><strong>&nbsp;LinkedIn</strong></p>
<p>LinkedIn also offers a simple URL for sharing:</p>
<blockquote>
<p><!-- p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Monaco; color: #2a00ff} span.s1 {color: #000000} -->http://www.linkedin.com/shareArticle?mini=true&url=http://example.com&title=The Post's Title</p>
</blockquote>
<p>Their share URL contains an additional parameter:</p>
<div>
<ul>
<li><strong>http://www.linkedin.com/shareArticle</strong>: the URL to the function that will share on LinkedIn</li>
<li><strong>mini=true</strong>: for some reason, the API requires this <strong>mini</strong> parameter to always be present and set to true.</li>
<li><strong>url=http://example.com</strong>:<strong>url</strong> is the variable name which represents the URL that you want to share</li>
<li><strong>title=The Post's Title</strong>: <strong>title</strong> is the variable name which represents the title of what will be shared</li>
</ul>
</div>
<p>LinkedIn provides <a href="http://developer.linkedin.com/docs/DOC-1075" target="_blank">well explained documentation</a> on how to use their sharing functionality. Although for the life of me I don't get why I would ever use the summary parameter.</p>
<p>&nbsp;</p>
<p><img title="ВКонтакте Icon" src="http://jansensan.net/images/icon-vkontakte.png" alt="ВКонтакте Icon" width="16" height="16" /><strong>&nbsp;ВКонтакте (vKontakte)</strong></p>
<p>ВКонтакте's share is <a href="http://vkontakte.ru/page-1_18900008" target="_blank">well documented</a>. Can't read Russian? Get <a href="http://www.google.com/chrome/" target="_blank">Google Chrome</a>, it translates websites quite well.</p>
<p>Even though there is a lot of options available for sharing, let's look at a simple share:</p>
<blockquote>
<p><!-- p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Monaco; color: #2a00ff} span.s1 {color: #000000} -->http://vkontakte.ru/share.php?url=http://example.com&title=The Post's Title</p>
</blockquote>
<p>Pretty much the same as Facebook:</p>
<div>
<ul>
<li><strong>http://vkontakte.ru/share.php</strong>: the URL to the PHP function that will share</li>
<li><strong>url=http://example.com</strong>:<strong>url</strong> is the variable name which represents the URL that you want to share</li>
<li><strong>title=The Post's Title</strong>: <strong>title</strong> is the variable name which represents the title of what will be shared</li>
</ul>
</div>
<p>&nbsp;</p>
<p><img title="人人网 Icon" src="http://jansensan.net/images/icon-renren.png" alt="人人网 Icon" width="16" height="16" /><strong>&nbsp;人人网 (RenRen)</strong></p>
<p>We now enter the realm of assumptions, I simply relay information I found about how to share on RenRen. A user named <a href="https://forums.jigsy.com/showthread.php?tid=877" target="_blank">Yuao shared his knowledge</a>, and it seems that again, a simple URL with some parameters do the trick:</p>
<blockquote>
<p><!-- p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Monaco; color: #2a00ff} span.s1 {color: #000000} -->http://share.renren.com/share/buttonshare.do?link=http://example.com&title=The Post's Title</p>
</blockquote>
<p>Let's see what that URL is made of:</p>
<div>
<ul>
<li><strong>http://share.renren.com/share/buttonshare.do</strong>: the URL to the function that will share on RenRen</li>
<li><strong>link=http://example.com</strong>: <strong>link</strong> is the variable name which represents the URL that you want to share</li>
<li><strong>title=The Post's Title</strong>: <strong>title</strong> is the variable name which represents the title of what will be shared</li>
</ul>
</div>
<p>Due to the documentation being all in Chinese, it's kind of hard to obtain more information. As far as I know, Google cannot sift through stuff in China easily. Also, the company itself did not want to provide help unless huge sums were paid upfront.</p>
<p>&nbsp;</p>
<p><strong>Other networks</strong></p>
<p>There are obviously other networks, but as you can see from the last one of which I spoke, my knowledge gets thin. <a href="http://hyves.nl/" target="_blank">Hyves</a>, <a href="http://www.orkut.com/" target="_blank">Orkut</a> and <a href="http://mixi.jp/" target="_blank">Mixi</a> may be of interest if your data shows that your users are part of such networks.</p>
<p>As for every project, make sure that implementing the sharing functionality makes sense. It may be obvious, but not all networks need to be targetted for every single project.</p>
<p>If you target multiple markets and social networks, do look into the proper etiquette of each. Last I heard, commercial presence was a bit frowned upon on Mixi.</p>
]]></content:encoded>
			<wfw:commentRss>http://jansensan.net/simple-share-functionality-for-multiple-social-networks/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugging Flash with Terminal or Command Prompt</title>
		<link>http://jansensan.net/debugging-flash-with-terminal-or-command-prompt</link>
		<comments>http://jansensan.net/debugging-flash-with-terminal-or-command-prompt#comments</comments>
		<pubDate>Fri, 25 Feb 2011 22:45:47 +0000</pubDate>
		<dc:creator>mat janson blanchet</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Workflow]]></category>
		<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash Player]]></category>
		<category><![CDATA[Terminal]]></category>

		<guid isPermaLink="false">http://jansensan.net/?p=488</guid>
		<description><![CDATA[All programming languages offer a way to debug and send some info to a console. In the Flash IDE, the traces are displayed in the output window, in Flash Builder/FDT/Eclipse the traces are sent to the console view. And so on. That is useful while debugging, however most often those traces are needed in the [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter" title="Terminal and Command Prompt" src="http://jansensan.net/images/blog/post0020_001.jpg" alt="" width="640" height="128" />All programming languages offer a way to debug and send some info to a console. In the Flash IDE, the traces are displayed in the output window, in Flash Builder/FDT/Eclipse the traces are sent to the console view. And so on.</p>
<p>That is useful while debugging, however most often those traces are needed in the final context of the Flash piece, like a website.</p>
<p>There are some awesome tools out there that do just that and in style. I'm thinking of <a title="De MonsterDebugger" href="http://demonsterdebugger.com/" target="_blank">De MonsterDebugger</a>. That tool is really good, although it requires some additional classes and long words to write to get something simple (eg: <code>MonsterDebugger.trace(this, "Hello World!")</code>).</p>
<p>There is a context in which that tool cannot be used, and that is Facebook. Ah! the ever hatable social network platform! Facebook does not allow <code>ExternalInterface</code> for security reasons, <span style="text-decoration: line-through;">that is why tools like De MonsterDebugger are not available, this is how data is sent to their debugger</span>. Edit: After receiving a comment, I investigated, it indeed turns out that MonsterDebugger is available when working on Facebook, as it indeed uses <code>LocalConnection</code>. My bad for assuming. However, the use of <code>ExternalInterface</code> is still not possible on Facebook, and the point of this post is more to show how to use the Terminal and Command Prompt to debug.</p>
<p>Sure you could create a textfield and add it to the display list, but that can become tedious if you need to add scrollbars and such.</p>
<p>A good alternative is to use a debugger that functions regardless of context. Adobe's open source <a title="Flex SDK" href="http://opensource.adobe.com/wiki/display/flexsdk/Flex+SDK" target="_blank">Flex SDK</a> comes with the Flash Debugger, a JAR package that can be run with Java from the command line.</p>
<p>Yikes!</p>
<p>That sounds more complicated than it really is actually. Let's see how to run the debugger on Mac OSX and Windows.</p>
<p><strong>Terminal on Mac OSX</strong></p>
<p><img class="aligncenter" title="Flash Debugger in Terminal" src="http://jansensan.net/images/blog/post0020_002.jpg" alt="" width="640" height="390" /></p>
<p>The screenshot above provides already a lot of information on how to run that package, but let's spell it out step by step:</p>
<ol>
<li>Open Terminal</li>
<li>Go to the directory where of the Flex SDK you want to use</li>
<li>Go to the "lib" directory</li>
<li>In that directory, you need to run the "fdb.jar" package. To do so, type "java -jar fdb.jar"</li>
<li>To start the debugger, type "r", for run. The traces and errors will then be displayed in the Terminal</li>
</ol>
<p>This will work for all SWFs published with debugging enabled, whether they are local or online, and that is where it becomes useful, for debugging stuff online. Just make sure then that you installed the debugger version of the Flash Player so it can communicate with the Terminal.</p>
<p><strong>Command Prompt on Windows</strong></p>
<p><img class="aligncenter" title="Flash Debugger in Command Prompt" src="http://jansensan.net/images/blog/post0020_003.jpg" alt="" width="640" height="390" /></p>
<p>It's pretty much the same on Windows with the Command Prompt. You can see here that I use Windows XP, so I'm sorry if there are slight differences on Vista or 7, I don't have those OSes.</p>
<ol>
<li>Open the Command Prompt</li>
<li>Go to the directory where of the Flex SDK you want to use</li>
<li>Go to the "lib" directory</li>
<li>In that directory, you need to run the "fdb.jar" package. To do so, type "java -jar fdb.jar"</li>
<li>To start the debugger, type "r", for run. The traces and errors will then be displayed in the Command Prompt</li>
</ol>
<p>Happy debugging!</p>
]]></content:encoded>
			<wfw:commentRss>http://jansensan.net/debugging-flash-with-terminal-or-command-prompt/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Loading an SWC at runtime in different IDEs</title>
		<link>http://jansensan.net/loading-an-swc-at-runtime-in-different-ides</link>
		<comments>http://jansensan.net/loading-an-swc-at-runtime-in-different-ides#comments</comments>
		<pubDate>Fri, 03 Dec 2010 14:41:39 +0000</pubDate>
		<dc:creator>mat janson blanchet</dc:creator>
				<category><![CDATA[Technologies]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Workflow]]></category>
		<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Ant]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[FDT]]></category>
		<category><![CDATA[Flash Builder]]></category>
		<category><![CDATA[FlashDevelop]]></category>
		<category><![CDATA[IntelliJ IDEA]]></category>

		<guid isPermaLink="false">http://jansensan.net/?p=444</guid>
		<description><![CDATA[Aah, the joy of being able to obtain code completion during development, what dev doesn't like this? A while back, I explained how to link to SWCs to get code completion, but not compile said SWC in the SWF, so it could be loaded at runtime. It turns out that all IDEs do not necessarily [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="aligncenter" title="From SWC to IDEs to SWF to Interwebs" src="http://jansensan.net/images/blog/post0019_001.png" alt="From SWC to IDEs to SWF to Interwebs" width="640" height="320" /></p>
<p>Aah, the joy of being able to obtain code completion during development, what dev doesn't like this? A while back, I explained how to link to SWCs to get code completion, but not compile said SWC in the SWF, so it could be loaded at runtime. It turns out that all IDEs do not necessarily behave the same way Flash Builder 4 does. Figures...</p>
<p>Ever since <a title="Flash on the Beach 2010 – The Aftermath" href="http://jansensan.net/flash-on-the-beach-2010-the-aftermath" target="_self">I went to Flash on the Beach</a>, I have been playing with more IDEs than <a title="Flash Builder" href="http://www.Adobe.com/FlashBuilder" target="_blank">Flash Builder</a>. I have to say I've been quite lucky: all attendees were given the <a title="FDT" href="http://www.fdt.powerflasher.com/" target="_blank">FDT 4</a> Pure IDE. Also, I have been <a title="Flash on the Beach raffle winners, more prizes at Adobe MAX" href="http://blogs.jetbrains.com/idea/2010/10/flash-on-the-beach-raffle-winners-more-prizes-at-adobe-max/" target="_blank">one of the lucky winners</a> of an <a title="IntelliJ IDEA" href="http://www.jetbrains.com/idea/" target="_blank">IntelliJ IDEA 9</a> licence. And since I was already trying all sorts of IDEs, why not look at <a title="FlashDevelop" href="http://www.flashdevelop.org/" target="_blank">FlashDevelop</a>, an open source IDE, sadly available only on Windows.</p>
<p>When I look at the metrics of my blog, it seems that loading a SWC at runtime (along with the <a title="Communication between AS3 and Facebook" href="http://jansensan.net/communication-between-as3-and-facebook" target="_self">post about Facebook and AS3</a>) is the most popular subject, so I though it would be better if I was more thorough and explain how to repeat the workflow mentioned above for all these IDEs. Obviously, if anybody knows even better ways to go about what I'm about to explain, by all means share with me.</p>
<p><strong>Flash Builder 4</strong></p>
<p>I already published <a title="Loading assets dynamically, part 2" href="http://jansensan.net/loading-assets-dynamically-part-2" target="_self">a post about this</a> workflow and this IDE, simply refer back to it.</p>
<p><strong>FDT 4</strong></p>
<p>FDT is pretty much the same thing as FlashBuilder, albeit better. It is also based on Eclipse, but all those <a title="My setup" href="http://jansensan.net/my-setup" target="_self">plugins I needed</a> or those that <a title="Sourcemate" href="http://www.elementriver.com/sourcemate">some devs boasted about</a>, they are already in place in FDT.</p>
<p>When you want to add an SWC, I must say it's quite simpler here. No need to go hunt in the preferences's subcategories. Right-click on that SWC, the select "Source Folder &gt; Add to Classpath".</p>
<p style="text-align: center;"><img class="alignnone" title="Source Folder &gt; Add to Classpath" src="http://jansensan.net/images/blog/post0019_002.jpg" alt="Source Folder &gt; Add to Classpath" width="518" height="311" /></p>
<p><span style="text-decoration: line-through;">Now, the small issue that this presents, there is no prompt that asks whether you want to embed said SWC in your code, or to load it at runtime. I hunted a bit for this one, but in the end it takes s simple manual change.</span> I was wrong on this one, as pointed out by <a rel="external nofollow" href="http://blog.mattes-groeger.de/">Mattes Groeger</a>:</p>
<blockquote><p>[...] you can set this option via GUI. Just call the project “Properties” window by right clicking the project in the Flash Explorer view. Then in “FDT Build Path” click on the “Build Order” Tab. Here you can choose for each library via check box if it should be used as runtime library or not.</p></blockquote>
<p style="text-align: center;"><img class="alignnone" title="Setting a library as runtime in FDT's project prefs" src="http://jansensan.net/images/blog/post0019_011.jpg" alt="Setting a library as runtime in FDT's project prefs" width="640" height="570" /></p>
<p>Alternatively, you can still go and play with the settings file, as I explain below.</p>
<p>Whenever you create a project in any Eclipse-based IDE, a .project file is created. With FlashBuilder, there is also a .actionScriptProperties. Do not worry if you have never seen these files, they are "invisible." They are actually XML files that contain project configurations. I'll probably have to come back to these sometime in the future.</p>
<p>I was then thinking, FDT probably creates those files too. Lo and behold, it does! Now, how do you edit these? At the top right of the Flash Explorer panel, there is a triangle pointing down. In there, select "<img title="Eclipse Filters Icon" src="http://jansensan.net/images/blog/post0019_003.jpg" alt="Eclipse Filters Icon" width="16" height="16" /> Filters...".</p>
<p><img class="aligncenter" title="Filters" src="http://jansensan.net/images/blog/post0019_004.jpg" alt="Filters" width="314" height="437" /></p>
<p>From there you can select what types of file you want and do no want to see. Personally, I really dislike it when there are hidden files, so I want to see all of them. Once you have that in place, you will be able to find the .settings folder.</p>
<p style="text-align: center;"><img class="alignnone" title=".settings folder" src="http://jansensan.net/images/blog/post0019_005.jpg" alt=".settings folder" width="220" height="70" /></p>
<p>I mentioned the .actionScriptProperties for FlashBuilder, for FDT you want to open com.powerflasher.fdt.classpath. In there, all the sources paths are listed. In order to change an SWC so that its code is not embedded in the SWF, change the <code>useAsSharedCode</code> attribute from <code>false</code> to <code>true</code>. Then just make sure you load your SWF before you instantiate any class from that shared library.</p>
<p>Voilà for FDT.</p>
<p><strong>IntelliJ Idea</strong></p>
<p>Now for another beast altogether. I must say I have not been able to familiarize myself much with this IDE yet, and I find it a bit more complex than necessary. I mostly had to refer myself to <a title="IntelliJ Idea9 ActionScript 3/Flex Workflow Part 1" href="http://insideria.com/2010/02/intellij-idea9-actionscript-3f.html" target="_blank">Jesse Freeman's posts</a> about the AS3 in IntelliJ IDEA workflow to be able to set up and compile a basic project. Once that behemoth in the room is taken care of and you know where to add SWCs and sources as libraries, it turns out to be simple.</p>
<p style="text-align: center;"><img class="alignnone" title="IntelliJ Idea - Module settings menu" src="http://jansensan.net/images/blog/post0019_006.jpg" alt="IntelliJ Idea - Module settings menu" width="380" height="716" /></p>
<p>First, right-click on your project (which is called a module in IntelliJ IDEA) and select "Module Settings".</p>
<p style="text-align: center;"><img class="alignnone" title="IntelliJ IDEA Module Settings" src="http://jansensan.net/images/blog/post0019_007.jpg" alt="IntelliJ IDEA Module Settings" width="640" height="346" /></p>
<p>In these settings, select your module and go to the Dependencies tab. If your SWCs are not there already, add them with–you guessed it!– the "Add" button. At the right of the path to that SWC, there is a combo box from which you can select <a title="JetBrains - Dependencies Tab" href="http://www.jetbrains.com/idea/webhelp/dependencies-tab.html" target="_blank">options for the scope</a>: "Compile", "Test", "Runtime" and "Provided". The one we need to choose in this case is "Provided".</p>
<p>You can now go about your business for the rest of your project.</p>
<p><strong>FlashDevelop</strong></p>
<p>Working with FlashDevelop is a bit tricky for me, since I'm on a Mac. However, back when I was working at an e-com shop 3 years ago, I was using FlashDevelop, I should be alright. What really help I have to say is that this tool has an active community, which saved me a great deal of trouble. A <a title="Google Search" href="http://lmgtfy.com/?q=swc+runtime+flashdevelop" target="_blank">search on Google</a> and I got <a title="FlashDevelop forum thread - Load SWC at runtime" href="http://www.flashdevelop.org/community/viewtopic.php?p=22197" target="_blank">this forum thread</a> which answers all my questions. So, credit given when credit due, now let's look at what is to be done with FlashDevelop.</p>
<p style="text-align: center;"><img title="FlashDevelop - Add SWC to library menu" src="http://jansensan.net/images/blog/post0019_008.jpg" alt="FlashDevelop - Add SWC to library menu" width="292" height="260" /></p>
<p>First, select your SWC from your library, and from the contextual menu, choose "Add To Library".</p>
<p style="text-align: center;"><img class="aligncenter" title="FlashDevelop - Library Options" src="http://jansensan.net/images/blog/post0019_009.jpg" alt="FlashDevelop - Library Options" width="300" height="282" /></p>
<p>Once the SWC is added, the label will show in blue. Right-click again on it, and then the "Options" menu is visible.</p>
<p style="text-align: center;"><img class="aligncenter" title="FlashDevelop - Library Options Window" src="http://jansensan.net/images/blog/post0019_010.jpg" alt="FlashDevelop - Library Options Window" width="330" height="304" /></p>
<p>In that window, you are presented with the necessary options. In this IDE as well the terminology changes, but at least a description is given. Check "External library (not included)".</p>
<p><strong>Ant</strong></p>
<p>As you will see from the sources I provide at the end of the article, all these IDEs actually play along quite well. But if for one reason or another, you are more the I-don't-depend-on-no-IDE kind of person, you can also compile your project directly into Ant. I know that after I have written <a title="Speed up development process with Ant" href="http://jansensan.net/speed-up-dev-process-with-ant">my post about Ant</a>, I should have written about how to compile with said insect, but since I faced the loading a SWC at runtime issue first when I started dabbling with FDT, I thought about writing about this workflow first.</p>
<p>Let's get to business. In ant, you need to use the <code>runtime-shared-library-path</code> tag so that when you compile your code is checked against your library, but that the library is not included.</p>
<blockquote><p><code>&lt;runtime-shared-library-path path-element="${ASSETS_DIR}/swf/assets.swc"&gt;<br />
&lt;url rsl-url="${ASSETS_DIR}/swf/assets.swf"/&gt;<br />
&lt;/runtime-shared-library-path&gt;</code></p></blockquote>
<p>Specify in the <code>path-element</code> attribute the path to the SWC, and in the <code>rsl-url</code> attribute of the <code>url</code> tag against what SWF the SWC is compared.</p>
<p>Simple no?</p>
<p><strong>Conclusion</strong></p>
<p>This is actually the first time I try to do something and see how each IDE behaves. I'm quite happy to have found out that all of these play well together, that is good news to know that if you commit all of your project built in one IDE to SVN, that another developer can check it out and run it in another IDE without conflict.</p>
<p>However, one thing that did bother me is the amount of crap (the worst are Flash Builder and IntelliJ IDEA) that these idea create, there are so many useless files! And I am sorry for this, they are all part of the sources as well.</p>
<p>More related to the issue though, it is nice that all IDEs offer a way to work with code completion of assets at development time.</p>
<p><strong>Sources</strong></p>
<p><a title="Sources" href="http://jansensan.net/compressed/Tutorial_LoadSWCAtRuntime.zip">As promised</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jansensan.net/loading-an-swc-at-runtime-in-different-ides/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Speed up development process with Ant</title>
		<link>http://jansensan.net/speed-up-dev-process-with-ant</link>
		<comments>http://jansensan.net/speed-up-dev-process-with-ant#comments</comments>
		<pubDate>Mon, 11 Oct 2010 14:32:01 +0000</pubDate>
		<dc:creator>mat janson blanchet</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Workflow]]></category>
		<category><![CDATA[Ant]]></category>
		<category><![CDATA[Eclipse]]></category>

		<guid isPermaLink="false">http://jansensan.net/?p=378</guid>
		<description><![CDATA[As developers, there are inevitably repetitive tasks that need to be done and redone with each new project we undertake. The best example I can provide is creating the folder structure for our projects. Obviously this is personal to each dev, and there is also some stuff that some of our IDEs–hmm, I'm looking at you Flash [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter" title="Apache Ant" src="http://jansensan.net/images/blog/post0016_001.jpg" alt="Apache Ant" width="640" height="128" /></p>
<p>As developers, there are inevitably repetitive tasks that need to be done and redone with each new project we undertake. The best example I can provide is creating the folder structure for our projects. Obviously this is personal to each dev, and there is also some stuff that some of our IDEs–hmm, I'm looking at you Flash Builder–add by themselves. Oftentimes I have simply copied/pasted the structure from project to project, even created a folder structure without any files, just so I could copy paste. That was a while ago.</p>
<p>In line with the <a title="My setup" href="http://jansensan.net/my-setup" target="_self">mission of this blog</a>, I present you with another useful tool: Ant.</p>
<p>Ant is basically <a title="Apache Ant User Manual" href="http://ant.apache.org/manual/index.html" target="_blank">a set of tasks</a> written in an XML file. Tasks such as moving and copying files and directories, but sometimes much more powerful stuff. If I am not mistaken, this is how a lot of patient developers call manually on the compiler from the Flex SDK to create their SWCs and SWFs.</p>
<p>As I said in my previous posts, I never pretend to be a master at what I explain, but I like to share functional and straightforward knowledge of the tools, languages and utilities I use.</p>
<p><strong>Of IDEs and Ant</strong></p>
<p>As I have mentioned a couple of times, I do not use the Flash Builder 4 standalone IDE, but rather FB4 as a plugin to my Eclipse IDE. I once mentioned that already-integrated XML editor of Eclipse was an advantage over the standalone FB4. Here also Eclipse wins. Go to Window > Show View... > Other... and this is how you obtain a convenient Ant view panel.</p>
<p><img class="aligncenter" title="Eclipse's Ant View Panel" src="http://jansensan.net/images/blog/post0016_002.jpg" alt="Eclipse's Ant View Panel" width="640" height="495" /></p>
<p>FDT also offers such a panel, so does <a title="IntelliJ IDEA" href="http://www.jetbrains.com/idea/" target="_blank">IntelliJ IDEA</a>, however I have no idea what is available for FlashDevelop. For the standalone version of Flash Builder, <a title="a search in Google" href="http://lmgtfy.com/?q=flash+builder+ant" target="_blank">a search on Google</a> quickly provides <a title="FB4 Standalone - How to Install Ant in Flash Builder 4 Premium" href="http://www.zoltanb.co.uk/articles/flash-blog/162-fb4-standalone-how-to-install-ant-in-flash-builder-4-premium" target="_blank">examples</a> of how to add the panel to the IDE.</p>
<p>Now, let's look at some Ant stuff!</p>
<p><strong>Structure and conventions</strong></p>
<p>Usually, Ant files are called "build.xml" and are placed in the "build" directory. That directory also contains a properties files called "build.properties."</p>
<p><img class="alignnone" title="Ant build files structure" src="http://jansensan.net/images/blog/post0016_003.jpg" alt="Ant build files structure" width="136" height="59" /></p>
<p>Let's look at them.</p>
<p>a) build.xml</p>
<p>Ant being an XML, it is obviously written in tags. However contrary to XML, tag names are important here:</p>
<p>Ant:</p>
<blockquote>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;project</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Project Name&quot;</span> <span style="color: #000066;">default</span>=<span style="color: #ff0000;">&quot;Default Task Name&quot;</span> <span style="color: #000066;">basedir</span>=<span style="color: #ff0000;">&quot;../&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- your tasks here --&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/project<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
</blockquote>
<p>So as you can see, the root tag is named  "project" and it contains some attributes:</p>
<ul>
<li>The "name" attribute is the label that will be presented in the the Ant view panel.</li>
<li>The "default" attribute is the task that is called by default. More explanations on that further.</li>
<li>The "basedir" attribute is the base directory from where the tasks are called for moving, creating, etc.</li>
</ul>
<p>b) build.properties</p>
<p>The properties file is a variables container.</p>
<p>Properties:</p>
<blockquote>
<pre class="xml">projectname=Tutorial_AntToLeverageInteractiveDev
&nbsp;
source.filename=Main.swf
source.debugdir=bin-debug/
source.releasedir=bin-release/
&nbsp;
target.filename=deployed-file.swf
target.deploydir=www/</pre>
</blockquote>
<p>Simply write the variable name, an equal sign ("=") and the value. There is no need for semi-colon at the end of the line.</p>
<p>To import it in the build, use the property tag. As we have seen, all paths are relative to the value of the basedir tag, defined before.</p>
<p>Ant:</p>
<blockquote>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;project</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Project Name&quot;</span> <span style="color: #000066;">default</span>=<span style="color: #ff0000;">&quot;Default Task Name&quot;</span> <span style="color: #000066;">basedir</span>=<span style="color: #ff0000;">&quot;../&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">file</span>=<span style="color: #ff0000;">&quot;build.properties&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/project<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
</blockquote>
<p>Now all those variables are available for you to use in your tasks. The syntax to use a property is simple: start with a dollar sign ($), and put the property name in curly brackets ({}).</p>
<p>Ant:</p>
<blockquote>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;echo<span style="font-weight: bold; color: black;">&gt;</span></span></span>This property has a value of ${someProperty}<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/project<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
</blockquote>
<p><strong>Tasks</strong></p>
<p>Tasks are basically Ant functions or methods. The <a title="Apache Ant Manual" href="http://ant.apache.org/manual/index.html" target="_blank">Apache Ant Manual</a> is full of examples that can be used quickly, but if you have never used Ant, it might be overwhelming. Let's start by looking at simple things that can be done.</p>
<p>One main thing that annoys me with Flash Builder is the superfluous use of the html-template folder and wrapper. Also, if I happen to put any directories or file in the bin-debug, they got overwritten for all sorts of reasons. I then took the habit of having some folders of my own at the root level of my project, namely:</p>
<ul>
<li>assets (for the assets used by the application)</li>
<li>build (for the Ant tasks)</li>
<li>libs (for external libraries)</li>
<li>res (for all resources that the application does not use, tracking plans, copy decks, etc., which is not committed to SVN)</li>
<li>www (the folder which actually contains the deployment files)</li>
</ul>
<p>As I create these directories for most of my projects, it was becoming tedious to create them all the time. This is where Ant is your friend.</p>
<p>Ant:</p>
<blockquote>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Create AS3 Project Directories&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- create the assets directories --&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;assets/css&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;assets/fla&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;assets/fonts/osx&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;assets/fonts/win&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;assets/images&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;assets/sounds&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;assets/swf&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;assets/videos&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;assets/xml&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- create the libs directories --&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;libs/src&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;libs/swc&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- create the www directories --&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;www/assets/css&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;www/assets/images&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;www/assets/sounds&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;www/assets/swf&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;www/assets/videos&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;www/assets/xml&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;www/css&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;www/js&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;www/images&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/target<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
</blockquote>
<p>The only thing needed when I start a new project is to copy my Ant build to the new project and then launch the Ant task, and all folders are created quickly. Let's analyze what I presented above.</p>
<p>The target tag contains the tasks. It is mandatory to give it a name, hence the name attribute. Inside that tag is where you place the Ant tasks, such as <a title="mkdir - Apache Ant Manual" href="http://ant.apache.org/manual/Tasks/mkdir.html" target="_blank">mkdir</a> (make directory).</p>
<p>It is interesting to note that you can immediately create a directory inside another one, even if the root one does not exist. Calling mkdir with the attribute "www/assets" will create both the "www" and the "assets" directories at once.</p>
<p><strong>Refreshing the workspace</strong></p>
<p>Whenever files are modified outside of an Eclipse-based IDE, it is good to refresh the workspace. When creating, moving, deleting or renaming files in Ant, such a refresh is also necessary. It is however quite annoying to do this manually every time.</p>
<p><img class="aligncenter" title="Eclipse's External Tools Configuration" src="http://jansensan.net/images/blog/post0016_004.jpg" alt="Eclipse's External Tools Configuration" width="480" height="208" /></p>
<p>The first time you run an Ant task, it will then be added in Eclipse's External Tools Configuration menu. You can access it by clicking on the button that has sort of a suitcase icon.</p>
<p>Earlier I wrote about the default task of the build task. I usually echo the timestamp as the default task, so I can then simply double click on my build in the Ant view to trigger it and avoid creating or deleting stuff too early. Then I go to the config.</p>
<p>Let's take a look at them.</p>
<p><img class="alignnone" title="Eclipse's External Tools Config - JRE" src="http://jansensan.net/images/blog/post0016_005.jpg" alt="Eclipse's External Tools Config - JRE" width="640" height="465" /></p>
<p>The first thing you want to do in these configs is to make sure that you check the "Run in the same JRE as the workspace" in the "JRE" tab, so that the refresh succeeds. Obviously, if you have other used for the JRE, rely on your knowledge, but for generic AS3 development, this serves my purpose.</p>
<p><img class="alignnone" title="Eclipse's External Tools Config - Refresh" src="http://jansensan.net/images/blog/post0016_006.jpg" alt="Eclipse's External Tools Config - Refresh" width="640" height="465" /></p>
<p>Then, head to the "Refresh" tab and check what you want to refresh once the Ant build has completed.</p>
<p><strong>Look at the ants on the floor<br />
They work real hard, lifting three times their mass<br />
And sometimes more</strong></p>
<p><a title="Nick Diamonds" href="http://en.wikipedia.org/wiki/Nicholas_Thorburn" target="_blank">Nick Diamonds</a> could not have phrased it better when he sung <a title="The Unicorns - Let's Get Known" href="http://www.youtube.com/watch?v=Qtj9_q6Zo80" target="_blank">Let's Get Known</a>. What I presented here and what I know of Ant is but a small part of what it can do. When you realize that you repeat actions often in your Finder or Windows Explorer, think of this tool, it will save you time.</p>
]]></content:encoded>
			<wfw:commentRss>http://jansensan.net/speed-up-dev-process-with-ant/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Communication between AS3 and Facebook</title>
		<link>http://jansensan.net/communication-between-as3-and-facebook</link>
		<comments>http://jansensan.net/communication-between-as3-and-facebook#comments</comments>
		<pubDate>Tue, 10 Aug 2010 06:29:47 +0000</pubDate>
		<dc:creator>mat janson blanchet</dc:creator>
				<category><![CDATA[Social Networks]]></category>
		<category><![CDATA[Technologies]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://jansensan.net/?p=312</guid>
		<description><![CDATA[For a recent project, we needed to have AS3 communicate with Facebook and the other way around. Facebook have created a whole framework around their application, which I believe makes some sense for security purposes, but is unfortunately not documented in an orderly manner. I do not pretend that I am the first one to write [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="aligncenter" title="Communication between AS3 and Facebook" src="http://jansensan.net/images/blog/post0015_001.jpg" alt="Communication between AS3 and Facebook" width="640" height="128" /></p>
<p>For a recent project, we needed to have AS3 communicate with Facebook and the other way around. Facebook have created a whole framework around their application, which I believe makes some sense for security purposes, but is unfortunately not documented in an orderly manner.</p>
<p>I do not pretend that I am the first one to write a post about how to communicate between AS3 and Facebook Javascript (or FBJS), but all those tutorials I found expected you to know some thing or another about programming for Facebook, never taking it from the start. So, here goes, communication between AS3 and Facebook for dummies. Or beginners, if you feel offended by the dummy title. Or if it infringes on patents and stuff.</p>
<p><strong>Of knowledge and tools</strong></p>
<p>In this tutorial, I do assume that you have some knowledge of the tools I will present, namely AS3, HTML, JS, CSS and PHP. You, however, do not need to be a master of all (I sure am not).</p>
<p>As presented before, I work in <a title="Eclipse" href="http://www.eclipse.org/" target="_blank">Eclipse</a>, meaning that with the <a title="Flash Builder 4 plugin" href="http://www.adobe.com/products/flashbuilder/" target="_blank">Flash Builder 4 plugin</a>, the <a title="Web Tools Platform" href="http://www.eclipse.org/webtools/" target="_blank">Web Tools Platform</a> and the <a title="PHPEclipse plugin" href="http://www.phpeclipse.com/" target="_blank">PHPElipse plugin</a>, I only need one environment. You will also need to make sure that your server runs PHP. Locally, you can use <a title="MAMP" href="http://www.mamp.info/" target="_blank">MAMP</a> to run a PHP server (thanks to <a title="Antti Kupila" href="http://www.anttikupila.com/" target="_blank">Antti Kupila</a> for the tip on that one).</p>
<p><strong>Facebook's framework</strong></p>
<p>Before we go on writing any code, maybe it is good to know a bit how Facebook works. In order to run JS, Facebook have their own subset or framework which they dubbed <a title="FBJS" href="http://wiki.developers.facebook.com/index.php/FBJS" target="_blank">FBJS</a>.</p>
<p>To embed an SWF, you need to create an <code><a title="Facebook - fb:swf" href="http://wiki.developers.facebook.com/index.php/Fb:swf" target="_blank">fb:swf</a></code> tag. Also, the Flash component does not start automatically, there needs to be a user interaction to trigger the embed of the SWF. It might be be of interest to mention that Facebook supports certain versions of Flash, namely 9 and 10. The exact version numbers can be found in the <a title="Flash Player versions supported on Facebook" href="http://wiki.developers.facebook.com/index.php/Fb:swf#Notes" target="_blank">notes of the <code>fb:swf</code> tag</a>.</p>
<p><strong>The index file</strong></p>
<p>Using PHP is totally optional in this case. Facebook does have a habit of caching files on their servers, therefore instead of having to hardcode all URLs, it is quite practical to use PHP variables. For images and CSS, I sometimes add the <code>time()</code> PHP function so I make sure that the file used is always the latest, like so:</p>
<p>PHP:</p>
<blockquote><p><code>$img_src = "http://www.example.com/images/image.jpg?" . time();</code></p></blockquote>
<p>The <code>time()</code> function is useful during development, but it is best not to use it once you deploy live.</p>
<p>We also need an id for the image element.</p>
<p>PHP:</p>
<blockquote><p><code>$img_id = "click_to_activate_id";</code></p></blockquote>
<p>Now, let's look at the JavaScript:</p>
<p>JavaScript:</p>
<blockquote>
<pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> embedSWF<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #009900; font-style: italic;">// remove image</span>
	<span style="color: #003366; font-weight: bold;">var</span> image = document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;click_to_activate_id&quot;</span><span style="color: #66cc66;">&#41;</span>;
	document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;swfWrapper&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span>image<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #009900; font-style: italic;">// create swf object</span>
	<span style="color: #003366; font-weight: bold;">var</span> swf = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;fb:swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
	swf.<span style="color: #006600;">setSWFSrc</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;communication_with_fb.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
	swf.<span style="color: #006600;">setId</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;communication_with_fb_id&quot;</span><span style="color: #66cc66;">&#41;</span>;
	swf.<span style="color: #006600;">setWidth</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;520&quot;</span><span style="color: #66cc66;">&#41;</span>;
	swf.<span style="color: #006600;">setHeight</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;600&quot;</span><span style="color: #66cc66;">&#41;</span>;
	document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;swfWrapper&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>swf<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#125;</span></pre>
</blockquote>
<p>In this block, I create a JS function that removes an image and generates an <code>fb:swf</code> tag in its stead.</p>
<p>PHP:</p>
<p><img class="alignnone" title="PHP Code" src="http://jansensan.net/images/blog/post0015_002.jpg" alt="&lt;div id=&quot;wrapper&quot;&gt; 	&lt;div id=&quot;swfWrapper&quot;&gt; 		&lt;img 			src=&quot;&lt;?php echo $img_src ?&gt;&quot; 			width=&quot;520&quot; height=&quot;600&quot; 			id=&quot;&lt;php echo $img_id ?&gt;&quot; 			class=&quot;hasPointer&quot; 			onclick=&quot;embedSWF()&quot; 		/&gt; 	&lt;/div&gt; &lt;/div&gt;" width="640" height="195" /></p>
<p>This block represents the image that the user will see first. Once the user clicks on the image, the JS function mentioned above is called and the SWF gets embedded.</p>
<p>It is important that the <code>fb:fbjs-bridge</code> tag is place before the wrapper that contains/will contain the SWF, otherwise it will not work.</p>
<p>CSS:</p>
<blockquote>
<pre class="css"><span style="color: #cc00cc;">#wrapper</span>
<span style="color: #66cc66;">&#123;</span>
	width<span style="color: #3333ff;">:<span style="color: #933;">520px</span></span>;
	<span style="color: #000000; font-weight: bold;">background-color</span>:<span style="color: #cc00cc;">#ffffff</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#swfWrapper</span>
<span style="color: #66cc66;">&#123;</span>
	width<span style="color: #3333ff;">:<span style="color: #933;">520px</span></span>;
	height<span style="color: #3333ff;">:<span style="color: #933;">600px</span></span>;
	margin-bottom<span style="color: #3333ff;">:<span style="color: #933;">16px</span></span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#buttonsWrapper</span>
<span style="color: #66cc66;">&#123;</span>
	width<span style="color: #3333ff;">:<span style="color: #933;">520px</span></span>;
	margin<span style="color: #3333ff;">:<span style="color: #933;">0</span></span> <span style="color: #993333;">auto</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.button</span>
<span style="color: #66cc66;">&#123;</span>
	cursor<span style="color: #3333ff;">:pointer</span>;
	background-image<span style="color: #3333ff;">:url</span><span style="color: #66cc66;">&#40;</span>../images/button<span style="color: #6666ff;">.jpg</span><span style="color: #66cc66;">&#41;</span>;
	width<span style="color: #3333ff;">:<span style="color: #933;">96px</span></span>;
	height<span style="color: #3333ff;">:<span style="color: #933;">16px</span></span>;
	margin<span style="color: #3333ff;">:<span style="color: #933;">0</span></span> <span style="color: #993333;">auto</span>;
	padding<span style="color: #3333ff;">:<span style="color: #933;">4px</span></span> <span style="color: #933;">0</span>;
	font-family<span style="color: #3333ff;">:Helvetica</span>, Arial, <span style="color: #993333;">sans-serif</span>;
	font-size<span style="color: #3333ff;">:<span style="color: #933;">16px</span></span>;
	text-align<span style="color: #3333ff;">:center</span>;
	<span style="color: #000000; font-weight: bold;">color</span>:<span style="color: #cc00cc;">#<span style="color: #933;">000</span></span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.hasPointer</span>
<span style="color: #66cc66;">&#123;</span>
	cursor<span style="color: #3333ff;">:pointer</span>;
<span style="color: #66cc66;">&#125;</span></pre>
</blockquote>
<p>This last block is just some CSS to hold the visuals together.</p>
<p><strong>Of ActionScript and FBML interaction</strong></p>
<p>Usually, when it is necessary to have AS3 interact with JS, one can simply use the <code><a title="AS3 Doc - ExternalInterface" href="http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html" target="_blank">ExternalInterface</a></code> class. Most probably for security reasons, Facebook makes our lives a bit more complicated when we need such an interaction once in their framework. As I said before, we have to use their FBJS Bridge. Let's see how.</p>
<p>As the SWF is embedded into Facebook, it receives a multitude of variables, which are listed in the <a title="FB Doc - fb:swf" href="http://wiki.developers.facebook.com/index.php/Fb:swf" target="_blank">older version of FB's doc</a>. There are two that are of interest to us here: "<code>fb_local_connection</code>" and "<code>fb_fbjs_connection</code>". These are the variable names needed to respectively create an outgoing connection and an incoming connection.</p>
<p>ActionScript 3.0:</p>
<blockquote>
<pre class="actionscript"><span style="color: #000000; font-weight: bold;">var</span> _outgoingConnection:<span style="color: #0066CC;">LocalConnection</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">LocalConnection</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> _outgoingConnectionName:<span style="color: #0066CC;">String</span> = loaderInfo.<span style="color: #006600;">parameters</span>.<span style="color: #006600;">fb_local_connection</span>;
<span style="color: #000000; font-weight: bold;">var</span> _incomingConnection:<span style="color: #0066CC;">LocalConnection</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">LocalConnection</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> _incomingConnectionName:<span style="color: #0066CC;">String</span> = loaderInfo.<span style="color: #006600;">parameters</span>.<span style="color: #006600;">fb_fbjs_connection</span>;</pre>
</blockquote>
<p>In the case of the outgoing connection, we use the name when calling a JS function, like so:</p>
<p>ActionScript 3.0:</p>
<blockquote>
<pre class="actionscript">_outgoingConnection.<span style="color: #0066CC;">send</span><span style="color: #66cc66;">&#40;</span>_outgoingConnectionName, <span style="color: #ff0000;">&quot;callFBJS&quot;</span>, <span style="color: #ff0000;">&quot;fbjsFunctionForAS3&quot;</span>, <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;Message from AS3&quot;</span>, <span style="color: #ff0000;">&quot;Hello Facebook!&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</pre>
</blockquote>
<p>Note that in this function, the second arguments must always be "callFBJS", as Facebook uses a <code><a title="AS Doc - LocalConnection" href="http://192.150.8.60/livedocs/flash/9.0/ActionScriptLangRefV3/flash/net/LocalConnection.html" target="_blank">LocalConnection</a></code> in an SWF of their own to communicate with JS in their framework.</p>
<p>The third argument is the name of the JS function you need to be called from Flash, and the last is an array of the arguments needed.</p>
<p>JavaScript:</p>
<blockquote>
<pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> fbjsFunctionForAS3<span style="color: #66cc66;">&#40;</span>title, message<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">new</span> Dialog<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">showMessage</span><span style="color: #66cc66;">&#40;</span>title, message<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
</blockquote>
<p>Another idiosyncrasy of Facebook is that the usual <code>alert()</code> JS function is not available. It may be frustrating at first, but their framework provide a solution that uses their own UI, which can be more interesting. As seen above, you can simply create a new Dialog() and right away call the showMessage() function, a blue Facebook window will pop over your text content. HTML is unfortunately not permitted there.</p>
<p>Now, you may also want to call AS3 functions from JS. Let's take a look at how this would work.</p>
<p>ActionScript 3.0:</p>
<blockquote>
<pre class="actionscript">_incomingConnection.<span style="color: #006600;">client</span> = <span style="color: #0066CC;">this</span>;
_incomingConnection.<span style="color: #0066CC;">connect</span><span style="color: #66cc66;">&#40;</span>_incomingConnectionName<span style="color: #66cc66;">&#41;</span>;</pre>
</blockquote>
<p>The first thing to do is to set a client to the connection, meaning what is the scope of the functions that the FBJS Bridge will call. I like to keep it at the same level as the class. Then, connect the connection.</p>
<p>You simply need to make a function available to be called.</p>
<p>ActionScript 3.0:</p>
<blockquote>
<pre class="actionscript"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> as3FunctionForFBJS<span style="color: #66cc66;">&#40;</span>title:<span style="color: #0066CC;">String</span>, <span style="color: #0066CC;">message</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>title, <span style="color: #0066CC;">message</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
</blockquote>
<p>And let's see how to call it from the Javascript side:</p>
<p>JavaScript:</p>
<blockquote>
<pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> fbjsFunctionForAS3<span style="color: #66cc66;">&#40;</span>title, message<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> swf document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;click_to_activate_id&quot;</span><span style="color: #66cc66;">&#41;</span>;
	swf.<span style="color: #006600;">callSWF</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;as3FunctionForFBJS&quot;</span>, <span style="color: #3366CC;">&quot;Message from Facebook&quot;</span>, <span style="color: #3366CC;">&quot;Hello AS3!&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
</blockquote>
<p>Obviously at this point you need UI elements to trigger these functions, but I leave that for you to figure out and create.</p>
<p><strong>Setting up the Facebook application</strong></p>
<p>First, you should head to Adobe's website where some tutorials present you with the basics of what you need to know (<a title="Getting your head around Flash on Facebook" href="http://www.adobe.com/devnet/facebook/articles/flash_on_facebook.html" target="_blank">link 1</a>, <a title="Build your first Facebook application" href="http://www.adobe.com/devnet/facebook/articles/video_facebook_quick_start.html" target="_blank">link 2</a>, <a title="Create your first Flex and Facebook web application – Part 1: Build and test locally" href="http://www.adobe.com/devnet/facebook/articles/first_flex_facebook_web_app_pt1.html" target="_blank">link 3</a>).</p>
<p>Once you created your app and all, make sure that your "Render Method" is set to "FBML" in the app settings:</p>
<p><strong><em><img class="aligncenter" title="Facebook App - Set Render Method to FBML" src="http://jansensan.net/images/blog/post0015_003.jpg" alt="" width="640" height="460" /><br />
</em></strong></p>
<p>There you go! You are now ready to have your Facebook projects communicate with AS3 and vice versa.</p>
<p><strong>Resources</strong></p>
<p>You may get the sources for this tutorial <a title="Tutorial sources" href="http://code.google.com/p/tutorial-communication-between-as3-and-facebook/" target="_blank">here</a>.</p>
<p>You may test what I have presented in this tutorial: <a title="Facebook App - Communication between AS3 and Facebook" href="http://apps.facebook.com/flash-to-fb/" target="_blank">http://apps.facebook.com/flash-to-fb/</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jansensan.net/communication-between-as3-and-facebook/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Communication between AS3 and Max/MSP/Jitter</title>
		<link>http://jansensan.net/communication-between-as3-and-max-msp-jitter</link>
		<comments>http://jansensan.net/communication-between-as3-and-max-msp-jitter#comments</comments>
		<pubDate>Thu, 27 May 2010 15:37:08 +0000</pubDate>
		<dc:creator>mat janson blanchet</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Technologies]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Max/MSP/Jitter]]></category>

		<guid isPermaLink="false">http://jansensan.net/?p=298</guid>
		<description><![CDATA[A while back, I thought of adding a touch screen film onto my laptop's screen, so I could use Max/MSP/Jitter's incredible sound processing tools and interface more easily than keyboard and mouse in the context of an installation or a performance. It turns out that a couple of years later, screens are touchable more easily, [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone" title="Communication between AS3 and Max/MSP/Jitter" src="http://jansensan.net/images/blog/post0014_001.jpg" alt="Communication between AS3 and Max/MSP/Jitter" width="640" height="128" /></p>
<p>A while back, I thought of adding a touch screen film onto my laptop's screen, so I could use <a title="Max" href="http://cycling74.com/products/maxmspjitter/" target="_blank">Max/MSP/Jitter</a>'s incredible sound processing tools and interface more easily than keyboard and mouse in the context of an installation or a performance. It turns out that a couple of years later, screens are touchable more easily, although I have yet to find a tablet that will be interesting enough for me to buy and want to use with Max.</p>
<p>Nevertheless, I had fiddled around with AS3 to communicate with Max. I had planned for a while to write a post about this, but I thought it was more important to present how to start and manage a project. What I will present here is how to communicate data between the two software, and since Max is a desktop installation, I will create a pure AS3 <a title="Adobe Air" href="http://www.adobe.com/products/air/" target="_blank">Air</a> project.</p>
<p>Just to give credit where credit is due, steps to create the Air project are mostly taken from <a title="Easily Create a Pure AS3 AIR Project in Flash Builder 4" href="http://swfhead.com/blog/?p=380" target="_blank">here</a>. Let's have at it now!</p>
<p><strong>Create an Air Project</strong></p>
<p style="text-align: center;"><img class="aligncenter" title="Create an Air Project (step 1)" src="http://jansensan.net/images/blog/post0014_002.jpg" alt="Create an Air Project (step 1)" width="640" height="709" /></p>
<p>Let's first create a Flex project in Flash Builder 4. Right-click in the package explorer window, select "New &gt; Flex Project". Make sure that you check the "Desktop (runs in Adobe Air)" option in the "Application type" subsection, you do not want to make a web project.</p>
<p style="text-align: center;"><img class="aligncenter" title="Create an Air Project (step 2)" src="http://jansensan.net/images/blog/post0014_003.jpg" alt="Create an Air Project (step 2)" width="640" height="818" /></p>
<p>In the next window, leave the "Output folder" field as is and click "Next". Then, in the "Main application" field, change the .MXML extension to .AS and click "Finish."</p>
<p>As a habit, I always have a initStage() function. In Air, you need to spawn an actual window, something we never do for the web. This is the perfect place to do so.</p>
<p>ActionScript 3.0:</p>
<blockquote>
<pre class="actionscript"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> initStage<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: #808080; font-style: italic;">// first, spawn a window</span>
	<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">nativeWindow</span>.<span style="color: #006600;">activate</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// then set stage params</span>
	<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span> = StageAlign.<span style="color: #006600;">TOP_LEFT</span>;
	<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span> = StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;
<span style="color: #66cc66;">&#125;</span></pre>
</blockquote>
<p>In order to communicate with Max, we need to create an <a title="XMLSocket - AS3 Documentation" href="http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/net/XMLSocket.html" target="_blank">XMLSocket</a>.</p>
<p>ActionScript 3.0:</p>
<blockquote>
<pre class="actionscript"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _socket:<span style="color: #0066CC;">XMLSocket</span>;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _host:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;127.0.0.1&quot;</span>;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _port:uint = <span style="color: #cc66cc;">8080</span>;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> initSocket<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	_socket = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">XMLSocket</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	_socket.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>IOErrorEvent.<span style="color: #006600;">IO_ERROR</span>, xmlSocketEventsHandler<span style="color: #66cc66;">&#41;</span>;
	_socket.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #0066CC;">CONNECT</span>, xmlSocketEventsHandler<span style="color: #66cc66;">&#41;</span>;
	_socket.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #0066CC;">CLOSE</span>, xmlSocketEventsHandler<span style="color: #66cc66;">&#41;</span>;
	_socket.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>DataEvent.<span style="color: #0066CC;">DATA</span>, xmlSocketDataHandler<span style="color: #66cc66;">&#41;</span>;
	_socket.<span style="color: #0066CC;">connect</span><span style="color: #66cc66;">&#40;</span>_host, _port<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> xmlSocketEventsHandler<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, <span style="color: #0066CC;">this</span>, <span style="color: #ff0000;">&quot;--- xmlSocketEventsHandler ---&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span>event.<span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;ioError&quot;</span>:
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>could not connect&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">break</span>;
&nbsp;
		<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;connect&quot;</span>:
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>socket connected&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">break</span>;
&nbsp;
		<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;close&quot;</span>:
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>socket closed&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">break</span>;
&nbsp;
		<span style="color: #000000; font-weight: bold;">default</span>:
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span> + event.<span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">break</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
</blockquote>
<p>There are a couple of things to go through here. At the top of this code block, I instantiate the XMLSocket. I also create variables for the host and the port, which are both necessary to connect the XMLSocket.</p>
<p>Then, the initSocket() function takes care of initializing the XMLSocket, also adding some listeners to it and finally connecting it. For the connection, you may want to read about your platform (whether it's Windows, Mac OS or other) to see what is the host IP and what ports can be used locally. You may also read up a bit on the Flash Player security, in case you need a cross domain XML file, although since this project is desktop-based and not going through a browser, I did not need it.</p>
<p>The xmlSocketEventsHandler() simply traces what events the XMLSocket receives.</p>
<p><strong>Create a Max patch</strong></p>
<p>Max files are called patches. It goes back to patching cords, but I won't go too deep into the Max language, you may read all about it yourself on the Internet.</p>
<p>By default, Max does not communicate with Flash, but just like AS3, there are some "libraries" that can be added to Max, they are called externals. A couple of years back, the Flashserver external was easy to obtain from the <a title="Flashserver - Olaf Matthes" href="http://www.nullmedium.de/dev/flashserver/" target="_blank">author's website</a> or from the <a title="Flashserver - Max Object Database" href="http://www.maxobjects.com/?v=objects&amp;id_objet=2487&amp;requested=FLASH&amp;operateur=AND&amp;id_plateforme=0&amp;id_format=0" target="_blank">Max Objects Database</a>, but it is sadly not the case anymore. I still have it and it is bundled with the source files for this tutorial, I hope I am not infringing anyone's copyrights by making it available again.</p>
<p>Then let's create a new patch and add the Flashserver object. This post is unfortunately not about how to use Max, thus I will not explain how to use the software here. I may in some future post, but this is way out of scope for this post.</p>
<p style="text-align: center;"><img class="aligncenter" title="Max patch" src="http://jansensan.net/images/blog/post0014_004.jpg" alt="Max patch" width="451" height="578" /></p>
<p>Simply look at the help for the Flashserver object if you need to, but quickly, after the name is the argument for what port to connect to ("8080") , and the last argument ("256") is the amount of clients or Flash applications that can connect to this Max patch.</p>
<p><strong>Talking with Max</strong></p>
<p>When Flash needs to communicate with other languages and software, the other software usually has the role of a server, therefore it needs to be opened before the Flash/Air app. Thus, make sure you open the Max patch before attempting to communicate with Flash.</p>
<p>First, let's look at receiving what Max may send to Flash. Earlier, there was one event handler that I did not define, here it is:</p>
<p>ActionScript 3.0:</p>
<blockquote>
<pre class="actionscript"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> xmlSocketDataHandler<span style="color: #66cc66;">&#40;</span>event:DataEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>event.<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
</blockquote>
<p><strong><span style="font-weight: normal;">So the data is actually what Max sends to Flash. To send multiple values to Flash, you'll need to create a pack in Max and send it as a string and then parse it in Flash.</span></strong></p>
<p><strong><span style="font-weight: normal;">Let's look at the code to send data to Max:</span></strong></p>
<p>ActionScript 3.0:</p>
<blockquote>
<pre class="actionscript">_socket.<span style="color: #0066CC;">send</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;hello max;&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre>
</blockquote>
<p><strong><span style="font-weight: normal;">It's as simple as that! Note that the data always has to end with a semicolon (";"). In the case where there are multiple values that need to be sent, separate them all with a semicolon, like so:</span></strong></p>
<p>ActionScript 3.0:</p>
<blockquote>
<pre class="actionscript">_socket.<span style="color: #0066CC;">send</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;hello max; from flash;&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre>
</blockquote>
<p>On Max's side, simply use the unpack object to be able to parse the values into your patch.</p>
<p><strong>Source files</strong></p>
<p>As promised, <a href="http://jansensan.net/compressed/Tutorial_CommunicationWithMax.zip">here are the files</a> that I used to create this project. The Air project was created in Eclipse with the Flash Builder 4 plugin, and the Max patch with Max 5, with the Flashserver external installed.</p>
]]></content:encoded>
			<wfw:commentRss>http://jansensan.net/communication-between-as3-and-max-msp-jitter/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

