<?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>Emanuele Feronato &#187; Flash</title>
	<atom:link href="http://www.emanueleferonato.com/category/flash/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emanueleferonato.com</link>
	<description>italian geek and PROgrammer</description>
	<lastBuildDate>Thu, 11 Mar 2010 00:11:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund</title>
		<link>http://www.emanueleferonato.com/2010/03/11/get-up-to-to-100000-for-your-next-flash-game-with-mochi-game-developer-fund/</link>
		<comments>http://www.emanueleferonato.com/2010/03/11/get-up-to-to-100000-for-your-next-flash-game-with-mochi-game-developer-fund/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 23:30:32 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>
		<category><![CDATA[Monetize]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2519</guid>
		<description><![CDATA[Imagine a Flash game sponsor encouraging you to use MochiAds, helping you to market and promote your game, reviewing and testing it putting his experience at your service and helping you to integrate the latest APIs in the most proficuous way.
For some developers this dream could become true because MochiMedia created the Mochi GAME Developer [...]]]></description>
			<content:encoded><![CDATA[<p>Imagine a Flash game sponsor encouraging you to use <a href="https://www.mochimedia.com/r/972ae333a3c92a2a" target = "_blank">MochiAds</a>, helping you to market and promote your game, reviewing and testing it putting his experience at your service and helping you to integrate the latest APIs in the most proficuous way.</p>
<p>For some developers this dream could become true because <a href="https://www.mochimedia.com/r/972ae333a3c92a2a" target = "_blank">MochiMedia</a> created the <strong>Mochi GAME Developer Fund</strong>: a $10 million fund that will help promising Flash and social game developers support their games through sponsorship, licensing and publishing deals.</p>
<p>The fund will assist small Flash game development studios and independent game developers worldwide in building world-class games and businesses.</p>
<p><a href="https://www.mochimedia.com/r/972ae333a3c92a2a" target = "_blank"><img src="/wp-content/uploads/2010/03/mochi-dev-fund-logo-300x198.png" /></a></p>
<p>You will find the details in the <a href="http://www.mochimedia.com/developers/fund.html" target = "_blank">official page</a>, with some FAQs. Here&#8217;s the four more interesting:</p>
<p><strong>Q: What is the Mochi GAME Developer Fund?</strong><br />
A: The Mochi GAME Developer Fund $10 million USD fund for investing in promising Flash and social game developers through sponsorship, licensing, and publishing. The goal of the Fund is to assist developers in launching and building rockstar premium games on the Mochi platform, and to cultivate long term relationships with Flash and social game developers looking to build world-class games and businesses. </p>
<p><strong>Q: What is the target deal size for the Mochi GAME Developer Fund?</strong><br />
A: The Mochi GAME Developer Fund intends to partner with Flash and social game developers on a sponsorship, licensing or publishing basis anywhere from $1,000 to $100,000. Additional consideration may be given to other types of relationships on a case-by-case basis. Generally, the funds will be given upon the achievement of certain milestones of the game development process. The specific milestones can be discussed with the Mochi GAME Developer Fund team. </p>
<p><strong>Q: What types of games is the Mochi GAME Developer Fund seeking?</strong><br />
A: We are open to Flash and social games of all genres and gameplay types targeting all kinds of audiences. Particular attention will be given to games that have high potential to monetize via MochiCoins microtransactions, include social elements that may be enhanced via MochiSocial or are particularly great games. </p>
<p><strong>Q: I don’t live in the United States. Can I still work with the Mochi GAME Developer Fund?</strong><br />
A: Absolutely! Flash and social game developers are located all across the world. We care less about where you live and care more about helping you build great games using the Mochi platform. </p>
<p>According to these FAQs, it looks like an exclusive sponsorship, but I hope to give you more information as I&#8217;m running to <a href="http://mochimedia.us1.list-manage.com/subscribe?u=1c2da7d6e8&#038;id=7ce02a64fb" target = "_blank">submit the form</a> and ask for some millions for my next game.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/03/11/get-up-to-to-100000-for-your-next-flash-game-with-mochi-game-developer-fund/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sell sitelocked version of your Flash games and even .fla sources to Free Online Games</title>
		<link>http://www.emanueleferonato.com/2010/03/09/sell-sitelocked-version-of-your-flash-games-and-even-fla-sources-to-free-online-games/</link>
		<comments>http://www.emanueleferonato.com/2010/03/09/sell-sitelocked-version-of-your-flash-games-and-even-fla-sources-to-free-online-games/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 09:28:23 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>
		<category><![CDATA[Monetize]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2500</guid>
		<description><![CDATA[Are you looking for a quick way to sell a site locked version of your game? Do you want to earn some more selling the .fla source code? Then Neil from Free Online Games (aka FOG) is your man.

FOG is looking for any game, no matter how old it is or how wide spread it [...]]]></description>
			<content:encoded><![CDATA[<p>Are you looking for a quick way to sell a site locked version of your game? Do you want to earn some more selling the <code>.fla</code> source code? Then <strong>Neil</strong> from <a href="http://www.freeonlinegames.com/" target = "_blank">Free Online Games</a> (aka FOG) is your man.</p>
<p><a href="http://www.freeonlinegames.com/" target = "_blank"><img src="/wp-content/uploads/2010/03/fog.jpg" /></a></p>
<p>FOG is looking for any game, no matter how old it is or how wide spread it is already, as long as it&#8217;s a decent game. In this case FOG will buy you a site locked version of your game, or even the <code>.fla</code> source code.</p>
<p><strong>What happens when <a href="http://www.freeonlinegames.com/" target = "_blank">FOG</a> buys a site locked version of your game</strong></p>
<p>* The game is sitelocked to <a href="http://www.freeonlinegames.com/" target = "_blank">FOG</a><br />
* You have to interface your game with their API<br />
* You have to remove all external branding and ads</p>
<p><strong>What happens when <a href="http://www.freeonlinegames.com/" target = "_blank">FOG</a> buys the source code of your game</strong></p>
<p>* <a href="http://www.freeonlinegames.com/" target = "_blank">FOG</a> retains the rights to re-release, resell, re-use, delete, make more version and all in all everything with your code<br />
* All branding, ads and credits will be removed</p>
<p>Note: if your game is already sponsored, you may no longer have the right to sell your source code. Check with your sponsor as <a href="http://www.freeonlinegames.com/" target = "_blank">FOG</a> is not responsible for your previous deals<span id="more-2500"></span></p>
<p><strong>My experience</strong></p>
<p>I am currently managing the latest small details about <a href="http://www.emanueleferonato.com/2009/12/09/play-barbalance-my-latest-game/">BarBalance</a>. I&#8217;m selling the source code. Working with Neil is a pleasure as he&#8217;s very kind, professional and will support you through the entire process. Just consider FOG&#8217;s HQ is located in United Kingdom when your plan to chat with him.</p>
<p>His email, as well as his Skype, Messenger, ICQ, Yahoo and GTalk ids is Neil[at]FOG[dot]COM</p>
<p>I asked him a couple of questions just to give you as much information as possible</p>
<p><strong>Are you really buying ANY kind of game <code>.fla</code>s?</strong></p>
<p>I primarily buy site locks of games, however if the source is available then we do try and purchase that too. I&#8217;m not out to purchase things left right and centre, all the games that are offered we review meticulously to see if it meets our standards, and at the moment, the majority of submissions are being rejected.</p>
<p><strong>What&#8217;s the meaning of buying source codes? Don&#8217;t you think in most cases the sources would be hard to understand (i.e variable names in foreign languages, no optimization and so on&#8230;)</strong></p>
<p>Sources in other languages aren&#8217;t too much of a concern as we have a multilingual staff base here at fog, if the code is not optimised then our in house programmer will go over it and see what can be done</p>
<p><strong>Do you already have an example of a bought and republished game?</strong></p>
<p>We have plenty of site locked games on our site. for games that we have bought source codes of and redone as fog games, and our own developed games can be seen at <a href="http://www.freegamesforyourwebsite.com/" target ="_blank">www.freegamesforyourwebsite.com</a></p>
<p>a few examples of site locks are</p>
<p><a href="http://www.freeonlinegames.com/rpg-games/pizza-pronto.html" target = "_blank">Pizza Pronto</a><br />
<a href="http://www.freeonlinegames.com/adventure-games/tank-in-action.html" target = "_blank">Tank In Action</a><br />
<a href="http://www.freeonlinegames.com/racing-games/mountain-rescue-driver.html" target = "_blank">Mountain Rescue Driver</a><br />
<a href="http://www.freeonlinegames.com/puzzle-games/perfect-balance-2.html" target = "_blank">Perfect Balance 2</a></p>
<p><strong>How much can a developer earn from <a href="<a href="http://www.freeonlinegames.com/" target = "_blank">FOG</a>?</strong></p>
<p>Generally prices range from $100-$500 for site locks, source code can be anything from $100-$1000 or more depending on the quality&#8230; I don&#8217;t feel that there is a limit on what a developer can earn for a<br />
decent game, if it is really that good and someone wants it they&#8217;ll lay the money on the table to get it.</p>
<p>Hope this will help you to further monetize your games.</p>
<p><strong>** edit **</strong></p>
<p><a href="http://www.emanueleferonato.com/2009/12/09/play-barbalance-my-latest-game/">BarBalance</a> source code sold :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/03/09/sell-sitelocked-version-of-your-flash-games-and-even-fla-sources-to-free-online-games/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Protect your work from ActionScript code theft with SWF Protector</title>
		<link>http://www.emanueleferonato.com/2010/03/05/protect-your-work-from-actionscript-code-theft-with-swf-protector/</link>
		<comments>http://www.emanueleferonato.com/2010/03/05/protect-your-work-from-actionscript-code-theft-with-swf-protector/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 10:03:32 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 2]]></category>
		<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Reviews]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2480</guid>
		<description><![CDATA[&#171; I&#8217;ve just found a site that won a top international online advertising award &#8211; and it&#8217;s been blatantly lifted from me!
Its a flash application that has the same vectors and almost certainly the same AS code. Is there anything I can do? I&#8217;m in the UK &#8211; they&#8217;re in Brazil. &#187;
The guy who opened [...]]]></description>
			<content:encoded><![CDATA[<p>&laquo; I&#8217;ve just found a site that won a top international online advertising award &#8211; and it&#8217;s been blatantly lifted from me!</p>
<p>Its a flash application that has the same vectors and <strong>almost certainly the same AS code</strong>. Is there anything I can do? I&#8217;m in the UK &#8211; they&#8217;re in Brazil. &raquo;</p>
<p>The guy who opened <a href="http://www.webmasterworld.com/webmaster/3033990.htm" target = "_blank">this thread</a> looks quite desperate&#8230; now imagine this happening to your latest Flash game.</p>
<p>That&#8217;s why you <strong>must</strong> protect your code. You don&#8217;t want someone else stealing your work.</p>
<p>I am going to review <a href="http://www.dcomsoft.com/" target = "_blank">DCOM Soft</a>&#8217;s SWF Protector.</p>
<p><a href="http://www.dcomsoft.com/" target = "_blank"><img src="/wp-content/uploads/2010/03/swfpro01.jpg" /></a></p>
<p><strong>SWF Protector</strong></p>
<p><a href="http://www.dcomsoft.com/" target = "_blank">SWF Protector</a> is an swf protector software working through actionscript encryption to secure your actionscript content. This means decompilers won&#8217;t be able to read your actionscript.<span id="more-2480"></span></p>
<p>The most interesting <a href="http://www.dcomsoft.com/" target = "_blank">SWF Protector</a> features are:</p>
<p>* Inexpensive: only $59.95 for a Business License. <a href="http://www.emanueleferonato.com/2009/12/07/milestone-reached-100-with-mochiads/">You can earn twice in just one day with a single game</a>.</p>
<p>* Simple and fast. You can encrypt your SWF with a couple of clicks.</p>
<p>* Cross-platform. Available for Windows, Mac and Linux.</p>
<p>* Mass protection. You can protect several SWFs at once.</p>
<p>This is the interface you&#8217;ll see when you are about to secure your flash content: </p>
<p><img src="/wp-content/uploads/2010/03/swfpro02.jpg" /></p>
<p>I am testing the software on the file created on <a href="http://www.emanueleferonato.com/2010/02/25/box2d-flash-game-creation-tutorial-part-2/">Box2D Flash game creation tutorial – part 2</a> tutorial.</p>
<p>This is the original content of the <code>custom_contact_listener.as</code> file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td 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> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Joints.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Contacts.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #9900cc; font-weight: bold;">class</span> custom_contact_listener extends b2ContactListener <span style="color: #000000;">&#123;</span>
		override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> BeginContact<span style="color: #000000;">&#40;</span>contact<span style="color: #000000; font-weight: bold;">:</span>b2Contact<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// getting the fixtures that collided</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> fixtureA<span style="color: #000000; font-weight: bold;">:</span>b2Fixture=contact.GetFixtureA<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> fixtureB<span style="color: #000000; font-weight: bold;">:</span>b2Fixture=contact.GetFixtureB<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// if the fixture is a sensor, mark the parent body to be removed</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>fixtureB.IsSensor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				fixtureB.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.SetUserData<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;remove&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>fixtureA.IsSensor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				fixtureA.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.SetUserData<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;remove&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Now, I will use a Flash decompiler to extract the same file from an unprotected version of the swf&#8230; here it is:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//class custom_contact_listener</span>
<span style="color: #9900cc; font-weight: bold;">package</span> 
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
    <span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Contacts.<span style="color: #000000; font-weight: bold;">*</span>;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">internal</span> <span style="color: #9900cc; font-weight: bold;">class</span> custom_contact_listener extends Box2D.Dynamics.b2ContactListener
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> custom_contact_listener<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;">&#41;</span>;
            <span style="color: #0033ff; font-weight: bold;">return</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">public</span> override <span style="color: #339966; font-weight: bold;">function</span> BeginContact<span style="color: #000000;">&#40;</span>arg1<span style="color: #000000; font-weight: bold;">:</span>Box2D.Dynamics.Contacts.b2Contact<span style="color: #000000;">&#41;</span><span style="color: #000000; 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> loc1<span style="color: #000000; font-weight: bold;">:*</span>=arg1.GetFixtureA<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #6699cc; font-weight: bold;">var</span> loc2<span style="color: #000000; font-weight: bold;">:*</span>=arg1.GetFixtureB<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>loc2.IsSensor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> 
            <span style="color: #000000;">&#123;</span>
                loc2.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.SetUserData<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;remove&quot;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>loc1.IsSensor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> 
            <span style="color: #000000;">&#123;</span>
                loc1.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.SetUserData<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;remove&quot;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0033ff; font-weight: bold;">return</span>;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>The code is amost more polished&#8230; and obviously it works.</p>
<p>Now let&#8217;s try to do the same on the obfuscated swf made by <a href="http://www.dcomsoft.com/" target = "_blank">SWF Protector</a>&#8230; that&#8217;s what I got (sorry for the italian screenshot: I&#8217;m currently running an italian version of Windows7)</p>
<p><img src="/wp-content/uploads/2010/03/swfpro03.jpg" /></p>
<p>The decompiler crashed&#8230; probably the amount of weird characters inserted by the flash obfuscator made it crash.</p>
<p>In the end, <a href="http://www.dcomsoft.com/" target = "_blank">SWF Protector</a> is simple to use and very fast. It has shown lightning-fast speeds of SWF files loading, processing and saving and uses four reliable protection algorithms.</p>
<p>You can download a trial version on the official site or <strong>win one of three business licenses</strong> I am giving away being the first to reply &#8220;I want one&#8221; when this post will be published on my <a href="http://www.facebook.com/pages/Emanuele-Feronato/50374255154">Facebook Fanpage</a>. This is a special giveaway for three of my Facebook fans. And won&#8217;t be the only one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/03/05/protect-your-work-from-actionscript-code-theft-with-swf-protector/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Understanding Box2D&#8217;s one-way platforms, aka CLOUDS</title>
		<link>http://www.emanueleferonato.com/2010/03/02/understanding-box2ds-one-way-platforms-aka-clouds/</link>
		<comments>http://www.emanueleferonato.com/2010/03/02/understanding-box2ds-one-way-platforms-aka-clouds/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 23:24:20 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2448</guid>
		<description><![CDATA[One of the new features introduced with Box2D 2.1a is the improved contact listener class which comes in hand when we want to create one-way platforms, or &#8220;clouds&#8221;.
This can be made thanks to a function called before the contact is processed&#8230; something like &#8220;hey, two bodies are about to collide, what should I do?&#8221;&#8230; so [...]]]></description>
			<content:encoded><![CDATA[<p>One of the new features introduced with <a href="http://www.emanueleferonato.com/2010/01/27/box2dflash-2-1a-released-what-changed/">Box2D 2.1a</a> is the improved contact listener class which comes in hand when we want to create one-way platforms, or &#8220;clouds&#8221;.</p>
<p>This can be made thanks to a function called <strong>before</strong> the contact is processed&#8230; something like &#8220;hey, two bodies are about to collide, what should I do?&#8221;&#8230; so you can decide to disable the contact for every collision you want.</p>
<p>The function used to do this task is <code>PreSolve</code>, working for all awake bodies that aren&#8217;t sensors.</p>
<p>If you don&#8217;t know what is a Box2D sensor, check <a href="http://www.emanueleferonato.com/2010/02/25/box2d-flash-game-creation-tutorial-part-2/">Box2D Flash game creation tutorial – part 2</a>.</p>
<p>So the concept is: listen for collisions, if a collision involves the cloud wall and the player, then check if the player is higher or lower than the cloud. If it&#8217;s lower, don&#8217;t process the collision and let the player fly through the cloud.</p>
<p>Let&#8217;s see the script, directly taken from <a href="http://www.emanueleferonato.com/2010/02/25/box2d-flash-game-creation-tutorial-part-2/">Box2D Flash game creation tutorial – part 2</a>:<span id="more-2448"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">KeyboardEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ball02 extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #009900;">// world creation</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world<span style="color: #000000; font-weight: bold;">:</span>b2World=<span style="color: #0033ff; font-weight: bold;">new</span> b2World<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">10.0</span><span style="color: #000000;">&#41;</span>,<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world_scale<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">30</span>;
		<span style="color: #009900;">// the player</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> player<span style="color: #000000; font-weight: bold;">:</span>b2Body;
		<span style="color: #009900;">// force to apply to the player</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> force<span style="color: #000000; font-weight: bold;">:</span>b2Vec2;
		<span style="color: #009900;">// variables to store whether the keys are pressed or not</span>
		<span style="color: #009900;">// true = pressed;</span>
		<span style="color: #009900;">// false = unpressed</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">left</span>,<span style="color: #004993;">right</span>,up,down<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
		<span style="color: #009900;">// declaring my custom contact listener class</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> contact_listener=<span style="color: #0033ff; font-weight: bold;">new</span> custom_contact_listener<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ball02<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// assigning the contact listener to the world</span>
			world.SetContactListener<span style="color: #000000;">&#40;</span>contact_listener<span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// calling debug draw function</span>
			debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// drawing the boundaries</span>
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;ground&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">200</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;left&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">200</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;right&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;roof&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">200</span>,<span style="color: #000000; font-weight:bold;">300</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;middle&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// adding the player at 250,200</span>
			add_player<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">350</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// adding some coins</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;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">1</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>=<span style="color: #000000; font-weight:bold;">5</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				draw_coin<span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">400</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">150</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">25</span>,<span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #009900;">// listeners needed for the game to work</span>
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, update<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_DOWN</span>,on_key_down<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_UP</span>,on_key_up<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// according to the key pressed, set the proper variable to &quot;true&quot;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> on_key_down<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; 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;">switch</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span><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;">37</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">left</span>=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">38</span> <span style="color: #000000; font-weight: bold;">:</span>
					up=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">39</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">right</span>=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">40</span> <span style="color: #000000; font-weight: bold;">:</span>
					down=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// according to the key released, set the proper variable to &quot;false&quot;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> on_key_up<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; 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;">switch</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span><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;">37</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">left</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">38</span> <span style="color: #000000; font-weight: bold;">:</span>
					up=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">39</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">right</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">40</span> <span style="color: #000000; font-weight: bold;">:</span>
					down=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// function to draw a coin</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> draw_coin<span style="color: #000000;">&#40;</span>px,py,r<span style="color: #000000;">&#41;</span><span style="color: #000000; 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> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_circle<span style="color: #000000; font-weight: bold;">:</span>b2CircleShape=<span style="color: #0033ff; font-weight: bold;">new</span> b2CircleShape<span style="color: #000000;">&#40;</span>r<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_circle;
			<span style="color: #009900;">// look! it's a sensor!!</span>
			my_fixture.isSensor=<span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> world_body<span style="color: #000000; font-weight: bold;">:</span>b2Body=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			world_body.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// simple function to draw a box</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> draw_box<span style="color: #000000;">&#40;</span>px,py,w,h,<span style="color: #004993;">d</span>,ud<span style="color: #000000;">&#41;</span><span style="color: #000000; 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> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				my_body.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_box<span style="color: #000000; font-weight: bold;">:</span>b2PolygonShape = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonShape<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_box.SetAsBox<span style="color: #000000;">&#40;</span>w<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale, h<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_box;
			<span style="color: #6699cc; font-weight: bold;">var</span> world_body<span style="color: #000000; font-weight: bold;">:</span>b2Body=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			world_body.SetUserData<span style="color: #000000;">&#40;</span>ud<span style="color: #000000;">&#41;</span>;
			world_body.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// function to add the player</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> add_player<span style="color: #000000;">&#40;</span>px,py<span style="color: #000000;">&#41;</span><span style="color: #000000; 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> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_circle<span style="color: #000000; font-weight: bold;">:</span>b2CircleShape=<span style="color: #0033ff; font-weight: bold;">new</span> b2CircleShape<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_circle;
			player=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			player.SetUserData<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;player&quot;</span><span style="color: #000000;">&#41;</span>;
			player.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// debug draw</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; 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> debug_draw<span style="color: #000000; font-weight: bold;">:</span>b2DebugDraw = <span style="color: #0033ff; font-weight: bold;">new</span> b2DebugDraw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> debug_sprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetSprite<span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetDrawScale<span style="color: #000000;">&#40;</span>world_scale<span style="color: #000000;">&#41;</span>;
			debug_draw.SetFlags<span style="color: #000000;">&#40;</span>b2DebugDraw.e_shapeBit<span style="color: #000000;">&#41;</span>;
			world.SetDebugDraw<span style="color: #000000;">&#40;</span>debug_draw<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// function to be executed at every frame</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// setting the force to null</span>
			force=<span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// according to the key(s) pressed, add the proper vector force</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">left</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">right</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>up<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>down<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #009900;">// if there is any force, then apply it</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>force.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">||</span>force.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				player.ApplyForce<span style="color: #000000;">&#40;</span>force,player.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			world.Step<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
			world.ClearForces<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// scanning through all bodies</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> worldbody<span style="color: #000000; font-weight: bold;">:</span>b2Body = world.GetBodyList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; worldbody; worldbody = worldbody.GetNext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #009900;">// if a body is marked as &quot;remove&quot;...</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>worldbody.GetUserData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>==<span style="color: #990000;">&quot;remove&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #009900;">// ... just remove it!!</span>
					world.DestroyBody<span style="color: #000000;">&#40;</span>worldbody<span style="color: #000000;">&#41;</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			world.DrawDebugData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Line 34</strong>: with the old function introduced at <a href="http://www.emanueleferonato.com/2010/02/16/understanding-box2d-applicable-forces/">Understanding Box2D applicable forces</a> I create a box marked as &#8220;middle&#8221;.</p>
<p>No other changes on the main file, now let&#8217;s see the <code>custom_contact_listener.as</code> file</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
</pre></td><td 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> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Joints.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Contacts.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #9900cc; font-weight: bold;">class</span> custom_contact_listener extends b2ContactListener <span style="color: #000000;">&#123;</span>
		override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> BeginContact<span style="color: #000000;">&#40;</span>contact<span style="color: #000000; font-weight: bold;">:</span>b2Contact<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// getting the fixtures that collided</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> fixtureA<span style="color: #000000; font-weight: bold;">:</span>b2Fixture=contact.GetFixtureA<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> fixtureB<span style="color: #000000; font-weight: bold;">:</span>b2Fixture=contact.GetFixtureB<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// if the fixture is a sensor, mark the parent body to be removed</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>fixtureB.IsSensor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				fixtureB.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.SetUserData<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;remove&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>fixtureA.IsSensor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				fixtureA.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.SetUserData<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;remove&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> PreSolve<span style="color: #000000;">&#40;</span>contact<span style="color: #000000; font-weight: bold;">:</span>b2Contact, oldManifold<span style="color: #000000; font-weight: bold;">:</span>b2Manifold<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// getting the fixtures that collided</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> fixtureA<span style="color: #000000; font-weight: bold;">:</span>b2Fixture=contact.GetFixtureA<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> fixtureB<span style="color: #000000; font-weight: bold;">:</span>b2Fixture=contact.GetFixtureB<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// variable to handle bodies y position</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> player_y_position<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> platform_y_position<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
			<span style="color: #009900;">// checking if the collision bodies are the ones marked as &quot;middle&quot; and &quot;player&quot;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>fixtureA.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetUserData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>==<span style="color: #990000;">&quot;middle&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> fixtureB.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetUserData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>==<span style="color: #990000;">&quot;player&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">||</span><span style="color: #000000;">&#40;</span>fixtureA.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetUserData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>==<span style="color: #990000;">&quot;player&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> fixtureB.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetUserData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>==<span style="color: #990000;">&quot;middle&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #009900;">// determining if the fixtureA represents the platform (&quot;middle&quot;) or the player</span>
				<span style="color: #0033ff; font-weight: bold;">switch</span> <span style="color: #000000;">&#40;</span>fixtureA.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetUserData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;middle&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						<span style="color: #009900;">// determining y positions</span>
						player_y_position=fixtureB.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetPosition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span>;
						platform_y_position=fixtureA.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetPosition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;player&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						<span style="color: #009900;">// determining y positions</span>
						player_y_position=fixtureA.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetPosition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span>;
						platform_y_position=fixtureB.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.GetPosition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span>;
<span style="color: #000000; font-weight: bold;">&lt;</span>strong<span style="color: #000000; font-weight: bold;">&gt;</span>						<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #000000;">&#125;</span>
				<span style="color: #009900;">// checking distance between bodies</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">distance</span> = player_y_position<span style="color: #000000; font-weight: bold;">-</span>platform_y_position;
				<span style="color: #009900;">// if the distance is greater than player radius + half of the platform height...</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>distance<span style="color: #000000; font-weight: bold;">&gt;-</span><span style="color: #000000; font-weight:bold;">14.5</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #009900;">// don't manage the contact</span>
					contact.SetEnabled<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Line 22</strong>: beginning of the PreSolve function, the core of this example</p>
<p><strong>Lines 24-25</strong>: getting the fixtures that generated the contact</p>
<p><strong>Lines 27-28</strong>: declaring two variables to store y position of both bodies</p>
<p><strong>Line 30</strong>: here I am checking if the fixtures are the one associated to the player and the one associated to the cloud</p>
<p><strong>Lines 32-43</strong>: according to the fixture associated to the player and the one associated to the cloud, I am saving in the variables declared at <strong>lines 27-28</strong> the y position of both bodies. I am multiplying directly by <code>30</code> without passing the right <code>world_scale</code> value declared at <strong>line 13</strong> of the main class because it&#8217;s not the purpose of this tutorial.</p>
<p><strong>Line 45</strong>: Determining the vertical distance from the player and the cloud</p>
<p><strong>Lines 47-50</strong>: If the player is not at least <code>14.5</code> pixels higher than the cloud, then disable the contact. Why <code>14.5</code>? It&#8217;s the sum of the ball radius (10) and half the cloud height (5)&#8230; and I am not using <code>15</code> because I found sometimes the distance when the ball falls on the cloud is <code>14.93</code>, so the cloud won&#8217;t &#8220;hold&#8221; the ball, letting it fall down. With <code>14.5</code>, I am sure this won&#8217;t happen.</p>
<p>And this is the result&#8230; </p>
<p><embed src="/wp-content/uploads/2010/03/oneway.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>Move the ball by tapping arrow keys and watch the static object in the center of the stage act as a cloud&#8230; you can fly through it from bottom to top, but you can&#8217;t do it from top to bottom.</p>
<p><a href="/wp-content/uploads/2010/03/oneway.zip">Download the source code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/03/02/understanding-box2ds-one-way-platforms-aka-clouds/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Box2D Flash game creation tutorial &#8211; part 2</title>
		<link>http://www.emanueleferonato.com/2010/02/25/box2d-flash-game-creation-tutorial-part-2/</link>
		<comments>http://www.emanueleferonato.com/2010/02/25/box2d-flash-game-creation-tutorial-part-2/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 09:56:23 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2434</guid>
		<description><![CDATA[After seeing the character creation in Box2D Flash game creation tutorial &#8211; part 1, it&#8217;s time to add some coins to collect.
This process will involve some interesting Box2D features, like sensors and custom collision management.
I would suggest to read the basics of sensors at Erase Box: the tutorial and custom collision management at Creation of [...]]]></description>
			<content:encoded><![CDATA[<p>After seeing the character creation in <a href="http://www.emanueleferonato.com/2010/02/19/box2d-flash-game-creation-tutorial-part-1/">Box2D Flash game creation tutorial &#8211; part 1</a>, it&#8217;s time to add some coins to collect.</p>
<p>This process will involve some interesting Box2D features, like sensors and custom collision management.</p>
<p>I would suggest to read the basics of sensors at <a href="http://www.emanueleferonato.com/2009/02/25/erase-box-the-tutorial/">Erase Box: the tutorial</a> and custom collision management at <a href="http://www.emanueleferonato.com/2009/11/23/creation-of-a-flash-stabilize-clone-using-box2d-part-4/">Creation of a Flash Stabilize! clone using Box2D – part 4</a>.</p>
<p>Although they are both referred to an older Box2D version, they&#8217;ll introduce you to sensor and collisions.</p>
<p>Now the concept is simple: we are placing some circular sensors around the stage (the coins), then we&#8217;ll create a custom contact listener class to check whether the player is over a coin or not. If it&#8217;s over, we&#8217;ll remove the coin.</p>
<p>So this is the main script:<span id="more-2434"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">KeyboardEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ball02 extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #009900;">// world creation</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world<span style="color: #000000; font-weight: bold;">:</span>b2World=<span style="color: #0033ff; font-weight: bold;">new</span> b2World<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">10.0</span><span style="color: #000000;">&#41;</span>,<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world_scale<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">30</span>;
		<span style="color: #009900;">// the player</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> player<span style="color: #000000; font-weight: bold;">:</span>b2Body;
		<span style="color: #009900;">// force to apply to the player</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> force<span style="color: #000000; font-weight: bold;">:</span>b2Vec2;
		<span style="color: #009900;">// variables to store whether the keys are pressed or not</span>
		<span style="color: #009900;">// true = pressed;</span>
		<span style="color: #009900;">// false = unpressed</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">left</span>,<span style="color: #004993;">right</span>,up,down<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
		<span style="color: #009900;">// declaring my custom contact listener class</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> contact_listener=<span style="color: #0033ff; font-weight: bold;">new</span> custom_contact_listener<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ball02<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// assigning the contact listener to the world</span>
			world.SetContactListener<span style="color: #000000;">&#40;</span>contact_listener<span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// calling debug draw function</span>
			debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// drawing the boundaries</span>
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;ground&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">200</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;left&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">200</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;right&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;roof&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// adding the player at 250,200</span>
			add_player<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// adding some coins</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;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">1</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>=<span style="color: #000000; font-weight:bold;">5</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				draw_coin<span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">400</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">300</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #009900;">// listeners needed for the game to work</span>
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, update<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_DOWN</span>,on_key_down<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_UP</span>,on_key_up<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// according to the key pressed, set the proper variable to &quot;true&quot;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> on_key_down<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; 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;">switch</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span><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;">37</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">left</span>=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">38</span> <span style="color: #000000; font-weight: bold;">:</span>
					up=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">39</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">right</span>=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">40</span> <span style="color: #000000; font-weight: bold;">:</span>
					down=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// according to the key released, set the proper variable to &quot;false&quot;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> on_key_up<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; 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;">switch</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span><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;">37</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">left</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">38</span> <span style="color: #000000; font-weight: bold;">:</span>
					up=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">39</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">right</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">40</span> <span style="color: #000000; font-weight: bold;">:</span>
					down=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// function to draw a coin</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> draw_coin<span style="color: #000000;">&#40;</span>px,py,r<span style="color: #000000;">&#41;</span><span style="color: #000000; 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> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_circle<span style="color: #000000; font-weight: bold;">:</span>b2CircleShape=<span style="color: #0033ff; font-weight: bold;">new</span> b2CircleShape<span style="color: #000000;">&#40;</span>r<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_circle;
			<span style="color: #009900;">// look! it's a sensor!!</span>
			my_fixture.isSensor=<span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> world_body<span style="color: #000000; font-weight: bold;">:</span>b2Body=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			world_body.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// simple function to draw a box</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> draw_box<span style="color: #000000;">&#40;</span>px,py,w,h,<span style="color: #004993;">d</span>,ud<span style="color: #000000;">&#41;</span><span style="color: #000000; 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> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				my_body.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_box<span style="color: #000000; font-weight: bold;">:</span>b2PolygonShape = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonShape<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_box.SetAsBox<span style="color: #000000;">&#40;</span>w<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale, h<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_box;
			<span style="color: #6699cc; font-weight: bold;">var</span> world_body<span style="color: #000000; font-weight: bold;">:</span>b2Body=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			world_body.SetUserData<span style="color: #000000;">&#40;</span>ud<span style="color: #000000;">&#41;</span>;
			world_body.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// function to add the player</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> add_player<span style="color: #000000;">&#40;</span>px,py<span style="color: #000000;">&#41;</span><span style="color: #000000; 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> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_circle<span style="color: #000000; font-weight: bold;">:</span>b2CircleShape=<span style="color: #0033ff; font-weight: bold;">new</span> b2CircleShape<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_circle;
			player=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			player.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// debug draw</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; 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> debug_draw<span style="color: #000000; font-weight: bold;">:</span>b2DebugDraw = <span style="color: #0033ff; font-weight: bold;">new</span> b2DebugDraw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> debug_sprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetSprite<span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetDrawScale<span style="color: #000000;">&#40;</span>world_scale<span style="color: #000000;">&#41;</span>;
			debug_draw.SetFlags<span style="color: #000000;">&#40;</span>b2DebugDraw.e_shapeBit<span style="color: #000000;">&#41;</span>;
			world.SetDebugDraw<span style="color: #000000;">&#40;</span>debug_draw<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// function to be executed at every frame</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// setting the force to null</span>
			force=<span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// according to the key(s) pressed, add the proper vector force</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">left</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">right</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>up<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>down<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #009900;">// if there is any force, then apply it</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>force.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">||</span>force.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				player.ApplyForce<span style="color: #000000;">&#40;</span>force,player.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			world.Step<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
			world.ClearForces<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// scanning through all bodies</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> worldbody<span style="color: #000000; font-weight: bold;">:</span>b2Body = world.GetBodyList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; worldbody; worldbody = worldbody.GetNext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #009900;">// if a body is marked as &quot;remove&quot;...</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>worldbody.GetUserData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>==<span style="color: #990000;">&quot;remove&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #009900;">// ... just remove it!!</span>
					world.DestroyBody<span style="color: #000000;">&#40;</span>worldbody<span style="color: #000000;">&#41;</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			world.DrawDebugData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Let&#8217;s see the interesting lines:</p>
<p><strong>Line 22</strong>: declaring my <code>contact_listener</code> variable, as <code>custom_contact_listener</code> type</p>
<p><strong>Line 25</strong>: assigning my custom contact listener class to Box2D world</p>
<p><strong>Lines 36-38</strong>: calling five times the <code>draw_coin</code> function passing three parameters: x position, y position and radius.</p>
<p><strong>Lines 79-89</strong>: the draw_coin function&#8230; just a basic function that draw a circle&#8230; just notice at <strong>line 86</strong> how I am declaring the circle as a sensor. This way the circle exists in the world but won&#8217;t physically collide with anything. Also, a sensor should be a static body, or it will fall down outside the stage as it won&#8217;t collide with anything.</p>
<p><strong>Lines 150-156</strong>: scanning through all bodies to find, and eventually remove, bodies marked with <code>remove</code>. Such marker is handled by the custom contact listener class, located in the <code>custom_contact_listener.as</code> file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td 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> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Joints.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.Contacts.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #9900cc; font-weight: bold;">class</span> custom_contact_listener extends b2ContactListener <span style="color: #000000;">&#123;</span>
		override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> BeginContact<span style="color: #000000;">&#40;</span>contact<span style="color: #000000; font-weight: bold;">:</span>b2Contact<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// getting the fixtures that collided</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> fixtureA<span style="color: #000000; font-weight: bold;">:</span>b2Fixture=contact.GetFixtureA<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> fixtureB<span style="color: #000000; font-weight: bold;">:</span>b2Fixture=contact.GetFixtureB<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// if the fixture is a sensor, mark the parent body to be removed</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>fixtureB.IsSensor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				fixtureB.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.SetUserData<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;remove&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>fixtureA.IsSensor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				fixtureA.GetBody<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.SetUserData<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;remove&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><code>BeginContact</code> function will give us the fixtures involved in the collision. Then at <strong>lines 15-17</strong> and <strong>18-20</strong> I am checking if the fixture is a sensor, then eventually mark its parent body to be removed.</p>
<p><strong>Important</strong>: don&#8217;t try to remove the body inside this function, because bodies have a <code>locked</code> status while they are in the middle of a timestep, so you should remove a body only after you performed the <code>Step</code> (<strong>line 147</strong> of the main file). This gave me a little headache, so you&#8217;ve been warned!</p>
<p>This is the result:</p>
<p><embed src="/wp-content/uploads/2010/02/ball02.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>Pick up all little circles moving the player tapping on arrow keys.</p>
<p><a href="/wp-content/uploads/2010/02/ball02.zip">Download the source code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/02/25/box2d-flash-game-creation-tutorial-part-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>11 Flash isometric engines you can use in your games</title>
		<link>http://www.emanueleferonato.com/2010/02/23/11-flash-isometric-engines-you-can-use-in-your-games/</link>
		<comments>http://www.emanueleferonato.com/2010/02/23/11-flash-isometric-engines-you-can-use-in-your-games/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 14:45:02 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>
		<category><![CDATA[Links]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2400</guid>
		<description><![CDATA[I am playing isometric games for more than 20 years&#8230; probably started with Q*bert, one of the oldest games I played is Head Over Heels on my Commodore 64, and I was an hardcore player of UFO: Enemy Unknown.
As you should know, isometric projection is a method of visually representing three-dimensional objects in two dimensions, [...]]]></description>
			<content:encoded><![CDATA[<p>I am playing isometric games for more than 20 years&#8230; probably started with <a href="http://en.wikipedia.org/wiki/Q*bert" target = "_blank">Q*bert</a>, one of the oldest games I played is <a href="http://en.wikipedia.org/wiki/Head_Over_Heels_(video_game)" target = "_blank">Head Over Heels</a> on my Commodore 64, and I was an hardcore player of <a href="http://en.wikipedia.org/wiki/UFO:_Enemy_Unknown" target = "_blank">UFO: Enemy Unknown</a>.</p>
<p>As you should know, isometric projection is a method of visually representing three-dimensional objects in two dimensions, in which the three coordinate axes appear equally foreshortened and the angles between any two of them are 120 degrees. A great way to represent 3D worlds on old 8-bit computers since their CPUs did not handle a real 3D world, and a great way to create casual adventure games nowadays.</p>
<p>I am showing you a list of 11 Flash isometric engines, some of them are free, others are commercial, and some still unreleased. I am writing to all authors to have more info about their engines, meanwhile this the most complete list I was able to make.</p>
<p>Engines are listed in alphabetical order.</p>
<p><strong>2D isometric engine</strong> &#8211; Free</p>
<p><a href="http://code.google.com/p/2d-isometric-engine/" target = "_blank">http://code.google.com/p/2d-isometric-engine/</a></p>
<p>Open source, multilevel and multiplayer tilebased isometric engine, reviewed in <a href="http://www.emanueleferonato.com/2009/08/18/as3-isometric-engine/">this post</a></p>
<p><strong>as3isolib</strong> &#8211; Free</p>
<p><img src="/wp-content/uploads/2010/02/engines_as3isolib.jpg" /></p>
<p><a href="http://as3isolib.wordpress.com/" target = "_blank">http://as3isolib.wordpress.com/</a></p>
<p>As3isolib is an open-source ActionScript 3.0 Isometric Library developed to assist in creating isometrically projected content (such as games and graphics) targeted for the Flash player platform. As3isolib includes utilities, primitives and views. As3isolib was developed with simplicity, speed and performance in mind so that developers can focus on actual implementations rather than having to learn a complex API.</p>
<p>You can see latest project using this engine at <a href="http://apps.facebook.com/downtowngame/" target = "_blank">http://apps.facebook.com/downtowngame/</a><span id="more-2400"></span></p>
<p><strong>FFilmation</strong> &#8211; Free</p>
<p><img src="/wp-content/uploads/2010/02/engines_ffilmation.jpg" /></p>
<p><a href="http://www.ffilmation.org/" target ="_blank">http://www.ffilmation.org/</a></p>
<p>The FFilmation Engine is an AS3 isometric programing engine, focused mainly on game development. The aim of the project is providing a robust development platform, where game designers can work on the game’s details and forget about the render engine. It is intended to be really usable from a “real production scenario” point of view.</p>
<p>See a demo playing <a href="http://www.ffilmation.org/mynameisponcho/" target = "_blank">My Name is Poncho</a></p>
<p><strong>Flash 3D Isometric Game Engine</strong> &#8211; Unreleased</p>
<p><a href="http://actionscripter.co.uk/blog/?page_id=76" target = "_blank">http://actionscripter.co.uk/blog/?page_id=76</a></p>
<p>Vector isometric engine allowing to zoom, rotate and pan the environment in a &#8220;real&#8221; 3D world. I&#8217;m asking the author more information, stay tuned</p>
<p><strong>isoengineas3</strong> &#8211; Free</p>
<p><a href="http://isoengineas3.sourceforge.net/" target = "_blank">http://isoengineas3.sourceforge.net/</a></p>
<p>2D Isometric Engine project build with Flash AS3. It is an open source, multilevel and multiplayer tilebased isometric engine. No more information.</p>
<p><strong>Isometric Game Engine</strong> &#8211; Unreleased</p>
<p><a href="http://www.davidworks.com/?id=int_iso" target = "_blank">http://www.davidworks.com/?id=int_iso</a></p>
<p>An isometric engine built from scratch for an online virtual campus. No more info.</p>
<p>The <a href="http://www.davidworks.com/content/int_iso/core.htm">demo</a> is quite interesting so I am going to contact the author for more information.</p>
<p><strong>Isometric terrain engine</strong> &#8211; Free</p>
<p><a href="http://yaief.wordpress.com/2008/12/05/isometric-terrain-engine/" target = "_blank">http://yaief.wordpress.com/2008/12/05/isometric-terrain-engine/</a></p>
<p>This is an isometric terrain generator, actually an abandoned project but still an inspiration.</p>
<p><strong>OpenSpace</strong> &#8211; Commercial &#8211; Trial version available</p>
<p><img src="/wp-content/uploads/2010/02/engines_openspace.jpg" /></p>
<p><a href="http://www.openspace-engine.com/">http://www.openspace-engine.com/</a></p>
<p>OpenSpace is a powerful Flash based isometric engine and framework for rapid development of multi-user virtual worlds and MMO communities.</p>
<p>Check <a href="http://www.footballvillage.net/" target ="_blank">Football Village</a> to see it in action.</p>
<p>OpenSpace is licensed on a &#8220;per-seat&#8221; basis: a separate license is required for each user working with OpenSpace.</p>
<p><strong>Sean Cooper Isometric Engine</strong> &#8211; Commercial</p>
<p><a href="http://www.games.seantcooper.com/IsometricEngine.aspx" target ="_blank">http://www.games.seantcooper.com/IsometricEngine.aspx</a></p>
<p>Made by Sean &#8220;BoxHead&#8221; Cooper, the engine is based on some techniques used in the PC game Syndicate, which he wrote in 1993. The technique is simple and extremely fast, well suited to Flash and will run fast on most machines.</p>
<p>At the moment there isn&#8217;t any project showcase, just a demo in the <a href="http://www.games.seantcooper.com/IsometricEngine.aspx" target ="_blank">official page</a>, and no info about the license, but being developed by a PROgrammer like Sean makes this engine quite interesting.</p>
<p><strong>TheoWorlds</strong> &#8211; Commercial</p>
<p><img src="/wp-content/uploads/2010/02/engines_theoworlds.jpg" /></p>
<p><a href="http://www.theoworlds.com/products.php">http://www.theoworlds.com/products.php</a></p>
<p>TheoWorlds Builder SDK v2 is a kit that will help you develop online virtual worlds much faster, without starting from scratch. At its core it contains the original Theo Isometric Engine™ that lets you create pseudo-3D maps, place on them objects and characters, and organize the interaction between them.</p>
<p>You can test the engine playing <a href="http://www.theoworlds.com/games/th/" target = "_blank">Treasure Hunter</a></p>
<p>TheoWorlds Builder SDK v2 is priced $3,200, while the light SDK v1 $799</p>
<p><strong>T.I.L.E. (Tangerine Isometric Level Editor)</strong> &#8211; Commercial</p>
<p><img src="/wp-content/uploads/2010/02/engines_tangerine.jpg" /></p>
<p><a href="http://www.tangerinepop.com/" target ="_blank">http://www.tangerinepop.com/</a> then click on &#8220;tech&#8221;</p>
<p>According to the site, T.I.L.E. provides the ability to rapidly build a standard lock &#038; key adventure game with hundreds of rooms. It also provides the primary software framework needed to build a quality MMO with numerous maps and new play modes via in increasing list of new game mechanics.</p>
<p>An example of a game developed with this technology is <a href="http://www.tangerinepop.com/GraveShift/" target = "_blank">GraveShift</a></p>
<p>Prices range from (as said on the site) &#8220;half to less than half the costs of building it yourself&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/02/23/11-flash-isometric-engines-you-can-use-in-your-games/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Monetize your Flash games with GamesChart</title>
		<link>http://www.emanueleferonato.com/2010/02/23/monetize-your-flash-games-with-gameschart/</link>
		<comments>http://www.emanueleferonato.com/2010/02/23/monetize-your-flash-games-with-gameschart/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 22:23:00 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>
		<category><![CDATA[Monetize]]></category>
		<category><![CDATA[Reviews]]></category>
		<category><![CDATA[Users contributions]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2403</guid>
		<description><![CDATA[Do you remember Emanuele Ornella from Mind the Move? It&#8217;s the guy behind the Come2Play multiplayer API tutorial posted about a month ago.
Now he is explaining us a new way to monetize Flash games: GamesChart
&#171; When I first saw the announcement on Flash Game Developers group on www.linkedIn.com about the GamesChart beta program I immediately [...]]]></description>
			<content:encoded><![CDATA[<p>Do you remember <strong>Emanuele Ornella</strong> from <a target = "_blank" href="http://www.mindthemove.com/">Mind the Move</a>? It&#8217;s the guy behind the <a href="http://www.emanueleferonato.com/2010/01/14/come2play-multiplayer-api-tutorial/">Come2Play multiplayer API tutorial</a> posted about a month ago.</p>
<p>Now he is explaining us a new way to monetize Flash games: <strong><a href="http://gameschart.com/" target = "_blank">GamesChart</a></strong></p>
<p>&laquo; When I first saw the announcement on <a href="http://www.linkedin.com/groups?home=&#038;gid=121017" target = "_blank">Flash Game Developers group</a> on www.linkedIn.com about the <strong><a href="http://gameschart.com/" target = "_blank">GamesChart</a></strong> beta program I immediately thought to participate. It was a surprise to me to find that my game <a href="http://gameschart.com/games/Haunted-House" target = "_blank">Haunted House</a> was at first position for the entire 4 weeks of the beta program!</p>
<p>Actually also my <a href="http://gameschart.com/games/Alice-Memory-1" target = "_blank">Alice Memory</a> game is also on the chart, even if never more than in the 6th position.</p>
<p><img src="/wp-content/uploads/2010/02/gc01.jpg" /></p>
<p>What <strong><a href="http://gameschart.com/" target = "_blank">GamesChart</a></strong> is about?</p>
<p>It’s another way to generate money from you Flash games. The nice thing is that it is not an alternative to the traditional banner you place during the loading of the game. But it goes together.<br />
The main idea is to have publisher to “bid” for which game will be in the chart and this will generate extra revenue to the developer, to the publisher who correctly bid and of course to the GamesChart organization.</p>
<p>This is from <strong>Barry White</strong> directly from linkedIn group.<span id="more-2403"></span></p>
<p>I thought I&#8217;d include some more information about how GamesChart can earn developers money:</p>
<p>+ Every game that has the GamesChart API embedded in it is tracked by GamesChart.</p>
<p>+ The developer can also choose to enable or disable a series of dynamic charts that actually display within the Flash game itself.</p>
<p>+ The most popular games each week make their way to the top of the charts.</p>
<p>+ Any gamers who click on a game listed in these charts is taken to play that game on another portal.</p>
<p>+ Publishers use an advanced pay per click bidding system to purchase this traffic and drive users back to their own sites to play the games they have bid on.</p>
<p>+ Games need to be uploaded to the Publishers portal before they can receive traffic. (great for seeding)</p>
<p>+ Developers receive up to a 50% revenue share of any outbound clicks generated from within their game should they choose to display the charts. (also full blacklisting available)</p>
<p>+ Publishers also receive 10% of total revenues generated from any GamesChart enabled games featured on their site.</p>
<p>I am going to explain the very easy steps to follow to have a game added to this GamesChart.</p>
<p>In order to do so I am going to provide my other game <a href="http://gameschart.com/games/Tokyo-Train" target = "_blank">Tokyo Train</a> with this service as well.</p>
<p>First thing you need to register as developer, if you don’t already. This is quite easy to do, just follow instruction on the site.</p>
<p>You then are prompted to add a game:</p>
<p><img src="/wp-content/uploads/2010/02/gc02.jpg" /></p>
<p>So do not hesitate: go to click the button and create the new game:</p>
<p><img src="/wp-content/uploads/2010/02/gc03.jpg" /></p>
<p>Instructions are easy and nice to follow: I have to admit they did a very good job on make your job easy.</p>
<p><img src="/wp-content/uploads/2010/02/gc04.jpg" /></p>
<p>You are now prompted with the game id that you need to put on the component property to have your game identified.<br />
Of course first of all you need to download the API library from the <a href="http://wiki.gameschart.com/index.php/Getting_Started" target = "_blank">“GamesChart Wiki” link</a>.</p>
<p>You have the choice to get the component for Adobe Flash IDE or the complete API if you use FlashDevelop and for both choices you can choose between AS3 and AS2. My choice was for the component and AS3.</p>
<p>The very first time you download the zip pack, you need to double click on the GamesChartAPI-AS3.mpx:</p>
<p><img src="/wp-content/uploads/2010/02/gc05.jpg" /></p>
<p>This will install the component, and that’s the only thing you really need to install: brilliant.</p>
<p>Now open your FLA and browse for the components (Windows > Components menu): you will find the GamesChartAPI one.</p>
<p><img src="/wp-content/uploads/2010/02/gc06.jpg" /></p>
<p>Now, again it’s easy: drag and drop the component onto the stage (better if at the top layer):</p>
<p><img src="/wp-content/uploads/2010/02/gc07.jpg" /></p>
<p>Do not worry about the position: it will be hidden once the game starts. Do you remember the game id? Go to grab it and paste it in the component inspector:</p>
<p><img src="/wp-content/uploads/2010/02/gc08.jpg" /></p>
<p>That’s all! You can now publish the game: you will notice a small G icon on top right of the stage.</p>
<p>You can force to have this cute icon can be hidden or display when you want. Or you can also move it.</p>
<p>I would suggest you to leave it for the entire game and in that position: it’s the best position, just adjust your buttons if you have any conflict.</p>
<p>In this way a player can click on the icon anytime during the game. This will let him vote your game and show chart. In fact when the icon is clicked the chart will appear and will let you open a new window with the games on the list.</p>
<p><img src="/wp-content/uploads/2010/02/gc09.jpg" /></p>
<p>You need one more thing: to publish the game. You can get your SWF file or you can first upload on MochiAds with version control as I am doing. This is the best solution because your game will be updated all over the world with the cute G icon.<br />
That’s all!<br />
Enjoy!</p>
<p>PS.: I just discovered that Haunted House is no more on first position! So let’s go there and click the cute “G” icon!! &raquo;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/02/23/monetize-your-flash-games-with-gameschart/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Box2D Flash game creation tutorial &#8211; part 1</title>
		<link>http://www.emanueleferonato.com/2010/02/19/box2d-flash-game-creation-tutorial-part-1/</link>
		<comments>http://www.emanueleferonato.com/2010/02/19/box2d-flash-game-creation-tutorial-part-1/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 00:16:24 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2395</guid>
		<description><![CDATA[My first Flash game tutorial ever was Flash game creation tutorial – part 1.
It was the first of a series to create a game like jmtb&#8217;s ball games. It was an old AS2 series, and some steps have been ported in AS3 with Create a Flash ball game using AS3.
The &#8220;ball&#8221; game is so simple [...]]]></description>
			<content:encoded><![CDATA[<p>My first Flash game tutorial ever was <a href="http://www.emanueleferonato.com/2006/10/29/flash-game-creation-tutorial-part-1/">Flash game creation tutorial – part 1</a>.</p>
<p>It was the first of a series to create a game like <a href="http://jmtb02.com/" target = "_blank">jmtb</a>&#8217;s ball games. It was an old AS2 series, and some steps have been ported in AS3 with <a href="http://www.emanueleferonato.com/2008/03/24/create-a-flash-ball-game-using-as3/">Create a Flash ball game using AS3</a>.</p>
<p>The &#8220;ball&#8221; game is so simple yet addictive and customizable that I think it&#8217;s the perfect game to start a tutorial series based on a new language.</p>
<p>This time I am not covering a new language but the famous Box2D library, but I am going to add all necessary features to make it an interesting game to play.</p>
<p>In this first chapter, I am going to create the ball and the way you control it, by tapping arrow keys.</p>
<p>I am using the basics of <a href="http://www.emanueleferonato.com/2010/02/16/understanding-box2d-applicable-forces/">Understanding Box2D applicable forces</a> and <a href="http://www.emanueleferonato.com/2010/02/01/box2d-tutorial-for-the-absolute-beginners-revamped/">Box2D tutorial for the absolute beginners – revamped</a>, which I recommend you to read.</p>
<p>Now this is the code:<span id="more-2395"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">KeyboardEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ball01 extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #009900;">// world creation</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world<span style="color: #000000; font-weight: bold;">:</span>b2World=<span style="color: #0033ff; font-weight: bold;">new</span> b2World<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">10.0</span><span style="color: #000000;">&#41;</span>,<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world_scale<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">30</span>;
		<span style="color: #009900;">// the player</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> player<span style="color: #000000; font-weight: bold;">:</span>b2Body;
		<span style="color: #009900;">// force to apply to the player</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> force<span style="color: #000000; font-weight: bold;">:</span>b2Vec2;
		<span style="color: #009900;">// variables to store whether the keys are pressed or not</span>
		<span style="color: #009900;">// true = pressed;</span>
		<span style="color: #009900;">// false = unpressed</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">left</span>,<span style="color: #004993;">right</span>,up,down<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ball01<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// calling thebug draw function</span>
			debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// drawing the boundaries</span>
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;ground&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">200</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;left&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">200</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;right&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;roof&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// adding the player at 250,200</span>
			<span style="color: #009900;">// some listeners</span>
			add_player<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">200</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, update<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_DOWN</span>,on_key_down<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_UP</span>,on_key_up<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// according to the key pressed, set the proper variable to &quot;true&quot;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> on_key_down<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; 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;">switch</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span><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;">37</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">left</span>=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">38</span> <span style="color: #000000; font-weight: bold;">:</span>
					up=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">39</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">right</span>=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">40</span> <span style="color: #000000; font-weight: bold;">:</span>
					down=<span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// according to the key released, set the proper variable to &quot;false&quot;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> on_key_up<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; 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;">switch</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span><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;">37</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">left</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">38</span> <span style="color: #000000; font-weight: bold;">:</span>
					up=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">39</span> <span style="color: #000000; font-weight: bold;">:</span>
					<span style="color: #004993;">right</span>=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">40</span> <span style="color: #000000; font-weight: bold;">:</span>
					down=<span style="color: #0033ff; font-weight: bold;">false</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// simple function to draw a box</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> draw_box<span style="color: #000000;">&#40;</span>px,py,w,h,<span style="color: #004993;">d</span>,ud<span style="color: #000000;">&#41;</span><span style="color: #000000; 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> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				my_body.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_box<span style="color: #000000; font-weight: bold;">:</span>b2PolygonShape = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonShape<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_box.SetAsBox<span style="color: #000000;">&#40;</span>w<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale, h<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_box;
			<span style="color: #6699cc; font-weight: bold;">var</span> world_body<span style="color: #000000; font-weight: bold;">:</span>b2Body=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			world_body.SetUserData<span style="color: #000000;">&#40;</span>ud<span style="color: #000000;">&#41;</span>;
			world_body.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// function to add the player</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> add_player<span style="color: #000000;">&#40;</span>px,py<span style="color: #000000;">&#41;</span><span style="color: #000000; 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> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_circle<span style="color: #000000; font-weight: bold;">:</span>b2CircleShape=<span style="color: #0033ff; font-weight: bold;">new</span> b2CircleShape<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_circle;
			player=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			player.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// debug draw</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; 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> debug_draw<span style="color: #000000; font-weight: bold;">:</span>b2DebugDraw = <span style="color: #0033ff; font-weight: bold;">new</span> b2DebugDraw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> debug_sprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetSprite<span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetDrawScale<span style="color: #000000;">&#40;</span>world_scale<span style="color: #000000;">&#41;</span>;
			debug_draw.SetFlags<span style="color: #000000;">&#40;</span>b2DebugDraw.e_shapeBit<span style="color: #000000;">&#41;</span>;
			world.SetDebugDraw<span style="color: #000000;">&#40;</span>debug_draw<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #009900;">// functiojn to be executed at every frame</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">// setting the force to null</span>
			force=<span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">// according to the key(s) pressed, add the proper vector force</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">left</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">right</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>up<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>down<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				force.Add<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #009900;">// if there is any force, then apply it</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>force.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">||</span>force.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				player.ApplyForce<span style="color: #000000;">&#40;</span>force,player.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			world.Step<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
			world.ClearForces<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			world.DrawDebugData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, I did not change that much from <a href="http://www.emanueleferonato.com/2010/02/16/understanding-box2d-applicable-forces/">Understanding Box2D applicable forces</a> and <a href="http://www.emanueleferonato.com/2010/02/01/box2d-tutorial-for-the-absolute-beginners-revamped/">Box2D tutorial for the absolute beginners – revamped</a>.</p>
<p><code>draw_circle </code> function at <strong>lines 19-30</strong> in <a href="http://www.emanueleferonato.com/2010/02/01/box2d-tutorial-for-the-absolute-beginners-revamped/">Box2D tutorial for the absolute beginners – revamped</a> now is called add_player (<strong>lines 86-95</strong>) and places a circle in the (<code>px</code>,<code>py</code>) coordinates.</p>
<p>The body is named <code>player</code> and is declared in the class at <strong>line 14</strong>, then is used at <strong>line 125</strong> to have the force applied with ApplyForce explained at <a href="http://www.emanueleferonato.com/2010/02/16/understanding-box2d-applicable-forces/">Understanding Box2D applicable forces</a>.</p>
<p>Notice how different forces are applied according to the arrow key pressed&#8230; playing with these values will change the gameplay&#8230; which values would you use?</p>
<p>This is the result:</p>
<p><embed src="/wp-content/uploads/2010/02/ball01.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>Tap arrow keys to move the ball.</p>
<p><a href="/wp-content/uploads/2010/02/ball01.zip">Download the source code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/02/19/box2d-flash-game-creation-tutorial-part-1/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Understanding Box2D applicable forces</title>
		<link>http://www.emanueleferonato.com/2010/02/16/understanding-box2d-applicable-forces/</link>
		<comments>http://www.emanueleferonato.com/2010/02/16/understanding-box2d-applicable-forces/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 12:19:07 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2378</guid>
		<description><![CDATA[In Box2d, bodies aren&#8217;t only affected by gravity and collisions, but you can also apply forces to them.
Knowing the right force to apply is very important when you want to control a body, as you may want to do in a Flash game
Let&#8217;s see the forces you can apply:
Applying a force
public function ApplyForce(force:b2Vec2, point:b2Vec2):void
Apply a [...]]]></description>
			<content:encoded><![CDATA[<p>In Box2d, bodies aren&#8217;t only affected by gravity and collisions, but you can also apply forces to them.</p>
<p>Knowing the right force to apply is very important when you want to control a body, as you may want to do in a Flash game</p>
<p>Let&#8217;s see the forces you can apply:</p>
<p><strong>Applying a force</strong></p>
<p><code>public function ApplyForce(force:b2Vec2, point:b2Vec2):void</code></p>
<p>Apply a force at a world point. If the force is not applied at the center of mass, it will generate a torque and affect the angular velocity. This wakes up the body, so you don&#8217;t have to wake it up by yourself.</p>
<p>The force is applied inNewtons (N). </p>
<p><strong>Applying an impulse</strong></p>
<p><code>public function ApplyImpulse(impulse:b2Vec2, point:b2Vec2):void</code></p>
<p>Apply an impulse at a point. This immediately modifies the velocity. It also modifies the angular velocity if the point of application is not at the center of mass. This wakes up the body, so you don&#8217;t have to wake it up by yourself.</p>
<p>The impulse is applied in Newton-seconds or kg-m/s. </p>
<p><strong>Setting a linear velocity</strong></p>
<p><code>public function SetLinearVelocity(v:b2Vec2):void</code></p>
<p>Set the linear velocity of the center of mass. This function will <strong>not</strong> work if the body is sleeping, so you must wake it up with <code>SetAwake</code> before applying the velocity.</p>
<p><strong>What force should I apply?</strong></p>
<p>You should choose the force according to the result you want to achieve. Let&#8217;s suppose you want to apply a vertical force, to make something jump in the air, and let&#8217;s suppose you can apply the force at any time, even when you just applied it.</p>
<p>You will have three cases:</p>
<p><strong>Applying the force when the body is not moving</strong>: this happens when you apply the force when the body lies on the ground or when it&#8217;s in the air, in the moment it finished its &#8220;jumping&#8221; speed and it&#8217;s about to fall down. In this case there aren&#8217;t any noticeable difference among applied forces.</p>
<p><strong>Applying the force when the body is moving up</strong>: this happens when the body is in the air and still has &#8220;jumping&#8221; speed. In this case, applying a force or an impulse will sum the &#8220;jumping&#8221; speed to the applied force to produce a &#8220;boost&#8221;, while setting the velocity again will just apply the velocity again, no matter the &#8220;jumping speed&#8221;</p>
<p><strong>Applying the force when the body is falling down</strong>: this happens when the body finished its &#8220;jumping&#8221; speed and it&#8217;s falling down. As in the previous case, applying a force or an impulse will sum the falling speed to the applied force, and according to falling speed and applied force, the body will continue falling or jump a little (how little? the difference between the falling force and the applied one). Setting the velocity will just stop the body to fall and make it jumping again, no matter of the falling speed.<span id="more-2378"></span></p>
<p><strong>The script</strong></p>
<p>This script will generate three tiny boxes. The left one is moved with a force, the middle one with an impulse, and the third one with a set velocity. You will apply forces clicking the mouse</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> forces extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world<span style="color: #000000; font-weight: bold;">:</span>b2World=<span style="color: #0033ff; font-weight: bold;">new</span> b2World<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">10.0</span><span style="color: #000000;">&#41;</span>,<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world_scale<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">30</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> forces<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">&quot;ground&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">&quot;left&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">&quot;middle&quot;</span><span style="color: #000000;">&#41;</span>;
			draw_box<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">400</span>,<span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">&quot;right&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, update<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>,applyforces<span style="color: #000000;">&#41;</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> draw_box<span style="color: #000000;">&#40;</span>px,py,w,h,<span style="color: #004993;">d</span>,ud<span style="color: #000000;">&#41;</span><span style="color: #000000; 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> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">/</span>world_scale, py<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				my_body.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_box<span style="color: #000000; font-weight: bold;">:</span>b2PolygonShape = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonShape<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_box.SetAsBox<span style="color: #000000;">&#40;</span>w<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale, h<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.shape=my_box;
			<span style="color: #6699cc; font-weight: bold;">var</span> world_body<span style="color: #000000; font-weight: bold;">:</span>b2Body=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			world_body.SetUserData<span style="color: #000000;">&#40;</span>ud<span style="color: #000000;">&#41;</span>;
			world_body.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</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> debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; 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> debug_draw<span style="color: #000000; font-weight: bold;">:</span>b2DebugDraw = <span style="color: #0033ff; font-weight: bold;">new</span> b2DebugDraw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> debug_sprite<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetSprite<span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetDrawScale<span style="color: #000000;">&#40;</span>world_scale<span style="color: #000000;">&#41;</span>;
			debug_draw.SetFlags<span style="color: #000000;">&#40;</span>b2DebugDraw.e_shapeBit<span style="color: #000000;">&#41;</span>;
			world.SetDebugDraw<span style="color: #000000;">&#40;</span>debug_draw<span style="color: #000000;">&#41;</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> applyforces<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; 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> force<span style="color: #000000; font-weight: bold;">:</span>b2Vec2;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> worldbody<span style="color: #000000; font-weight: bold;">:</span>b2Body = world.GetBodyList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; worldbody; worldbody = worldbody.GetNext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</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>worldbody.GetUserData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;left&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						force=<span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">450</span><span style="color: #000000;">&#41;</span>;
						worldbody.ApplyForce<span style="color: #000000;">&#40;</span>force,worldbody.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;middle&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						force=<span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">15</span><span style="color: #000000;">&#41;</span>;
						worldbody.ApplyImpulse<span style="color: #000000;">&#40;</span>force,worldbody.GetWorldCenter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;right&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						force=<span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">15</span><span style="color: #000000;">&#41;</span>;
						worldbody.SetAwake<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
						worldbody.SetLinearVelocity<span style="color: #000000;">&#40;</span>force<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #000000;">&#125;</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> update<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			world.Step<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">30</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
			world.ClearForces<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			world.DrawDebugData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And this is the result:</p>
<p><embed src="/wp-content/uploads/2010/02/forces.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>Apply forces by clicking the mouse in various times, to see the difference among <code>ApplyForce</code>, <code>ApplyImpulse</code> and <code>SetLinearVelocity</code>.</p>
<p><a href="/wp-content/uploads/2010/02/forces.zip">Download the source code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/02/16/understanding-box2d-applicable-forces/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Flash AS3 Pixel based circle collision engine</title>
		<link>http://www.emanueleferonato.com/2010/02/13/flash-as3-pixel-based-circle-collision-engine/</link>
		<comments>http://www.emanueleferonato.com/2010/02/13/flash-as3-pixel-based-circle-collision-engine/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 23:37:07 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>
		<category><![CDATA[Users contributions]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2368</guid>
		<description><![CDATA[After seeing the Lineball video teaser I got some email asking me how did I make the hollow circle with Box2D, and how to get a smooth drawing using box primitives.
Well, I have to say I didn&#8217;t use Box2d, but another library called Collision Detection Kit.
I&#8217;ll publish some tutorial about it once I&#8217;ll complete the [...]]]></description>
			<content:encoded><![CDATA[<p>After seeing the <a href="http://www.emanueleferonato.com/2010/02/11/lineball-video-teaser/">Lineball video teaser</a> I got some email asking me how did I make the hollow circle with Box2D, and how to get a smooth drawing using box primitives.</p>
<p>Well, I have to say I didn&#8217;t use Box2d, but another library called <a href="http://www.coreyoneil.com/portfolio/index.php?project=5" target = "_blank">Collision Detection Kit</a>.</p>
<p>I&#8217;ll publish some tutorial about it once I&#8217;ll complete the game, but it&#8217;s not the point of today&#8217;s post.</p>
<p>A reader from Argentina, <strong>Adolfo Chacon</strong>, sent me an AS3 basic engine to do the same thing (maybe even better) using the concepts I explained soooooo long ago in the <a href="http://www.emanueleferonato.com/2007/01/08/create-a-flash-draw-game-like-line-rider-or-others-part-1/">Create a flash draw game like Line Rider or others</a> series.</p>
<p>It&#8217;s a basic, uncommented script because it&#8217;s just a prototype&#8230; basically Adolfo took my old script, adjusted trigonometry and did the magic dividing the simulation in steps to manage slower speeds.</p>
<p>I resized it, changed variable names (translating from spanish) and some operators since the auto format option gave me errors when trying to format <code>a&lt;-b</code>, forcing me to change into <code>a&lt;b*-1</code>.</p>
<p>Now enjoy the script<span id="more-2368"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> pixel_engine extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> ball_radius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> precision<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">120</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> degrees_to_radians<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0.0174532925</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> x_speed<span style="color: #000000; 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;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> y_speed<span style="color: #000000; 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;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> friction<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0.9</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> max_speed<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">15</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> gravity<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0.98</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> steps=<span style="color: #000000; font-weight:bold;">2</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> ball<span style="color: #000000; font-weight: bold;">:</span>ball_mc = <span style="color: #0033ff; font-weight: bold;">new</span> ball_mc<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> terrain<span style="color: #000000; font-weight: bold;">:</span>terrain_mc = <span style="color: #0033ff; font-weight: bold;">new</span> terrain_mc<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> pixel_engine<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>ball<span style="color: #000000;">&#41;</span>;
			ball.<span style="color: #004993;">x</span>=<span style="color: #000000; font-weight:bold;">250</span>;
			ball_radius=ball.<span style="color: #004993;">width</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>terrain<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, update<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_DOWN</span>, reverse_gravity<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> reverse_gravity<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			gravity<span style="color: #000000; font-weight: bold;">*</span>=<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</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> update<span style="color: #000000;">&#40;</span>e <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; 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>ball.y<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight:bold;">400</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				ball.y<span style="color: #000000; font-weight: bold;">-</span>=<span style="color: #000000; font-weight:bold;">400</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>ball.y<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				ball.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">+</span>=<span style="color: #000000; font-weight:bold;">400</span>;
			<span style="color: #000000;">&#125;</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> j<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">1</span>; j<span style="color: #000000; font-weight: bold;">&lt;</span>=steps; j<span style="color: #000000; 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> number_of_collisions<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">0</span>;
				<span style="color: #6699cc; font-weight: bold;">var</span> sum_x_collision<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0</span>;
				<span style="color: #6699cc; font-weight: bold;">var</span> sum_y_collision<span style="color: #000000; 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;">if</span> <span style="color: #000000;">&#40;</span>x_speed<span style="color: #000000; font-weight: bold;">&gt;</span>max_speed<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					x_speed=max_speed;
				<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>x_speed<span style="color: #000000; font-weight: bold;">&lt;</span>max_speed<span style="color: #000000; font-weight: bold;">*-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						x_speed=<span style="color: #000000; font-weight: bold;">-</span> max_speed;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>y_speed<span style="color: #000000; font-weight: bold;">&gt;</span>max_speed<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					y_speed=max_speed;
				<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>y_speed<span style="color: #000000; font-weight: bold;">&lt;</span>max_speed<span style="color: #000000; font-weight: bold;">*-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						y_speed=<span style="color: #000000; font-weight: bold;">-</span> max_speed;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
				ball.<span style="color: #004993;">x</span>=ball.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">+</span>x_speed<span style="color: #000000; font-weight: bold;">/</span>steps;
				ball.<span style="color: #004993;">y</span>=ball.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">+</span>y_speed<span style="color: #000000; font-weight: bold;">/</span>steps;
				y_speed=y_speed<span style="color: #000000; font-weight: bold;">+</span>gravity<span style="color: #000000; font-weight: bold;">/</span>steps;
				<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;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">1</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>=precision; i<span style="color: #000000; 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> x_test<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=ball.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">+</span>ball_radius<span style="color: #000000; font-weight: bold;">*</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">sin</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">360</span><span style="color: #000000; font-weight: bold;">/</span>precision<span style="color: #000000; font-weight: bold;">*</span>degrees_to_radians<span style="color: #000000;">&#41;</span>;
					<span style="color: #6699cc; font-weight: bold;">var</span> y_test<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=ball.y<span style="color: #000000; font-weight: bold;">-</span>ball_radius<span style="color: #000000; font-weight: bold;">*</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">cos</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">360</span><span style="color: #000000; font-weight: bold;">/</span>precision<span style="color: #000000; font-weight: bold;">*</span>degrees_to_radians<span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>terrain.<span style="color: #004993;">hitTestPoint</span><span style="color: #000000;">&#40;</span>x_test,y_test,<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>
						number_of_collisions<span style="color: #000000; font-weight: bold;">++</span>;
						sum_x_collision=sum_x_collision<span style="color: #000000; font-weight: bold;">+</span>x_test;
						sum_y_collision=sum_y_collision<span style="color: #000000; font-weight: bold;">+</span>y_test;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> average_x<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=sum_x_collision<span style="color: #000000; font-weight: bold;">/</span>number_of_collisions;
				<span style="color: #6699cc; font-weight: bold;">var</span> average_y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=sum_y_collision<span style="color: #000000; font-weight: bold;">/</span>number_of_collisions;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>number_of_collisions<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #6699cc; font-weight: bold;">var</span> collision_angle<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #004993;">Math</span>.<span style="color: #004993;">atan2</span><span style="color: #000000;">&#40;</span>ball.x<span style="color: #000000; font-weight: bold;">-</span>average_x,ball.y<span style="color: #000000; font-weight: bold;">-</span>average_y<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>degrees_to_radians<span style="color: #000000; font-weight: bold;">*-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">90</span>;
					<span style="color: #6699cc; font-weight: bold;">var</span> collision_x<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = ball.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">-</span> ball_radius <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">sin</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>collision_angle <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">90</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">*</span> degrees_to_radians<span style="color: #000000;">&#41;</span>;
					<span style="color: #6699cc; font-weight: bold;">var</span> collision_y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = ball.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">+</span> ball_radius <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">cos</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>collision_angle <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">90</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">*</span> degrees_to_radians<span style="color: #000000;">&#41;</span>;
					<span style="color: #6699cc; font-weight: bold;">var</span> delta_x<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000;">&#40;</span>collision_x <span style="color: #000000; font-weight: bold;">-</span> ball.<span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> ball_radius <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>;
					<span style="color: #6699cc; font-weight: bold;">var</span> delta_y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000;">&#40;</span>collision_y <span style="color: #000000; font-weight: bold;">-</span> ball.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> ball_radius <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>;
					<span style="color: #6699cc; font-weight: bold;">var</span> speed<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #004993;">Math</span>.<span style="color: #004993;">sqrt</span><span style="color: #000000;">&#40;</span>x_speed<span style="color: #000000; font-weight: bold;">*</span>x_speed<span style="color: #000000; font-weight: bold;">+</span>y_speed<span style="color: #000000; font-weight: bold;">*</span>y_speed<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span>friction;
					<span style="color: #6699cc; font-weight: bold;">var</span> bounce<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #004993;">Math</span>.<span style="color: #004993;">atan2</span><span style="color: #000000;">&#40;</span>x_speed,y_speed<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>degrees_to_radians<span style="color: #000000; font-weight: bold;">*-</span><span style="color: #000000; font-weight:bold;">1</span>;
					<span style="color: #6699cc; font-weight: bold;">var</span> ricochet<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">*</span>collision_angle<span style="color: #000000; font-weight: bold;">-</span>bounce<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">180</span>;
					x_speed=<span style="color: #004993;">Math</span>.<span style="color: #004993;">sin</span><span style="color: #000000;">&#40;</span>ricochet<span style="color: #000000; font-weight: bold;">*</span>degrees_to_radians<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span>speed;
					y_speed=<span style="color: #000000; font-weight: bold;">-</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">cos</span><span style="color: #000000;">&#40;</span>ricochet<span style="color: #000000; font-weight: bold;">*</span>degrees_to_radians<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span>speed;
					<span style="color: #0033ff; font-weight: bold;">while</span> <span style="color: #000000;">&#40;</span>terrain.<span style="color: #004993;">hitTestPoint</span><span style="color: #000000;">&#40;</span>collision_x, collision_y,<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>
						ball.<span style="color: #004993;">x</span>=ball.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">+</span>delta_x;
						ball.<span style="color: #004993;">y</span>=ball.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">+</span>delta_y;
						collision_x=ball.x<span style="color: #000000; font-weight: bold;">-</span>delta_x<span style="color: #000000; font-weight: bold;">*</span>ball_radius;
						collision_y=ball.y<span style="color: #000000; font-weight: bold;">-</span>delta_y<span style="color: #000000; font-weight: bold;">*</span>ball_radius;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And play with the result&#8230; just watch the simulation and press left mouse button to reverse gravity.</p>
<p><embed src="/wp-content/uploads/2010/02/pixel_engine.as_.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p><a href="/wp-content/uploads/2010/02/pixel_engine.as_.zip">Download the source code</a>. Who said draw games are dead?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/02/13/flash-as3-pixel-based-circle-collision-engine/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
