<?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>MochiLand &#187; Tutorials</title>
	<atom:link href="http://mochiland.com/category/mochiland/tutorials/feed" rel="self" type="application/rss+xml" />
	<link>http://mochiland.com</link>
	<description>for(developer in flash_games){ this.mochiland = _root }</description>
	<lastBuildDate>Fri, 18 May 2012 19:04:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Tutorial: Custom Leaderboards With Mochi API (AS3)</title>
		<link>http://mochiland.com/articles/tutorial-custom-leaderboards-with-mochi-api-as3</link>
		<comments>http://mochiland.com/articles/tutorial-custom-leaderboards-with-mochi-api-as3#comments</comments>
		<pubDate>Thu, 29 Mar 2012 17:44:58 +0000</pubDate>
		<dc:creator>uNik</dc:creator>
				<category><![CDATA[Developer Feed]]></category>
		<category><![CDATA[Flash Development]]></category>
		<category><![CDATA[Front Page]]></category>
		<category><![CDATA[MochiLand Topics]]></category>
		<category><![CDATA[Random]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[creation]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[leaderboard]]></category>
		<category><![CDATA[Mochi Scores]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://mochiland.com/?p=6242</guid>
		<description><![CDATA[Hi everyone, I haven&#8217;t found any example how to create a custom leaderboard, so I&#8217;ve decided to create one. I&#8217;m sure that it can be useful for you :) To follow this tutorial, you should be familiar with the information in the Mochi Media docs. With just a little bit of time, we will create [...]]]></description>
			<content:encoded><![CDATA[ <p>Hi everyone,<br />
I haven&#8217;t found any example how to create a custom leaderboard, so I&#8217;ve decided to create one. I&#8217;m sure that it can be useful for you :)<br />
To follow this tutorial, you should be familiar with the information in the <a href="https://www.mochimedia.com/support/dev_docs#The_MochiScores_API">Mochi Media docs</a>.</p>
<p>With just a little bit of time, we will create something like this:</p>
<p><a href="http://mochiland.com/wp-content/uploads/2012/03/pic-1.jpg"><img class="alignleft size-full wp-image-6252" title="Custome Leaderboard" style="border: 0px" src="http://mochiland.com/wp-content/uploads/2012/03/pic-1.jpg" alt="" width="640" height="480" /></a></p>
<p>Let&#8217;s go!</p>
<p><strong>We need 6 buttons:</strong> 2 function buttons (Load &#8211; to load our leaderboard, Send &#8211; to send the score)and 4 buttons to filter the score: daily/weekly/monthly/all</p>
<p>We also need 3 columns (Text fields) to output name/score/date of players.</p>
<h2>Main code:</h2>
<ol>
<li>initialization function

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">initBoardfunction 
initBoard<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
	MochiServices<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">connect</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;MochiGameID&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">root</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">// connecting to MochiServices</span>
	MochiScores<span style="color: #000066; font-weight: bold;">.</span>setBoardID<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;LeaderboardID&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// set the board ID to the leaderboard you wish to work</span>
withbtnDaily<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span> showLeaderboard_d<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
btnWeekly<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span> showLeaderboard_w<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
btnMonthly<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span> showLeaderboard_m<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
btnAll<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span> showLeaderboard_a<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> 
&nbsp;
btnLoad<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span> loadLeaderboard<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
btnSend<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span> sendScore<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><span style="color: #000000;">&#125;</span></pre></div></div>

</li>
<li>request is a list of score: onScoresReceived &#8211; callback function that gets the scoresfunction

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">loadLeaderboard<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
&nbsp;
<span style="color: #000000;">&#123;</span>
MochiScores<span style="color: #000066; font-weight: bold;">.</span>requestList<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">,</span> onScoresReceived<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">// request the scores and send them to the onScoresReceived method in this object or class}</span>
&nbsp;
<span style="color: #009900; font-style: italic;">//description onScoresReceived functionfunction onScoresReceived (args:Object):void{</span>
<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>args<span style="color: #000066; font-weight: bold;">.</span>scores <span style="color: #000066; font-weight: bold;">!</span>= <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
allScores = MochiScores<span style="color: #000066; font-weight: bold;">.</span>scoresArrayToObjects<span style="color: #000000;">&#40;</span>args<span style="color: #000066; font-weight: bold;">.</span>scores<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
showScore<span style="color: #000000;">&#40;</span>allScores<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">15</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #3f5fbf;">/*
* showScore(scores:Object, typeTop:int = 0, countPlaces:int = 10):void
* @scores - array of objects whose keys are derived from cols
* @typeTop - values:
* 		0 : alltime score
* 		1 : monthly score
* 		2 : weekly score
* 		3 : daily score
* @countPlaces - number of scores. show top 15 players for example
*/</span>
<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>args<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">error</span><span style="color: #000000;">&#41;</span> <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Error: &quot;</span> <span style="color: #000066; font-weight: bold;">+</span> args<span style="color: #000066; font-weight: bold;">.</span>errorCode<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>	<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#125;</span></pre></div></div>

</li>
<li>implementation of the showScore function:

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> showScore<span style="color: #000000;">&#40;</span>scores<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000066; font-weight: bold;">,</span> typeTop<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> countPlaces<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> scoresOb<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> place<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
txt_name<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span>=<span style="color: #990000;">&quot;&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
txt_score<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span>=<span style="color: #990000;">&quot;&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
txt_date<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span>=<span style="color: #990000;">&quot;&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&amp;</span>lt<span style="color: #000066; font-weight: bold;">;</span> countPlaces<span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
<span style="color: #0033ff; font-weight: bold;">switch</span><span style="color: #000000;">&#40;</span>typeTop<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">:</span> scoresOb = scores<span style="color: #000066; font-weight: bold;">.</span>alltime<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">:</span> scoresOb = scores<span style="color: #000066; font-weight: bold;">.</span>monthly<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">:</span> scoresOb = scores<span style="color: #000066; font-weight: bold;">.</span>weekly<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">:</span> scoresOb = scores<span style="color: #000066; font-weight: bold;">.</span>daily<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>scoresOb<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
txt_name<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">appendText</span><span style="color: #000000;">&#40;</span>place<span style="color: #000066; font-weight: bold;">++</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #990000;">&quot; &quot;</span> <span style="color: #000066; font-weight: bold;">+</span> scoresOb<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">name</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #990000;">'<span style="">\n</span>'</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
txt_score<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">appendText</span><span style="color: #000000;">&#40;</span>scoresOb<span style="color: #000066; font-weight: bold;">.</span>score <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #990000;">'<span style="">\n</span>'</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
txt_date<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">appendText</span><span style="color: #000000;">&#40;</span>showDate<span style="color: #000000;">&#40;</span>scoresOb<span style="color: #000066; font-weight: bold;">.</span>timestamp<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #990000;">'<span style="">\n</span>'</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</li>
<li>function to display the correct date, for users :)

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> showDate<span style="color: #000000;">&#40;</span><span style="color: #004993;">date</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> myDate<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Date</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">date</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> arMonths<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;January&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;February&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;March&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;April&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;May&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;June&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;July&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;August&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;September&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;October&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;November&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;December&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000;">&#40;</span>arMonths<span style="color: #000000;">&#91;</span>myDate<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">getMonth</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #990000;">&quot; &quot;</span> <span style="color: #000066; font-weight: bold;">+</span> myDate<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">getDate</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #990000;">&quot;, &quot;</span> <span style="color: #000066; font-weight: bold;">+</span> myDate<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">getFullYear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</li>
<li>to send the score

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> sendScore<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> score<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">300</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">name</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;Typenamehere&quot;</span>
MochiScores<span style="color: #000066; font-weight: bold;">.</span>submit<span style="color: #000000;">&#40;</span>score<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">name</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">,</span> SubmitComplete<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #339966; font-weight: bold;">function</span> SubmitComplete<span style="color: #000000;">&#40;</span>ok<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>ok<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Scores submitted!&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span><span style="color: #0033ff; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Error&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</li>
</ol>
<p>That&#8217;s it. Here, I&#8217;ve described how to create a simple leaderboard. With this is simple leaderboard we can also add the current rank of the player, country flags, and a scroll bar&#8230;but I&#8217;ll save that for another tutorial :)</p>
<p><a href="http://nsbrotherhood.com/download/LeaderBoard.rar"> Download the source here</a></p>
 <div class='series_toc'></div>	<p></p>
	<hr noshade style="margin:0;height:1px" />
	<p>&copy; uNik for <a href="http://mochiland.com">MochiLand</a>, 2012. |
	  <a href="http://mochiland.com/articles/tutorial-custom-leaderboards-with-mochi-api-as3">Permalink</a> |
	  <a href="http://mochiland.com/articles/tutorial-custom-leaderboards-with-mochi-api-as3#comments">One comment</a></p>
	<p>Add to <a href="http://del.icio.us/post?url=http://mochiland.com/articles/tutorial-custom-leaderboards-with-mochi-api-as3&amp;title=Tutorial: Custom Leaderboards With Mochi API (AS3)">del.icio.us</a></p>
	<p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mochiland.com/articles/tutorial-custom-leaderboards-with-mochi-api-as3" title="Search on Technorati">Technorati</a></p>
	<p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mochiland.com/category/mochiland/developer-feed" title="View all posts in Developer Feed" rel="category tag">Developer Feed</a>, <a href="http://mochiland.com/category/mochiland/flash" title="View all posts in Flash Development" rel="category tag">Flash Development</a>, <a href="http://mochiland.com/category/mochiland/front-page" title="View all posts in Front Page" rel="category tag">Front Page</a>, <a href="http://mochiland.com/category/mochiland" title="View all posts in MochiLand Topics" rel="category tag">MochiLand Topics</a>, <a href="http://mochiland.com/category/mochiland/random" title="View all posts in Random" rel="category tag">Random</a>, <a href="http://mochiland.com/category/mochiland/tutorials" title="View all posts in Tutorials" rel="category tag">Tutorials</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://mochiland.com/articles/tutorial-custom-leaderboards-with-mochi-api-as3/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Adding Mochi Ads &amp; Scores to a Stencyl Game</title>
		<link>http://mochiland.com/articles/adding-mochi-ads-scores-to-a-stencyl-game</link>
		<comments>http://mochiland.com/articles/adding-mochi-ads-scores-to-a-stencyl-game#comments</comments>
		<pubDate>Mon, 28 Nov 2011 16:00:20 +0000</pubDate>
		<dc:creator>JonathanChung</dc:creator>
				<category><![CDATA[Developer Feed]]></category>
		<category><![CDATA[Flash Development]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[MochiLand Topics]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[integration]]></category>
		<category><![CDATA[Mochi Media]]></category>
		<category><![CDATA[Stencyl]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://mochiland.com/?p=5755</guid>
		<description><![CDATA[Mochi Media is one of the most popular ways for Flash game developers to make money with their games using  their in-game ads product Mochi Ads. Mochi is also a one-stop solution for game analytics, achievements, leaderboards, and virtual currency. StencylWorks is an easy way to visually create Flash and iOS games for non-coders and [...]]]></description>
			<content:encoded><![CDATA[ <p>Mochi Media is one of the most popular ways for Flash game developers to make money with their games using  their in-game ads product <strong>Mochi Ads</strong>. Mochi is also a one-stop solution for game analytics, achievements, leaderboards, and virtual currency.</p>
<div class="entry-content">
<p><strong>StencylWorks</strong> is an easy way to visually create Flash and iOS games for non-coders and programmers alike. Starting with StencylWorks 1.2, developers can add Mochi Ads and scores to their games in just a few minutes!</p>
<h3 style="text-align: center;"><strong><a href="http://static.stencyl.com/content/Balloons.zip">Download the Sample Game</a></strong>&nbsp;</p>
<p><em>(Unzip and place “Balloons” folder under the StencylWorks “games” directory.)</em></h3>
<p>This balloon-popping game records how long it took for you to beat the (only) level. We’ll use Mochi to record the score on a leaderboard and show an ad after clicking the reset button.</p>
<h3>Sign up on Mochi’s Site</h3>
<p><a href="https://www.mochimedia.com/register/">Sign up on Mochi’s site</a> if you haven’t done so already.</p>
<h3>Add a Game on Mochi’s Site</h3>
<p>Each game you wish to support you must add to Mochi’s site.</p>
<p>1) Click the “Add Game” tab</p>
<p><a href="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-28-at-10.31.30-PM.png"><img class="alignnone size-full wp-image-382" style="border: 0px;" src="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-28-at-10.31.30-PM.png" alt="" width="103" height="46" /></a></p>
<p>2) Fill in the page as shown below and submit.</p>
<p><a href="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-28-at-10.32.58-PM.png"><img class="alignnone size-full wp-image-383" style="border: 0px;" src="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-28-at-10.32.58-PM.png" alt="" width="230" height="245" /></a></p>
<p>Stencyl games don’t function with Live Updates at this time.</p>
<p>&nbsp;</p>
<h3>Locate the game’s Mochi Game ID</h3>
<p>Every game has a unique game ID, so Mochi can identify it and store all of its data in one place. You can find the Game ID box on the game’s page.</p>
<p><a href="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-28-at-10.35.02-PM.png"><img class="alignnone size-full wp-image-384" style="border: 0px;" src="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-28-at-10.35.02-PM.png" alt="" width="165" height="94" /></a></p>
<p>To reach a game’s page, visit the Dashboard and click on the game’s name.</p>
<p>&nbsp;</p>
<h3>Enter the Mochi Game ID in to your game</h3>
<p>Now that you’ve set your game up on Mochi’s site, you need to enter in the game’s ID so it can establish a connection with Mochi’s servers to present ads and scores.</p>
<p>1) Open your game in StencylWorks.</p>
<p>2) Click on Advanced Settings under the Game Center tab. Enter the game’s Mochi Game ID.</p>
<p><a href="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-28-at-10.38.16-PM.png"><img class="alignnone size-medium wp-image-385" style="border: 0px;" src="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-28-at-10.38.16-PM-300x211.png" alt="" width="300" height="211" /></a></p>
<p>3) Save your game.</p>
<p>&nbsp;</p>
<h3>How to Display a Mochi Ad</h3>
<p>Displaying an ad is simple. Add the “show Mochi ad” block precisely at the time you wish to show the ad.</p>
<p><a href="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-8.40.19-PM.png"><img class="aligncenter size-full wp-image-402" style="border: 0px;" src="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-8.40.19-PM.png" alt="" width="262" height="36" /></a></p>
<p>For our Balloon game, we want to <strong>show an ad after clicking the reset button</strong>.</p>
<p>1) Open up the “MochiAd” behavior under Game Center &gt; Logic &gt; Scene Behaviors</p>
<p><a href="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-8.45.18-PM.png"><img class="aligncenter size-medium wp-image-405" style="border: 0px;" src="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-8.45.18-PM-300x109.png" alt="" width="300" height="109" /></a></p>
<p>This behavior instructs the game to display the ad right away. Our approach in this game is to set up a separate scene exclusively for displaying the ad, so that the ad doesn’t disrupt regular gameplay.</p>
<p><a href="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-8.38.02-PM.png"><img class="aligncenter size-medium wp-image-401" style="border: 0px;" src="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-8.38.02-PM-300x70.png" alt="" width="300" height="70" /></a></p>
<p>2) To make this behavior work, we need to attach it to a scene. Click “Attach to Scene” and choose “Ad Scene.”</p>
<p><a href="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-8.37.18-PM.png"><img class="aligncenter size-medium wp-image-400" style="border: 0px;" src="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-8.37.18-PM-300x219.png" alt="" width="300" height="219" /></a></p>
<p>3) Save and run the game. Notice the ad now appears after you click the Reset Button.</p>
<p><a href="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-8.36.02-PM.png"><img class="aligncenter size-medium wp-image-399" style="border: 0px;" src="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-8.36.02-PM-300x188.png" alt="" width="300" height="188" /></a></p>
<p>(Get an error? Jump to the bottom. Ad is a little slow in showing up? That’s normal when testing offline.)</p>
<p>&nbsp;</p>
<h3>How to Submit a Score and Display a Leaderboard</h3>
<p>Displaying a leaderboard requires setting up the leaderboard first on Mochi’s site.</p>
<p>1) Visit the Scores page for your game by clicking “Scores” in the sidebar.</p>
<p>2) Click “Add Leaderboard” – it’s a small button on the right-hand side of the page.</p>
<p><a href="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-28-at-10.45.39-PM.png"><img class="alignnone size-full wp-image-386" style="border: 0px;" src="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-28-at-10.45.39-PM.png" alt="" width="120" height="29" /></a></p>
<p>3) Fill in an appropriate title. Leave the rest of the fields as-is and submit.</p>
<p>4) Click on the “actionscript code” link on the next page.</p>
<p>5) Look for “Leaderboard Id” and copy it to your clipboard – this is what you’ll enter in to StencylWorks.</p>
<p>6) Now, open up the “MochiScores” behavior under Game Center &gt; Logic &gt; Scene Behaviors.</p>
<p><a href="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-8.46.44-PM.png"><img class="aligncenter size-medium wp-image-406" style="border: 0px;" src="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-8.46.44-PM-300x85.png" alt="" width="300" height="85" /></a></p>
<p>7) Locate the “submit score” block that’s already placed. Provide the Leaderboard Id you found in Step 5. For this game, we are recording how many seconds it took for you to pop all 40 balloons.</p>
<p><a href="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-8.41.48-PM.png"><img class="aligncenter size-full wp-image-403" style="border: 0px;" src="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-8.41.48-PM.png" alt="" width="484" height="75" /></a></p>
<p>8) To make this behavior work, we need to attach it to a scene. Click “Attach to Scene” and choose “First”</p>
<p><a href="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-8.51.47-PM.png"><img class="aligncenter size-medium wp-image-407" style="border: 0px;" src="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-8.51.47-PM-300x219.png" alt="" width="300" height="219" /></a></p>
<p>9) Save and run the game. The game will now show the leaderboard after finishing the level.</p>
<p><a href="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-8.43.23-PM.png"><img class="aligncenter size-medium wp-image-404" style="border: 0px;" src="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-8.43.23-PM-300x188.png" alt="" width="300" height="188" /></a></p>
<p>(Get an error? Jump to the bottom.)</p>
<h3>&nbsp;</p>
<p>What’s Next?</h3>
<p>Now that you’ve learned how to add scores and ads to your games, what’s next?</p>
<p><strong>Get Your Game Approved by Mochi</strong></p>
<p>You won’t start making money off those ads until your game’s been approved by Mochi. To get approval, you need to test game locally to present an ad and fill out your game profile on Mochi’s site. Mochi approves most games within 24 hours.</p>
<p><strong>Let Mochi Distribute Your Game</strong></p>
<p>Mochi not only maintains an ad network and APIs for its games, it also maintains a distribution network for Flash games, so your game can reach more players. It does this by offering feeds and embeddable “arcade” widgets that website owners can embed in their sites to present games relevant to their site.</p>
<p>To participate in this program, visit your game through the Dashboard and locate this box.</p>
<p style="text-align: center;"><a href="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-9.06.07-AM.png"><img class="size-full wp-image-395 aligncenter" style="border: 0px;" src="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-9.06.07-AM.png" alt="" width="394" height="107" /></a></p>
<h3>Troubleshooting: What to do if ads and scores do not show up, or if you receive an error.</h3>
<p>If you are testing locally, and want to be able to see ads or scores, you may need to do the following.</p>
<p>Add the location of your development files to your global security settings in the <a href="http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html" target="_blank">Adobe Flash Player Settings Manager</a>. This way, you can give all SWF files in this location <em>local-trusted</em> access, allowing access to both local and remote data.</p>
<p>The folder you want to add is the entire StencylWorks folder. This will catch everything underneath it, including the game itself. If you’ve chosen a custom workspace, you also need to add the folder for that.</p>
<p style="text-align: center;"><a href="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-9.21.22-AM.png"><img class="aligncenter size-full wp-image-396" style="border: 0px;" src="http://blog.stencyl.com/wp-content/uploads/2011/10/Screen-shot-2011-10-31-at-9.21.22-AM.png" alt="" width="405" height="279" /></a></p>
<p>&nbsp;</p>
<h2 style="text-align: center;"><a href="http://community.stencyl.com/index.php/topic,4308.0.html"></a><strong><a href="http://community.stencyl.com/index.php/topic,4308.0.html">Discuss this Article on our Forums!</a></strong></h2>
</div>
 <div class='series_toc'></div>	<p></p>
	<hr noshade style="margin:0;height:1px" />
	<p>&copy; JonathanChung for <a href="http://mochiland.com">MochiLand</a>, 2011. |
	  <a href="http://mochiland.com/articles/adding-mochi-ads-scores-to-a-stencyl-game">Permalink</a> |
	  <a href="http://mochiland.com/articles/adding-mochi-ads-scores-to-a-stencyl-game#comments">No comment</a></p>
	<p>Add to <a href="http://del.icio.us/post?url=http://mochiland.com/articles/adding-mochi-ads-scores-to-a-stencyl-game&amp;title=Adding Mochi Ads &amp; Scores to a Stencyl Game">del.icio.us</a></p>
	<p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mochiland.com/articles/adding-mochi-ads-scores-to-a-stencyl-game" title="Search on Technorati">Technorati</a></p>
	<p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mochiland.com/category/mochiland/developer-feed" title="View all posts in Developer Feed" rel="category tag">Developer Feed</a>, <a href="http://mochiland.com/category/mochiland/flash" title="View all posts in Flash Development" rel="category tag">Flash Development</a>, <a href="http://mochiland.com/category/mochiland/game-design" title="View all posts in Game Design" rel="category tag">Game Design</a>, <a href="http://mochiland.com/category/mochiland" title="View all posts in MochiLand Topics" rel="category tag">MochiLand Topics</a>, <a href="http://mochiland.com/category/mochiland/tutorials" title="View all posts in Tutorials" rel="category tag">Tutorials</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://mochiland.com/articles/adding-mochi-ads-scores-to-a-stencyl-game/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building A Retro Platform Game In Flixel, Pt. 1</title>
		<link>http://mochiland.com/articles/building-a-retro-platform-game-in-flixel-pt-1</link>
		<comments>http://mochiland.com/articles/building-a-retro-platform-game-in-flixel-pt-1#comments</comments>
		<pubDate>Thu, 03 Nov 2011 20:26:24 +0000</pubDate>
		<dc:creator>Photon Storm</dc:creator>
				<category><![CDATA[Developer Feed]]></category>
		<category><![CDATA[Developer Spotlight]]></category>
		<category><![CDATA[Flash Development]]></category>
		<category><![CDATA[Front Page]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[MochiLand Topics]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Flixel]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[Mochi Media]]></category>
		<category><![CDATA[Photon Storm]]></category>

		<guid isPermaLink="false">http://mochiland.com/?p=5618</guid>
		<description><![CDATA[Building a retro platform game in Flixel, Part 1 Note: This tutorial was originally written for .net magazine. I&#8217;m now allowed to publish it here, so read on and enjoy! Grab the files for download before starting! Retro style games are becoming more popular than ever online. Commonly referred to as &#8220;8-bit&#8221; these games have [...]]]></description>
			<content:encoded><![CDATA[ <p><strong>Building a retro platform game in Flixel, Part 1</strong></p>
<p>Note: This tutorial was <a title="My .net magazine " href="http://www.photonstorm.com/archives/1436/my-net-magazine-build-a-retro-game-in-flixel-article-is-now-out">originally written</a> for .net magazine. I&#8217;m now allowed to publish it here, so read on and enjoy! Grab the files for <a href="http://www.photonstorm.com/archives/2247/flash-game-dev-tip-12-building-a-retro-platform-game-in-flixel-part-1">download</a> before starting!</p>
<p>Retro style games are becoming more popular than ever online. Commonly referred to as &#8220;8-bit&#8221; these games have pixel-art graphics and &#8220;chip tune&#8221; music that apes the consoles of old, but often have surprisingly innovative gameplay mechanics. And Flash has turned out to be the perfect tool for creating them, as hit titles like <a href="http://adamatomic.com/canabalt/">Canabalt</a> and <a href="http://adamatomic.com/fathom" target="_blank">Fathom</a> demonstrate.</p>
<div id="attachment_2248" class="wp-caption alignnone" style="width: 417px"><img src="http://c251763.r63.cf3.rackcdn.com/wp-content/uploads/2011/09/Image-Fathom.png" alt="" /><p class="wp-caption-text">Fathom was one of the first games built with Flixel and is a good example of what it can do.</p></div>
<p>This may seem at odds with a technology known for its vector graphics and timeline animation. But under the hood Flash is perfectly capable of pushing around large volumes of pixels which is exactly what is required. Flixel is a game framework born from a desire to create 8-bit style games. But it has evolved into a powerful way to rapidly create games in Flash. With a strong community and plug-ins available it&#8217;s the perfect choice to create our game with. In this two part series we&#8217;ll explore how Flixel works and build a retro-styled platform game in the process.</p>
<p><a href="http://sandbox.photonstorm.com/netmag/Building%20a%20retro%20Flash%20game%20in%20Flixel%20Part%201.zip">Download the tutorial files</a> and look through the source code alongside reading the article, because for the sake of space not all of it can be included here and we&#8217;ve got a lot of ground to cover.</p>
<h2>Getting started</h2>
<p>For Windows users there is a guide on <a href="http://www.photonstorm.com/archives/1200/flash-game-dev-tip-6-setting-up-flashdevelop-and-flixel-for-the-first-time" target="_blank">installing FlashDevelop with Flixel</a>. On Mac OS X you can use Adobe Flash Builder. <a href="http://flashgamedojo.com/wiki/index.php?title=Hello_World_-_Flash_Builder_(Flixel)" target="_blank">FlashGameDojo has a tutorial</a> for this. Or run <a href="http://www.flashdevelop.org/community/viewtopic.php?f=9&amp;t=2463" target="_blank">FlashDevelop under Parallels</a>.</p>
<p>Even if you use FlashDevelop / Flash Builder already I&#8217;d still recommend following the guides so you become familiar with the Flixel package structure.</p>
<h2>Game Design Brief</h2>
<p>It helps to have a plan of your game <em>before</em> you start on it. While the plan should never restrict you from iterating your game during development, it will set a base ground on which to build. Here is a concise brief for our game:</p>
<p>&#8220;Player controls a chick who can run left/right and jump on platforms across a horizontally scrolling level. The environment is a lush green jungle with trees, grass, flowers and ruins to explore. There will be static and moving platforms and stars to collect. Collect enough stars and the level exit opens. Player cannot die by falling unless he hits the water. Animal baddies move around the platforms and are fatal to touch.&#8221;</p>
<p>From this brief we can instantly visualise how the game will look and play. This allows us to break the code structure into logical objects such as Player, Map, Star and Enemy. The artist can also create concept sketches to set the style of the game, and provide a point of reference for the pixel art.</p>
<div id="attachment_2249" class="wp-caption alignnone" style="width: 443px"><a href="http://c251763.r63.cf3.rackcdn.com/wp-content/uploads/2011/09/Image-Nutmeg-Concept-Sketches-433x480.jpg"><img class="size-medium wp-image-2249" style="border: 0px;" src="http://c251763.r63.cf3.rackcdn.com/wp-content/uploads/2011/09/Image-Nutmeg-Concept-Sketches-433x480.jpg" alt="" width="433" height="480" /></a><p class="wp-caption-text">Concept Sketches These help set a visual style for the game, introduce main characters and act as a reference for the pixel art.</p></div>
<h2>It starts with FlxGame</h2>
<p>Open <em>Main.as</em> from the &#8220;Part 1&#8243; source folder:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Main <span style="color: #0033ff; font-weight: bold;">extends</span> FlxGame
<span style="color: #000000;">&#123;</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">320</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">240</span><span style="color: #000066; font-weight: bold;">,</span> PlayState<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">60</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">60</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
forceDebugger = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>FlxGame is the wrapper for your whole game, setting up Flixel in the process. The call to <em>super</em> sets the resolution of our game (320 x 240), the initial State (PlayState), the zoom level (2) and the frame rates (60 fps).</p>
<p>While 320 x 240 may seem small at a zoom level of 2 it will display at 640 x 480. The up-scaling of retro games is a common trait that helps show-off the detailed pixel art. <em>forceDebugger</em> enables the built-in debugger which is activated by pressing the &#8216; key. This lets you view bounding boxes, collision, paths, variables, logs and save out game replays.</p>
<p>Your game can have only one FlxGame class, but it can have many State<em>s</em>. States are a way to split your game into logical sections. For example a <em>MainMenuState</em>, a <em>PlayState</em> with the guts of the game and a <em>GameOverState</em>. Only one State can be active at a time.</p>
<h2>Getting into a right State</h2>
<p>States in Flixel extend FlxState and they are the guts of your game. When the State is initialised it calls the function <em>create</em>. This is used to construct any assets or variables the game needs. Once your game is running Flixel calls the <em>update</em> function every frame. In Flash terms it&#8217;s the equivalent of an Enter Frame event. Here is where you handle anything that needs to happen continuously. We&#8217;ll use it to detect collision between game objects.</p>
<p>Inside the <em>create</em> function we make two types of object: the Player sprite and some platforms.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">player = <span style="color: #0033ff; font-weight: bold;">new</span> Player<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">32</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #004993;">floor</span> = <span style="color: #0033ff; font-weight: bold;">new</span> FlxTileblock<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">208</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">320</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">32</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #004993;">floor</span><span style="color: #000066; font-weight: bold;">.</span>makeGraphic<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">320</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">32</span><span style="color: #000066; font-weight: bold;">,</span> 0xff689c16<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #004993;">add</span><span style="color: #000000;">&#40;</span>player<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #004993;">add</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">floor</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>The <em>floor</em> variable is one such platform. Although we&#8217;ll replace it in the final game it&#8217;s a great rapid way to provide objects the player can jump on to test the physics. The objects are then added to the State, allowing Flixel to draw them when it renders the scene. It&#8217;s similar to <em>addChild</em> in native Flash and the order in which you add defines how they are drawn.</p>
<p>All player specific code is held in the Player class, represented here through the <em>player</em> variable. This keeps it organised and easy to re-use. Before we start that however we need to give our player some wings &#8211; quite literally &#8211; by preparing the sprite sheet.</p>
<div id="attachment_2250" class="wp-caption alignnone" style="width: 650px"><a href="http://c251763.r63.cf3.rackcdn.com/wp-content/uploads/2011/09/Image-Player-Sprite-Sheet-640x182.png"><img class="size-medium wp-image-2250" style="border: 0px;" src="http://c251763.r63.cf3.rackcdn.com/wp-content/uploads/2011/09/Image-Player-Sprite-Sheet-640x182.png" alt="" width="640" height="182" /></a><p class="wp-caption-text">Nutmeg The sprite sheet for the pixel hero of our game, drawn in 16 x 18 sized frames.</p></div>
<p>Flixel can automatically create reversed frames so we only draw him facing right. The first frame is an idle pose. Frames 2 and 3 are the walk cycle, and frame 4 is a hurt expression. If you are more comfortable animating inside of Flash Professional then you can create a timeline sequence as usual, export it to a SWF and use a tool such as Grant Skinner&#8217;s Zoe (<a href="http://easeljs.com/zoe.html">http://easeljs.com/zoe.html</a>) to convert it to a sprite sheet.</p>
<p>Sprites in Flixel are called FlxSprite which is what our Player extends. These differ from regular Flash Sprites in that as well as graphics data they contain extra values to allow you to easily control physics, animation, direction, collision and rotation. Inside Player.as we&#8217;ve embedded the sprite sheet:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Embed<span style="color: #000000;">&#40;</span><span style="color: #004993;">source</span> = <span style="color: #990000;">'../assets/player.png'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> playerPNG<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Class</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>And then tell FlxSprite to use it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">loadGraphic<span style="color: #000000;">&#40;</span>playerPNG<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">16</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">18</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>This tells Flixel  to chop the sheet into 16&#215;18 frames and create reversed versions. Animation works in a similar way to Flash MovieClips.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">addAnimation<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;walk&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>This creates a new animation sequence called &#8220;walk&#8221; which cycles through frames 0, 1, 0, 2 at a rate of 10 frames per second. The true parameter makes it loop. To play an animation you use its label:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">play</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;walk&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<h2>What goes up must come down</h2>
<p>Making the player sprite run and jump smoothly provides us with a great opportunity to explore ones of Flixel&#8217;s strengths: its plug-ins. Much like with WordPress and Drupal, the Flixel community release plug-ins which extend the framework further and ultimately save you time.</p>
<p>One such plug-in is FlxControl which is part of the Flixel Power Tools. They are included in the tutorial zips but are constantly updated, so check <a href="https://github.com/photonstorm/Flixel-Power-Tools" target="_blank">https://github.com/photonstorm/Flixel-Power-Tools</a> for updates. If downloading fresh there is a Getting Started guide in the Docs folder.</p>
<p>The plug-in needs to be activated and assigned to our player FlxSprite:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>FlxG<span style="color: #000066; font-weight: bold;">.</span>getPlugin<span style="color: #000000;">&#40;</span>FlxControl<span style="color: #000000;">&#41;</span> == <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
FlxG<span style="color: #000066; font-weight: bold;">.</span>addPlugin<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> FlxControl<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
FlxControl<span style="color: #000066; font-weight: bold;">.</span>create<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">,</span> FlxControlHandler<span style="color: #000066; font-weight: bold;">.</span>MOVEMENT_ACCELERATES<span style="color: #000066; font-weight: bold;">,</span> FlxControlHandler<span style="color: #000066; font-weight: bold;">.</span>STOPPING_DECELERATES<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>The parameters tell it that we want the sprite to accelerate and decelerate. If we didn&#8217;t pick this the sprite would start and stop immediately making the experience very abrupt.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">FlxControl<span style="color: #000066; font-weight: bold;">.</span>player1<span style="color: #000066; font-weight: bold;">.</span>setCursorControl<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
FlxControl<span style="color: #000066; font-weight: bold;">.</span>player1<span style="color: #000066; font-weight: bold;">.</span>setJumpButton<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;SPACE&quot;</span><span style="color: #000066; font-weight: bold;">,</span> FlxControlHandler<span style="color: #000066; font-weight: bold;">.</span>KEYMODE_PRESSED<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000066; font-weight: bold;">,</span> FlxObject<span style="color: #000066; font-weight: bold;">.</span>FLOOR<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">250</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
FlxControl<span style="color: #000066; font-weight: bold;">.</span>player1<span style="color: #000066; font-weight: bold;">.</span>setMovementSpeed<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">400</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">400</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
FlxControl<span style="color: #000066; font-weight: bold;">.</span>player1<span style="color: #000066; font-weight: bold;">.</span>setGravity<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">400</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>This enables cursor control for the left and right keys, and sets SPACE BAR to be the jump button. The player can only jump if on the floor and will try to jump to a height of 200 pixels. setMovementSpeed allows the sprite to accelerate at 400px/sec up to top speed of 100px/sec. Modify these values to much lower or higher numbers to see the effect! Finally we set some gravity, pulling the player back down to earth. Try changing the gravity value to make the player float or drop like a lead balloon. The best thing you can do to learn about Flixel and its plug-ins is to mess around with them. You never know what might come out of it! I&#8217;ve seen whole game ideas born from what were code bugs, so it pays to experiment.</p>
<h2>Putting it all together</h2>
<p>Run the code at this point and you&#8217;ll see we have a cute little chick sprite who can run and leap around at will. It collides smoothly with the ground, and there&#8217;s even a platform to leap on. If you press the debug key (&#8216;) you can watch the physics values in real-time. Click the box icon in the top right to turn on the visual outlines.</p>
<div id="attachment_2251" class="wp-caption alignnone" style="width: 650px"><a href="http://c251763.r63.cf3.rackcdn.com/wp-content/uploads/2011/09/Image-Test-1.png"><img class="size-full wp-image-2251" style="border: 0px;" src="http://c251763.r63.cf3.rackcdn.com/wp-content/uploads/2011/09/Image-Test-1.png" alt="" width="640" height="480" /></a><p class="wp-caption-text">We have lift off! A running and jumping player with platforms to leap on</p></div>
<p>Granted it isn&#8217;t quite Mario, but we&#8217;re not finished just yet.</p>
<h2>Building a Level Map with DAME</h2>
<p>Green blocks are all well and good, but we need a proper level for the player to explore. Flixel has built-in support for Tile Maps which are an efficient and fast way to construct game levels. Tile Maps work by using Tile Set in a series of tiles aligned to a fixed-size grid. Here is the Tile Set created for our game:</p>
<div id="attachment_2252" class="wp-caption alignnone" style="width: 650px"><a href="http://c251763.r63.cf3.rackcdn.com/wp-content/uploads/2011/09/Image-Tileset-640x202.png"><img class="size-medium wp-image-2252" style="border: 0px;" src="http://c251763.r63.cf3.rackcdn.com/wp-content/uploads/2011/09/Image-Tileset-640x202.png" alt="" width="640" height="202" /></a><p class="wp-caption-text">The Tile Set used to draw the game map, complete with water, plants and platforms.</p></div>
<p>Each tile is 16&#215;16 pixels in size, and you can see that some of them join together to form game objects such as the platform in the bottom right. The organisation of the Tile Set is up to you (or your artist!), but it&#8217;s sensible to leave big chunks of empty tiles should you need to insert new tiles later on in development. Tiles are separated into two types: Tiles you can collide with, and tiles you never collide with. In Flixel this is determined by the tile number and is called the Collide Index. Every tile after the index is treated as a tile you collide with. In our Tile Set you&#8217;ll see on the top row items such as flowers, bushes and shrubs. These are all non-colliding tiles, as we don&#8217;t want our player getting stuck on flowers.</p>
<p>With our Tile Set created we now need to design the level. To do this we use a Tile Map Editor. For this tutorial we&#8217;ll use an editor called DAME. Although the interface takes some getting used-to, the actual map view in DAME is rendered directly with Flixel so you know for sure that what you see in the editor is how it appears in game.</p>
<p>DAME can be downloaded from <a href="http://dambots.com/dame-editor/" target="_blank">http://dambots.com/dame-editor/</a> and works on Windows, OS X and Linux. Create 3 map layers in DAME, one for the Sky background, one for the level itself and one for the stars which the player can collect. You can find our DAME maps by downloading the tutorial files and looking in &#8220;Nutmeg Part 2 &#8211; The Map&#8221;. With the Map Layer selected click on a tile in the Tile window and then paint with it, just like an art package:</p>
<div id="attachment_2253" class="wp-caption alignnone" style="width: 650px"><a href="http://c251763.r63.cf3.rackcdn.com/wp-content/uploads/2011/09/Map-Editing-with-Dame-640x360.png"><img class="size-medium wp-image-2253" style="border: 0px;" src="http://c251763.r63.cf3.rackcdn.com/wp-content/uploads/2011/09/Map-Editing-with-Dame-640x360.png" alt="" width="640" height="360" /></a><p class="wp-caption-text">Tile Map Editor DAME in action showing part of the level created for the game</p></div>
<p>Once you are happy with the design of your level you can export the map data to a CSV file from the File ñ Export menu.</p>
<h2>Lights, Camera, Action</h2>
<p>Add a new class to the project called Level1 that extends FlxGroup. This is Flixel&#8217;s way of grouping display objects together and keeps our code logical and structured. We can also check for collision against a whole group, which is significantly faster than checking every element individually. Our map is held in a local variable:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">map</span> = <span style="color: #0033ff; font-weight: bold;">new</span> FlxTilemap<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">.</span>loadMap<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> mapCSV<span style="color: #000066; font-weight: bold;">,</span> mapTilesPNG<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">16</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">16</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">31</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #004993;">add</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">map</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>loadMap takes our CSV map data exported from DAME, and an embedded Tile Set png and creates a Tile Map object which is then added to the group. The same process is repeated for the Sky background.</p>
<p>Back in the PlayState we add the level to the display. But before we can unleash the player we tell Flixelís camera system to follow them around and restrict the camera to the map boundary:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">FlxG<span style="color: #000066; font-weight: bold;">.</span>camera<span style="color: #000066; font-weight: bold;">.</span>setBounds<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">level</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">level</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
FlxG<span style="color: #000066; font-weight: bold;">.</span>camera<span style="color: #000066; font-weight: bold;">.</span>follow<span style="color: #000000;">&#40;</span>player<span style="color: #000066; font-weight: bold;">,</span> FlxCamera<span style="color: #000066; font-weight: bold;">.</span>STYLE_PLATFORMER<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>The last task is to add a collision check into the PlayState update function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">FlxG<span style="color: #000066; font-weight: bold;">.</span>collide<span style="color: #000000;">&#40;</span>player<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">level</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>With the map loaded, the camera set and collision running we can now test the game. The chick now happily bounces around the level, landing on platforms and not colliding with flowers.</p>
<h2>Sprinkling a little stardust</h2>
<p>Now we&#8217;ve a pretty landscape to traverse we&#8217;ll add stars for the player to collect, and display the total collected as a score in the game. Flixel has a text function called FlxText which we use to display the score:</p>
<p>&gt;</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">score = <span style="color: #0033ff; font-weight: bold;">new</span> FlxText<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
score<span style="color: #000066; font-weight: bold;">.</span>scrollFactor<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
score<span style="color: #000066; font-weight: bold;">.</span>scrollFactor<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>The score is added at the top left of the screen and has a scrollFactor of zero. This means the text will be locked in place at the coordinates given, and not scroll around as the camera moves.</p>
<p>In the DAME map the Stars layer was exported to a CSV file and weíll use this map data to create Star sprites. The function parseStars in the Level1 class works by looping through the CSV map data, and for every tile it creates a new Star sprite at those coordinates. All of these sprites are added to an FlxGroup called stars. In the PlayState update function we check to see if the player is colliding with a star:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">FlxG<span style="color: #000066; font-weight: bold;">.</span>overlap<span style="color: #000000;">&#40;</span>player<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">level</span><span style="color: #000066; font-weight: bold;">.</span>stars<span style="color: #000066; font-weight: bold;">,</span> hitStar<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>If a collision occurs this will call the function hitStar, passing in the two objects that collided, in this case the player and the star he hit. The star is removed from the game and the score increased:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">star<span style="color: #000066; font-weight: bold;">.</span>kill<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
FlxG<span style="color: #000066; font-weight: bold;">.</span>score <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
score<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = FlxG<span style="color: #000066; font-weight: bold;">.</span>score<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #990000;">&quot; / &quot;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #004993;">level</span><span style="color: #000066; font-weight: bold;">.</span>totalStars<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>Finally the score text is updated to show them how many stars they&#8217;ve collected, and how many are left. With this one small addition we&#8217;ve now got a real playable game with an objective to collect all stars:</p>
<div id="attachment_2254" class="wp-caption alignnone" style="width: 650px"><a href="http://c251763.r63.cf3.rackcdn.com/wp-content/uploads/2011/09/nutmeg_preview1.png"><img class="size-full wp-image-2254" style="border: 0px;" src="http://c251763.r63.cf3.rackcdn.com/wp-content/uploads/2011/09/nutmeg_preview1.png" alt="" width="640" height="480" /></a><p class="wp-caption-text">Reach for the stars With the stars in place our game now has a purpose - try to complete the level</p></div>
<p>In part 2 we will introduce baddies for you to avoid, sound effects, a user interface (HUD) and Game Over sequence. If you feel inspired to build your own games be sure to visit <a href="http://flixel.org" target="_blank">http://flixel.org</a> which is home to a vibrant developer community. You&#8217;ll also find stacks of Flixel related tutorials at <a href="http://www.photonstorm.com" target="_blank">http://www.photonstorm.com</a> along with the Flixel Power Tools plug-ins. Finally for reference to the classic games of old <a href="http://www.retrogamingtimes.com" target="_blank">http://www.retrogamingtimes.com</a> is well worth a visit.</p>
<p>The following 2 parts were the &#8220;box-outs&#8221; that appeared in the print article. I&#8217;ve included them here because they&#8217;re interesting :)</p>
<h2>Adam Saltsman talks about why he created Flixel</h2>
<p>I guess it was around 7 years ago that I tried to make a little &#8220;game engine&#8221;, something that would allow me to make Super NES style games. It just seemed like a fun thing to do. After a few abortive attempts in C++ / Python / Java I finally settled on AS3. It was the pixel-level stuff in it that really interested me. However I&#8217;d grown very cautious about &#8220;engine building&#8221; and not actually making any games. So I stuck with AS3 and created game after game, and noticed which patterns evolved. The games kept getting more complex, and re-used code from previous projects, and eventually those parts I was seeing in every project got moved to their own folder. I think a lot (if not most) developers have a similar folder somewhere, just a pile of &#8220;really handy&#8221; code.</p>
<p>Originally this was just for me, something I&#8217;d use to sketch new game designs in. But it wasn&#8217;t long before I thought that with a little polish here and there I might be able to release it publicly. Turns out it needed a lot of polish (and still does), but I really like where it&#8217;s going. Lately I have been thinking a lot about how to &#8220;make stuff that matters&#8221;, which is a very loose idea, but something I obsess over, for better or worse. I think Flixel is a cool thing. It&#8217;s not really &#8220;art&#8221; in and of itself, but it contributes to the cultural food chain, and that makes me feel like a hero.</p>
<p>Eventually it would be great to add official support for a physics engine like Nape, and port it over to Molehill, the hardware-accelerated Flash Player by the end of the year. I am very hesitant to ever add game-specific features to the framework, but these all seem like good general systems and will just make making games easier and more fun.</p>
<p>At the risk of leaving out something awesome I would say the following games are shining examples of Flixel in action:</p>
<p><a href="http://adamatomic.com/canabalt/" target="_blank">Canabalt</a><br />
<a href="http://patkemp.com/wp-gallery/games/Station38.html" target="_blank">Station 38</a><br />
<a href="http://www.newgrounds.com/portal/view/522276" target="_blank">Tuper Tario Tros</a><br />
<a href="http://www.newgrounds.com/portal/view/555641" target="_blank">Cat Astro Phi</a></p>
<div id="attachment_2255" class="wp-caption alignnone" style="width: 539px"><a href="http://c251763.r63.cf3.rackcdn.com/wp-content/uploads/2011/09/Image-Box-Out-1-Cat-Astro-Phi-529x480.png"><img class="size-medium wp-image-2255" style="border: 0px;" src="http://c251763.r63.cf3.rackcdn.com/wp-content/uploads/2011/09/Image-Box-Out-1-Cat-Astro-Phi-529x480.png" alt="" width="529" height="480" /></a><p class="wp-caption-text">Cat Astro Phi Designed to look, sound and feel like a Gameboy Classic title it&#39;s a good example of what Flixel is capable of.</p></div>
<h2>Pushing Pixels</h2>
<h3>Mastering the style of pixel art</h3>
<p>Professional pixel artist Ilija Melentijevic explains how he created the artwork for Nutmeg:</p>
<p>Pixel art is a time-consuming but rewarding approach to creating game graphics. If you have little screen space such as on a mobile device, or are simply looking for that charm and precision it offers, then itís worth making the extra effort. Also, it&#8217;s incredibly fun!</p>
<p>It&#8217;s a good idea to start from the big picture. First make a mock-up of a game scene, defining the style, colours, characters, etc. Once you have such solid foundation it&#8217;s easy to turn it into actual game assets. Your art must fit and enhance the game, not just look nice on its own, so make sure everything reads well and speaks to the player in a consistent way. In a busy shooter, use colours to separate bullets from power-ups, or to make the foreground objects pop out. Immersion is a priority in any game, and understanding what&#8217;s going on plays a big part in this. Fire up your favourite games and look how the pros do it.</p>
<p>There are a number of tools you can use and they vary widely. Some people even use Microsoft Paint! Many professionals use Photoshop and it certainly gets the job done, but I prefer a dedicated pixel-oriented program. Closest to the industry standard is <a href="http://cosmigo.com" target="_blank">Cosmigo Promotion</a> which boasts an impressive set of features and will look familiar to Photoshop users. My personal favourite is the 100% free <a href="http://code.google.com/p/grafx2" target="_blank">GraFX2</a>. Currently it lacks animation, but otherwise provides for an unparalleled workflow and ease of use.</p>
<p>Make sure you join an active pixel art community that will provide both feedback and motivation. <a href="http://www.pixeljoint.com" target="_blank">PixelJoint</a> lets you comment, rate and vote on other people&#8217;s work, submit your own, and even take part in weekly challenges, perhaps the best way to hone your skills.</p>
<p>For a less casual approach consider <a href="http://pixelation.wayofthepixel.net" target="_blank">Pixelation</a>, a community focused on pushing the medium forward. Here you&#8217;ll find discussions on theory, game art analysis, collaborations and more. Many masters of the form frequent the forum, so it&#8217;s a great place to post your work to get some constructive critique.</p>
<p>View Ilija&#8217;s artwork at <a href="http://www.photonstorm.com/topics/art" target="_blank">http://www.photonstorm.com/topics/art</a></p>
<div id="attachment_2256" class="wp-caption alignnone" style="width: 650px"><a href="http://c251763.r63.cf3.rackcdn.com/wp-content/uploads/2011/09/Box-Out-2-Grafx2-640x457.png"><img class="size-medium wp-image-2256" style="border: 0px;" src="http://c251763.r63.cf3.rackcdn.com/wp-content/uploads/2011/09/Box-Out-2-Grafx2-640x457.png" alt="" width="640" height="457" /></a><p class="wp-caption-text">GraFX2 A lightweight and free (as in free beer) pixel-pushing program for Windows, Mac and Linux</p></div>
 <div class='series_toc'></div>	<p></p>
	<hr noshade style="margin:0;height:1px" />
	<p>&copy; Photon Storm for <a href="http://mochiland.com">MochiLand</a>, 2011. |
	  <a href="http://mochiland.com/articles/building-a-retro-platform-game-in-flixel-pt-1">Permalink</a> |
	  <a href="http://mochiland.com/articles/building-a-retro-platform-game-in-flixel-pt-1#comments">6 comments</a></p>
	<p>Add to <a href="http://del.icio.us/post?url=http://mochiland.com/articles/building-a-retro-platform-game-in-flixel-pt-1&amp;title=Building A Retro Platform Game In Flixel, Pt. 1">del.icio.us</a></p>
	<p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mochiland.com/articles/building-a-retro-platform-game-in-flixel-pt-1" title="Search on Technorati">Technorati</a></p>
	<p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mochiland.com/category/mochiland/developer-feed" title="View all posts in Developer Feed" rel="category tag">Developer Feed</a>, <a href="http://mochiland.com/category/mochiland/developer-spotlight" title="View all posts in Developer Spotlight" rel="category tag">Developer Spotlight</a>, <a href="http://mochiland.com/category/mochiland/flash" title="View all posts in Flash Development" rel="category tag">Flash Development</a>, <a href="http://mochiland.com/category/mochiland/front-page" title="View all posts in Front Page" rel="category tag">Front Page</a>, <a href="http://mochiland.com/category/mochiland/game-design" title="View all posts in Game Design" rel="category tag">Game Design</a>, <a href="http://mochiland.com/category/mochiland" title="View all posts in MochiLand Topics" rel="category tag">MochiLand Topics</a>, <a href="http://mochiland.com/category/mochiland/tutorials" title="View all posts in Tutorials" rel="category tag">Tutorials</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://mochiland.com/articles/building-a-retro-platform-game-in-flixel-pt-1/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Tutorial: 3D Car With A Camera Following It</title>
		<link>http://mochiland.com/articles/tutorial-3d-car-with-a-camera-following-it</link>
		<comments>http://mochiland.com/articles/tutorial-3d-car-with-a-camera-following-it#comments</comments>
		<pubDate>Tue, 24 May 2011 19:00:46 +0000</pubDate>
		<dc:creator>TomKrcha</dc:creator>
				<category><![CDATA[Developer Feed]]></category>
		<category><![CDATA[Flash Development]]></category>
		<category><![CDATA[Front Page]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[MochiLand Topics]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[Adobe]]></category>
		<category><![CDATA[Camera]]></category>
		<category><![CDATA[Car]]></category>
		<category><![CDATA[Mochi]]></category>
		<category><![CDATA[Tom Krcha]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://mochiland.com/?p=4886</guid>
		<description><![CDATA[Ever wondered how to build a simple 3D car movement with camera that follows the car? Let&#8217;s have a look at this tutorial, which uses Alternativa 3D 8 (Molehill). Download Alternativa 3D 8. This tutorial consist of these parts: - move the car with keyboard controls - follow the car with the camera The final [...]]]></description>
			<content:encoded><![CDATA[ <p>Ever wondered how to build a simple 3D car movement with camera that follows the car?<br />
Let&#8217;s have a look at this tutorial, which uses <a href="http://alternativaplatform.com/">Alternativa 3D</a> 8 (<a href="http://labs.adobe.com/downloads/flashplatformruntimes_incubator.html">Molehill</a>).</p>
<p>Download <a href="http://blog.alternativaplatform.com/en/2011/04/30/meet-early-build-of-alternativa3d-8/">Alternativa 3D 8</a>.</p>
<p>This tutorial consist of these parts:<br />
- move the car with keyboard controls<br />
- follow the car with the camera</p>
<p>The final app will look like this:<br />
<a href="http://flashrealtime.com/demos/carfollowcamera/CarWithFollowCamera.html"><img class="alignnone size-full wp-image-2292" style="border: 0px;" src="http://www.flashrealtime.com/wp-content/uploads/2011/05/CarAppScreenshot.png" alt="" width="480" height="339" /></a><br />
(*requires <a href="http://labs.adobe.com/downloads/flashplatformruntimes_incubator.html">Flash Player with Molehill</a>)</p>
<p><span id="more-4886"></span></p>
<p>To understand the car&#8217;s movement let&#8217;s do a little bit of math. I&#8217;ve got inspired by <a href="http://www.emanueleferonato.com/2010/06/16/create-a-flash-racing-game-tutorial-as3-version/">this tutorial</a> and <a href="http://www.actionscript.org/resources/articles/648/2/Car-Movement/Page2.html">this one</a>. But anyway, there is no rocket science involved. Just a simple trigonometric functions and that&#8217;s great, because anyone can do it easily.</p>
<h3>Speed / Acceleration</h3>
<p>When you press UP key, you add acceleration to the speed. When DOWN key, you subtract acceleration from the speed. When none of them is pressed, you slow down by multiplying actual speed by 0.96 &#8211; note it can be whatever, depends on how fast or slow you want to slow down.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">// don't allow to speed up more than maximum speed</span>
<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>accelerate <span style="color: #000066; font-weight: bold;">&amp;</span>amp<span style="color: #000066; font-weight: bold;">;&amp;</span>amp<span style="color: #000066; font-weight: bold;">;</span> speed0<span style="color: #000066; font-weight: bold;">.</span>3<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		speed<span style="color: #000066; font-weight: bold;">*</span>=<span style="color: #000000; font-weight:bold;">0.96</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
		speed=<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>Steering / Rotation</h3>
<p>When we press LEFT or RIGHT key, we turn the car by Math.PI/40 radians (4.5 degrees). Again, pass number that fits you best.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>turn_left<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	steering <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">PI</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">40</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>turn_right<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	steering <span style="color: #000066; font-weight: bold;">-</span>= <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">PI</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">40</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>You will also find that the solution above is not exactly correct, because we can turn the car even when standing on place with zero speed. To fix that, we will multiply the angle by speed/speedMax (a number between 0-1). Now it will behave correctly</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>turn_left<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	steering <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">PI</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">40</span><span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000;">&#40;</span>speed<span style="color: #000066; font-weight: bold;">/</span>speedMax<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>turn_right<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	steering <span style="color: #000066; font-weight: bold;">-</span>= <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">PI</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">40</span><span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000;">&#40;</span>speed<span style="color: #000066; font-weight: bold;">/</span>speedMax<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>When we are done with speed and acceleration, we can apply it to our car. To calculate the real position of the car, we have to use a simple <a href="http://en.wikipedia.org/wiki/Trigonometric_functions#Sine.2C_cosine.2C_and_tangent">trigonometric functions</a> &#8211; sine and cosine. Note that we know only speed (vector magnitude) and steering (angle, rotation of the car).</p>
<p>By the definition of the sine and the cosine, we are able to calculate the position (X and Y) of the car from the angle and speed.<br />
<a href="http://www.flashrealtime.com/wp-content/uploads/2011/05/SpeedSinCosine.png"><img class="alignnone size-full wp-image-2282" style="border: 0px;" src="http://www.flashrealtime.com/wp-content/uploads/2011/05/SpeedSinCosine.png" alt="" width="480" height="283" /></a></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">car<span style="color: #000066; font-weight: bold;">.</span>rotationZ = <span style="color: #000066; font-weight: bold;">-</span>steering<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
car<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span>= speed<span style="color: #000066; font-weight: bold;">*</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">sin</span><span style="color: #000000;">&#40;</span>steering<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
car<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= speed<span style="color: #000066; font-weight: bold;">*</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">cos</span><span style="color: #000000;">&#40;</span>steering<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>Let&#8217;s have a look what we have achieved so far:<br />
<a href="http://flashrealtime.com/demos/carfollowcamera/CarWithFollowCameraStep1.html"><img class="alignnone size-full wp-image-2293" style="border: 0px" src="http://www.flashrealtime.com/wp-content/uploads/2011/05/CarAppStep1Screenshot.png" alt="" width="480" height="341" /></a><br />
(<a href="http://flashrealtime.com/demos/carfollowcamera/CarWithFollowCameraStep1.as">source code here</a>)</p>
<h3>Camera that follows the car</h3>
<p>To add a so called &#8220;follow camera&#8221;, we have to do one simple thing &#8211; keep it behind the car in a specific distance, again by using trigonometric functions.</p>
<p><img class="alignnone size-full wp-image-2300" style="border: 0px;" src="http://www.flashrealtime.com/wp-content/uploads/2011/05/CameraDistanceAngle.png" alt="" width="324" height="329" /></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">camera<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = car<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">sin</span><span style="color: #000000;">&#40;</span>car<span style="color: #000066; font-weight: bold;">.</span>rotationZ<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span>cameraDistance<span style="color: #000066; font-weight: bold;">;</span>
camera<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = car<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">cos</span><span style="color: #000000;">&#40;</span>car<span style="color: #000066; font-weight: bold;">.</span>rotationZ<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span>cameraDistance<span style="color: #000066; font-weight: bold;">;</span>
camera<span style="color: #000066; font-weight: bold;">.</span>rotationZ = car<span style="color: #000066; font-weight: bold;">.</span>rotationZ<span style="color: #000066; font-weight: bold;">+</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">PI</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// Math.PI = 180 deg</span></pre></div></div>

<p>Right now, we have a camera, that follows the car. Try the <a href="http://flashrealtime.com/demos/carfollowcamera/CarWithFollowCameraStep2.html">demo here</a> (<a href="http://flashrealtime.com/demos/carfollowcamera/CarWithFollowCameraStep2.as">source</a>). But as you see, it feels very hard to play. So we add a little bit of smoothing interpolation to make the game smoother to play.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> targetX<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = car<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">sin</span><span style="color: #000000;">&#40;</span>car<span style="color: #000066; font-weight: bold;">.</span>rotationZ<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span>cameraDistance<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> targetY<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = car<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">cos</span><span style="color: #000000;">&#40;</span>car<span style="color: #000066; font-weight: bold;">.</span>rotationZ<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span>cameraDistance<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> targetZRotation<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = car<span style="color: #000066; font-weight: bold;">.</span>rotationZ<span style="color: #000066; font-weight: bold;">+</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">PI</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
camera<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">-</span>= <span style="color: #000000;">&#40;</span>camera<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">-</span> targetX<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*.</span>2<span style="color: #000066; font-weight: bold;">;</span>
camera<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span>= <span style="color: #000000;">&#40;</span>camera<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> targetY<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*.</span>2<span style="color: #000066; font-weight: bold;">;</span>
camera<span style="color: #000066; font-weight: bold;">.</span>rotationZ <span style="color: #000066; font-weight: bold;">-</span>= <span style="color: #000000;">&#40;</span>camera<span style="color: #000066; font-weight: bold;">.</span>rotationZ <span style="color: #000066; font-weight: bold;">-</span> targetZRotation<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*.</span>2<span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>Now it feels much better. Try the <a href="http://flashrealtime.com/demos/carfollowcamera/CarWithFollowCamera.html">final app here</a>.</p>
<p>You will have also tons of questions, like adding slipping, physics, collisions, etc. Well &#8211; next time ;)</p>
<h3>Source</h3>
<p><a href="http://flashrealtime.com/demos/carfollowcamera/CarWithFollowCamera.as">Download the source code</a></p>
<p>The complete source code looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> alternativa<span style="color: #000066; font-weight: bold;">.</span>engine3d<span style="color: #000066; font-weight: bold;">.</span>core<span style="color: #000066; font-weight: bold;">.</span>Camera3D<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> alternativa<span style="color: #000066; font-weight: bold;">.</span>engine3d<span style="color: #000066; font-weight: bold;">.</span>core<span style="color: #000066; font-weight: bold;">.</span>Object3D<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> alternativa<span style="color: #000066; font-weight: bold;">.</span>engine3d<span style="color: #000066; font-weight: bold;">.</span>core<span style="color: #000066; font-weight: bold;">.</span>Resource<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> alternativa<span style="color: #000066; font-weight: bold;">.</span>engine3d<span style="color: #000066; font-weight: bold;">.</span>core<span style="color: #000066; font-weight: bold;">.</span>View<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> alternativa<span style="color: #000066; font-weight: bold;">.</span>engine3d<span style="color: #000066; font-weight: bold;">.</span>materials<span style="color: #000066; font-weight: bold;">.</span>FillMaterial<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> alternativa<span style="color: #000066; font-weight: bold;">.</span>engine3d<span style="color: #000066; font-weight: bold;">.</span>materials<span style="color: #000066; font-weight: bold;">.</span>TextureMaterial<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> alternativa<span style="color: #000066; font-weight: bold;">.</span>engine3d<span style="color: #000066; font-weight: bold;">.</span>primitives<span style="color: #000066; font-weight: bold;">.</span>Box<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> alternativa<span style="color: #000066; font-weight: bold;">.</span>engine3d<span style="color: #000066; font-weight: bold;">.</span>resources<span style="color: #000066; font-weight: bold;">.</span>TextureResource<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span>Stage3D<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">StageAlign</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">StageScaleMode</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> flash<span style="color: #000066; font-weight: bold;">.</span>display3D<span style="color: #000066; font-weight: bold;">.</span>textures<span style="color: #000066; font-weight: bold;">.</span>Texture<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.geom</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Point</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.ui</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Keyboard</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #000000;">&#91;</span>SWF<span style="color: #000000;">&#40;</span><span style="color: #004993;">width</span>=<span style="color: #990000;">&quot;800&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">height</span>=<span style="color: #990000;">&quot;600&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">frameRate</span>=<span style="color: #990000;">&quot;30&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CarWithFollowCamera <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">Sprite</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> rootContainer<span style="color: #000066; font-weight: bold;">:</span>Object3D = <span style="color: #0033ff; font-weight: bold;">new</span> Object3D<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> camera<span style="color: #000066; font-weight: bold;">:</span>Camera3D<span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> stage3D<span style="color: #000066; font-weight: bold;">:</span>Stage3D<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> speed<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">0</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> steering<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">0</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> accelerate<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> brake<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> turn_left<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> turn_right<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> cameraDistance<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">800</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #000000;">&#91;</span>Embed<span style="color: #000000;">&#40;</span><span style="color: #004993;">source</span>=<span style="color: #990000;">&quot;sand.jpg&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> texture<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Class</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> car<span style="color: #000066; font-weight: bold;">:</span>Object3D<span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> carBody<span style="color: #000066; font-weight: bold;">:</span>Box<span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> carFront<span style="color: #000066; font-weight: bold;">:</span>Box<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> CarWithFollowCamera<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">align</span> = <span style="color: #004993;">StageAlign</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">TOP_LEFT</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleMode</span> = <span style="color: #004993;">StageScaleMode</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">NO_SCALE</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #009900; font-style: italic;">// CAMERA</span>
			camera = <span style="color: #0033ff; font-weight: bold;">new</span> Camera3D<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0.1</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">10000</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			camera<span style="color: #000066; font-weight: bold;">.</span>view = <span style="color: #0033ff; font-weight: bold;">new</span> View<span style="color: #000000;">&#40;</span><span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">stageWidth</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">stageHeight</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>camera<span style="color: #000066; font-weight: bold;">.</span>view<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #009900; font-style: italic;">// init camera settings</span>
			camera<span style="color: #000066; font-weight: bold;">.</span>rotationX = deg2rad<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">70</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #009900; font-style: italic;">//camera.y = -300;</span>
			camera<span style="color: #000066; font-weight: bold;">.</span>rotationZ = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">PI</span><span style="color: #000066; font-weight: bold;">;</span>
			camera<span style="color: #000066; font-weight: bold;">.</span>z = <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">400</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			rootContainer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>camera<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #009900; font-style: italic;">// CAR</span>
			car = <span style="color: #0033ff; font-weight: bold;">new</span> Object3D<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #009900; font-style: italic;">// car body</span>
			carBody = <span style="color: #0033ff; font-weight: bold;">new</span> Box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">200</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">300</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> material<span style="color: #000066; font-weight: bold;">:</span>FillMaterial = <span style="color: #0033ff; font-weight: bold;">new</span> FillMaterial<span style="color: #000000;">&#40;</span>0xFFFF00<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0.8</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			carBody<span style="color: #000066; font-weight: bold;">.</span>setMaterialToAllSurfaces<span style="color: #000000;">&#40;</span>material<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			carBody<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">200</span><span style="color: #000066; font-weight: bold;">;</span>
			car<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>carBody<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #009900; font-style: italic;">// car front</span>
			carFront = <span style="color: #0033ff; font-weight: bold;">new</span> Box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">200</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			carFront<span style="color: #000066; font-weight: bold;">.</span>setMaterialToAllSurfaces<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> FillMaterial<span style="color: #000000;">&#40;</span>0xFFFF00<span style="color: #000066; font-weight: bold;">,.</span>9<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			car<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>carFront<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>		
&nbsp;
			car<span style="color: #000066; font-weight: bold;">.</span>z = <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">50</span><span style="color: #000066; font-weight: bold;">;</span>
			rootContainer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>car<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #009900; font-style: italic;">// MAP</span>
			generateRandomMapOfCubes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
			<span style="color: #009900; font-style: italic;">// INIT Stage3D</span>
			stage3D = <span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span>stage3Ds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
			stage3D<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span>CONTEXT3D_CREATE<span style="color: #000066; font-weight: bold;">,</span> onContextCreate<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			stage3D<span style="color: #000066; font-weight: bold;">.</span>requestContext3D<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> followCarWithCamera<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
&nbsp;
			<span style="color: #009900; font-style: italic;">// Simple smoothing</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> targetX<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = car<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">sin</span><span style="color: #000000;">&#40;</span>car<span style="color: #000066; font-weight: bold;">.</span>rotationZ<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span>cameraDistance<span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> targetY<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = car<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">cos</span><span style="color: #000000;">&#40;</span>car<span style="color: #000066; font-weight: bold;">.</span>rotationZ<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span>cameraDistance<span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> targetZRotation<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = car<span style="color: #000066; font-weight: bold;">.</span>rotationZ<span style="color: #000066; font-weight: bold;">+</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">PI</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			camera<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">-</span>= <span style="color: #000000;">&#40;</span>camera<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">-</span> targetX<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*.</span>2<span style="color: #000066; font-weight: bold;">;</span>
			camera<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span>= <span style="color: #000000;">&#40;</span>camera<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> targetY<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*.</span>2<span style="color: #000066; font-weight: bold;">;</span>
			camera<span style="color: #000066; font-weight: bold;">.</span>rotationZ <span style="color: #000066; font-weight: bold;">-</span>= <span style="color: #000000;">&#40;</span>camera<span style="color: #000066; font-weight: bold;">.</span>rotationZ <span style="color: #000066; font-weight: bold;">-</span> targetZRotation<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*.</span>2<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">loop</span><span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>accelerate <span style="color: #000066; font-weight: bold;">&amp;</span>amp<span style="color: #000066; font-weight: bold;">;&amp;</span>amp<span style="color: #000066; font-weight: bold;">;</span> speed<span style="color: #000066; font-weight: bold;">&amp;</span>lt<span style="color: #000066; font-weight: bold;">;</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> 				speed<span style="color: #000066; font-weight: bold;">+</span>=<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span> 				 			<span style="color: #000000;">&#125;</span><span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>brake<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> 				speed<span style="color: #000066; font-weight: bold;">-</span>=<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span> 			<span style="color: #000000;">&#125;</span><span style="color: #0033ff; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span> 				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span>speed<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">&amp;</span>gt<span style="color: #000066; font-weight: bold;">;</span><span style="color: #000000; font-weight:bold;">0.3</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					speed<span style="color: #000066; font-weight: bold;">*</span>=<span style="color: #000000; font-weight:bold;">0.96</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
					speed=<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>turn_left<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				steering <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">PI</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">40</span><span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000;">&#40;</span>speed<span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>turn_right<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				steering <span style="color: #000066; font-weight: bold;">-</span>= <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">PI</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">40</span><span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000;">&#40;</span>speed<span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
			car<span style="color: #000066; font-weight: bold;">.</span>rotationZ = <span style="color: #000066; font-weight: bold;">-</span>steering
&nbsp;
			car<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span>= speed<span style="color: #000066; font-weight: bold;">*</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">sin</span><span style="color: #000000;">&#40;</span>steering<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			car<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= speed<span style="color: #000066; font-weight: bold;">*</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">cos</span><span style="color: #000000;">&#40;</span>steering<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			followCarWithCamera<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			camera<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">render</span><span style="color: #000000;">&#40;</span>stage3D<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> onKeyDown<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">38</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				accelerate=<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">40</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				brake=<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">37</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				turn_left=<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">39</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				turn_right=<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> onKeyUp<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">38</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				accelerate=<span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">40</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				brake=<span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">37</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				turn_left=<span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">39</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				turn_right=<span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900; font-style: italic;">// HELPER METHODS // not important to study them</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> onContextCreate<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> resource<span style="color: #000066; font-weight: bold;">:</span>Resource <span style="color: #0033ff; font-weight: bold;">in</span> rootContainer<span style="color: #000066; font-weight: bold;">.</span>getResources<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				resource<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">upload</span><span style="color: #000000;">&#40;</span>stage3D<span style="color: #000066; font-weight: bold;">.</span>context3D<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">loop</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">KEY_DOWN</span><span style="color: #000066; font-weight: bold;">,</span> onKeyDown<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">KEY_UP</span><span style="color: #000066; font-weight: bold;">,</span> onKeyUp<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> generateRandomMapOfCubes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000000;">&#123;</span>
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> ground<span style="color: #000066; font-weight: bold;">:</span>Box = <span style="color: #0033ff; font-weight: bold;">new</span> Box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10000</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">10000</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			ground<span style="color: #000066; font-weight: bold;">.</span>setMaterialToAllSurfaces<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> FillMaterial<span style="color: #000000;">&#40;</span>0xCCCCCC<span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0.3</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			rootContainer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>ground<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>i<span style="color: #000066; font-weight: bold;">&amp;</span>lt<span style="color: #000066; font-weight: bold;">;</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000066; font-weight: bold;">;</span>i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">height</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">700</span><span style="color: #000066; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">200</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">:</span>Box = <span style="color: #0033ff; font-weight: bold;">new</span> Box<span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">700</span><span style="color: #000066; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">200</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">700</span><span style="color: #000066; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">200</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #004993;">height</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span>setMaterialToAllSurfaces<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> FillMaterial<span style="color: #000000;">&#40;</span>0xCCCCCC<span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0.5</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
				<span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span>z = <span style="color: #000066; font-weight: bold;">-</span><span style="color: #004993;">height</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">5000</span><span style="color: #000066; font-weight: bold;">-</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">10000</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">5000</span><span style="color: #000066; font-weight: bold;">-</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">10000</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
				rootContainer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">b</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> rad2deg<span style="color: #000000;">&#40;</span>radnum<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> radnum<span style="color: #000066; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">180</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">PI</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> deg2rad<span style="color: #000000;">&#40;</span>degnum<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> degnum<span style="color: #000066; font-weight: bold;">*</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">PI</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">180</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

 <div class='series_toc'></div>	<p></p>
	<hr noshade style="margin:0;height:1px" />
	<p>&copy; TomKrcha for <a href="http://mochiland.com">MochiLand</a>, 2011. |
	  <a href="http://mochiland.com/articles/tutorial-3d-car-with-a-camera-following-it">Permalink</a> |
	  <a href="http://mochiland.com/articles/tutorial-3d-car-with-a-camera-following-it#comments">5 comments</a></p>
	<p>Add to <a href="http://del.icio.us/post?url=http://mochiland.com/articles/tutorial-3d-car-with-a-camera-following-it&amp;title=Tutorial: 3D Car With A Camera Following It">del.icio.us</a></p>
	<p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mochiland.com/articles/tutorial-3d-car-with-a-camera-following-it" title="Search on Technorati">Technorati</a></p>
	<p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mochiland.com/category/mochiland/developer-feed" title="View all posts in Developer Feed" rel="category tag">Developer Feed</a>, <a href="http://mochiland.com/category/mochiland/flash" title="View all posts in Flash Development" rel="category tag">Flash Development</a>, <a href="http://mochiland.com/category/mochiland/front-page" title="View all posts in Front Page" rel="category tag">Front Page</a>, <a href="http://mochiland.com/category/mochiland/game-design" title="View all posts in Game Design" rel="category tag">Game Design</a>, <a href="http://mochiland.com/category/mochiland" title="View all posts in MochiLand Topics" rel="category tag">MochiLand Topics</a>, <a href="http://mochiland.com/category/mochiland/tutorials" title="View all posts in Tutorials" rel="category tag">Tutorials</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://mochiland.com/articles/tutorial-3d-car-with-a-camera-following-it/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Virtual Controllers for Touch-Based Devices pt. 1</title>
		<link>http://mochiland.com/articles/virtual-controllers-for-touch-based-devices-pt-1</link>
		<comments>http://mochiland.com/articles/virtual-controllers-for-touch-based-devices-pt-1#comments</comments>
		<pubDate>Mon, 25 Apr 2011 23:52:04 +0000</pubDate>
		<dc:creator>MikeJones</dc:creator>
				<category><![CDATA[Flash Development]]></category>
		<category><![CDATA[Front Page]]></category>
		<category><![CDATA[Industry News]]></category>
		<category><![CDATA[MochiLand Topics]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Adobe]]></category>
		<category><![CDATA[developement]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[help]]></category>
		<category><![CDATA[Mike Jones]]></category>
		<category><![CDATA[Mochi]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[virtual controller]]></category>

		<guid isPermaLink="false">http://mochiland.com/?p=4771</guid>
		<description><![CDATA[Virtual Controllers For Touch Based Devices (pt.1) As I&#8217;m working on a series of articles on Pushbutton Engine I thought I&#8217;d use that as a basis for testing some virtual controllers I have be playing about with. For one, it saves me for having to create the majority of the scaffold code and concentrate on [...]]]></description>
			<content:encoded><![CDATA[ <p><strong>Virtual Controllers For Touch Based Devices (pt.1)</strong><br />
As I&#8217;m working on a series of articles on Pushbutton Engine I thought I&#8217;d use that as a basis for testing some virtual controllers I have be playing about with. For one, it saves me for having to create the majority of the scaffold code and concentrate on the actual controllers and how they integrate. One thing I was acutely aware of  was keeping the integration of the controller as generic as possible with the idea that it could be used in any game, regardless of the code that supports it.</p>
<p>I also made a few assumptions, firstly I used my thumbs as the basis for determining the size of the controller&#8217;s thumb stick &#8216;head&#8217; &#8211; I have quite big thumbs so I assumed that this would suffice for most people. The second one was more technical. As most PC games tend to rely heavily on keyboard input (and Flash web games even more so) for controlling your ship / player; I opted to emulate the 4 arrow keys &#8211; Up, Down, Left and Right instead of using co-ordinate translation*. There were a few reasons for key input emulation over co-ordinate translation. The first one I have already outlined. The second was to do with ease of conversion; as most Flash games use the keyboard already adding in the virtual controller shouldn&#8217;t require a major update to any pre-existing code. Lastly, there is the testing and debugging.</p>
<p>*I am working on a version that uses co-ordinate translation and will post it up soon.</p>
<p><strong>Designing The Controller</strong></p>
<p><img class="floatRight alignright" style="float: right;border: 0px" src="http://blog.flashgen.com/images/gaming/devices/virtualjoystick.png" alt="" />The first thing to consider is the actual visual appearance of your controller &#8211; in my case this was going to be a thumb stick like those found on the Playstation or XBox controllers. For ease of mapping the users touch input on the thumb stick you really need two elements. First the thumb area, i.e. the place where the user places their thumb; secondly you need a surround. The surround is useful for two reasons. It provides a visual indication to the user on how far the thumb stick moves as well as allowing you to use this as the boundaries of your entire controller for the purpose of determining where the users thumb actually is and more importantly when they lift their thumb up. You can see a simple design for the entire thumb stick controller (including both the thumb and surround) in the image on the right.</p>
<p>Interestingly you don&#8217;t actually need to have any visuals if you don&#8217;t want them. Some games forgo them and just map the users thumb location. This can be confusing for some so depending on type of users you are expecting to attract to your game may determine whether you can remove the visual elements entirely. If in doubt make the controllers slightly transparent.</p>
<p>As their is a visual aspect to this controller it makes sense to leverage Flash Professional CS5 &#8211; you can do this all in Flash Builder if you wish, but it makes the initial set up easier using a more visual tool. Create a new FLA file and save it somewhere on your machine with the file name <span style="font-family: 'Courier New';font-size: 14px">VirtualControllers</span>. Next create a new ActionScript 3.0 class, call it <span style="font-family: 'Courier New';font-size: 14px">ThumbStick.as</span> and save it in the same directory as the one that contains <span style="font-family: 'Courier New';font-size: 14px">VirtualControllers.fla</span>. I use a package structure for my classes so I have nested folders as the image shows. You don&#8217;t have to do this if you don&#8217;t want to and in this article you&#8217;ll be using a flat package structure.</p>
<p><img src="http://blog.flashgen.com/images/gaming/devices/folderhierarchy.png" alt="" /></p>
<p>Before we start writing code though you need to create a symbol in the Library for the controller, and import the image assets for both the surround and the thumb stick (which you can download via the link at the bottom of this article, or create your own).</p>
<ul>
<li>Create a new symbol (Insert &gt; New Symbol&#8230;)</li>
<li>Call this new symbol ThumbStick</li>
<li>Locate the downloaded assets for the surround and the thumb stick and import them in to your library (File &gt; Import &gt; Import to library)</li>
<li>Flash Pro CS5 will automatically create two new symbols (Symbol1 and Symbol2) in your Symbol library</li>
<li>Open the Symbol library (Window &gt; Library)</li>
<li>Select Symbol1 and rename it based on the graphic it contains (it&#8217;ll either be the surround or the thumb image)</li>
<li>Repeat this process for Symbol2</li>
</ul>
<p>Once you have renamed the symbols you need to edit them both to make sure that the enclosed image is centred in the middle of the symbol, as well as making sure that they are set as MovieClip symbols not Graphic symbols (which is what they will be by default).</p>
<ul>
<li>Right click on the <span style="font-family: 'Courier New';font-size: 14px">Surround</span> symbol in your library</li>
<li>Select Properties from the context menu</li>
<li>When the properties dialog opens change the top Type drop down from Graphic to MovieClip</li>
<li>Click OK to apply and close this dialog window</li>
<li>Repeat this for the <span style="font-family: 'Courier New';font-size: 14px">Thumb</span> symbol too</li>
<li>With the Thumb symbol still selected double click it to enter edit mode</li>
<li>The stage should update with the image asset placed approximately in the middle of the screen</li>
<li>Select the image (a bounding blue box should appear)</li>
<li>From the Properties panel (Window &gt; Properties) set the X and Y values to -45, this will centre the Thumb image as it&#8217;s 90&#215;90 pixels</li>
<li>Repeat this for the Surround symbol, except set the X and Y values to -75 as the Surround image is 150&#215;150. Again this will centre it</li>
</ul>
<p><img class="floatRight" style="float: right;border: 0px" src="http://blog.flashgen.com/images/gaming/devices/symbolalignment.png" alt="" /></p>
<p>Now you have the main elements of your Thumb Stick controller defined you&#8217;ll need to combine them. To do this first edit the symbol you created called <span style="font-family: 'Courier New';font-size: 14px">ThumbStick</span></p>
<ul>
<li>Double click on the ThumbStick symbol in your Library to enter edit mode</li>
<li>Rename the default layer in your Timeline (Window &gt; Timeline) to Thumb by right clicking on the layer and selecting Properties from the context menu</li>
<li>A dialog will open where you can set the new layer name. You can also just double click the layer to edit it directly</li>
<li>Create a new layer in the Timeline panel (click the New Layer icon in the bottom left of the Timeline) and name it Surround</li>
<li>Make sure you still have the Surround layer selected</li>
<li>Drag the Surround symbol from your library and make sure it is centred in the middle of your symbol</li>
<li>With the Surround symbol still selected open the Properties panel and give it an instance name of <span style="font-family: 'Courier New';font-size: 14px">surround</span></li>
<li>Once you are happy with its position select the Thumb layer</li>
<li>With the Thumb layer selected drag the Thumb symbol on to the stage of the ThumbStick symbol, making sure it&#8217;s on the Thumb layer</li>
<li>Again centre the Thumb symbol so it is in the exact centre (note the cross hair in the middle of the symbols in the image)</li>
<li>And as with the Surround symbol, open the Properties panel (making sure it is still selected) and give the Thumb symbol the instance name of <span style="font-family: 'Courier New';font-size: 14px">thumb</span></li>
</ul>
<blockquote><p><em>Quick Tip</em></p>
<p>If the bitmap assets are looking a bit &#8216;jaggy&#8217; you can always apply smoothing to them by right clicking on each image in the library and selecting Properties from the context menu. When the dialog opens just tick the Smoothing checkbox and click OK to apply.</p></blockquote>
<p>Now you have the FLA and ActionScript files saved we can get about creating the initial code. The first thing you need to do is make sure your class extends Sprite (by default it doesn&#8217;t extend anything). You&#8217;re also going to need some private variables to store some of the controllers info in. To that end add the following (or just copy and paste the code below):</p>
<p><strong>Defining The Controller</strong></p>
<p>Now that you have a design, and you have created your actual controller symbol, you need to start thinking about adding the code to actually control it when the user interacts with it. The best approach is to break this process down initially as a bulleted list of its functional parts. The list will likely look something like this:</p>
<ul>
<li>Move thumb stick in a circular motion</li>
<li>Limit thumb stick movement to within the surround</li>
<li>Return thumb stick to centre of controller when the user removes their thumb</li>
<li>Dispatch an event when the user moves the thumb stick</li>
</ul>
<p>Obviously you are free to add more items to the list but these four cover the basic functionality. With that in mind lets start coding&#8230;</p>
<p><strong>Writing The Code</strong></p>
<p>The first thing you need to do is make sure that your ThumbStick class extends Sprite &#8211; by default Flash Professional creates non-inheriting classes. Now your code will look like this (I&#8217;ve applied a bit of formatting to my structure in case yours is formatted slightly differently)<br />
<pre><pre>package
{
&nbsp;&nbsp;import flash.display.Sprite;

&nbsp;&nbsp;public class ThumbStick extends Sprite
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;public function ThumbStick()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
}</pre></pre><br />
Looking back at the four functional points I laid down earlier, the first related to moving the thumb stick in a circular motion let&#8217;s start by moving the thumb stick. This is a fairly complex process so I&#8217;m going to save that for part 2 of this article. However what you are going to do is set the controller so when you press down on the thumb stick it will drag in the direction of your thumb movement and on release it will re-centre itself back to the middle of the surround</p>
<p>To do this you need to add a couple of event listeners. These will deal with responding to the touch events when the user presses down with their thumb as well as when they remove it. Even though you can directly interact with the symbols you have within your controller (as you gave them instance names), the code hinting in Flash Professional&#8217;s code editor doesn&#8217;t kick in &#8211; so I tend to make a private variable that I map to the symbol to make this easier.</p>
<p>The code below has the handlers included as well as the event listeners so you can see the flow and structure (there is a problem with this code and I&#8217;ll explain what it is in a moment)<br />
<pre><pre>package
{
&nbsp;&nbsp;import flash.display.Sprite;
&nbsp;&nbsp;import flash.events.TouchEvent;

&nbsp;&nbsp;public class ThumbStick extends Sprite
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;private var _thumb&nbsp;&nbsp;:Sprite;
&nbsp;&nbsp;&nbsp;&nbsp;private var _surround:Sprite;

&nbsp;&nbsp;&nbsp;&nbsp;public function ThumbStick()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_thumb = thumb;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_surround = surround;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_thumb.addEventListener(TouchEvent.TOUCH_BEGIN, onThumbDown);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_thumb.addEventListener(TouchEvent.TOUCH_END, onThumbUp);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_surround.addEventListener(TouchEvent.TOUCH_END, onThumbUp);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;protected function onThumbDown(e:TouchEvent):void
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;trace(&quot;Thumb Down&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;protected function onThumbUp(e:TouchEvent):void
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;trace(&quot;Thumb Up&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
}</pre></pre><br />
So what&#8217;s the problem then? Well it has to do with testing. You see if you run this from inside Flash Professional you won&#8217;t be able to test the TouchEvents as there is no equivalent on a normal desktop PC. Not particularly useful if you don&#8217;t have a physical device at hand to test on (at this point I will emphasise how important it is to test you games , applications etc on the target hardware at some point).</p>
<p>One approach is to check the type of hardware you are running on and adjusting the code flow based on that by using the <span style="font-family: 'Courier New';font-size: 14px">Capabilities()</span> class. So if I update the code above further I can test to see if this is running on an ARM based processor or not (i.e most Smart phones and tablets). I can then tailor how the code is initialized.<br />
<pre><pre>package
{
&nbsp;&nbsp;import flash.display.Sprite;
&nbsp;&nbsp;import flash.events.TouchEvent;
&nbsp;&nbsp;import flash.system.Capabilities;
&nbsp;&nbsp;import flash.events.MouseEvent;

&nbsp;&nbsp;public class ThumbStick extends Sprite
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;private var _thumb&nbsp;&nbsp;:Sprite;
&nbsp;&nbsp;&nbsp;&nbsp;private var _surround:Sprite;

&nbsp;&nbsp;&nbsp;&nbsp;public function ThumbStick()
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_thumb = thumb;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_surround = surround;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(Capabilities.cpuArchitecture == &quot;ARM&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_thumb.addEventListener(TouchEvent.TOUCH_BEGIN, onThumbDown);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_thumb.addEventListener(TouchEvent.TOUCH_END, onThumbUp);
&nbsp;&nbsp;_surround.addEventListener(TouchEvent.TOUCH_END, onThumbUp);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_thumb.addEventListener(MouseEvent.MOUSE_DOWN, onTestThumbDown);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_thumb.addEventListener(MouseEvent.MOUSE_UP, onTestThumbUp);
&nbsp;&nbsp;_surround.addEventListener(MouseEvent.MOUSE_UP, onTestThumbUp);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;protected function onThumbDown(e:TouchEvent):void
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;trace(&quot;Thumb Down&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;protected function onThumbUp(e:TouchEvent):void
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;trace(&quot;Thumb Up&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;// These are just for testing within Flash Pro
&nbsp;&nbsp;&nbsp;&nbsp;protected function onTestThumbDown(e:MouseEvent):void
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;trace(&quot;Mouse Down&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;protected function onTestThumbUp(e:MouseEvent):void
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;trace(&quot;Mouse Up&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
}</pre></pre><br />
This is useful, but I tend to remove all of the non essential code just before I test on the actual device &#8211; after all why include code you will never execute? Moving on&#8230;</p>
<p>The next functionality you need to implement is the actual ability to move the thumb stick. This is pretty easy to do and I&#8217;ve just created two methods to control this &#8211; one to deal with the actual movement when the user is touching the thumb stick, and one to reposition the thumb stick back to the centre of the surround<br />
<pre><pre>protected function initDrag():void
{
&nbsp;&nbsp;_thumb.startDrag();
}

protected function resetThumb():void
{
&nbsp;&nbsp;_thumb.stopDrag();
&nbsp;&nbsp;_thumb.x = 0;
&nbsp;&nbsp;_thumb.y = 0;
}</pre></pre><br />
To access these methods you need to place a call to <span style="font-family: 'Courier New';font-size: 14px">initDrag()</span> inside your event handlers that respond to the user placing their thumb on the actual thumb stick &#8211; <span style="font-family: 'Courier New';font-size: 14px">onThumbDown()</span> and <span style="font-family: 'Courier New';font-size: 14px">onTestThumbDown()</span>. Likewise the <span style="font-family: 'Courier New';font-size: 14px">resetThumb()</span> method needs to be called when the user removes their thumb from the controller, so you need to add a call to it within the <span style="font-family: 'Courier New';font-size: 14px">onThumbUp()</span> and <span style="font-family: 'Courier New';font-size: 14px">onTestThumbUp()</span> methods.</p>
<p>If you test it now it should function in a similar manner to the example below. Clicking and dragging the thumb stick moves it (try the example below). Releasing the mouse button will centre it back to its original position in the middle of the surround &#8211; note that while you can click and drag the thumb stick with your mouse, but it isn&#8217;t confined by the boundaries of the surround. This isn&#8217;t exactly what we want. Worry not though, you&#8217;ll be addressing that next.</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="200" height="200" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="quality" value="high" /><param name="src" value="http://mochiland.com/wp-content/uploads/2011/04/VirtualControllers11.swf" /><embed type="application/x-shockwave-flash" width="200" height="200" src="http://mochiland.com/wp-content/uploads/2011/04/VirtualControllers11.swf" quality="high"></embed></object></p>
<p><strong>Setting Controller Boundaries</strong></p>
<p>As I mentioned earlier, this gives you basic drag functionality within your virtual controller and repositions the thumb stick on releasing the mouse button. However it doesn&#8217;t restrict the movement to within the actual controller surround. So with out further ado let&#8217;s jump right in the deep end and start with some Trigonometry :p. I&#8217;m not going to give you a trig primer here, but I will explain where applicable what is actually happening &#8211; if you want to know more there are many good resources online (a simple google search should suffice)</p>
<p>The first thing you need to do is add in a new event listener to monitor the thumb stick as you move it so it can be restricted when it hits the desired position at the boundaries of the controller surround. Now, normally you&#8217;d probably use the <span style="font-family: 'Courier New';font-size: 14px">MouseEvent.MOUSE_MOVE</span> event to monitor the movement of the thumb stick. However this event is quite expensive in terms of event dispatching and usage compared to the <span style="font-family: 'Courier New';font-size: 14px">Event.ENTER_FRAME</span> event. With that in mid you need to add the following line to end of the <span style="font-family: 'Courier New';font-size: 14px">initDrag()</span> method<br />
<pre>addEventListener(Event.ENTER_FRAME, onThumbDrag);</pre><br />
Likewise you need to remove this event when the user stops interacting with the controller. To achieve this you just need to remove the event listener in the <span style="font-family: 'Courier New';font-size: 14px">resetThumb()</span> method. Like the <span style="font-family: 'Courier New';font-size: 14px">initDrag()</span> method add the following line to the beginning of the <span style="font-family: 'Courier New';font-size: 14px">resetThumb()</span> method. That way the event is removed straight away and any other house keeping routines can be processed once this has happened<br />
<pre>removeEventListener(Event.ENTER_FRAME, onThumbDrag);</pre><br />
You&#8217;ll notice that both lines of code refer to an as yet unimplemented method called <span style="font-family: 'Courier New';font-size: 14px">onThumbDrag()</span>. This method is where all of calculations relating to the thumb stick&#8217;s movement boundaries are set. Below is the entire <span style="font-family: 'Courier New';font-size: 14px">onThumbDrag()</span> method code you&#8217;ll need to implement in your code.<br />
<pre><pre>protected function onThumbDrag(e:Event):void
{
&nbsp;&nbsp;// Store the current x/y of the knob
&nbsp;&nbsp;var _currentX&nbsp;&nbsp;&nbsp;&nbsp;:Number = _thumb.x;
&nbsp;&nbsp;var _currentY&nbsp;&nbsp;&nbsp;&nbsp;:Number = _thumb.y;

&nbsp;&nbsp;// Store the registration point of the surrounding &#039;joystick holder&#039;
&nbsp;&nbsp;var _registrationX&nbsp;&nbsp;:Number = _surround.x;
&nbsp;&nbsp;var _registrationY&nbsp;&nbsp;:Number = _surround.y;

&nbsp;&nbsp;// Subtract the two from each other to get the actual x/y
&nbsp;&nbsp;var _actualX&nbsp;&nbsp;:Number = _currentX - _registrationX;
&nbsp;&nbsp;var _actualY&nbsp;&nbsp;:Number =&nbsp;&nbsp;_currentY - _registrationY;

&nbsp;&nbsp;// Calculate the degrees for use when creating the zones.
&nbsp;&nbsp;_degrees = Math.round(Math.atan2(_actualY, _actualX) * 180/Math.PI);

&nbsp;&nbsp;// Calculate the radian value of the knobs current position
&nbsp;&nbsp;var _angle&nbsp;&nbsp;&nbsp;&nbsp; :Number = _degrees * (Math.PI / 180);

&nbsp;&nbsp;// As we want to lock the orbit of the knob we need to calculate x/y at the maximum distance
&nbsp;&nbsp;var _maxX&nbsp;&nbsp;&nbsp;&nbsp;:Number = Math.round((_radius * Math.cos(_angle)) + _registrationX);
&nbsp;&nbsp;var _maxY&nbsp;&nbsp;&nbsp;&nbsp;:Number = Math.round((_radius * Math.sin(_angle)) + _registrationY);

&nbsp;&nbsp;// Check to make sure that the value is positive or negative
&nbsp;&nbsp;if(_currentX &amp;gt; 0 &amp;amp;&amp;amp; _currentX &amp;gt; _maxX || _currentX &amp;lt; 0 &amp;amp;&amp;amp; _currentX &amp;lt; _maxX)&nbsp;&nbsp;&nbsp;&nbsp; _thumb.x = _maxX;&nbsp;&nbsp; if(_currentY &amp;gt; 0 &amp;amp;&amp;amp; _currentY &amp;gt; _maxY || _currentY &amp;lt; 0 &amp;amp;&amp;amp; _currentY &amp;lt; _maxY)
&nbsp;&nbsp;&nbsp;&nbsp;_thumb.y = _maxY;
}</pre></pre><br />
Before I go through the various aspects of this method you&#8217;ve probably noticed that there are two variable fields being referenced beyond all of the local variables. They are <span style="font-family: 'Courier New';font-size: 14px">_degrees</span> and <span style="font-family: 'Courier New';font-size: 14px">_radius</span>. Both of which are defined below<br />
<pre><pre>private var _degrees&nbsp;&nbsp;:Number;
private var _radius&nbsp;&nbsp;:Number = 25;</pre></pre><br />
The <span style="font-family: 'Courier New';font-size: 14px">_degrees</span> variable I&#8217;ll cover later on, but the <span style="font-family: 'Courier New';font-size: 14px">_radius</span> variable deals with the amount of movement within the boundary of your controller. The reason it is set to 25 here is that the thumb stick is 50&#215;50 pixels therefore half of that is 25 and the surround is 150&#215;150 pixels &#8211; again half of that is 75. If you add 25 to the thumbs 50&#215;50 dimensions you 75. Simple really. So when the user drags it the most it can move is to the actual edge of the surround. If you want it to move beyond the edge slightly just increase the value. Likewise if you want it to move a shorter distance reduce this amount.</p>
<p><strong>The Math &#8216;Bit&#8217;</strong></p>
<p>That&#8217;s all well and good in theory, but how does that actually get applied in the code above? Well let&#8217;s start at the beginning of the method and look at each block of code so you can get a clearer understanding of the actual flow. First up you have a set of local storage variables. These just contain references to the position of the thumb stick and the surround:<br />
<pre><pre>// Store the current x/y of the knob
var _currentX&nbsp;&nbsp;&nbsp;&nbsp;:Number = _thumb.x;
var _currentY&nbsp;&nbsp;&nbsp;&nbsp;:Number = _thumb.y;

// Store the registration point of the surrounding &#039;joystick holder&#039;
var _registrationX&nbsp;&nbsp;:Number = _surround.x;
var _registrationY&nbsp;&nbsp;:Number = _surround.y;</pre></pre><br />
Next up are the variables that store the actual X/Y position within the controller itself by subtracting the thumb stick&#8217;s position from the surround&#8217;s position.<br />
<pre><pre>// Subtract the two from each other to get the actual x/y
var _actualX&nbsp;&nbsp;:Number = _currentX - _registrationX;
var _actualY&nbsp;&nbsp;:Number =&nbsp;&nbsp;_currentY - _registrationY;</pre></pre><br />
Next up is the <span style="font-family: 'Courier New';font-size: 14px">_degrees</span> variable. As I mentioned earlier I&#8217;ll be going in to more detail on what you&#8217;ll be using this for shortly. All you need to know at this point is that it takes the values of <span style="font-family: 'Courier New';font-size: 14px">_actualX</span> and <span style="font-family: 'Courier New';font-size: 14px">_actualY</span> and converts them to radians (using the <span style="font-family: 'Courier New';font-size: 14px">Math.atan()</span> method) which are them in turn converted in to degrees &#8211; i.e. a value from 0 to 180 and -179 to -1<br />
<pre><pre>// Calculate the degrees for use when creating the zones.
_degrees = Math.round(Math.atan2(_actualY, _actualX) * 180/Math.PI);</pre></pre><br />
The <span style="font-family: 'Courier New';font-size: 14px">_angles</span> variable just converts the <span style="font-family: 'Courier New';font-size: 14px">_degrees</span> value back in to radian. While I appreciate that I could have simplified this code and just used <span style="font-family: 'Courier New';font-size: 14px">Math.atan()</span> like the <span style="font-family: 'Courier New';font-size: 14px">_degrees</span> variable. This way you can at least see how to convert between degrees and radians (should you ever need to)<br />
<pre><pre>// Calculate the radian value of the knobs current position
var _angle&nbsp;&nbsp;&nbsp;&nbsp; :Number = _degrees * (Math.PI / 180);</pre></pre><br />
You&#8217;re almost at the end of this process so hang on in there. There are two parts to restricting the thumb sticks X and Y position. First of all you need to calculate the maximum distance the thumb stick can move. The second part then checks to see if the thumb stick&#8217;s actual X or Y values are within or beyond those values. If they are beyond those values it automatically sets the X or Y of the thumb stick to the maximum distance for that property if outside of the boundary. To calculate the distance isn&#8217;t that obvious as you are measuring the edge of a circle. That&#8217;s where the trigonometry I mentioned comes in. By calculating the two sides of a right angle triangle within the controller surround you can easily then work out the hypotenuse (longest side) of the triangle from the centre. And that is all that is happening here:<br />
<pre><pre>// As we want to lock the orbit of the knob we need to calculate x/y at the maximum distance
var _maxX&nbsp;&nbsp;&nbsp;&nbsp;:Number = Math.round((_radius * Math.cos(_angle)) + _registrationX);
var _maxY&nbsp;&nbsp;&nbsp;&nbsp;:Number = Math.round((_radius * Math.sin(_angle)) + _registrationY);</pre></pre><br />
As I said the second part then actually checks the position of the thumb stick and if it is beyond the maximum position it resets it to that maximum:<br />
<pre><pre>// Check to make sure that the value is positive or negative
if(_currentX &amp;gt; 0 &amp;amp;&amp;amp; _currentX &amp;gt; _maxX || _currentX &amp;lt; 0 &amp;amp;&amp;amp; _currentX &amp;lt; _maxX)&nbsp;&nbsp; _thumb.x = _maxX;&nbsp;&nbsp;&nbsp;&nbsp; if(_currentY &amp;gt; 0 &amp;amp;&amp;amp; _currentY &amp;gt; _maxY || _currentY &amp;lt; 0 &amp;amp;&amp;amp; _currentY &amp;lt; _maxY)
&nbsp;&nbsp;_thumb.y = _maxY;</pre></pre><br />
With this code in place you can now test your controller. Now it is restricted to the orbit that defined within the onThumbDrag() method, so it doesn&#8217;t move beyond the bounds of the actual surround just like the version below</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="200" height="200" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="quality" value="high" /><param name="src" value="http://mochiland.com/wp-content/uploads/2011/04/VirtualControllers2.swf" /><embed type="application/x-shockwave-flash" width="200" height="200" src="http://mochiland.com/wp-content/uploads/2011/04/VirtualControllers2.swf" quality="high"></embed></object></p>
<p><strong>Where&#8217;s The Thumb Again?</strong></p>
<p>One thing you will have already noticed is that if your mouse moves outside of the thumb stick symbol, when you release it, the thumb stick doesn&#8217;t return to the centred position in the middle of the surround. This is because there is no event registered to capture the release of the thumb stick if you accidentally move out of it. This needs resolving as it is highly likely that your user will move their thumbs beyond the bounds of the thumb stick when playing your game (it&#8217;s just one of those things as they aren&#8217;t looking at the controller and as it&#8217;s virtual there isn&#8217;t any real feedback that they have extended beyond the desired bounds).</p>
<p>To fix this you could just register the stage and respond when the users releases the thumb stick. This is fine desktop / web apps as they don&#8217;t generally support multi-touch so you can only interact with one element at a time with the mouse. On devices this isn&#8217;t the case. Because you can have multiple touch points if you listened for the release via the stage if you have a virtual button &#8211; for firing your ships guns for example &#8211; then every time you lifted your finger off the button it would reset the thumb stick. Not ideal really.</p>
<p>A better way would be to create a invisible area that sits behind the actual controller graphics and listens for the release. That way it doesn&#8217;t matter if the user moves off the actual thumb stick symbol as this area would still respond to the release. Now one thing you would need to decide upon if you chose to implement this is how big should this &#8216;run off&#8217; area be? Well that can be a bit subjective, but if you think about how the user will hold their device to use the controller then that should give you a good starting point.</p>
<p>For starters you don&#8217;t need it to cover a massive area &#8211; if fact I would say no more than a third of the width of the screen (working on the premise that the game is probably displayed as landscape). After all it&#8217;s unlikely the user will move their thumb a big distance given that they will be holding the device at the same time. If in doubt, test it out until you get a happy medium.</p>
<p>In the example below I have updated the <span style="font-family: 'Courier New';font-size: 14px">ThumbStick</span> symbol in Flash Professional by adding another layer called &#8216;Boundary&#8217; and placed a new 25&#215;25 pixel filled (red) circlular symbol in it. I gave it the instance name of boundary and made sure the layer was underneath all of the other layers in the <span style="font-family: 'Courier New';font-size: 14px">ThumbStick</span> symbol.</p>
<p>Obviously 25&#215;25 pixels in size isn&#8217;t going to be that useful. However once you map the symbol to a variable in your <span style="font-family: 'Courier New';font-size: 14px">ThumbStick()</span> class you can easily set its dimensions. That way you could easily add in a getter/setter that would allow you to dynamically set the dimensions of the controller. In the code below I&#8217;ve just set this boundary symbol&#8217;s width and height with hard values within the constructor.<br />
<pre><pre>private var _thumb&nbsp;&nbsp;&nbsp;&nbsp;:Sprite;
private var _surround&nbsp;&nbsp;:Sprite;
private var _boundary&nbsp;&nbsp;:Sprite;

private var _degrees&nbsp;&nbsp;:Number;
private var _radius&nbsp;&nbsp;:Number = 25;&nbsp;&nbsp;

public function ThumbStick()
{
&nbsp;&nbsp;_thumb = thumb;
&nbsp;&nbsp;_surround = surround;
&nbsp;&nbsp;_boundary = boundary;
&nbsp;&nbsp;_boundary.width = 200;
&nbsp;&nbsp;_boundary.height = 200;

&nbsp;&nbsp;if(Capabilities.cpuArchitecture == &quot;ARM&quot;)
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;_thumb.addEventListener(TouchEvent.TOUCH_BEGIN, onThumbDown);
&nbsp;&nbsp;&nbsp;&nbsp;_thumb.addEventListener(TouchEvent.TOUCH_END, onThumbUp);
&nbsp;&nbsp;&nbsp;&nbsp;_surround.addEventListener(TouchEvent.TOUCH_END, onThumbUp);
&nbsp;&nbsp;&nbsp;&nbsp;_boundary.addEventListener(TouchEvent.TOUCH_END, onThumbUp);
&nbsp;&nbsp;}
&nbsp;&nbsp;else
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;_thumb.addEventListener(MouseEvent.MOUSE_DOWN, onTestThumbDown);
&nbsp;&nbsp;&nbsp;&nbsp;_thumb.addEventListener(MouseEvent.MOUSE_UP, onTestThumbUp);
&nbsp;&nbsp;&nbsp;&nbsp;_surround.addEventListener(MouseEvent.MOUSE_UP, onTestThumbUp);
&nbsp;&nbsp;&nbsp;&nbsp;_boundary.addEventListener(MouseEvent.MOUSE_UP, onTestThumbUp);
&nbsp;&nbsp;}
}</pre></pre><br />
I&#8217;ve left the boundary symbols alpha value at 1 so you can see it (as shown in the SWF below &#8211; it&#8217;s the red circle :p). Try it out this time and even though you may drag off the actual thumb stick, as long as you are still over the boundary area it will still reset the thumb stick back to the centre once released. Obviously if the boundary area isn&#8217;t big enough you can enlarge it as you feel is necessary &#8211; it doesn&#8217;t even need to be circular. A rectangle would work just as well.</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="200" height="200" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="quality" value="high" /><param name="src" value="http://mochiland.com/wp-content/uploads/2011/04/VirtualControllers3.swf" /><embed type="application/x-shockwave-flash" width="200" height="200" src="http://mochiland.com/wp-content/uploads/2011/04/VirtualControllers3.swf" quality="high"></embed></object></p>
<p><strong>Summary</strong></p>
<p>In this article you got a good foundation in how to approach creating a virtual controller that can be used on touch based devices (and we&#8217;ll be looking in to that part of the process in more detail in part 2). You also saw how to restrict the movement of the thumb stick to within the actual controller surround; as well as what should happen if the user accidentally moves their thumb off of the thumb stick and then removes their thumb from the screen.</p>
<p>With the foundation set you can now leverage this to add further enhancements such as dispatching events when the user moves the controller so that you can wire it in to your game easily.</p>
<p>You can read the second part of this article here: <a href="http://blog.flashgen.com/gaming/general/virtual-controllers-for-touch-based-devices-pt-2/">Virtual Controllers For Touch Based Devices (pt.2)</a></p>
<p><strong>Related Files</strong></p>
<p><a href="http://http://blog.flashgen.com/files/gaming/VirtualController_pt1.zip">VirtualController_pt1.zip</a></p>
 <div class='series_toc'></div>	<p></p>
	<hr noshade style="margin:0;height:1px" />
	<p>&copy; MikeJones for <a href="http://mochiland.com">MochiLand</a>, 2011. |
	  <a href="http://mochiland.com/articles/virtual-controllers-for-touch-based-devices-pt-1">Permalink</a> |
	  <a href="http://mochiland.com/articles/virtual-controllers-for-touch-based-devices-pt-1#comments">6 comments</a></p>
	<p>Add to <a href="http://del.icio.us/post?url=http://mochiland.com/articles/virtual-controllers-for-touch-based-devices-pt-1&amp;title=Virtual Controllers for Touch-Based Devices pt. 1">del.icio.us</a></p>
	<p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mochiland.com/articles/virtual-controllers-for-touch-based-devices-pt-1" title="Search on Technorati">Technorati</a></p>
	<p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mochiland.com/category/mochiland/flash" title="View all posts in Flash Development" rel="category tag">Flash Development</a>, <a href="http://mochiland.com/category/mochiland/front-page" title="View all posts in Front Page" rel="category tag">Front Page</a>, <a href="http://mochiland.com/category/mochiland/gaming-news" title="View all posts in Industry News" rel="category tag">Industry News</a>, <a href="http://mochiland.com/category/mochiland" title="View all posts in MochiLand Topics" rel="category tag">MochiLand Topics</a>, <a href="http://mochiland.com/category/mochiland/tutorials" title="View all posts in Tutorials" rel="category tag">Tutorials</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://mochiland.com/articles/virtual-controllers-for-touch-based-devices-pt-1/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Flash Game Developer Tips by Photon Storm!</title>
		<link>http://mochiland.com/articles/flash-game-developer-tips-by-photon-storm</link>
		<comments>http://mochiland.com/articles/flash-game-developer-tips-by-photon-storm#comments</comments>
		<pubDate>Wed, 06 Apr 2011 17:08:53 +0000</pubDate>
		<dc:creator>Photon Storm</dc:creator>
				<category><![CDATA[Flash Development]]></category>
		<category><![CDATA[MochiLand Topics]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Flixel]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[Photon Storm]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://mochiland.com/?p=4608</guid>
		<description><![CDATA[Setting-up FlashDevelop and Flixel for the first time Last updated March 22nd 2011 for FlashDevelop version: 3.3.4 and Flixel version: 2.43 Hello World The popularity of Flixel attracts a lot of new developers. Often they are coming into it not from Flash, but from other languages and game making tools. There&#8217;s nothing wrong with this, [...]]]></description>
			<content:encoded><![CDATA[ <p><img class="alignnone size-full wp-image-1126" src="http://www.photonstorm.com/wp-content/uploads/2011/02/Flash-Game-Dev-Tips-Header.png" alt="" width="672" height="84" /></p>
<p><strong>Setting-up FlashDevelop and Flixel for the first time</strong></p>
<p>Last updated March 22nd 2011 for FlashDevelop version: 3.3.4 and Flixel version: 2.43</p>
<h2>Hello World</h2>
<p>The popularity of Flixel attracts a lot of new developers. Often they are coming into it not from Flash, but from other languages and game making tools. There&#8217;s nothing wrong with this, but it&#8217;s all too easy to fall at the first hurdle: simply getting a clean working development environment set-up. One which you can then hit &#8220;compile&#8221; and see the all-magic &#8220;Hello World&#8221; appear, rather than a blank white window or an error message.</p>
<p>There are lots of tutorials on setting-up FlashDevelop (see &#8220;Further Reading&#8221; for some), and a few on getting started with Flixel. But they are mostly out of date, and none of them marry the two things together. So I hope to address that in this tip. I&#8217;ll keep this tip updated as major new versions of FlashDevelop and Flixel are released.</p>
<h2>OS X dudes, take a different path</h2>
<p>First things first. You need to be using Windows. FlashDevelop IS coming for OS X soon. You can also <a href="http://www.flashdevelop.org/community/viewtopic.php?f=9&amp;t=2463">run it under Parallels</a>. And there are builds in development that you can download from the forum, but I&#8217;m not covering it here. Sorry if that excludes you. You&#8217;re welcome to come back and join us at the point where we integrate Flixel, as that will still be relevant to you.</p>
<p>Windows people, let&#8217;s continue &#8230;</p>
<h2>The Development Process</h2>
<p>It&#8217;s important you understand what each part of your development environment does. It&#8217;s easy to get mixed-up with terminology, and not really knowing where in the chain a certain piece fits. The following diagram shows the build process:</p>
<p><img class="alignnone size-full wp-image-1201" src="http://www.photonstorm.com/wp-content/uploads/2011/03/FlashDevelop-Cycle.png" alt="" width="600" height="280" /></p>
<p><strong>FlashDevelop</strong> is our editor, our IDE. It&#8217;s where you&#8217;ll type in all the code, and spend the majority of your time.</p>
<p>When it comes time to test your code, FlashDevelop will run a program called <em>mxmlc.exe</em> which is part of the <strong>Flex SDK</strong>. It&#8217;s the job of this program to grab everything it needs, and fire it off to the compiler. The main compiler is written in Java, hence the requirement for Java 1.6 to be installed on your PC.</p>
<p>At this point it&#8217;s very easy to get confused by the word &#8220;Flex&#8221; or even the involvement of Java. Traditionally Flex has been associated with Flex Builder, the Adobe application used for creating mostly dull and dry&#8230; sorry, for creating <em>rich</em> internet applications. Flex Builder uses MXML files to control UI layout amongst other things. Adobe since renamed it to FlashBuilder (I guess they get a kick out of confusing people). But you don&#8217;t need to care about this. All you need to know is that the <strong>Flex SDK</strong> contains the compiler, and that is what FlashDevelop uses. As for the presence of Java in this equation: the compiler is written in Java. But it doesn&#8217;t <em>create</em> java apps.</p>
<p>What the compiler does is to take your <strong>Flixel /†ActionScript 3 source code</strong> and process it.</p>
<p>Providing there were no errors, the compiler will have turned your code into machine code, and output a <strong>SWF file</strong>.</p>
<p>SWF files need to be loaded into <strong>Flash Player</strong> to be viewed. Most people are familiar with experiencing flash within their web browser, but you can (and will!) also download a stand-alone Flash Player to run SWF files from Windows, without needing a browser.</p>
<p>Once your SWF is loaded into Flash Player, your game will begin!</p>
<p>This whole process is repeated over and over during development. So let&#8217;s create this environment locally, and put together a set of flixel template files that will allow you to roll-out future projects quickly and easily.</p>
<h2>Downloading spree</h2>
<h3>Java 1.6</h3>
<p>As we&#8217;ve seen above the Flex SDK requires Java 1.6+ in order to compile. This isn&#8217;t standard with Windows, so unless you are a Java developer and have a Java IDE installed such as Eclipse or Netbeans, you&#8217;ll need to download and install Java first.</p>
<p>Go here: <a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">http://www.oracle.com/technetwork/java/javase/downloads/index.html</a></p>
<p>You need <strong>Java Platform SE</strong> (SE standards for &#8220;Standard Edition&#8221;). The current release is Java SE 6 Update 24, but any newer version should work just as well. Download the JRE (Java Runtime Environment) version. Get the 32-bit version, even if you run a 64-bit version of Windows. Install it and disable all of the annoying Sun Java update alerts.</p>
<h3>FlashDevelop</h3>
<p>Download and install <a href="http://www.flashdevelop.org/wikidocs/index.php?title=Main_Page">FlashDevelop</a>. I&#8217;m not going to screen shot the whole process, because to be fair if you can&#8217;t click &#8220;Next&#8221; a couple of times then you&#8217;re probably not yet ready to be coding games. The only important thing to note is this: <strong>When FlashDevelop asks you if you want to download and install the Flex SDK &#8211; <span style="text-decoration: underline;">SAY YES</span>!</strong></p>
<h3>Flash Player</h3>
<p>Flash Player is typically installed as a plug-in in most web browsers. This means if you drag a SWF file onto your browser, it should play. This is a less than ideal way to test your games though, so we&#8217;ll download the stand-alone Flash Player in two versions: Release and Debug.</p>
<p>Go here: <a href="http://www.adobe.com/support/flashplayer/downloads.html">http://www.adobe.com/support/flashplayer/downloads.html</a></p>
<p>and get these two files:</p>
<p><a href="http://download.macromedia.com/pub/flashplayer/updaters/10/flashplayer_10_sa_debug.exe">Download the Windows Flash Player 10.2 Projector content debugger</a> (EXE, 6.36MB) and<br />
<a href="http://download.macromedia.com/pub/flashplayer/updaters/10/flashplayer_10_sa.exe">Download the Windows Flash Player 10.2 Projector</a> (EXE, 5.37MB)</p>
<p>If a later version of Flash Player is available when you read this, get those instead.</p>
<p>You will now have 2 EXE files. Copy them somewhere you are not likely to accidentally delete them!</p>
<h3>Debug Player vs. Release Player</h3>
<p>If you drag a SWF file onto either of these new Flash Player exes it will open and play. The reason for getting both is that one is the <strong>Debug Player</strong>, and the other is the <strong>Release Player</strong>. The Debug Player (the one with _sa_debug in its name) contains the ability for your games to throw run-time errors, view redraw regions and report back to the Flash Debugger (fdb) that FlashDevelop uses. This last part is vital for your sanity, as it will report and catch errors during testing back to you (where-as Release Player typically fails silently).</p>
<p>You should always develop using the Debug Player, and then test it on the Release Player before you go live. Release Player will give you a free speed-boost, as it doesn&#8217;t contain all the debugging baggage, so it runs quicker. It&#8217;s also what the majority of people seeing flash on the web are using. If your game is running a little slow under Debug, it may well be fine under Release.</p>
<p><span style="font-size: 15px;font-weight: bold">Configure Flash Player to open SWFs</span></p>
<p>By default if you double-click a SWF file in Windows it&#8217;ll probably ask you to select a program to launch it with, or it may launch your browser. To change this behaviour so it opens SWFs with the Debug Player we just downloaded do the following:</p>
<p>Find a SWF file on your hard drive, <strong>right-click</strong> it and select <strong>Properties</strong>. You&#8217;ll see a properties window similar to this:</p>
<p><img class="alignnone size-full wp-image-1203" src="http://www.photonstorm.com/wp-content/uploads/2011/03/SWFassociation1.png" alt="" width="599" height="633" /></p>
<p>Click the &#8220;Change &#8230;&#8221; button to get this:</p>
<p><img class="alignnone size-full wp-image-1204" src="http://www.photonstorm.com/wp-content/uploads/2011/03/SWFassociation2.png" alt="" width="579" height="478" /></p>
<p>Click on the &#8220;Browse &#8230;&#8221; button and then select the Flash Player Debugger EXE you just downloaded. Make sure that &#8220;<em>Always use the selected program to open this kind of file</em>&#8221; is checked and click OK. Once you&#8217;re done it will look like the above window. And what&#8217;s more, double-clicking SWF files will now automatically launch them into Flash Player. Don&#8217;t worry, this action won&#8217;t change the way your web browser runs Flash. That will still work in the same way.</p>
<p><span style="font-size: 15px;font-weight: bold">flixel</span></p>
<p><img class="alignleft size-full wp-image-1205" src="http://www.photonstorm.com/wp-content/uploads/2011/03/github.png" alt="" width="123" height="63" />Adam (the creator of flixel) keeps the source code on github.com &#8211; a social source sharing site. There are 3 branches of flixel at the moment: Master, Dev and Beta. We&#8217;re only interested in the Beta branch, as it&#8217;s the most up to date and feature packed (at the time of writing). Don&#8217;t be overly concerned about the label &#8220;beta&#8221;, I&#8217;ve used it successfully in several large games without issue.</p>
<p>So point your browser to <a href="https://github.com/AdamAtomic/flixel/tree/beta">https://github.com/AdamAtomic/flixel/tree/beta</a></p>
<p>.. and click the big <strong>Downloads</strong> button, and pick the .zip package.</p>
<p>You&#8217;ll have a new file called<strong> AdamAtomic-flixel-719aabe.zip</strong> ready for use in a moment.</p>
<h2>Project Folder</h2>
<p>You now need to decide where you want to create all of your Flash projects.</p>
<p>I personally have a folder called &#8220;Flash&#8221; on one of my drives, which is then broken down into &#8220;Games&#8221;, &#8220;Demos&#8221;, and various other things. For the sake of this tutorial I&#8217;ll assume you have picked somewhere to keep all of your work, and I&#8217;ll refer to that place as your <em>Projects Folder</em>.</p>
<p>Extract the flixel zip file you just downloaded into your <em>Projects Folder</em>. It will create a new folder called <strong>AdamAtomic-flixel-719aabe &#8211; </strong>rename it to &#8220;<strong>flixel 2.43</strong>&#8221; (for your sanity more than anything). You&#8217;ve now got the latest release of flixel. In the <strong>docs</strong> folder you&#8217;ll find documentation. In the <strong>org</strong> folder is everything we&#8217;ll need in a moment when we create our first flixel project.</p>
<h2>Create your first FlashDevelop Project</h2>
<p><strong>Note:</strong> If you know that FlashDevelop works fine on your PC, skip this step.</p>
<p>If all went well earlier you should have a shiny new FlashDevelop icon on your desktop. Double-click it to get started and the &#8220;New Project&#8221; window should appear (if it doesn&#8217;t click on the <em>Project</em> menu item and pick <em>New</em>)</p>
<p>You&#8217;ll be presented with this:</p>
<p><img class="alignnone size-full wp-image-1206" src="http://www.photonstorm.com/wp-content/uploads/2011/03/flashdevelop-first-test.png" alt="" width="703" height="544" /></p>
<p>Select &#8220;AS3 Project&#8221;, give it a Name (I used &#8220;Testing Flex Compiles&#8221; above) and set the Location to be your Project Folder. Have &#8220;Create directory for project&#8221; ticked and click OK. It may now prompt you for some details, such as your name, fill them in once and it will remember them.</p>
<p>A big blank empty FlashDevelop project will open. In the Project window you should see 3 folders like below:</p>
<p><img class="alignnone size-full wp-image-1207" src="http://www.photonstorm.com/wp-content/uploads/2011/03/testing-flex.png" alt="" width="475" height="155" /></p>
<p>The folders are:</p>
<p><strong>bin</strong> &#8211; Where the final SWF file gets compiled to<br />
<strong>lib</strong> &#8211; You can put external compiled library files here, such as SWCs †(don&#8217;t put AS3 source code here)<br />
<strong>src</strong> &#8211; The source folder, where all of the project source code lives</p>
<p>I automatically create 3 new folders at the same level:</p>
<p><strong>assets</strong> &#8211; Where all assets that the game requires are placed, this is sub-foldered into <em>graphics</em>, <em>music</em>, <em>maps</em> and <em>sound</em><br />
<strong>dame</strong> &#8211; When creating level tilemaps I always use the map editor DAME, and store my tilemaps here<br />
<strong>psds</strong> &#8211; High-res and working graphics are stored here. I call it psds only because I use Photoshop</p>
<p>Obviously you don&#8217;t have to create these 3 folders, but I would strongly recommend you do create an <em>assets</em> folder. The difference between <em>assets</em> and <em>psds</em> is that <em>psds</em> for me contains all of the work in progress graphics and Photoshop files. Where-as <em>assets</em> contains the PNGs <span style="text-decoration: underline;">exported</span> out of Photoshop, for final use in my game.</p>
<p>If you expand the <em>src</em> folder you&#8217;ll find a file called <strong>Main.as</strong> inside. Double-click it to edit it.</p>
<p>This file is created automatically by FlashDevelop and it&#8217;s where your journey with flash coding begins. We want to see if the Flex compiler is working, so we&#8217;ll add in a couple of lines of code. Modify the init function so it looks like this:<br />
<pre>[[code]]czoyNjY6XCINCnByaXZhdGUgZnVuY3Rpb24gaW5pdChlOkV2ZW50ID0gbnVsbCk6dm9pZA0Kew0KCXJlbW92ZUV2ZW50TGlzdGVuZXJ7WyYqJl19KEV2ZW50LkFEREVEX1RPX1NUQUdFLCBpbml0KTsNCgkvLyBlbnRyeSBwb2ludA0KDQoJdmFyIHJlZGJveDpTcHJpdGUgPSBuZXcgU3tbJiomXX1wcml0ZTsNCg0KCXJlZGJveC5ncmFwaGljcy5iZWdpbkZpbGwoMHhGRjAwMDAsIDEpOw0KCXJlZGJveC5ncmFwaGljcy5kcmF3UmVje1smKiZdfXQoMCwgMCwgMTAwLCAxMDApOw0KDQoJYWRkQ2hpbGQocmVkYm94KTsNCn0NClwiO3tbJiomXX0=[[/code]]</pre><br />
What we are doing here is creating a new Sprite called redbox, and making it look like a 100&#215;100 pixel sized red square. We then add it to the display list so it renders.</p>
<p>Press F5 and FlashDevelop will compile and try to run your SWF.</p>
<p>If all has gone well (no typos, no set-up mistakes, etc) then Flash Player will open with an 800&#215;600 sized white window, with a red square in the top left of it. Ok so it&#8217;s hardly Super Mario, but everyone has to start somewhere right?!</p>
<h2>Your first flixel project</h2>
<p>Create a new FlashDevelop project, but this time pick the Project type &#8220;AS3 Project with Preloader&#8221;, call it &#8220;Goodbye Cruel World&#8221; and click OK.</p>
<p>The same 3 folders as before will be created. Create any extra ones you need (hint: assets!)</p>
<p>Before we code anything we&#8217;re going to configure the Project Properties. To do this click on the <strong>Project</strong> menu and pick <strong>Properties</strong>. Change the Dimensions to 640 x 480, the Background colour to black and the Framerate to 60 fps. Also ensure that the Target is Flash Player 10 and in the Test Movie panel it says &#8220;Play (FlashViewer default)&#8221;. It should look like this:</p>
<p><img src="http://www.photonstorm.com/wp-content/uploads/2011/03/projectproperties.png" alt="" width="484" height="494" /></p>
<p>This told FlashDevelop that we want our game to be 640&#215;480 in size, have a black background by default, run at 60 frames per second and that we&#8217;ll test it in the Flash Player associated with SWF files (which we set-up earlier).</p>
<p>Now remember that zip file we downloaded from github? The one actually containing flixel? Go back to where you extracted it and copy the <strong>org</strong> folder into the <strong>src</strong> folder of your new project. Do NOT include docs, build_docs.sh, flx.py or license.txt. Just take the org folder and nothing else.</p>
<p>If you do this correctly the Project structure window in FlashDevelop will look like this:</p>
<p><img class="alignnone size-full wp-image-1208" src="http://www.photonstorm.com/wp-content/uploads/2011/03/goodbye.png" alt="" width="250" height="589" /></p>
<p>It&#8217;s very important to note the structure here. The <strong>org</strong> folder is sitting at the root (the top) of the <strong>src</strong> folder. This is vital. If you have it located anywhere else it&#8217;ll fail to compile. In the shot above I&#8217;ve expanded out the <strong>org/flixel</strong> folder so you can see the contents. You&#8217;ll also notice the <strong>Main.as</strong> file as before, and a new <strong>Preloader.as</strong> file.</p>
<p>Right-click the src folder and select &#8220;Add &#8211; New Class &#8230;&#8221;:</p>
<p><img class="alignnone size-full wp-image-1209" src="http://www.photonstorm.com/wp-content/uploads/2011/03/newclass.png" alt="" width="426" height="436" /></p>
<p>In the panel that appears ignore everything other than the <strong>Name</strong> (which will default to <em>NewClass</em>). Change it to <strong>FirstGame</strong> and click OK.</p>
<p>Repeat this process again, only instead of calling the class FirstGame, call it <strong>MenuState</strong>.</p>
<p>Repeat the process one final time, and call it <strong>PlayState</strong>.</p>
<p>By the end of this our Project window has the following files, all in the correct place:</p>
<p><img class="alignnone size-full wp-image-1212" src="http://www.photonstorm.com/wp-content/uploads/2011/03/filesreadyforcode.png" alt="" width="221" height="245" /></p>
<p>Before we dive in, it&#8217;s worth talking about how flixel structures itself. This is best illustrated with a diagram:</p>
<p><img class="alignnone size-full wp-image-1213" src="http://www.photonstorm.com/wp-content/uploads/2011/03/flixelrelationship1.png" alt="" width="169" height="467" /></p>
<p>When the SWF is loaded into Flash Player the code in the Preloader will run. This will carry on running until the SWF has fully loaded.</p>
<p>At this point the class Main gets created.</p>
<p>Main in turn creates a new class: FlxGame &#8211; this is the start-up class for flixel, it tells flixel what size your game is, if it should be zoomed and which state to run by default.</p>
<p>FlxGame then calls the FlxState you nominated, and things kick off from here.</p>
<p>Technically Main and FlxGame can be bundled into the same class file. I don&#8217;t like to do this personally as I&#8217;ll explain in a future tutorial, but there&#8217;s nothing wrong in doing so.</p>
<p>Right, time to put some code into those files we created!</p>
<h3>Edit Main.as</h3>
<p>Open Main.as in FlashDevelop and edit it to this:<br />
<pre>[[code]]czo0NTY6XCINCnBhY2thZ2UNCnsNCglpbXBvcnQgZmxhc2guZGlzcGxheS5TcHJpdGU7DQoJaW1wb3J0IGZsYXNoLmV2ZW50cy5FdmV7WyYqJl19bnQ7DQoNCglbRnJhbWUoZmFjdG9yeUNsYXNzPVwiUHJlbG9hZGVyXCIpXQ0KDQoJcHVibGljIGNsYXNzIE1haW4gZXh0ZW5kcyBTcHJpe1smKiZdfXRlDQoJew0KDQoJCXB1YmxpYyBmdW5jdGlvbiBNYWluKCk6dm9pZA0KCQl7DQoJCQlpZiAoc3RhZ2UpIGluaXQoKTsNCgkJCWVsc2V7WyYqJl19IGFkZEV2ZW50TGlzdGVuZXIoRXZlbnQuQURERURfVE9fU1RBR0UsIGluaXQpOw0KCQl9DQoNCgkJcHJpdmF0ZSBmdW5jdGlvbiBpbntbJiomXX1pdChlOkV2ZW50ID0gbnVsbCk6dm9pZA0KCQl7DQoJCQlyZW1vdmVFdmVudExpc3RlbmVyKEV2ZW50LkFEREVEX1RPX1NUQUdFLCBpe1smKiZdfW5pdCk7DQoNCgkJCXZhciBnYW1lOkZpcnN0R2FtZSA9IG5ldyBGaXJzdEdhbWU7DQoNCgkJCWFkZENoaWxkKGdhbWUpOw0KCQl9DQp7WyYqJl19CX0NCn0NClwiO3tbJiomXX0=[[/code]]</pre><br />
This is the default file as created by FlashDevelop, with two extra lines. It waits for the pre-loader to have finished and then it creates a new local variable called game, which is an instance of the FirstGame class. This is then added to the display list.</p>
<p>In short, it&#8217;s just made your flixel game visible.</p>
<h3>Edit FirstGame.as</h3>
<p>Open FirstGame.as in FlashDevelop and edit it to this:<br />
<pre>[[code]]czoxNzM6XCINCnBhY2thZ2UNCnsNCglpbXBvcnQgb3JnLmZsaXhlbC5GbHhHYW1lOw0KDQoJcHVibGljIGNsYXNzIEZpcnN0R2FtZSB7WyYqJl19ZXh0ZW5kcyBGbHhHYW1lDQoJew0KCQlwdWJsaWMgZnVuY3Rpb24gRmlyc3RHYW1lKCkNCgkJew0KCQkJc3VwZXIoMzIwLCAyNDAsIHtbJiomXX1NZW51U3RhdGUsIDIpOw0KCQl9DQoJfQ0KfQ0KXCI7e1smKiZdfQ==[[/code]]</pre><br />
Quite simply this creates our FlxGame instance. The call to super takes 4 parameters. The width and height of our game, the FlxState to call when FlxGame is ready, and the zoom level we&#8217;re running at.</p>
<p>In this case our SWF dimensions were 640&#215;480, but the size set here is 320&#215;240 with a zoom level of 2. So basically every pixel is going to appear doubled-up in size, for that 8-bit retro style ;) MenuState is the FlxState that will be called first.</p>
<h3>Edit MenuState.as</h3>
<p>Open MenuState.as in FlashDevelop and edit it to this:<br />
<pre>[[code]]czo3MDE6XCINCnBhY2thZ2UNCnsNCglpbXBvcnQgb3JnLmZsaXhlbC4qOw0KDQoJcHVibGljIGNsYXNzIE1lbnVTdGF0ZSBleHRlbmR7WyYqJl19cyBGbHhTdGF0ZQ0KCXsNCgkJcHJpdmF0ZSB2YXIgZ2FtZVRpdGxlOkZseFRleHQ7DQoJCXByaXZhdGUgdmFyIHN0YXJ0R2FtZUJ1dHtbJiomXX10b246Rmx4QnV0dG9uOw0KDQoJCXB1YmxpYyBmdW5jdGlvbiBNZW51U3RhdGUoKQ0KCQl7DQoJCQlzdXBlcigpOw0KCQl9DQoNCgkJe1smKiZdfW92ZXJyaWRlIHB1YmxpYyBmdW5jdGlvbiBjcmVhdGUoKTp2b2lkDQoJCXsNCgkJCXN1cGVyLmNyZWF0ZSgpOw0KDQoJCQlGbHhHLm17WyYqJl19b3VzZS5zaG93KCk7DQoNCgkJCWdhbWVUaXRsZSA9IG5ldyBGbHhUZXh0KDAsIDE2LCAzMjAsIFwiTXkgRmlyc3QgR2FtZSFcIik7DQoJe1smKiZdfQkJZ2FtZVRpdGxlLmFsaWdubWVudCA9IFwiY2VudGVyXCI7DQoNCgkJCXN0YXJ0R2FtZUJ1dHRvbiA9IG5ldyBGbHhCdXR0b24oMTEwLHtbJiomXX0gOTAsIHN0YXJ0R2FtZSk7DQoJCQlzdGFydEdhbWVCdXR0b24ubG9hZFRleHQobmV3IEZseFRleHQoMCwgMCwgMTAwLCBcIlN0YXJ0IHtbJiomXX1HYW1lXCIpKTsNCg0KCQkJYWRkKGdhbWVUaXRsZSk7DQoJCQlhZGQoc3RhcnRHYW1lQnV0dG9uKTsNCgkJfQ0KDQoJCXByaXZhdGUgZntbJiomXX11bmN0aW9uIHN0YXJ0R2FtZSgpOnZvaWQNCgkJew0KCQkJRmx4Ry5tb3VzZS5oaWRlKCk7DQoNCgkJCUZseEcuc3RhdGUgPSBuZXcge1smKiZdfVBsYXlTdGF0ZTsNCgkJfQ0KCX0NCn0NClwiO3tbJiomXX0=[[/code]]</pre><br />
This looks more complicated, but all it&#8217;s actually doing is creating a title, and a button that the user can click to start the game. FlxStates have a function called create which you must over-ride and then fill with your code. In our case we&#8217;ve told flixel to show the mouse pointer, create a game title text field and create a start game button.</p>
<p>Look at the flixel documentation for details on what all of these things do. Or, as you&#8217;re now cooking with the power of FlashDevelop you can get context-sensitive help on most flixel functions by simply clicking inside of the braces and pressing CTRL+SHIFT+SPACE. You&#8217;ll see a pop-up like this, and as you move along through the parameters it&#8217;ll tell you what each of them do.</p>
<p><img class="alignnone size-full wp-image-1214" src="http://www.photonstorm.com/wp-content/uploads/2011/03/codeinsight.png" alt="" width="667" height="101" /></p>
<h3>Edit PlayState.as</h3>
<p>Open PlayState.as in FlashDevelop and edit it to this:<br />
<pre>[[code]]czozMDY6XCINCnBhY2thZ2UNCnsNCglpbXBvcnQgb3JnLmZsaXhlbC4qOw0KDQoJcHVibGljIGNsYXNzIFBsYXlTdGF0ZSBleHRlbmR7WyYqJl19cyBGbHhTdGF0ZQ0KCXsNCgkJcHJpdmF0ZSB2YXIgcGxheWVyOkZseFNwcml0ZTsNCg0KCQlwdWJsaWMgZnVuY3Rpb24gUGxheVN0YXtbJiomXX10ZSgpDQoJCXsNCgkJCXN1cGVyKCk7DQoJCX0NCg0KCQlvdmVycmlkZSBwdWJsaWMgZnVuY3Rpb24gY3JlYXRlKCk6dm9pZA0KCQl7e1smKiZdfQ0KCQkJc3VwZXIuY3JlYXRlKCk7DQoNCgkJCXBsYXllciA9IG5ldyBGbHhTcHJpdGUoKTsNCg0KCQkJYWRkKHBsYXllcik7DQoJCX17WyYqJl19DQoJfQ0KfQ0KXCI7e1smKiZdfQ==[[/code]]</pre><br />
Here we create one single FlxSprite called player and add it up for display. As we&#8217;ve not loaded in any graphics this will appear as a white 8&#215;8 sized square. It&#8217;s not much, but it lets us know that our MenuState is hooked to our PlayState correctly.</p>
<h2>F5 = The moment of truth</h2>
<p>Press F5 to test your game.</p>
<p>FlashDevelop will invoke the compiler and if all was well, it&#8217;ll launch Flash Player with your SWF in. Hopefully you&#8217;ll see this:</p>
<p><img class="alignnone size-full wp-image-1215" src="http://www.photonstorm.com/wp-content/uploads/2011/03/firstgame.png" alt="" width="640" height="480" /></p>
<p>And clicking Start Game will show a black screen with a small white square in it.</p>
<p>Success! You&#8217;ve just compiled and launched your first flixel project :)</p>
<p>You&#8217;ve also got a good FlashDevelop template here to use for future games. Just copy the whole project folder, rename it and you&#8217;re good to go once more.</p>
<p>Before you know it you&#8217;ll be creating games like this too:</p>
<p><a href="http://www.photonstorm.com/games/"><img class="alignnone size-full wp-image-1216" src="http://www.photonstorm.com/wp-content/uploads/2011/03/sshot-2010-02-06-01-10-37.png" alt="" width="640" height="480" /></a></p>
<p>:)</p>
<p>If you have any trouble with any part of this tutorial, please post about it on the official Flixel forums: <a href="http://flixel.org/forums">http://flixel.org/forums</a></p>
<h2>Further Reading</h2>
<p>To learn about the Flex 3 compiler design there is a great <a href="http://opensource.adobe.com/wiki/display/flexsdk/Flex+3+Compiler+Design">white-paper</a> on the Adobe site.</p>
<p>To understand how mxmlc works, read <a href="http://www.senocular.com/flash/tutorials/as3withmxmlc/">this article by Senocular</a>.</p>
<p><a href="http://active.tutsplus.com/tutorials/beginners-guide-to-flashdevelop-intro-basix/">A Beginners Guide to FlashDevelop</a> is a great article if you want to learn more about what FlashDevelop can do.</p>
<p>And the <a title="Flash Game Dev Tips Google Code Project" href="http://code.google.com/p/flash-game-dev-tips/" target="_blank">Flash Game Dev Tips Google Code Project page</a> contains lots of other tips you can use with your new-found flixel skills.</p>
 <div class='series_toc'></div>	<p></p>
	<hr noshade style="margin:0;height:1px" />
	<p>&copy; Photon Storm for <a href="http://mochiland.com">MochiLand</a>, 2011. |
	  <a href="http://mochiland.com/articles/flash-game-developer-tips-by-photon-storm">Permalink</a> |
	  <a href="http://mochiland.com/articles/flash-game-developer-tips-by-photon-storm#comments">4 comments</a></p>
	<p>Add to <a href="http://del.icio.us/post?url=http://mochiland.com/articles/flash-game-developer-tips-by-photon-storm&amp;title=Flash Game Developer Tips by Photon Storm!">del.icio.us</a></p>
	<p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mochiland.com/articles/flash-game-developer-tips-by-photon-storm" title="Search on Technorati">Technorati</a></p>
	<p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mochiland.com/category/mochiland/flash" title="View all posts in Flash Development" rel="category tag">Flash Development</a>, <a href="http://mochiland.com/category/mochiland" title="View all posts in MochiLand Topics" rel="category tag">MochiLand Topics</a>, <a href="http://mochiland.com/category/mochiland/tutorials" title="View all posts in Tutorials" rel="category tag">Tutorials</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://mochiland.com/articles/flash-game-developer-tips-by-photon-storm/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Session Recordings from Flash Gaming Summit</title>
		<link>http://mochiland.com/articles/session-recordings-from-flash-gaming-summit</link>
		<comments>http://mochiland.com/articles/session-recordings-from-flash-gaming-summit#comments</comments>
		<pubDate>Tue, 09 Mar 2010 21:15:13 +0000</pubDate>
		<dc:creator>ada</dc:creator>
				<category><![CDATA[Flash Development]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[Games Industry]]></category>
		<category><![CDATA[Industry News]]></category>
		<category><![CDATA[Mochi Media Announcements]]></category>
		<category><![CDATA[MochiLand Topics]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://mochiland.com/?p=2837</guid>
		<description><![CDATA[Thanks to everyone who joined us at Flash Gaming Summit to make it a success! We had over 500 attendees registered who participated in two tracks. Visit the Flash Gaming Summit Website for the full program details. Robertson Auditorium Presentations Opening Keynote &#8211; Jameson Hsu, CEO, Mochi Media Panel: 4 keys to a successful social [...]]]></description>
			<content:encoded><![CDATA[ <p>Thanks to everyone who joined us at Flash Gaming Summit to make it a success! We had over 500 attendees registered who participated in two tracks. Visit <a href="http://www.flashgamingsummit.com/program.html">the Flash Gaming Summit Website for the full program details</a>.</p>
<h4>Robertson Auditorium Presentations</h4>
<ul>
<li><a href="http://partners.adobe.acrobat.com/p45641756/">Opening Keynote &#8211; Jameson Hsu, CEO, Mochi Media</a></li>
<li><a href="http://partners.adobe.acrobat.com/p14597651/">Panel: 4 keys to a successful social game</a></li>
<li><a href="http://partners.adobe.acrobat.com/p15717763/">Panel: Money in Flash: Next Gen Monetization Games</a></li>
<li><a href="http://partners.adobe.acrobat.com/p24978689/">Adobe Tools and Services for Flash Games</a> </li>
<li><a href="http://partners.adobe.acrobat.com/p88075999/">The Mochis Award Show</a></li>
<li><a href="http://partners.adobe.acrobat.com/p86895881/">Panel: Monetizing Your Game Outside of Sponsorship</a></li>
<li><a href="http://partners.adobe.acrobat.com/p37769968/">Panel: Everything About Sponsorship &amp; Licensing</a></li>
<li><a href="http://partners.adobe.acrobat.com/p72234415/">The ups and downs of getting a premium flash game to success</a></li>
</ul>
<h4>Fisher Room Presentations</h4>
<ul>
<li><a href="http://partners.adobe.acrobat.com/p22458437/">Canabalt: Behind the Skyline</a></li>
<li><a href="http://partners.adobe.acrobat.com/p39181110/">From Bedroom to Medium Sized Office&#8230; The Nitrome Story</a></li>
<li><a href="http://partners.adobe.acrobat.com/p26778869/">PushButton Engine 101</a></li>
<li><a href="http://partners.adobe.acrobat.com/p24858677/">Flash Games Invade the 3rd Dimension! Papervision3D</a></li>
<li><a href="http://partners.adobe.acrobat.com/p90486240/">haXe for Flash Game Developers</a></li>
<li><a href="http://partners.adobe.acrobat.com/p39611153/">Boxhead (unreleased) Deconstruction</a></li>
</ul>
<h4>Photos</h4>
<p><a href="http://www.flickr.com/photos/mochimedia/sets/72157623579282114/show/">Hop on over to Flickr</a> or watch the slide slow below. </p>
<p><object width="500" height="375"><param name="flashvars" value="offsite=true&#038;lang=en-us&#038;page_show_url=%2Fphotos%2Fmochimedia%2Fsets%2F72157623579282114%2Fshow%2F&#038;page_show_back_url=%2Fphotos%2Fmochimedia%2Fsets%2F72157623579282114%2F&#038;set_id=72157623579282114&#038;jump_to="></param><param name="movie" value="http://www.flickr.com/apps/slideshow/show.swf?v=71649"></param><param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/slideshow/show.swf?v=71649" allowFullScreen="true" flashvars="offsite=true&#038;lang=en-us&#038;page_show_url=%2Fphotos%2Fmochimedia%2Fsets%2F72157623579282114%2Fshow%2F&#038;page_show_back_url=%2Fphotos%2Fmochimedia%2Fsets%2F72157623579282114%2F&#038;set_id=72157623579282114&#038;jump_to=" width="500" height="375"></embed></object></p>
<h4>Coverage</h4>
<ul>
<li><a href="http://sebleedelisle.com/2010/03/3d-engine-in-10-lines/">3D engine in 10 lines*</a> &#8211; Seb Lee-Delisle&#8217;s 3D Particle code from FGS preso</li>
<li><a href="http://freetoplay.biz/2010/03/08/monetizing-your-game-outside-of-sponsorship-flash-gaming-summit/">Monetizing Your Game Outside of Sponsorship</a> &#8211; Great panel transcript and recap by Adrian Crook of FreeToPlay.Biz</li>
<li><a href="http://www.gamedev.net/community/forums/mod/journal/journal.asp?jn=254106&#3616574">Haxe for Flash Development</a> &#8211; GameDev.net</li>
<li><a href="http://www.gamedev.net/community/forums/mod/journal/journal.asp?jn=254106&#3616573">Adobe Tools and Services for Flash Games</a> &#8211; GameDev.net</li>
<li><a href="http://www.gamedev.net/community/forums/mod/journal/journal.asp?jn=254106&#3616570">Pushbutton Engine 101 &#8211; Ben Garney</a> &#8211; GameDev.net</li>
<li><a href="http://www.gamedev.net/community/forums/mod/journal/journal.asp?jn=254106&#3616569">Panel &#8211; 4 Keys to a Successful Social Game that All Game Developers Should Know</a> &#8211; GameDev.net</li>
<li><a href="http://www.stumbleupon.com/su/2Ry9Nx/blog.addictinggames.com/2010/03/flash-gaming-summit-the-first-three-hours//r:t">Addicting News: Flash Gaming Summit (The First Three Hours)</a> &#8211; AddictingGames.com blog</li>
<li><a href="http://www.untoldentertainment.com/blog/2010/03/09/flash-gaming-summit-2010/">Flash Gaming Summit 2010</a> &#8211; Untold Entertainment</li>
<li><a href="http://www.8bitrocket.com/newsdisplay.aspx?newspage=37596">Flash Gaming Summit Report #1: The Longest Day</a> &#8211; 8bitrocket</li>
<li><a href="http://www.gamezebo.com/news/2010/03/08/top-flash-games-honored-mochis-2010">Top Flash games honored at The Mochis 2010</a> &#8211; Gamezebo</li>
<li><a href="http://www.socialtimes.com/2010/03/the-7-elements-of-social-game-success/">The 7 Elements Of Social Game Success</a> &#8211; Social Times </li>
<li><a href="http://www.8bitrocket.com/newsdisplay.aspx?newspage=37768">Flash Indie Game Interweb Mash-up: March 9, 2010: Flash Gaming Summit Edition</a> &#8211; 8bitrocket</li>
<li><a href="http://tumblog.activeden.net/post/440811690/so-how-was-the-flash-gaming-summit">So, How Was The Flash Gaming Summit?</a> &#8211; ActiveDen</li>
<li><a href="http://jmtb02.com/2010/03/10/flash-gaming-summit-retrospect/">Flash Gaming Summit: Retrospect</a> &#8211; Jmtb02</li>
</ul>
<p>Until next year, <a href="http://twitter.com/fgsummit">follow @fgsummit on Twitter</a> and also subscribe for updates to the mailing list (right side-bar) on the <a href="http://www.flashgamingsummit.com">Flash Gaming Summit website</a>.</p>
 <div class='series_toc'></div>	<p></p>
	<hr noshade style="margin:0;height:1px" />
	<p>&copy; ada for <a href="http://mochiland.com">MochiLand</a>, 2010. |
	  <a href="http://mochiland.com/articles/session-recordings-from-flash-gaming-summit">Permalink</a> |
	  <a href="http://mochiland.com/articles/session-recordings-from-flash-gaming-summit#comments">10 comments</a></p>
	<p>Add to <a href="http://del.icio.us/post?url=http://mochiland.com/articles/session-recordings-from-flash-gaming-summit&amp;title=Session Recordings from Flash Gaming Summit">del.icio.us</a></p>
	<p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mochiland.com/articles/session-recordings-from-flash-gaming-summit" title="Search on Technorati">Technorati</a></p>
	<p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mochiland.com/category/mochiland/flash" title="View all posts in Flash Development" rel="category tag">Flash Development</a>, <a href="http://mochiland.com/category/mochiland/game-design" title="View all posts in Game Design" rel="category tag">Game Design</a>, <a href="http://mochiland.com/category/mochiland/games-industry" title="View all posts in Games Industry" rel="category tag">Games Industry</a>, <a href="http://mochiland.com/category/mochiland/gaming-news" title="View all posts in Industry News" rel="category tag">Industry News</a>, <a href="http://mochiland.com/category/mochiland/mochi-media-announcements" title="View all posts in Mochi Media Announcements" rel="category tag">Mochi Media Announcements</a>, <a href="http://mochiland.com/category/mochiland" title="View all posts in MochiLand Topics" rel="category tag">MochiLand Topics</a>, <a href="http://mochiland.com/category/mochiland/tutorials" title="View all posts in Tutorials" rel="category tag">Tutorials</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://mochiland.com/articles/session-recordings-from-flash-gaming-summit/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>The work scheme of an independent Flash developer</title>
		<link>http://mochiland.com/articles/the-work-scheme-of-an-independent-flash-developer</link>
		<comments>http://mochiland.com/articles/the-work-scheme-of-an-independent-flash-developer#comments</comments>
		<pubDate>Mon, 24 Aug 2009 17:06:59 +0000</pubDate>
		<dc:creator>Vadim Starygin</dc:creator>
				<category><![CDATA[Games Industry]]></category>
		<category><![CDATA[MochiLand Topics]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://mochiland.com/?p=1831</guid>
		<description><![CDATA[In consideration of all the feedback which this post has generated, we have decided to remove it from the MochiLand blog. While it&#8217;s great to have opinions heard and a perspective from another side of development &#8212; at the same time, we don&#8217;t want to be in the position of being seen as endorsing game [...]]]></description>
			<content:encoded><![CDATA[ <p><em>In consideration of all the feedback which this post has generated, we have decided to remove it from the MochiLand blog. While it&#8217;s great to have opinions heard and a perspective from another side of development &#8212; at the same time, we don&#8217;t want to be in the position of being seen as endorsing game development practices that are in the gray area. It&#8217;s clear from all the feedback (thanks!) on the article that people&#8217;s expectations of MochiLand content are high, and we aim to deliver.</em></p>
 <div class='series_toc'></div>	<p></p>
	<hr noshade style="margin:0;height:1px" />
	<p>&copy; Vadim Starygin for <a href="http://mochiland.com">MochiLand</a>, 2009. |
	  <a href="http://mochiland.com/articles/the-work-scheme-of-an-independent-flash-developer">Permalink</a> |
	  <a href="http://mochiland.com/articles/the-work-scheme-of-an-independent-flash-developer#comments">28 comments</a></p>
	<p>Add to <a href="http://del.icio.us/post?url=http://mochiland.com/articles/the-work-scheme-of-an-independent-flash-developer&amp;title=The work scheme of an independent Flash developer">del.icio.us</a></p>
	<p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mochiland.com/articles/the-work-scheme-of-an-independent-flash-developer" title="Search on Technorati">Technorati</a></p>
	<p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mochiland.com/category/mochiland/games-industry" title="View all posts in Games Industry" rel="category tag">Games Industry</a>, <a href="http://mochiland.com/category/mochiland" title="View all posts in MochiLand Topics" rel="category tag">MochiLand Topics</a>, <a href="http://mochiland.com/category/mochiland/tutorials" title="View all posts in Tutorials" rel="category tag">Tutorials</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://mochiland.com/articles/the-work-scheme-of-an-independent-flash-developer/feed</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>Triqui&#8217;s WordPress Plugin for MochiAds Released!</title>
		<link>http://mochiland.com/articles/triquis-wordpress-plugin-for-mochiads-released</link>
		<comments>http://mochiland.com/articles/triquis-wordpress-plugin-for-mochiads-released#comments</comments>
		<pubDate>Mon, 29 Jun 2009 18:11:01 +0000</pubDate>
		<dc:creator>ada</dc:creator>
				<category><![CDATA[Flash Development]]></category>
		<category><![CDATA[MochiLand Topics]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://mochiland.com/?p=1530</guid>
		<description><![CDATA[Haven&#8217;t you always wanted your own game arcade? It&#8217;s always exciting to see members of our community innovating and helping each other out. That&#8217;s why I wanted to give a little plug to encourage you guys to check out the latest creation of Emanuele Feronato, who by the way also writes an excellent Flash games [...]]]></description>
			<content:encoded><![CDATA[ <p>Haven&#8217;t you always wanted your own game arcade? It&#8217;s always exciting to see members of our community innovating and helping each other out.  That&#8217;s why I wanted to give a little plug to encourage you guys to check out the latest creation of Emanuele Feronato, who by the way also writes an <a href="http://www.emanueleferonato.com/">excellent Flash games blog</a>. Emanuele has recently announced the release of a WordPress plugin that enables anybody to create a Flash games arcade using the MochiAds game feed.  I&#8217;ve included his teaser video below for you guys to check out.</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/2zuG7D0hQ88&#038;hl=en&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/2zuG7D0hQ88&#038;hl=en&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
<p>Visit the <a href="http://www.emanueleferonato.com/triqui-mochiads-arcade-plugin-for-wordpress-official-page/">Triqui MochiAds Arcade plugin for WordPress official page</a> for more details. Note that this plugin was independently developed (kudos to Emanuele!) so please direct any questions you have about it to him. </p>
 <div class='series_toc'></div>	<p></p>
	<hr noshade style="margin:0;height:1px" />
	<p>&copy; ada for <a href="http://mochiland.com">MochiLand</a>, 2009. |
	  <a href="http://mochiland.com/articles/triquis-wordpress-plugin-for-mochiads-released">Permalink</a> |
	  <a href="http://mochiland.com/articles/triquis-wordpress-plugin-for-mochiads-released#comments">8 comments</a></p>
	<p>Add to <a href="http://del.icio.us/post?url=http://mochiland.com/articles/triquis-wordpress-plugin-for-mochiads-released&amp;title=Triqui&#8217;s WordPress Plugin for MochiAds Released!">del.icio.us</a></p>
	<p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mochiland.com/articles/triquis-wordpress-plugin-for-mochiads-released" title="Search on Technorati">Technorati</a></p>
	<p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mochiland.com/category/mochiland/flash" title="View all posts in Flash Development" rel="category tag">Flash Development</a>, <a href="http://mochiland.com/category/mochiland" title="View all posts in MochiLand Topics" rel="category tag">MochiLand Topics</a>, <a href="http://mochiland.com/category/mochiland/tutorials" title="View all posts in Tutorials" rel="category tag">Tutorials</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://mochiland.com/articles/triquis-wordpress-plugin-for-mochiads-released/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Building a WordPress Flash Games Portal &#8211; Gambrinous Games</title>
		<link>http://mochiland.com/articles/building-a-wordpress-flash-games-portal-gambrinous-games</link>
		<comments>http://mochiland.com/articles/building-a-wordpress-flash-games-portal-gambrinous-games#comments</comments>
		<pubDate>Tue, 19 May 2009 17:46:15 +0000</pubDate>
		<dc:creator>Peter McKenna</dc:creator>
				<category><![CDATA[Art / Visual Design]]></category>
		<category><![CDATA[MochiLand Topics]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://mochiland.com/?p=1440</guid>
		<description><![CDATA[Recently, we announced the launch of our brand new game portal, GambrinousGames.com. I&#8217;d like to take a few minutes of your time to talk to you about how we launched our portal in record time, and how this same process can be quick and painless for you too. Gather &#8217;round everyone and let me guide [...]]]></description>
			<content:encoded><![CDATA[ <p>Recently, we announced the launch of our brand new game portal, <a href="http://www.GambrinousGames.com">GambrinousGames.com</a>. I&#8217;d like to take a few minutes of your time to talk to you about how we launched our portal in record time, and how this same process can be quick and painless for you too. Gather &#8217;round everyone and let me guide you through our journey to Game Portaldom.</p>
<h2>What were we trying to achieve?</h2>
<p>The game portal we wanted to build was slightly different from the usual. Rather than grabbing an RSS feed from somewhere like Kongregate and re-publishing everything they publish, we wanted to hand-pick our favourite games and just publish them. Quality over quantity; unless you&#8217;re talking about cupcakes.</p>
<p>Naturally we still wanted all the normal things that game portals do (categories, RSS feeds of games, ratings, comments etc). Both of us being programmers, we considered writing our own game portal software for, oh I don&#8217;t know, maybe 6 seconds? We ultimately decided against this because it would take too long and maintaining the code would be a bit of a nightmare (seriously, you have never seen my code).</p>
<p>We took a look at the portal software that was already out there. The two main players were <a href="https://secure.agaresmedia.com/v6/products/arcade-script/">Arcadem Pro</a> and <a href="http://www.phparcadescript.com/">PHPAS</a>. They both had some neat features alright, but overall the solutions seemed too messy. They did way more than we could ever want or need, and it really just seemed like way too much effort to apply our own design to either theme. In our search for simplicity and flexibility, it turned out we had been looking at the solution every time we visited <a href="http://blog.gambrinous.com/">our blog</a>.</p>
<h2>WordPress: The Unlikely Hero</h2>
<p><a href="http://www.wordpress.org/">WordPress</a> is a blogging platform that I&#8217;m sure many of you are already familiar with. It powers millions of blogs all over the Internet, but we were able to take many of the features it offers and use them to build our portal.</p>
<h4>So, what makes it great for a game portal?!</h4>
<p>Our basic need was simply to publish games that we had hand-picked from various sources, and include a few details about the game and our own mini-review. We thought about it for a little while and realised that the process of publishing games for the portal was very similar to how we published out blog posts. The features that WordPress offered also made many aspects of managing the portal a lot simpler.</p>
<ul>
<li>1. <strong>RSS Feeds:</strong> WordPress automatically creates RSS feeds of content published through it, so we had a ready-made feed where people could subscribe to see game releases right in their feed reader.</li>
<li>2. <strong>Pretty URLs:</strong> They&#8217;re nice to look at and it&#8217;s easy for users to remember them. Google is pretty fond of them too. All these factors make games on the portal a whole lot easier to find.</li>
<li>3. <strong>Scheduled Posts:</strong> This one&#8217;s a beauty. You could spend one night queueing up your planned releases for the next couple of months and then fly out to the Bahamas while WordPress sits at home in the rain, busily publishing out your games just like you told it to.</li>
<li>4. <strong>Comments:</strong> WordPress makes it very easy for your users have their say about each game that you post.</li>
<li>5. <strong>Categories &#038; Tags:</strong> It&#8217;s super simple to add games to specific categories or tag them with specific keywords straight out of the box.</li>
</ul>
<h4>A little help from our friends</h4>
<p>Of course, WordPress didn&#8217;t bow to our every command straight out of the box. We wanted our users to be able to rate the games that we published, and, <a href="http://twitter.com/gambrinous">being the Twitter nerds that we are</a>, we wanted to announce the launch of each game via Twitter on our <a href="http://twitter.com/gambrinousgames">dedicated Gambrinous Games feed</a>. Luckily, all of these issues were easily rectified by the vast WordPress developer community. We added the following plugins to help us along the way:</p>
<ul>
<li><a href="http://wordpress.org/extend/plugins/twitter-tools/">Twitter Tools</a>: A nice a simple plug-in that will automagically add new posts to Twitter.</li>
<li><a href="http://wordpress.org/extend/plugins/wp-postratings/">WP-PostRatings</a>: Add ratings to each post in WordPress. It also has a nice feature to allow you to customise what information gets displayed around the star ratings.</li>
<li><a href="http://akismet.com/">Akismet</a>: This comes with WordPress and is a must for any blog that accepts comments. It will filter out all those nasty Viagra ads.</li>
</ul>
<p>Already at this point, we had the bones of our game portal. There was one more feature in WordPress that came in handy, and that was Custom Fields. There was certain information that wasn&#8217;t easily conveyed through WordPress&#8217; default functionality or through plug-ins. Certain things like setting the width and height of the flash game or setting thumbnails and screen-shots of each game we publish.</p>
<p>To get around this, we added a few custom fields to our blog posts for each piece of information that we needed, then added it to our WordPress theme where required. It really was remarkably painless.</p>
<h2>The Science Bit</h2>
<p>We brought everything together in a WordPress theme. We took the default Kubrick theme and modified it to fit our needs. The excellent <a href="http://codex.wordpress.org/Main_Page">WordPress Codex</a> served as our guide throughout this process, required reading if you&#8217;re going to be making your own templates. The first thing you need to understand is the anatomy of a WordPress theme. All themes are stored in individual folders named after the theme. If you created a theme called &#8220;My Super Game Portal&#8221;, it would be found here:</p>
<p><code>/wp-content/themes/my-super-game-portal/</code></p>
<p>Another handy feature of WordPress themes is the functions.php file. Simply create a file called functions.php in your theme folder and  add any custom functions to it. Everything you put in there will be available to your theme automagically. We have custom functions for pulling our top rated games and our newest games out of the database.</p>
<p>The final technical detail you really need to know before being able to customise your own WordPress theme is how to use those custom fields I mentioned earlier. One way we use the custom fields is to set the width and height of each game that we publish.</p>
<p>To use these fields in our template, we simply use the following code on the game page (single.php):</p>
<p><pre><code>&amp;lt;?php
echo get_post_meta($post-&gt;ID, &#039;game_width&#039;, true);
&nbsp;&nbsp;&nbsp;&nbsp;echo get_post_meta($post-&gt;ID, &#039;game_height&#039;, true);
?&gt;
</code></pre></p>
<p>That should be enough to get you started. For more information, you should check out the <a href="http://codex.wordpress.org/Theme_Development">Theme Development</a> article within the WordPress Codex.</p>
<h2>Finally and in Conclusion</h2>
<p>So, that&#8217;s how we did it. It was quick and simple, and all made possible by our new friend WordPress. What are you doing still reading? Go and use WordPress to build that game portal you&#8217;ve been talking about for the last 2 months!</p>
 <div class='series_toc'></div>	<p></p>
	<hr noshade style="margin:0;height:1px" />
	<p>&copy; Peter McKenna for <a href="http://mochiland.com">MochiLand</a>, 2009. |
	  <a href="http://mochiland.com/articles/building-a-wordpress-flash-games-portal-gambrinous-games">Permalink</a> |
	  <a href="http://mochiland.com/articles/building-a-wordpress-flash-games-portal-gambrinous-games#comments">54 comments</a></p>
	<p>Add to <a href="http://del.icio.us/post?url=http://mochiland.com/articles/building-a-wordpress-flash-games-portal-gambrinous-games&amp;title=Building a WordPress Flash Games Portal &#8211; Gambrinous Games">del.icio.us</a></p>
	<p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mochiland.com/articles/building-a-wordpress-flash-games-portal-gambrinous-games" title="Search on Technorati">Technorati</a></p>
	<p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mochiland.com/category/mochiland/art-visual-design" title="View all posts in Art / Visual Design" rel="category tag">Art / Visual Design</a>, <a href="http://mochiland.com/category/mochiland" title="View all posts in MochiLand Topics" rel="category tag">MochiLand Topics</a>, <a href="http://mochiland.com/category/mochiland/tutorials" title="View all posts in Tutorials" rel="category tag">Tutorials</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://mochiland.com/articles/building-a-wordpress-flash-games-portal-gambrinous-games/feed</wfw:commentRss>
		<slash:comments>54</slash:comments>
		</item>
	</channel>
</rss>

