<?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>Done With Computers</title>
	<atom:link href="http://rengelbert.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://rengelbert.com/blog</link>
	<description>Portfolio and Personal Site of Roger Engelbert</description>
	<lastBuildDate>Wed, 22 May 2013 08:34:36 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Moon-Herder WebGL: with Cocos2d HTML5</title>
		<link>http://rengelbert.com/blog/moon-herder-webgl-with-cocos2d-html5/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=moon-herder-webgl-with-cocos2d-html5</link>
		<comments>http://rengelbert.com/blog/moon-herder-webgl-with-cocos2d-html5/#comments</comments>
		<pubDate>Sun, 19 May 2013 14:00:41 +0000</pubDate>
		<dc:creator>roger</dc:creator>
				<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://rengelbert.com/blog/?p=354</guid>
		<description><![CDATA[<p><span class="drop-cap">A</span> quick update. I downloaded the latest <a href="http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Cocos2d-html5" target="_blank">Cocos2d-HTML5</a> version, with the fixes for missing particle textures, and you can now run the same JavaScript code for canvas and/or WebGL rendering.</p>
<p>You can download the source for this version on <a href="https://github.com/rengelbert/moon-herder-webgl" target="_blank">github</a>.</p>
<p><a href="http://www.rengelbert.com/blog/html5/MoonHerder-WebGL/" target="_blank">You can run the game here.</a></p>]]></description>
				<content:encoded><![CDATA[<div id="tweetbutton354" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Frengelbert.com%2Fblog%2Fmoon-herder-webgl-with-cocos2d-html5%2F&amp;text=Moon-Herder%20WebGL%3A%20with%20Cocos2d%20HTML5&amp;related=&amp;lang=en&amp;count=none&amp;counturl=http%3A%2F%2Frengelbert.com%2Fblog%2Fmoon-herder-webgl-with-cocos2d-html5%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://rengelbert.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><span class="drop-cap">A</span> quick update. I downloaded the latest <a href="http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Cocos2d-html5" target="_blank">Cocos2d-HTML5</a> version, with the fixes for missing particle textures, and you can now run the same JavaScript code for canvas and/or WebGL rendering.</p>
<p>You can download the source for this version on <a href="https://github.com/rengelbert/moon-herder-webgl" target="_blank">github</a>.</p>
<p><a href="http://www.rengelbert.com/blog/html5/MoonHerder-WebGL/" target="_blank">You can run the game here.</a></p>
<p>And you can even use the <a href="http://rengelbert.com/blog/html5/MoonHerder-WebGL/simulator.html" target="_blank">new simulator here</a>, just press play.</p>
<p>Nothing much changes in this version, except for the <b>renderMode</b> flag you add to your configuration object (you&#8217;ll find this inside the file MoonHerder-html5.js, set to a value of 2, which means WEBGL.)</p>
<p>Of notice are the changes I had to make to the drawing logic in the game. This remains the one bit of logic in Cocos2D-HTML5 that still needs to be ported differently depending on your target. But if you are interested in learning how to draw primitives in your games I suggest you compare the logic inside the DrawLayer.js script from the various different versions you&#8217;ll find on my github account.</p>
]]></content:encoded>
			<wfw:commentRss>http://rengelbert.com/blog/moon-herder-webgl-with-cocos2d-html5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=rengelbert&amp;popout=1&amp;url=http%3A%2F%2Frengelbert.com%2Fblog%2Fmoon-herder-webgl-with-cocos2d-html5%2F&amp;language=en_GB&amp;category=text&amp;title=Moon-Herder+WebGL%3A+with+Cocos2d+HTML5&amp;description=TweetA+quick+update.+I+downloaded+the+latest+Cocos2d-HTML5+version%2C+with+the+fixes+for+missing+particle+textures%2C+and+you+can+now+run+the+same+JavaScript+code+for+canvas+and%2For+WebGL+rendering....&amp;tags=blog%2Cflash%2Cactionscript%2Cgame+development%2Cios%2Ciphone%2Cipad%2Candroid%2Clibgdx%2Ccocos2d%2Ccocos2d-x%2Csparrow%2Cprogramming%2Cnd2d%2Cstarling%2Cbox2d%2Cgames%2Ctutorials" type="text/html" />
	</item>
		<item>
		<title>The Doctor and EaselJS</title>
		<link>http://rengelbert.com/blog/the-doctor-and-easeljs/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-doctor-and-easeljs</link>
		<comments>http://rengelbert.com/blog/the-doctor-and-easeljs/#comments</comments>
		<pubDate>Wed, 01 May 2013 00:01:00 +0000</pubDate>
		<dc:creator>roger</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[easelJS Tutorial]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[HTML5 Tutorial]]></category>
		<category><![CDATA[Javascript Tutorial]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://rengelbert.com/blog/the-doctor-and-easeljs/</guid>
		<description><![CDATA[
<img class ="entry-img" src="http://www.rengelbert.com/blog/wp-content/themes/donewithcomputers/images/entries/img_easeljs.png"/>

<p><span class="drop-cap">I</span>'ve decided to take a very simple application done in Flash and transform it in JavaScript for Canvas, using an open source framework, trying to use as many features typically associated with Flash as I could cram in a tiny bit of code, and see how the framework handled them (mainly: events, bitmap data manipulation, the display list and masks).</p>

]]></description>
				<content:encoded><![CDATA[<div id="tweetbutton352" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Frengelbert.com%2Fblog%2Fthe-doctor-and-easeljs%2F&amp;text=The%20Doctor%20and%20EaselJS&amp;related=&amp;lang=en&amp;count=none&amp;counturl=http%3A%2F%2Frengelbert.com%2Fblog%2Fthe-doctor-and-easeljs%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://rengelbert.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><img class ="entry-img" src="http://www.rengelbert.com/blog/wp-content/themes/donewithcomputers/images/entries/img_easeljs.png"/></p>
<p><span class="drop-cap">I</span>&#8216;ve decided to take a very simple application done in Flash and transform it in JavaScript for Canvas, using an open source framework, trying to use as many features typically associated with Flash as I could cram in a tiny bit of code, and see how the framework handled them (mainly: events, bitmap data manipulation, the display list and masks).</p>
<p>The application is the core logic inside an old project, FlashNose3D, which I used years ago to talk about PureMVC. Only here, I&#8217;m just using the animation bit.</p>
<p>The framework I picked was EaselJS, which is based on ActionScript.</p>
<p>Here is the test application in Flash, using circles:</p>
<div class="flash-game" style="width:600px;">

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_effectAS3_1387307636"
			class="flashmovie"
			width="600"
			height="450">
	<param name="movie" value="http://www.rengelbert.com/blog/swf/effectAS3.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.rengelbert.com/blog/swf/effectAS3.swf"
			name="fm_effectAS3_1387307636"
			width="600"
			height="450">
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
</div>
<p>And here are the classes:</p>
<h3>Main.as</h3>
<div class="as-code">

<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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #db5821; font-weight: bold;">package</span>  <span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #3399cc; font-weight: bold;">import</span> <span style="color: #db5821;">flash.display</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">Sprite</span><span style="color: #666666; font-weight: bold;">;</span>
	<span style="color: #3399cc; font-weight: bold;">import</span> <span style="color: #db5821;">flash.events</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">Event</span><span style="color: #666666; font-weight: bold;">;</span>
	<span style="color: #3399cc; font-weight: bold;">import</span> <span style="color: #db5821;">flash.geom</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">Point</span><span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #3399cc; font-weight: bold;">public</span> <span style="color: #db5821; font-weight: bold;">class</span> Main <span style="color: #3399cc; font-weight: bold;">extends</span> <span style="color: #3399cc;">Sprite</span> <span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #3399cc; font-weight: bold;">private</span> <span style="color: #db5821; font-weight: bold;">var</span> _slices<span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">Array</span><span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #3399cc; font-weight: bold;">function</span> Main <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			_slices = <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #db5821; font-weight: bold;">var</span> startWidth<span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">int</span> = <span style="color: #db5821; font-weight:bold;">400</span><span style="color: #666666; font-weight: bold;">;</span>
			<span style="color: #db5821; font-weight: bold;">var</span> numSlices<span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">int</span> = <span style="color: #db5821; font-weight:bold;">40</span><span style="color: #666666; font-weight: bold;">;</span>
			<span style="color: #db5821; font-weight: bold;">var</span> fraction<span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">int</span> = startWidth<span style="color: #666666; font-weight: bold;">/</span><span style="color: #000000;">&#40;</span>numSlices <span style="color: #666666; font-weight: bold;">-</span> <span style="color: #db5821; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
			<span style="color: #db5821; font-weight: bold;">var</span> s<span style="color: #666666; font-weight: bold;">:</span>Slice<span style="color: #666666; font-weight: bold;">;</span>
			<span style="color: #3399cc; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #db5821; font-weight: bold;">var</span> i<span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">int</span> = <span style="color: #db5821; font-weight:bold;">0</span><span style="color: #666666; font-weight: bold;">;</span> i <span style="color: #666666; font-weight: bold;">&lt;</span> numSlices<span style="color: #666666; font-weight: bold;">;</span> i<span style="color: #666666; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				s = <span style="color: #3399cc; font-weight: bold;">new</span> Slice <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>startWidth <span style="color: #666666; font-weight: bold;">-</span> i <span style="color: #666666; font-weight: bold;">*</span> fraction<span style="color: #000000;">&#41;</span> <span style="color: #666666; font-weight: bold;">*</span> <span style="color: #db5821; font-weight:bold;">0.5</span><span style="color: #666666; font-weight: bold;">,</span> i<span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
				s<span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">x</span> = <span style="color: #3399cc;">stage</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">stageWidth</span> <span style="color: #666666; font-weight: bold;">&gt;&gt;</span> <span style="color: #db5821; font-weight:bold;">1</span><span style="color: #666666; font-weight: bold;">;</span>
				s<span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">y</span> = <span style="color: #3399cc;">stage</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">stageHeight</span> <span style="color: #666666; font-weight: bold;">&gt;&gt;</span> <span style="color: #db5821; font-weight:bold;">1</span><span style="color: #666666; font-weight: bold;">;</span>
				<span style="color: #3399cc; font-weight: bold;">this</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">addChild</span><span style="color: #000000;">&#40;</span>s<span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
				_slices<span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">push</span><span style="color: #000000;">&#40;</span>s<span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #3399cc;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #3399cc;">Event</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #db5821;">ENTER_FRAME</span><span style="color: #666666; font-weight: bold;">,</span> on_Loop<span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
		<span style="color: #3399cc; font-weight: bold;">private</span> <span style="color: #3399cc; font-weight: bold;">function</span> on_Loop <span style="color: #000000;">&#40;</span>event<span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #db5821; font-weight: bold;">var</span> len<span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">int</span> = _slices<span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">length</span><span style="color: #666666; font-weight: bold;">;</span>
			<span style="color: #db5821; font-weight: bold;">var</span> s<span style="color: #666666; font-weight: bold;">:</span>Slice<span style="color: #666666; font-weight: bold;">;</span>
			<span style="color: #db5821; font-weight: bold;">var</span> previous<span style="color: #666666; font-weight: bold;">:</span>Slice<span style="color: #666666; font-weight: bold;">;</span>
			<span style="color: #3399cc; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #db5821; font-weight: bold;">var</span> i<span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">int</span> = len<span style="color: #666666; font-weight: bold;">-</span><span style="color: #db5821; font-weight:bold;">1</span><span style="color: #666666; font-weight: bold;">;</span> i <span style="color: #666666; font-weight: bold;">&gt;</span>= <span style="color: #db5821; font-weight:bold;">0</span><span style="color: #666666; font-weight: bold;">;</span> i<span style="color: #666666; font-weight: bold;">--</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				s = _slices<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #666666; font-weight: bold;">;</span>
				<span style="color: #3399cc; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>i == len<span style="color: #666666; font-weight: bold;">-</span><span style="color: #db5821; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					s<span style="color: #666666; font-weight: bold;">.</span>update<span style="color: #000000;">&#40;</span><span style="color: #3399cc; font-weight: bold;">new</span> <span style="color: #3399cc;">Point</span><span style="color: #000000;">&#40;</span><span style="color: #3399cc;">mouseX</span><span style="color: #666666; font-weight: bold;">,</span> <span style="color: #3399cc;">mouseY</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
				<span style="color: #000000;">&#125;</span> <span style="color: #3399cc; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
					previous = _slices<span style="color: #000000;">&#91;</span>i <span style="color: #666666; font-weight: bold;">+</span> <span style="color: #db5821; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #666666; font-weight: bold;">;</span>
					s<span style="color: #666666; font-weight: bold;">.</span>update<span style="color: #000000;">&#40;</span><span style="color: #3399cc; font-weight: bold;">new</span> <span style="color: #3399cc;">Point</span><span style="color: #000000;">&#40;</span>previous<span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">x</span><span style="color: #666666; font-weight: bold;">,</span> previous<span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</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>

</div>
<h3>Slice.as</h3>
<div class="as-code">

<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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #db5821; font-weight: bold;">package</span>  <span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #3399cc; font-weight: bold;">import</span> <span style="color: #db5821;">flash.display</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">Sprite</span><span style="color: #666666; font-weight: bold;">;</span>
	<span style="color: #3399cc; font-weight: bold;">import</span> <span style="color: #db5821;">flash.geom</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">Point</span><span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #3399cc; font-weight: bold;">public</span> <span style="color: #db5821; font-weight: bold;">class</span> Slice <span style="color: #3399cc; font-weight: bold;">extends</span> <span style="color: #3399cc;">Sprite</span> <span style="color: #000000;">&#123;</span>
&nbsp;
&nbsp;
		<span style="color: #3399cc; font-weight: bold;">private</span> <span style="color: #db5821; font-weight: bold;">var</span> _vx<span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">Number</span> = <span style="color: #db5821; font-weight:bold;">0</span><span style="color: #666666; font-weight: bold;">;</span>
		<span style="color: #3399cc; font-weight: bold;">private</span> <span style="color: #db5821; font-weight: bold;">var</span> _vy<span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">Number</span> = <span style="color: #db5821; font-weight:bold;">0</span><span style="color: #666666; font-weight: bold;">;</span>
		<span style="color: #3399cc; font-weight: bold;">private</span> <span style="color: #db5821; font-weight: bold;">var</span> _friction<span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">Number</span> = <span style="color: #db5821; font-weight:bold;">0.2</span><span style="color: #666666; font-weight: bold;">;</span>
		<span style="color: #3399cc; font-weight: bold;">private</span> <span style="color: #db5821; font-weight: bold;">var</span> _spring<span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">Number</span> = <span style="color: #db5821; font-weight:bold;">2</span><span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #3399cc; font-weight: bold;">function</span> Slice <span style="color: #000000;">&#40;</span>radius<span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">int</span><span style="color: #666666; font-weight: bold;">,</span> <span style="color: #3399cc;">index</span><span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">int</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
			<span style="color: #3399cc; font-weight: bold;">this</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">graphics</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">beginFill</span><span style="color: #000000;">&#40;</span><span style="color: #3399cc;">index</span> <span style="color: #666666; font-weight: bold;">*</span> <span style="color: #db5821; font-weight:bold;">50</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
			<span style="color: #3399cc; font-weight: bold;">this</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">graphics</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">drawCircle</span><span style="color: #000000;">&#40;</span><span style="color: #db5821; font-weight:bold;">0</span><span style="color: #666666; font-weight: bold;">,</span> <span style="color: #db5821; font-weight:bold;">0</span><span style="color: #666666; font-weight: bold;">,</span> radius<span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
			<span style="color: #3399cc; font-weight: bold;">this</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">graphics</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
		<span style="color: #3399cc; font-weight: bold;">public</span> <span style="color: #3399cc; font-weight: bold;">function</span> update <span style="color: #000000;">&#40;</span><span style="color: #3399cc;">target</span><span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">Point</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
			<span style="color: #db5821; font-weight: bold;">var</span> diffx<span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">Number</span><span style="color: #666666; font-weight: bold;">;</span>
			<span style="color: #db5821; font-weight: bold;">var</span> diffy<span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">Number</span><span style="color: #666666; font-weight: bold;">;</span>
			<span style="color: #db5821; font-weight: bold;">var</span> ax<span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">Number</span><span style="color: #666666; font-weight: bold;">;</span>
			<span style="color: #db5821; font-weight: bold;">var</span> ay<span style="color: #666666; font-weight: bold;">:</span><span style="color: #3399cc;">Number</span><span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
			diffx = <span style="color: #3399cc;">target</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">x</span> <span style="color: #666666; font-weight: bold;">-</span> <span style="color: #3399cc;">x</span><span style="color: #666666; font-weight: bold;">;</span>
			diffy = <span style="color: #3399cc;">target</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">y</span> <span style="color: #666666; font-weight: bold;">-</span> <span style="color: #3399cc;">y</span><span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
			ax = diffx <span style="color: #666666; font-weight: bold;">*</span> _spring<span style="color: #666666; font-weight: bold;">;</span>
			ay = diffy <span style="color: #666666; font-weight: bold;">*</span> _spring<span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
			_vx <span style="color: #666666; font-weight: bold;">+</span>= ax<span style="color: #666666; font-weight: bold;">;</span>
			_vy <span style="color: #666666; font-weight: bold;">+</span>= ay
&nbsp;
			_vx <span style="color: #666666; font-weight: bold;">*</span>= _friction<span style="color: #666666; font-weight: bold;">;</span>
			_vy <span style="color: #666666; font-weight: bold;">*</span>= _friction<span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #3399cc; font-weight: bold;">this</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">x</span> <span style="color: #666666; font-weight: bold;">+</span>= _vx<span style="color: #666666; font-weight: bold;">;</span>
			<span style="color: #3399cc; font-weight: bold;">this</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">y</span> <span style="color: #666666; font-weight: bold;">+</span>= _vy<span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

</div>
<p>Then the EaselJS version. Click anywhere on the image to start the animation (although the effect looks better if you click on: nose, or mouth, or eyes).</p>
<p><script type="text/javascript" src="http://rengelbert.com/blog/html5/nose3d/easeljs-0.5.0.min.js"></script><br />
<script type="text/javascript" src="http://rengelbert.com/blog/html5/nose3d/Slice.js"></script><br />
<script type="text/javascript" src="http://rengelbert.com/blog/html5/nose3d/Main.js"></script></p>
<div class="canvasHolder">
	<canvas id="testCanvas" width="600" height="400"></canvas>
</div>
<p>And the code:</p>
<h3>Main.js</h3>
<div class="as-code">

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//declare our globals</span>
<span style="color: #003366; font-weight: bold;">var</span> NUM_SLICES <span style="color: #339933;">=</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> FRICTION <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.2</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> SPRING <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">animate</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">slices</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">stage</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">image</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
 	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">init</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">animate</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">slices</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">image</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Image<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #006600; font-style: italic;">//handler to preload our image</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">image</span>.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> handleImageLoad<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">image</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;doctor3.jpg&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">handleImageLoad</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> canvas <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;testCanvas&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		stage <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> createjs.<span style="color: #660066;">Stage</span><span style="color: #009900;">&#40;</span>canvas<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> ratio <span style="color: #339933;">=</span> image.<span style="color: #660066;">width</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span>NUM_SLICES <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> s<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> NUM_SLICES<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				s <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Slice <span style="color: #009900;">&#40;</span> image<span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
				s <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Slice <span style="color: #009900;">&#40;</span> image<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>image.<span style="color: #660066;">width</span> <span style="color: #339933;">-</span> i <span style="color: #339933;">*</span> ratio<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">0.5</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				
&nbsp;
			<span style="color: #009900;">&#125;</span>
			s.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">width</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">0.5</span><span style="color: #339933;">;</span>
			s.<span style="color: #660066;">y</span> <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">height</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">0.5</span><span style="color: #339933;">;</span>
&nbsp;
			stage.<span style="color: #660066;">addChild</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
			slices.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
		<span style="color: #006600; font-style: italic;">//add stage event: Mouse Click</span>
		stage.<span style="color: #660066;">onClick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>animate<span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #003366; font-weight: bold;">var</span> p <span style="color: #339933;">=</span> slices<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">globalToLocal</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">stageX</span><span style="color: #339933;">,</span> event.<span style="color: #660066;">stageY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> len <span style="color: #339933;">=</span> slices.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				slices<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">setNose</span><span style="color: #009900;">&#40;</span>p.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> p.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			
			<span style="color: #009900;">&#125;</span>
&nbsp;
			animate <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #006600; font-style: italic;">//render stage</span>
		stage.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">//register main loop event</span>
		createjs.<span style="color: #660066;">Ticker</span>.<span style="color: #660066;">setFPS</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createjs.<span style="color: #660066;">Ticker</span>.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #006600; font-style: italic;">//the main loop</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">tick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>animate<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #003366; font-weight: bold;">var</span> len <span style="color: #339933;">=</span> slices.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> s<span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> previous<span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> len<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				s <span style="color: #339933;">=</span> slices<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">==</span> len<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					s.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span>stage.<span style="color: #660066;">mouseX</span><span style="color: #339933;">,</span> stage.<span style="color: #660066;">mouseY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
					previous <span style="color: #339933;">=</span> slices<span style="color: #009900;">&#91;</span>i <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
					s.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span>previous.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> previous.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
		<span style="color: #006600; font-style: italic;">//render stage</span>
		stage.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> init<span style="color: #339933;">;</span></pre></td></tr></table></div>

</div>
<h3>Slice.js</h3>
<div class="as-code">

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
&nbsp;
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">circleMask</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">vx</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">vy</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> Slice<span style="color: #009900;">&#40;</span>image<span style="color: #339933;">,</span> radius<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">initialize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> canvas <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;testCanvas&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">image</span> <span style="color: #339933;">=</span> image<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">regX</span> <span style="color: #339933;">=</span> image.<span style="color: #660066;">width</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">0.5</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">regY</span> <span style="color: #339933;">=</span> image.<span style="color: #660066;">height</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">0.5</span><span style="color: #339933;">;</span>
&nbsp;
        vx <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
        vy <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>radius <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">circleMask</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> createjs.<span style="color: #660066;">Shape</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">circleMask</span>.<span style="color: #660066;">graphics</span>.<span style="color: #660066;">beginFill</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;0xFFF&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">circleMask</span>.<span style="color: #660066;">graphics</span>.<span style="color: #660066;">drawCircle</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>radius<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">circleMask</span>.<span style="color: #660066;">graphics</span>.<span style="color: #660066;">endFill</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 	
        <span style="color: #009900;">&#125;</span>    
    <span style="color: #009900;">&#125;</span>
&nbsp;
    Slice.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> createjs.<span style="color: #660066;">Bitmap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    Slice.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">circleMask</span><span style="color: #339933;">;</span>
    Slice.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">vx</span><span style="color: #339933;">;</span>
    Slice.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">vy</span><span style="color: #339933;">;</span>
&nbsp;
    Slice.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">Bitmap_initialize</span> <span style="color: #339933;">=</span> Slice.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">initialize</span><span style="color: #339933;">;</span>
&nbsp;
    Slice.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">initialize</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">Bitmap_initialize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    Slice.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">setNose</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>noseX<span style="color: #339933;">,</span> noseY<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">regX</span> <span style="color: #339933;">=</span> noseX<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">regY</span> <span style="color: #339933;">=</span> noseY<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">circleMask</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">mask</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">circleMask</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    Slice.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">update</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>targetX<span style="color: #339933;">,</span> targetY<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> diffx<span style="color: #339933;">,</span> diffy<span style="color: #339933;">,</span> ax<span style="color: #339933;">,</span> ay<span style="color: #339933;">;</span>
&nbsp;
        diffx <span style="color: #339933;">=</span> targetX <span style="color: #339933;">-</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span><span style="color: #339933;">;</span>
        diffy <span style="color: #339933;">=</span> targetY <span style="color: #339933;">-</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span><span style="color: #339933;">;</span>
&nbsp;
        ax <span style="color: #339933;">=</span> diffx <span style="color: #339933;">*</span> SPRING<span style="color: #339933;">;</span>
        ay <span style="color: #339933;">=</span> diffy <span style="color: #339933;">*</span> SPRING<span style="color: #339933;">;</span>
&nbsp;
        vx <span style="color: #339933;">+=</span> ax<span style="color: #339933;">;</span>
        vy <span style="color: #339933;">+=</span> ay
&nbsp;
        vx <span style="color: #339933;">*=</span> FRICTION<span style="color: #339933;">;</span>
        vy <span style="color: #339933;">*=</span> FRICTION<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">+=</span> vx<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">+=</span> vy<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">circleMask</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">circleMask</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">circleMask</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    Slice.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">normalize</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	<span style="color: #003366; font-weight: bold;">var</span> length <span style="color: #339933;">=</span> Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#40;</span>p.<span style="color: #660066;">x</span><span style="color: #339933;">*</span>p.<span style="color: #660066;">x</span><span style="color: #339933;">+</span>p.<span style="color: #660066;">y</span><span style="color: #339933;">*</span>p.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #000066; font-weight: bold;">return</span> 	<span style="color: #003366; font-weight: bold;">new</span> createjs.<span style="color: #660066;">Point</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>p.<span style="color: #660066;">x</span><span style="color: #339933;">/</span>length<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>p.<span style="color: #660066;">y</span><span style="color: #339933;">/</span>length<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
    window.<span style="color: #660066;">Slice</span> <span style="color: #339933;">=</span> Slice<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</div>
<p>Of course, the JS version uses an actual image, so the classes are a bit more extensive.</p>
<p>But you can see, among other things:</p>
<p>- how to write classes with EaselJS<br />
- how you can extend a display object in EaselJS. In the code I extended the Bitmap class.<br />
- how to draw vector shapes. I used a circle for the mask.<br />
- how to mask a bitmap with a vector shape.<br />
- how to add the main loop<br />
- how to add mouse events on stage. The same logic would work for any display object.<br />
- how to change the registration point of a display object with its properties regX and regY.</p>
]]></content:encoded>
			<wfw:commentRss>http://rengelbert.com/blog/the-doctor-and-easeljs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=rengelbert&amp;popout=1&amp;url=http%3A%2F%2Frengelbert.com%2Fblog%2Fthe-doctor-and-easeljs%2F&amp;language=en_GB&amp;category=text&amp;title=The+Doctor+and+EaselJS&amp;description=Tweet+I%26%238216%3Bve+decided+to+take+a+very+simple+application+done+in+Flash+and+transform+it+in+JavaScript+for+Canvas%2C+using+an+open+source+framework%2C+trying+to+use+as+many+features...&amp;tags=Actionscript%2CeaselJS+Tutorial%2CFlash%2CGame+Development%2CHTML5+Tutorial%2CJavascript+Tutorial%2CProgramming%2Cblog%2Cflash%2Cactionscript%2Cgame+development%2Cios%2Ciphone%2Cipad%2Candroid%2Clibgdx%2Ccocos2d%2Ccocos2d-x%2Csparrow%2Cprogramming%2Cnd2d%2Cstarling%2Cbox2d%2Cgames%2Ctutorials" type="text/html" />
	</item>
		<item>
		<title>Cocos2d-x: the Book</title>
		<link>http://rengelbert.com/blog/cocos2d-x-the-book/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cocos2d-x-the-book</link>
		<comments>http://rengelbert.com/blog/cocos2d-x-the-book/#comments</comments>
		<pubDate>Sun, 28 Apr 2013 00:01:00 +0000</pubDate>
		<dc:creator>roger</dc:creator>
				<category><![CDATA[Asides]]></category>
		<category><![CDATA[C++ Tutorial]]></category>
		<category><![CDATA[Cocos2D-x Tutorial]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[iOS Game Development]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://rengelbert.com/blog/cocos2d-x-the-book/</guid>
		<description><![CDATA[<img class ="entry-img" src="http://www.rengelbert.com/blog/wp-content/themes/donewithcomputers/images/entries/img_cocos2dx.png"/>

<p><span class="drop-cap">Y</span>ep, I wrote a book. It's called <a href="http://www.amazon.com/COCOS2d-X-Example-Roger-Engelbert/dp/178216734X/ref=sr_sp-btf_title_1_10?ie=UTF8&#038;qid=1367149300&#038;sr=8-10&#038;keywords=cocos2d-x" target="_blank">Cocos2d-x by Example Beginner's Guide</a>, and as the name implies, it covers the development of games with the C++ based framework Cocos2d-x. You've probably seen a few tutorials on this blog related to Cocos2d-x and C++, but now, there's a whole book of them! </p>]]></description>
				<content:encoded><![CDATA[<div id="tweetbutton337" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Frengelbert.com%2Fblog%2Fcocos2d-x-the-book%2F&amp;text=Cocos2d-x%3A%20the%20Book&amp;related=&amp;lang=en&amp;count=none&amp;counturl=http%3A%2F%2Frengelbert.com%2Fblog%2Fcocos2d-x-the-book%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://rengelbert.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><img class ="entry-img" src="http://www.rengelbert.com/blog/wp-content/themes/donewithcomputers/images/entries/img_cocos2dx.png"/></p>
<p><span class="drop-cap">Y</span>ep, I wrote a book. It&#8217;s called <a href="http://www.amazon.com/COCOS2d-X-Example-Roger-Engelbert/dp/178216734X/ref=sr_sp-btf_title_1_10?ie=UTF8&#038;qid=1367149300&#038;sr=8-10&#038;keywords=cocos2d-x" target="_blank">Cocos2d-x by Example Beginner&#8217;s Guide</a>, and as the name implies, it covers the development of games with the C++ based framework Cocos2d-x. You&#8217;ve probably seen a few tutorials on this blog related to Cocos2d-x and C++, but now, there&#8217;s a whole book of them! </p>
<p>The book covers the development of 6 games, two of them with Box2D. Here are some screenshots:</p>
<h3>Air Hockey</h3>
<p><img class ="content-img" style="margin:0px auto;" src="http://www.rengelbert.com/blog/wp-content/themes/donewithcomputers/images/entries/img_airhockey.png"/></p>
<h3>Sky Defense</h3>
<p><img class ="content-img" style="margin:0px auto;" src="http://www.rengelbert.com/blog/wp-content/themes/donewithcomputers/images/entries/img_sky.png"/></p>
<h3>Rocket Through</h3>
<p><img class ="content-img" style="margin:0px auto;" src="http://www.rengelbert.com/blog/wp-content/themes/donewithcomputers/images/entries/img_rocket.png"/></p>
<h3>Victorian Rush Hour</h3>
<p><img class ="content-img" style="margin:0px auto;" src="http://www.rengelbert.com/blog/wp-content/themes/donewithcomputers/images/entries/img_victorian.png"/></p>
<h3>MiniPool (Box2D)</h3>
<p><img class ="content-img" style="margin:0px auto;" src="http://www.rengelbert.com/blog/wp-content/themes/donewithcomputers/images/entries/img_minipool.png"/></p>
<h3>Eskimo (Box2D)</h3>
<p><img class ="content-img" style="margin:0px auto;" src="http://www.rengelbert.com/blog/wp-content/themes/donewithcomputers/images/entries/img_eskimo.png"/></p>
<p>All the games are universal apps, and I talk a lot about developing games for multiple screen sizes.</p>
<p>The code can be easily followed by anyone with any experience with a C based language, including ActionScript, JavaScript, PHP, as well as the usual bunch: Java, C# and of course Objective-C.</p>
<p>Cocos2d-x can be used to build games for iOS, Android, Windows Phone, Blackberry&#8230; among others, with the same code. And it&#8217;s incredibly easy to use.</p>
<p>Here is the <a href="http://www.packtpub.com/cocos2d-x-by-example/book" target="_blank">Packt page about the book</a>, and some links to the book at an online store near you!</p>
<p><a href="http://www.amazon.com/COCOS2d-X-Example-Roger-Engelbert/dp/178216734X/ref=sr_sp-btf_title_1_10?ie=UTF8&#038;qid=1367149300&#038;sr=8-10&#038;keywords=cocos2d-x" target="_blank">Amazon</a><br />
<a href="http://www.amazon.co.uk/Cocos2d-x-Example-Beginners-Guide-ebook/dp/B00CITNP8A/ref=sr_1_3?ie=UTF8&#038;qid=1367149191&#038;sr=8-3&#038;keywords=cocos2d-x<br />
" target="_blank">Amazon.co.uk</a><br />
<a href="http://www.barnesandnoble.com/w/cocos2d-x-by-example-roger-engelbert/1115193548?ean=9781782167341" target="_blank">Barnes &#038; Noble</a></p>
<p>I hope this will be counted as a helpful source of information on Cocos2d-x development, along side the likes of the <a href="http://paralaxer.com/cocos2d-x-book/" target="_blank">Paralaxer project</a>.</p>
<p>I certainly put a lot of effort into developing game examples that were fun to play! I hope you&#8217;ll enjoy reading the book as much I enjoyed writing it.</p>
]]></content:encoded>
			<wfw:commentRss>http://rengelbert.com/blog/cocos2d-x-the-book/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=rengelbert&amp;popout=1&amp;url=http%3A%2F%2Frengelbert.com%2Fblog%2Fcocos2d-x-the-book%2F&amp;language=en_GB&amp;category=text&amp;title=Cocos2d-x%3A+the+Book&amp;description=Tweet+Yep%2C+I+wrote+a+book.+It%26%238217%3Bs+called+Cocos2d-x+by+Example+Beginner%26%238217%3Bs+Guide%2C+and+as+the+name+implies%2C+it+covers+the+development+of+games+with+the+C%2B%2B+based+framework+Cocos2d-x....&amp;tags=C%2B%2B+Tutorial%2CCocos2D-x+Tutorial%2CGame+Development%2CiOS+Game+Development%2CProgramming%2Cblog%2Cflash%2Cactionscript%2Cgame+development%2Cios%2Ciphone%2Cipad%2Candroid%2Clibgdx%2Ccocos2d%2Ccocos2d-x%2Csparrow%2Cprogramming%2Cnd2d%2Cstarling%2Cbox2d%2Cgames%2Ctutorials" type="text/html" />
	</item>
		<item>
		<title>Frogger in Lua</title>
		<link>http://rengelbert.com/blog/frogger-in-lua/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=frogger-in-lua</link>
		<comments>http://rengelbert.com/blog/frogger-in-lua/#comments</comments>
		<pubDate>Sun, 31 Mar 2013 00:01:00 +0000</pubDate>
		<dc:creator>roger</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[iOS Game Development]]></category>
		<category><![CDATA[Lua Tutorial]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://rengelbert.com/blog/frogger-in-lua/</guid>
		<description><![CDATA[<p><span class="drop-cap">R</span>emember the Frogger series of tutorials where the same code was translated to different Frameworks? Well, there is a new version to be added to the list, this time courtesy of <a href="https://github.com/Shrike78/" target="_blank">Luca Ferrara</a>, and it's a Lua version.</p>
]]></description>
				<content:encoded><![CDATA[<div id="tweetbutton331" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Frengelbert.com%2Fblog%2Ffrogger-in-lua%2F&amp;text=Frogger%20in%20Lua&amp;related=&amp;lang=en&amp;count=none&amp;counturl=http%3A%2F%2Frengelbert.com%2Fblog%2Ffrogger-in-lua%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://rengelbert.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><span class="drop-cap">R</span>emember the Frogger series of tutorials where the same code was translated to different Frameworks? Well, there is a new version to be added to the list, this time courtesy of <a href="https://github.com/Shrike78/" target="_blank">Luca Ferrara</a>, and it&#8217;s a Lua version.</p>
<p>This is especially cool for Flash developers because Luca used a framework he&#8217;s developing (<a href="https://github.com/Shrike78/Shilke2D" target="_blank">Shilke2d</a>) that aims to port Starling to Moai, a Lua game development framework that can run on both iOS and Android.</p>
<p>If you never worked with Lua before, you probably heard that the syntax is similar to ActionScript/JavaScript and this port should help you test that theory for yourself, and this is probably the best way you&#8217;ll ever find to learn it.</p>
<p>So here is what you&#8217;ll need.</p>
<p>- Download the <a href="http://getmoai.com/" target = "_blank">Moai SDK</a>.</ br><br />
- Download the free <a href="http://studio.zerobrane.com/" target = "_blank">ZeroBrane Studio</a> Lua IDE.</ br><br />
- Download <a href="https://github.com/Shrike78/Shilke2D" target = "_blank">Shilke2d</a>.</ br><br />
- And finally Download <a href="https://github.com/Shrike78/Frogger-Shilke2D" target = "_blank">Luca&#8217;s port of Frogger</a>.</ br></p>
<p>Next comes the setup. These steps are for Mac users, but should be easily converted to Windows users.</p>
<p>- Inside the Shilke2D package you will find a folder called Shilke2D which is the library files. Copy and paste this folder inside the Frogger project you just downloaded.</ br><br />
- Next go to Applications or wherever you installed ZeroBranedStudio and click to show package contents. Navigate to <b>ZeroBraneStudio/cfg/user-sample.lua</b> and open this file in a Text Editor.</ br><br />
- Edit the path to moai excutable. Mine reads:</ br><br />
<i>path.moai = &#8216;/Users/rengelbert/frameworks/moai-sdk-1.4p0/bin/osx/moai&#8217;</i></ br><br />
- Edit the language used. Mine reads:</ br><br />
<i>language = &#8220;en&#8221;</i></ br><br />
- Here, you can also set the path to other common frameworks like Love and Corona.</ br><br />
- Rename the file to <b>user.lua</b>.</ br><br />
- Open ZeroBraneStudio and open the Frogger project and its main.lua file.</ br><br />
- Then go to the menu <b>Project|Lua Interpreter</b> and select <b>Moai</b>.<br />
- You should now be able to run the game, on <b>Project|Run</b>.</ br></p>
<div class="qanda content-box">
<p><b>Here is a description of Shilke2D in Luca&#8217;s own words:</b></p>
<p>Shilke2D takes birth as unofficial port for MOAI / lua of the as3 Starling framework. It exposes interfaces and logics very similar to Starling, including the juggler as animator of objects that implements IAnimatable interface.</p>
<p>Shilke2D allows to easily move to lua and MOAI if coming from Flash / actionscript 3<br />
development, in particular from people already used to work with Starling.</p>
<p>Moreover make it easy to move between the two environments when needed. Writing the frogger sample in lua was mainly just a matter of translation from as3 code.</p>
<p>Shilke2D differs from Starling/Flash for simpler event handling:</p>
<p>- events do not bubble</ br><br />
- events related to displayList changes are not handled</ br></p>
<p>It offers a slightly more advanced tween support compared to Starling, giving default support for:</p>
<p>- bezier curves</ br><br />
- delay tween</ br><br />
- sequence of tweens</ br><br />
- parallel execution of more tweens (synchronized)</ br></p>
<p>The code of frogger written for Shilke2D is mainly a translation of the original Starling code. Main differnces are in initialization phase.</p>
<p>Shilke2D defines specific callbacks for game execution:</p>
<p>- <b>setup()</b> called once at the beginning of the execution, as soon as <b>shilke2D:start()</b> is called. It&#8217;s the place where to put initialization of everything you need, like scene setup.</ br><br />
- <b>update(elapsedTime)</b>: called every frame, replace the ENTER_FRAME event of Flash/Starling.</ br><br />
- <b>onDirectDraw()</b>: Called after the stage draw call, it’s used to make vectorial or debug draw.</ br><br />
- <b>onKeyboardEvent(key,down)</b>: only on desktop platforms, it&#8217;s called whenever a key is pressed or released.</ br><br />
- <b>touched(touch)</b>: called whenever an unhandled touch event occurs. Note that by default every touch event that hit a displayObj is handled directly by Shilke2D.</ br></p>
<p>Frogger doesn&#8217;t need to implement a specific <b>touched()</b> function beacause all the touch events are handled by registered touch event handlers.</p>
</div>
<p>Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://rengelbert.com/blog/frogger-in-lua/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=rengelbert&amp;popout=1&amp;url=http%3A%2F%2Frengelbert.com%2Fblog%2Ffrogger-in-lua%2F&amp;language=en_GB&amp;category=text&amp;title=Frogger+in+Lua&amp;description=TweetRemember+the+Frogger+series+of+tutorials+where+the+same+code+was+translated+to+different+Frameworks%3F+Well%2C+there+is+a+new+version+to+be+added+to+the+list%2C+this+time+courtesy...&amp;tags=Actionscript%2CFlash%2CFlash+Games%2CGame+Development%2CiOS+Game+Development%2CLua+Tutorial%2CProgramming%2Cblog%2Cflash%2Cactionscript%2Cgame+development%2Cios%2Ciphone%2Cipad%2Candroid%2Clibgdx%2Ccocos2d%2Ccocos2d-x%2Csparrow%2Cprogramming%2Cnd2d%2Cstarling%2Cbox2d%2Cgames%2Ctutorials" type="text/html" />
	</item>
		<item>
		<title>Cocos2d HTML5: The Syntax</title>
		<link>http://rengelbert.com/blog/cocos2d-html5-the-syntax/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cocos2d-html5-the-syntax</link>
		<comments>http://rengelbert.com/blog/cocos2d-html5-the-syntax/#comments</comments>
		<pubDate>Sun, 24 Mar 2013 00:01:00 +0000</pubDate>
		<dc:creator>roger</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[C++ Tutorial]]></category>
		<category><![CDATA[Cocos2d Html5 Tutorial]]></category>
		<category><![CDATA[Cocos2D Tutorial]]></category>
		<category><![CDATA[Cocos2D-x Tutorial]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[HTML5 Tutorial]]></category>
		<category><![CDATA[iOS Game Development]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://rengelbert.com/blog/cocos2d-html5-the-syntax/</guid>
		<description><![CDATA[
<p><span class="drop-cap">T</span>he syntax differences between Cocos2d-x (C++) and Cocos2d HTML5 (JavaScript) are surprisingly small. In fact you could do away with most of them by using Find and Replace: just look for the <b>CC</b> prefix and replace it with <b>cc.</b> (that is "cc" and a dot).</p>
]]></description>
				<content:encoded><![CDATA[<div id="tweetbutton329" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Frengelbert.com%2Fblog%2Fcocos2d-html5-the-syntax%2F&amp;text=Cocos2d%20HTML5%3A%20The%20Syntax&amp;related=&amp;lang=en&amp;count=none&amp;counturl=http%3A%2F%2Frengelbert.com%2Fblog%2Fcocos2d-html5-the-syntax%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://rengelbert.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><span class="drop-cap">T</span>he syntax differences between Cocos2d-x (C++) and Cocos2d HTML5 (JavaScript) are surprisingly small. In fact you could do away with most of them by using Find and Replace: just look for the <b>CC</b> prefix and replace it with <b>cc.</b> (that is &#8220;cc&#8221; and a dot).</p>
<p>You could also look for specific data types and replace them with <b>var</b>. But this would not work for function parameters, though these could be easily fixed. Next you could also Find the string <b>::</b> and <b>-></b> and Replace with <b>.</b> (dot).</p>
<p>Let me show you some examples so you understand what I&#8217;m talking about.</p>
<h3>Creating a Sprite and positioning it</h3>
<div class="as-code">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #A0A0A0; font-style: italic;">//C++</span>
_bgLight = CCSprite<span style="color: #666666; font-weight: bold;">::</span>create<span style="color: #000000;">&#40;</span><span style="color: #009933;">&quot;bg_light.jpg&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
_bgLight<span style="color: #666666; font-weight: bold;">-&gt;</span>setPosition<span style="color: #000000;">&#40;</span>ccp<span style="color: #000000;">&#40;</span>_screenSize<span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">width</span> <span style="color: #666666; font-weight: bold;">*</span> 0<span style="color: #666666; font-weight: bold;">.</span>5f<span style="color: #666666; font-weight: bold;">,</span> _screenSize<span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">height</span> <span style="color: #666666; font-weight: bold;">*</span> 0<span style="color: #666666; font-weight: bold;">.</span>5f<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
<span style="color: #A0A0A0; font-style: italic;">//JS</span>
<span style="color: #3399cc; font-weight: bold;">this</span><span style="color: #666666; font-weight: bold;">.</span>_bgLight = cc<span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">Sprite</span><span style="color: #666666; font-weight: bold;">.</span>create<span style="color: #000000;">&#40;</span>bgLight<span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
<span style="color: #3399cc; font-weight: bold;">this</span><span style="color: #666666; font-weight: bold;">.</span>_bgLight<span style="color: #666666; font-weight: bold;">.</span>setPosition<span style="color: #000000;">&#40;</span>cc<span style="color: #666666; font-weight: bold;">.</span>p<span style="color: #000000;">&#40;</span>SCREEN_SIZE<span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">width</span> <span style="color: #666666; font-weight: bold;">*</span> <span style="color: #db5821; font-weight:bold;">0.5</span><span style="color: #666666; font-weight: bold;">,</span> SCREEN_SIZE<span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">height</span> <span style="color: #666666; font-weight: bold;">*</span> <span style="color: #db5821; font-weight:bold;">0.5</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span></pre></td></tr></table></div>

</div>
<p>In the JavaScript version I made use of globals for the screen size and the path to the jpg file (which is set in Resources.js for preloading.)</p>
<h3>Creating a SpriteBatchNode</h3>
<div class="as-code">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #A0A0A0; font-style: italic;">//C++</span>
_gameBatchNode = CCSpriteBatchNode<span style="color: #666666; font-weight: bold;">::</span>create<span style="color: #000000;">&#40;</span><span style="color: #009933;">&quot;sprite_sheet.png&quot;</span><span style="color: #666666; font-weight: bold;">,</span> <span style="color: #db5821; font-weight:bold;">800</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
this<span style="color: #666666; font-weight: bold;">-&gt;</span>addChild<span style="color: #000000;">&#40;</span>_gameBatchNode<span style="color: #666666; font-weight: bold;">,</span> kMiddleground<span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
<span style="color: #A0A0A0; font-style: italic;">//JS</span>
<span style="color: #3399cc; font-weight: bold;">this</span><span style="color: #666666; font-weight: bold;">.</span>_gameBatchNode = cc<span style="color: #666666; font-weight: bold;">.</span>SpriteBatchNode<span style="color: #666666; font-weight: bold;">.</span>create<span style="color: #000000;">&#40;</span>spriteSheetImg<span style="color: #666666; font-weight: bold;">,</span> <span style="color: #db5821; font-weight:bold;">800</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
<span style="color: #3399cc; font-weight: bold;">this</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">addChild</span><span style="color: #000000;">&#40;</span><span style="color: #3399cc; font-weight: bold;">this</span><span style="color: #666666; font-weight: bold;">.</span>_gameBatchNode<span style="color: #666666; font-weight: bold;">,</span>MH<span style="color: #666666; font-weight: bold;">.</span>Z<span style="color: #666666; font-weight: bold;">.</span>kBackground<span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span></pre></td></tr></table></div>

</div>
<h3>Particles, Sound</h3>
<div class="as-code">

<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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #A0A0A0; font-style: italic;">//C++</span>
_boostHitParticles = CCParticleSystem<span style="color: #666666; font-weight: bold;">::</span>create<span style="color: #000000;">&#40;</span><span style="color: #009933;">&quot;boost_hit.plist&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
_boostHitParticles<span style="color: #666666; font-weight: bold;">-&gt;</span>stopSystem<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
this<span style="color: #666666; font-weight: bold;">-&gt;</span>addChild<span style="color: #000000;">&#40;</span>_boostHitParticles<span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
_boostHitParticles<span style="color: #666666; font-weight: bold;">-&gt;</span>setPosition<span style="color: #000000;">&#40;</span>star<span style="color: #666666; font-weight: bold;">-&gt;</span>getPosition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
_boostHitParticles<span style="color: #666666; font-weight: bold;">-&gt;</span>resetSystem<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
SimpleAudioEngine<span style="color: #666666; font-weight: bold;">::</span>sharedEngine<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">-&gt;</span>playEffect<span style="color: #000000;">&#40;</span><span style="color: #009933;">&quot;star_hit.wav&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
SimpleAudioEngine<span style="color: #666666; font-weight: bold;">::</span>sharedEngine<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">-&gt;</span>stopBackgroundMusic<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
SimpleAudioEngine<span style="color: #666666; font-weight: bold;">::</span>sharedEngine<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">-&gt;</span>playBackgroundMusic<span style="color: #000000;">&#40;</span><span style="color: #009933;">&quot;bg.mp3&quot;</span><span style="color: #666666; font-weight: bold;">,</span> <span style="color: #3399cc; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
<span style="color: #A0A0A0; font-style: italic;">//JS</span>
<span style="color: #3399cc; font-weight: bold;">this</span><span style="color: #666666; font-weight: bold;">.</span>_boostHitParticles = cc<span style="color: #666666; font-weight: bold;">.</span>ParticleSystem<span style="color: #666666; font-weight: bold;">.</span>create<span style="color: #000000;">&#40;</span>boost_hit<span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
<span style="color: #3399cc; font-weight: bold;">this</span><span style="color: #666666; font-weight: bold;">.</span>_boostHitParticles<span style="color: #666666; font-weight: bold;">.</span>stopSystem<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
<span style="color: #3399cc; font-weight: bold;">this</span><span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">addChild</span><span style="color: #000000;">&#40;</span><span style="color: #3399cc; font-weight: bold;">this</span><span style="color: #666666; font-weight: bold;">.</span>_boostHitParticles<span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
<span style="color: #3399cc; font-weight: bold;">this</span><span style="color: #666666; font-weight: bold;">.</span>_boostHitParticles<span style="color: #666666; font-weight: bold;">.</span>setPosition<span style="color: #000000;">&#40;</span>star<span style="color: #666666; font-weight: bold;">.</span>getPosition<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
<span style="color: #3399cc; font-weight: bold;">this</span><span style="color: #666666; font-weight: bold;">.</span>_boostHitParticles<span style="color: #666666; font-weight: bold;">.</span>resetSystem<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
cc<span style="color: #666666; font-weight: bold;">.</span>AudioEngine<span style="color: #666666; font-weight: bold;">.</span>getInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">.</span>playEffect<span style="color: #000000;">&#40;</span>last_star_snd<span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
cc<span style="color: #666666; font-weight: bold;">.</span>AudioEngine<span style="color: #666666; font-weight: bold;">.</span>getInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">.</span>stopMusic<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
cc<span style="color: #666666; font-weight: bold;">.</span>AudioEngine<span style="color: #666666; font-weight: bold;">.</span>getInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">.</span>playMusic<span style="color: #000000;">&#40;</span>bgMusic<span style="color: #666666; font-weight: bold;">,</span> <span style="color: #3399cc; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span></pre></td></tr></table></div>

</div>
<p>In a more recent version of Cocos2d HTML5 the word Background was removed from all AudioEngine commands related to background music. I believe the same will be done to Cocos2d-x if not already.</p>
<h3>Scene creation and transition</h3>
<div class="as-code">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #A0A0A0; font-style: italic;">//C++</span>
CCScene <span style="color: #666666; font-weight: bold;">*</span>scene = CCScene<span style="color: #666666; font-weight: bold;">::</span>create<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
scene<span style="color: #666666; font-weight: bold;">-&gt;</span>addChild<span style="color: #000000;">&#40;</span>GameLayer<span style="color: #666666; font-weight: bold;">::</span>create<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
CCScene<span style="color: #666666; font-weight: bold;">*</span> newScene = CCTransitionFlipY<span style="color: #666666; font-weight: bold;">::</span>create<span style="color: #000000;">&#40;</span>0<span style="color: #666666; font-weight: bold;">.</span>5f<span style="color: #666666; font-weight: bold;">,</span> MenuLayer<span style="color: #666666; font-weight: bold;">::</span>scene<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">,</span> kOrientationDownOver<span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
CCDirector<span style="color: #666666; font-weight: bold;">::</span>sharedDirector<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">-&gt;</span>replaceScene<span style="color: #000000;">&#40;</span>newScene<span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
<span style="color: #A0A0A0; font-style: italic;">//JS</span>
<span style="color: #db5821; font-weight: bold;">var</span> scene = cc<span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">Scene</span><span style="color: #666666; font-weight: bold;">.</span>create<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
scene<span style="color: #666666; font-weight: bold;">.</span><span style="color: #3399cc;">addChild</span><span style="color: #000000;">&#40;</span>GameLayer<span style="color: #666666; font-weight: bold;">.</span>create<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
<span style="color: #db5821; font-weight: bold;">var</span> newScene = cc<span style="color: #666666; font-weight: bold;">.</span>TransitionProgressRadialCCW<span style="color: #666666; font-weight: bold;">.</span>create<span style="color: #000000;">&#40;</span><span style="color: #db5821; font-weight:bold;">0.5</span><span style="color: #666666; font-weight: bold;">,</span> MenuLayer<span style="color: #666666; font-weight: bold;">.</span>scene<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
cc<span style="color: #666666; font-weight: bold;">.</span>Director<span style="color: #666666; font-weight: bold;">.</span>getInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">.</span>replaceScene<span style="color: #000000;">&#40;</span>newScene<span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span></pre></td></tr></table></div>

</div>
<p>Not every scene transition effect is supported in Cocos2d HTML5. You can see the ones not supported commented out in the scene transitions test class.</p>
<h3>Class declaration</h3>
<div class="as-code">

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//C++</span>
<span style="color: #003366; font-weight: bold;">class</span> Star <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">public</span> cocos2d<span style="color: #339933;">::</span>CCSprite
&nbsp;
Star<span style="color: #339933;">::</span>Star<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	this<span style="color: #339933;">-&gt;</span>initWithSpriteFrameName<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;star_1.png&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #006600; font-style: italic;">//JS</span>
<span style="color: #003366; font-weight: bold;">var</span> Star <span style="color: #339933;">=</span> cc.<span style="color: #660066;">Sprite</span>.<span style="color: #660066;">extend</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	ctor<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>._super<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cc.<span style="color: #660066;">associateWithNative</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> cc.<span style="color: #660066;">Sprite</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">initWithSpriteFrameName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;star_1.png&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #006600; font-style: italic;">//...</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</div>
<p>I changed the logic a bit here compared to what is actually used in the game, so as not to have too many things in the way of the syntax.<br />
But in the HTML5 version is important to use this odd JS constructor and call associateWithNative. This is necessary when taking the code to Cocos2d-x and using the native bindings.</p>
]]></content:encoded>
			<wfw:commentRss>http://rengelbert.com/blog/cocos2d-html5-the-syntax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=rengelbert&amp;popout=1&amp;url=http%3A%2F%2Frengelbert.com%2Fblog%2Fcocos2d-html5-the-syntax%2F&amp;language=en_GB&amp;category=text&amp;title=Cocos2d+HTML5%3A+The+Syntax&amp;description=TweetThe+syntax+differences+between+Cocos2d-x+%28C%2B%2B%29+and+Cocos2d+HTML5+%28JavaScript%29+are+surprisingly+small.+In+fact+you+could+do+away+with+most+of+them+by+using+Find+and+Replace%3A+just+look...&amp;tags=Actionscript%2CC%2B%2B+Tutorial%2CCocos2d+Html5+Tutorial%2CCocos2D+Tutorial%2CCocos2D-x+Tutorial%2CFlash%2CFlash+Games%2CGame+Development%2CHTML5+Tutorial%2CiOS+Game+Development%2CProgramming%2Cblog%2Cflash%2Cactionscript%2Cgame+development%2Cios%2Ciphone%2Cipad%2Candroid%2Clibgdx%2Ccocos2d%2Ccocos2d-x%2Csparrow%2Cprogramming%2Cnd2d%2Cstarling%2Cbox2d%2Cgames%2Ctutorials" type="text/html" />
	</item>
		<item>
		<title>Cocos2d HTML5: Project Setup</title>
		<link>http://rengelbert.com/blog/cocos2d-html5-project-setup/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cocos2d-html5-project-setup</link>
		<comments>http://rengelbert.com/blog/cocos2d-html5-project-setup/#comments</comments>
		<pubDate>Sat, 09 Mar 2013 00:01:00 +0000</pubDate>
		<dc:creator>roger</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[C++ Tutorial]]></category>
		<category><![CDATA[Cocos2d Html5 Tutorial]]></category>
		<category><![CDATA[Cocos2D Tutorial]]></category>
		<category><![CDATA[Cocos2D-x Tutorial]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[HTML5 Tutorial]]></category>
		<category><![CDATA[iOS Game Development]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://rengelbert.com/blog/cocos2d-html5-project-setup/</guid>
		<description><![CDATA[
<p><span class="drop-cap">S</span>etting up a Cocos2d HTML5 project is a remarkably easy process and here I'll show you how to do it both for the web and the native versions. But first you will need to download the <a href="http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Download" target="_blank">Cocos2d HTML5 framework, and a matching Cocos2d-x framework</a>. I used version 2.1.1. It's much easier to develop both web and native versions if the frameworks match. And even easier with this 2.1.1 version.
</p>
]]></description>
				<content:encoded><![CDATA[<div id="tweetbutton326" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Frengelbert.com%2Fblog%2Fcocos2d-html5-project-setup%2F&amp;text=Cocos2d%20HTML5%3A%20Project%20Setup&amp;related=&amp;lang=en&amp;count=none&amp;counturl=http%3A%2F%2Frengelbert.com%2Fblog%2Fcocos2d-html5-project-setup%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://rengelbert.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><span class="drop-cap">S</span>etting up a Cocos2d HTML5 project is a remarkably easy process and here I&#8217;ll show you how to do it both for the web and the native versions. But first you will need to download the <a href="http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Download" target="_blank">Cocos2d HTML5 framework, and a matching Cocos2d-x framework</a>. I used version 2.1.1. It&#8217;s much easier to develop both web and native versions if the frameworks match. And even easier with this 2.1.1 version.
</p>
<h3>The Web Version</h3>
<p>In the html page, and therefore web version of the game, all you need to do is create a canvas tag and run one of them weird self-running anonymous functions, which is usually saved inside an external javascript file. In this game that file is called MoonHerder-html5.js.</p>
<div class="as-code">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div&gt;
    &lt;canvas id=&quot;gameCanvas&quot; width=&quot;320&quot; height=&quot;480&quot;&gt;&lt;/canvas&gt;
&lt;/div&gt;
&lt;script src=&quot;MoonHerder-html5.js&quot;&gt;&lt;/script&gt;</pre></td></tr></table></div>

</div>
<p>And here is the MoonHerder-html5.js script:</p>
<div class="as-code">

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> MH <span style="color: #339933;">=</span> MH <span style="color: #339933;">||</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> d <span style="color: #339933;">=</span> document<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> c <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        menuType<span style="color: #339933;">:</span><span style="color: #3366CC;">'canvas'</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">//whether to use canvas mode menu or dom menu</span>
        COCOS2D_DEBUG<span style="color: #339933;">:</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">//0 to turn debug off, 1 for basic debug, and 2 for full debug</span>
        showFPS<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        frameRate<span style="color: #339933;">:</span><span style="color: #CC0000;">60</span><span style="color: #339933;">,</span>
        tag<span style="color: #339933;">:</span><span style="color: #3366CC;">'gameCanvas'</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">//the dom element to run cocos2d on</span>
        engineDir<span style="color: #339933;">:</span><span style="color: #3366CC;">'libs/cocos2d/'</span><span style="color: #339933;">,</span>
        appFiles<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>
            <span style="color: #3366CC;">'src/GameConfig.js'</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'src/Resource.js'</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'src/GameManager.js'</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'src/GameLayer.js'</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'src/HelpLayer.js'</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'src/MenuLayer.js'</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'src/DrawLayer.js'</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'src/Line.js'</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'src/Moon.js'</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'src/Sun.js'</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">'src/Star.js'</span>
        <span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    window.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'DOMContentLoaded'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">//first load engine file if specified</span>
        <span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> d.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'script'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        s.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> c.<span style="color: #660066;">engineDir</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'platform/jsloader.js'</span><span style="color: #339933;">;</span>
        d.<span style="color: #660066;">body</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        document.<span style="color: #660066;">ccConfig</span> <span style="color: #339933;">=</span> c<span style="color: #339933;">;</span>
        s.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'cocos2d-html5'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</div>
<p>I developed the structure of the game somewhat based on the way the demo game MoonWarriors was built. So I created this global MH object to store game related data just as MoonWarriors does. But I confess I wasn&#8217;t very happy with it, as it serves no real purpose that justify having a separate object. Choosing a way to organize globals is a personal choice here. Developers have different styles and structures for this and the samples included in the framework show some good examples. But it&#8217;s fun to work with one big scope again, not having done that since ActionScript 1. Of course, you should be careful how much data you store globally, and let the stack bear the brunt of your application.</p>
<p>So the anonymous self running function creates this configuration object, called c, listing among other things all the script files used in the game. And then in the &#8220;on content load&#8221; event the jsloader script gets added to the page, and this will load the framework scripts as well as the app script files listed in c. You have the option of using a single file for the framework, minified or not, to load all the framework in one go. And of course, you could create your own single, minified file for your app scripts.</p>
<p>This will eventually call on the main.js script which you must implement. It creates a class that extends cc.Application and wraps it around your app with all required director configurations, such as you would have in AppDelegate in iOS.</p>
<p>Here is the code in main.js:</p>
<div class="as-code">

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> cocos2dApp <span style="color: #339933;">=</span> cc.<span style="color: #660066;">Application</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    config<span style="color: #339933;">:</span>document.<span style="color: #660066;">ccConfig</span><span style="color: #339933;">,</span>
    ctor<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>scene<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>._super<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">startScene</span> <span style="color: #339933;">=</span> scene<span style="color: #339933;">;</span>
        <span style="color: #006600; font-style: italic;">//cc.COCOS2D_DEBUG = this.config.COCOS2D_DEBUG;</span>
        cc.<span style="color: #660066;">setup</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">config</span>.<span style="color: #660066;">tag</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cc.<span style="color: #660066;">Loader</span>.<span style="color: #660066;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onloading</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            cc.<span style="color: #660066;">LoaderScene</span>.<span style="color: #660066;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">draw</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        cc.<span style="color: #660066;">Loader</span>.<span style="color: #660066;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            cc.<span style="color: #660066;">AppController</span>.<span style="color: #660066;">shareAppController</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">didFinishLaunchingWithOptions</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        cc.<span style="color: #660066;">Loader</span>.<span style="color: #660066;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">preload</span><span style="color: #009900;">&#40;</span>g_ressources<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    applicationDidFinishLaunching<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// initialize director</span>
        <span style="color: #003366; font-weight: bold;">var</span> director <span style="color: #339933;">=</span> cc.<span style="color: #660066;">Director</span>.<span style="color: #660066;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// turn on display FPS</span>
        director.<span style="color: #660066;">setDisplayStats</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">config</span>.<span style="color: #660066;">showFPS</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// set FPS. the default value is 1.0/60 if you don't call this</span>
        director.<span style="color: #660066;">setAnimationInterval</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1.0</span> <span style="color: #339933;">/</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">config</span>.<span style="color: #660066;">frameRate</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// run</span>
        director.<span style="color: #660066;">runWithScene</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">startScene</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</div>
<p>With these we create our extended class, cocos2dApp, passing it our first scene. This class will create our loading screen using cc.LoaderScene (you will need to extend this class if you want your own loading screen), and passing it a global variable, which in this example is called g_resources and is created inside the Resource.js script. This is an array of key-value pair objects populated with all the required external media and data files. You need to supply a resource type tag and the path to the file like this:</p>
<div class="as-code">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//in Resources.js</span>
<span style="color: #003366; font-weight: bold;">var</span> g_resources <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
    <span style="color: #009900;">&#123;</span>type<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;image&quot;</span><span style="color: #339933;">,</span> src<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;res/bg_dark.jpg&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span>type<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;plist&quot;</span><span style="color: #339933;">,</span> src<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;res/sprite_sheet.plist&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span>type<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;sound&quot;</span><span style="color: #339933;">,</span> src<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;res/sounds/bg.mp3&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span>type<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;fnt&quot;</span><span style="color: #339933;">,</span> src<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;res/font.fnt&quot;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</div>
<p>If for some reason you decide to use earlier versions of the framework than the 2.1.1 version used here, bear in mind that some of the type tags may be different. And who knows, it might change again in future versions.</p>
<p>Now back to main.js, we start things rolling with this one line at the end of that script file:</p>
<div class="as-code">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myApp <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> cocos2dApp<span style="color: #009900;">&#40;</span>MenuLayer.<span style="color: #660066;">scene</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</div>
<h3>The Native Version</h3>
<p>In the native version we replace MoonHerder-html5.js with MoonHerder-jsb.js:</p>
<div class="as-code">

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;jsb.js&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> MH <span style="color: #339933;">=</span> MH <span style="color: #339933;">||</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> appFiles <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
	<span style="color: #3366CC;">'src/Resource.js'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'src/Star.js'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'src/GameConfig.js'</span><span style="color: #339933;">,</span>
	<span style="color: #3366CC;">'src/GameManager.js'</span><span style="color: #339933;">,</span>
	<span style="color: #3366CC;">'src/GameLayer.js'</span><span style="color: #339933;">,</span>
	<span style="color: #3366CC;">'src/HelpLayer.js'</span><span style="color: #339933;">,</span>
	<span style="color: #3366CC;">'src/MenuLayer.js'</span><span style="color: #339933;">,</span>
	<span style="color: #3366CC;">'src/DrawLayer.js'</span><span style="color: #339933;">,</span>
	<span style="color: #3366CC;">'src/Line.js'</span><span style="color: #339933;">,</span>
	<span style="color: #3366CC;">'src/Moon.js'</span><span style="color: #339933;">,</span>
	<span style="color: #3366CC;">'src/Sun.js'</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> appFiles.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    require<span style="color: #009900;">&#40;</span> appFiles<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> director <span style="color: #339933;">=</span> cc.<span style="color: #660066;">Director</span>.<span style="color: #660066;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
director.<span style="color: #660066;">setDisplayStats</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// set FPS. the default value is 1.0/60 if you don't call this</span>
director.<span style="color: #660066;">setAnimationInterval</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1.0</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> mainScene <span style="color: #339933;">=</span> MenuLayer.<span style="color: #660066;">scene</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// run</span>
director.<span style="color: #660066;">runWithScene</span><span style="color: #009900;">&#40;</span>mainScene<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</div>
<p>With the native version, and in iOS, the MoonHerder-jsb.js script is called inside AppDelegate in the appDidFinishLaunching method with this line:</p>
<div class="as-code">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;">ScriptingCore<span style="color: #666666; font-weight: bold;">::</span>getInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">-</span>&gt;runScript<span style="color: #000000;">&#40;</span><span style="color: #009933;">&quot;MoonHerder-jsb.js&quot;</span><span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

</div>
<p>So in the native version main.js is not used, as cc.Application serves as a replacement for AppDelegate in the web version.</p>
<p>And that&#8217;s all it is really. I recommend you take a look at the samples folder in the Cocos2d-x framework files (versions 2.1.1) and then inside its JavaScript folder for more examples. </p>
<p><a href="https://github.com/rengelbert/moon-herder-html5">You may download the source code from gitHub.</a></p>
<p>In the next post, I&#8217;ll go over the syntax and show how the game starts running.</p>
]]></content:encoded>
			<wfw:commentRss>http://rengelbert.com/blog/cocos2d-html5-project-setup/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=rengelbert&amp;popout=1&amp;url=http%3A%2F%2Frengelbert.com%2Fblog%2Fcocos2d-html5-project-setup%2F&amp;language=en_GB&amp;category=text&amp;title=Cocos2d+HTML5%3A+Project+Setup&amp;description=TweetSetting+up+a+Cocos2d+HTML5+project+is+a+remarkably+easy+process+and+here+I%26%238217%3Bll+show+you+how+to+do+it+both+for+the+web+and+the+native+versions.+But+first...&amp;tags=Actionscript%2CC%2B%2B+Tutorial%2CCocos2d+Html5+Tutorial%2CCocos2D+Tutorial%2CCocos2D-x+Tutorial%2CFlash%2CFlash+Games%2CGame+Development%2CHTML5+Tutorial%2CiOS+Game+Development%2CProgramming%2Cblog%2Cflash%2Cactionscript%2Cgame+development%2Cios%2Ciphone%2Cipad%2Candroid%2Clibgdx%2Ccocos2d%2Ccocos2d-x%2Csparrow%2Cprogramming%2Cnd2d%2Cstarling%2Cbox2d%2Cgames%2Ctutorials" type="text/html" />
	</item>
		<item>
		<title>Moon Herder: in Cocos2d HTML5</title>
		<link>http://rengelbert.com/blog/moon-herder-in-cocos2d-html5/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=moon-herder-in-cocos2d-html5</link>
		<comments>http://rengelbert.com/blog/moon-herder-in-cocos2d-html5/#comments</comments>
		<pubDate>Mon, 04 Mar 2013 00:01:00 +0000</pubDate>
		<dc:creator>roger</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[C++ Tutorial]]></category>
		<category><![CDATA[Cocos2d Html5 Tutorial]]></category>
		<category><![CDATA[Cocos2D Tutorial]]></category>
		<category><![CDATA[Cocos2D-x Tutorial]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[HTML5 Tutorial]]></category>
		<category><![CDATA[iOS Game Development]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://rengelbert.com/blog/moon-herder-in-cocos2d-html5n/</guid>
		<description><![CDATA[
<img class ="entry-img" src="http://www.rengelbert.com/blog/wp-content/themes/donewithcomputers/images/entries/img_moonherderhtml5.png"/>

<p><span class="drop-cap">S</span>o finally, we arrive at the last port of the game. As a recap, this was a <a href="http://rengelbert.com/blog/moon-herder-a-slope-game/" target="_blank">Flash game</a>, which I then expanded and ported to <a href="http://rengelbert.com/blog/moonherder-cocos2d-cocos2d-x/" target="_blank">Objective-C with Cocos2D, then C++ with Cocos2d-x</a>. And now I ported it to HTML5 with Cocos2d HTML5, made it run in a browser and republish it inside Cocos2d-x with the framework's JavaScript bindings.</p>
]]></description>
				<content:encoded><![CDATA[<div id="tweetbutton320" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Frengelbert.com%2Fblog%2Fmoon-herder-in-cocos2d-html5%2F&amp;text=Moon%20Herder%3A%20in%20Cocos2d%20HTML5&amp;related=&amp;lang=en&amp;count=none&amp;counturl=http%3A%2F%2Frengelbert.com%2Fblog%2Fmoon-herder-in-cocos2d-html5%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://rengelbert.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><img class ="entry-img" src="http://www.rengelbert.com/blog/wp-content/themes/donewithcomputers/images/entries/img_moonherderhtml5.png"/></p>
<p><span class="drop-cap">S</span>o finally, we arrive at the last port of the game. As a recap, this was a <a href="http://rengelbert.com/blog/moon-herder-a-slope-game/" target="_blank">Flash game</a>, which I then expanded and ported to <a href="http://rengelbert.com/blog/moonherder-cocos2d-cocos2d-x/" target="_blank">Objective-C with Cocos2D, then C++ with Cocos2d-x</a>. And now I ported it to HTML5 with Cocos2d HTML5, made it run in a browser and republish it inside Cocos2d-x with the framework&#8217;s JavaScript bindings.</p>
<p><a href="http://rengelbert.com/blog/html5/MoonHerder/" target="_blank">You can play the HTML5 version of the game here.</a></p>
<p>So I&#8217;ll write a series of posts now covering the HTML5 port and the few differences I encountered trying to develop one version for both browser and native bindings. Mostly, these have to do with the draw api, so no biggie.</p>
<p>The HTML5 code is on <a href="http://github.com/rengelbert/moon-herder-html5" target="_blank">github</a>.</p>
<p>So, more posts on this soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://rengelbert.com/blog/moon-herder-in-cocos2d-html5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=rengelbert&amp;popout=1&amp;url=http%3A%2F%2Frengelbert.com%2Fblog%2Fmoon-herder-in-cocos2d-html5%2F&amp;language=en_GB&amp;category=text&amp;title=Moon+Herder%3A+in+Cocos2d+HTML5&amp;description=Tweet+So+finally%2C+we+arrive+at+the+last+port+of+the+game.+As+a+recap%2C+this+was+a+Flash+game%2C+which+I+then+expanded+and+ported+to+Objective-C+with+Cocos2D%2C...&amp;tags=Actionscript%2CC%2B%2B+Tutorial%2CCocos2d+Html5+Tutorial%2CCocos2D+Tutorial%2CCocos2D-x+Tutorial%2CFlash%2CFlash+Games%2CGame+Development%2CHTML5+Tutorial%2CiOS+Game+Development%2CProgramming%2Cblog%2Cflash%2Cactionscript%2Cgame+development%2Cios%2Ciphone%2Cipad%2Candroid%2Clibgdx%2Ccocos2d%2Ccocos2d-x%2Csparrow%2Cprogramming%2Cnd2d%2Cstarling%2Cbox2d%2Cgames%2Ctutorials" type="text/html" />
	</item>
		<item>
		<title>MoonHerder: the line hack</title>
		<link>http://rengelbert.com/blog/moonherder-the-line-hack/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=moonherder-the-line-hack</link>
		<comments>http://rengelbert.com/blog/moonherder-the-line-hack/#comments</comments>
		<pubDate>Tue, 12 Feb 2013 00:01:00 +0000</pubDate>
		<dc:creator>roger</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[C++ Tutorial]]></category>
		<category><![CDATA[Cocos2D Tutorial]]></category>
		<category><![CDATA[Cocos2D-x Tutorial]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[iOS Game Development]]></category>
		<category><![CDATA[Java Tutorial]]></category>
		<category><![CDATA[LibGDX Tutorial]]></category>
		<category><![CDATA[Objective-C Tutorial]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://rengelbert.com/blog/moonherder-the-line-hack/</guid>
		<description><![CDATA[
<p><span class="drop-cap">T</span>here is a tiny bit of an issue when drawing curved lines in OpenGL, because of the way the line "caps" are drawn. Of course the issue is only an issue if you want thick lines and don't want to texture them properly (with triangle fanned vertices.) But there is a quick solution to that.</p>]]></description>
				<content:encoded><![CDATA[<div id="tweetbutton313" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Frengelbert.com%2Fblog%2Fmoonherder-the-line-hack%2F&amp;text=MoonHerder%3A%20the%20line%20hack&amp;related=&amp;lang=en&amp;count=none&amp;counturl=http%3A%2F%2Frengelbert.com%2Fblog%2Fmoonherder-the-line-hack%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://rengelbert.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><span class="drop-cap">T</span>here is a tiny bit of an issue when drawing curved lines in OpenGL, because of the way the line &#8220;caps&#8221; are drawn. Of course the issue is only an issue if you want thick lines and don&#8217;t want to texture them properly (with triangle fanned vertices.) But there is a quick solution to that.</p>
<p>The image below should give you an idea of what the problem is. </p>
<p><img class ="content-img" style="margin:10px auto;" src="http://www.rengelbert.com/blog/wp-content/themes/donewithcomputers/images/caps.jpg"/></p>
<p>Caps are drawn either vertically or horizontally. So lines drawn in an angle will have an odd looking cap. Unless you texture them, which I chose not to do in Moon Herder.<br />
But the problem quickly becomes an ugly one if you want to draw curved lines, which are processed in OpenGL as a combination of straight lines following one of the bezier formulas.</p>
<p>When I was building Moon Herder I decided to apply a very simple hack. To draw tiny circles where the lines joined. And I discovered that if I find the correct radius for the circle (and this will depend primarily on the line&#8217;s thickness) that the hack seems to do away with 99% of the problem.</p>
<p>In Objective-C the draw methods core logic is written in C:</p>
<div class="as-code">

<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
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> drawQuadBezier<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>CGPoint<span style="color: #009900;">&#41;</span> origin  control<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>CGPoint<span style="color: #009900;">&#41;</span> control  destination<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>CGPoint<span style="color: #009900;">&#41;</span> destination  segments<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span> segments <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #993333;">float</span> t <span style="color: #339933;">=</span> <span style="color:#800080;">0.0f</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">float</span> x_<span style="color: #339933;">;</span>
	<span style="color: #993333;">float</span> y_<span style="color: #339933;">;</span>
&nbsp;
    CGPoint previous <span style="color: #339933;">=</span> origin<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> segments <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        x_ <span style="color: #339933;">=</span> powf<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span> <span style="color: #339933;">-</span> t<span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> origin.<span style="color: #202020;">x</span> <span style="color: #339933;">+</span> <span style="color:#800080;">2.0f</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span> <span style="color: #339933;">-</span> t<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> t <span style="color: #339933;">*</span> control.<span style="color: #202020;">x</span> <span style="color: #339933;">+</span> t <span style="color: #339933;">*</span> t <span style="color: #339933;">*</span> destination.<span style="color: #202020;">x</span><span style="color: #339933;">;</span>
        y_ <span style="color: #339933;">=</span> powf<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span> <span style="color: #339933;">-</span> t<span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> origin.<span style="color: #202020;">y</span> <span style="color: #339933;">+</span> <span style="color:#800080;">2.0f</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span> <span style="color: #339933;">-</span> t<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> t <span style="color: #339933;">*</span> control.<span style="color: #202020;">y</span> <span style="color: #339933;">+</span> t <span style="color: #339933;">*</span> t <span style="color: #339933;">*</span> destination.<span style="color: #202020;">y</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
        ccDrawCircle<span style="color: #009900;">&#40;</span>previous<span style="color: #339933;">,</span> <span style="color:#800080;">2.2</span><span style="color: #339933;">,</span> M_PI<span style="color: #339933;">,</span> <span style="color: #0000dd;">6</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        ccDrawLine<span style="color: #009900;">&#40;</span>previous<span style="color: #339933;">,</span> ccp <span style="color: #009900;">&#40;</span>x_<span style="color: #339933;">,</span> y_<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        previous <span style="color: #339933;">=</span> ccp<span style="color: #009900;">&#40;</span>x_<span style="color: #339933;">,</span> y_<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        t <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span> <span style="color: #0000dd;">1</span> <span style="color: #339933;">/</span> segments<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</div>
<p>So in C++ the differences are tiny. CGPoint becomes CCPoint and that&#8217;s it.</p>
<div class="as-code">

<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
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #db5821; font-weight: bold;">void</span> DrawLayer<span style="color: #666666; font-weight: bold;">::</span><span style="">drawQuadBezier</span><span style="color: #000000;">&#40;</span>CCPoint origin, CCPoint control, CCPoint destination,  <span style="color: #db5821; font-weight: bold;">int</span> segments<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
&nbsp;
    <span style="color: #db5821; font-weight: bold;">float</span> t <span style="color: #666666; font-weight: bold;">=</span> <span style="color: #db5821; font-weight:bold;">0.0f</span><span style="color: #666666; font-weight: bold;">;</span>
    <span style="color: #db5821; font-weight: bold;">float</span> x_<span style="color: #666666; font-weight: bold;">;</span>
    <span style="color: #db5821; font-weight: bold;">float</span> y_<span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
    CCPoint previous <span style="color: #666666; font-weight: bold;">=</span> origin<span style="color: #666666; font-weight: bold;">;</span>
    <span style="color: #3399cc; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #db5821; font-weight: bold;">int</span> i <span style="color: #666666; font-weight: bold;">=</span> <span style="color: #db5821; font-weight:bold;">0</span><span style="color: #666666; font-weight: bold;">;</span> i <span style="color: #666666; font-weight: bold;">&lt;</span> segments <span style="color: #666666; font-weight: bold;">+</span> <span style="color: #db5821; font-weight:bold;">1</span><span style="color: #666666; font-weight: bold;">;</span> i<span style="color: #666666; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
        x_ <span style="color: #666666; font-weight: bold;">=</span> powf<span style="color: #000000;">&#40;</span><span style="color: #db5821; font-weight:bold;">1</span> <span style="color: #666666; font-weight: bold;">-</span> t, <span style="color: #db5821; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #666666; font-weight: bold;">*</span> origin.<span style="">x</span> <span style="color: #666666; font-weight: bold;">+</span> <span style="color: #db5821; font-weight:bold;">2.0f</span> <span style="color: #666666; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span><span style="color: #db5821; font-weight:bold;">1</span> <span style="color: #666666; font-weight: bold;">-</span> t<span style="color: #000000;">&#41;</span> <span style="color: #666666; font-weight: bold;">*</span> t <span style="color: #666666; font-weight: bold;">*</span> control.<span style="">x</span> <span style="color: #666666; font-weight: bold;">+</span> t <span style="color: #666666; font-weight: bold;">*</span> t <span style="color: #666666; font-weight: bold;">*</span> destination.<span style="">x</span><span style="color: #666666; font-weight: bold;">;</span>
        y_ <span style="color: #666666; font-weight: bold;">=</span> powf<span style="color: #000000;">&#40;</span><span style="color: #db5821; font-weight:bold;">1</span> <span style="color: #666666; font-weight: bold;">-</span> t, <span style="color: #db5821; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #666666; font-weight: bold;">*</span> origin.<span style="">y</span> <span style="color: #666666; font-weight: bold;">+</span> <span style="color: #db5821; font-weight:bold;">2.0f</span> <span style="color: #666666; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span><span style="color: #db5821; font-weight:bold;">1</span> <span style="color: #666666; font-weight: bold;">-</span> t<span style="color: #000000;">&#41;</span> <span style="color: #666666; font-weight: bold;">*</span> t <span style="color: #666666; font-weight: bold;">*</span> control.<span style="">y</span> <span style="color: #666666; font-weight: bold;">+</span> t <span style="color: #666666; font-weight: bold;">*</span> t <span style="color: #666666; font-weight: bold;">*</span> destination.<span style="">y</span><span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
        <span style="color: #3399cc; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>i <span style="color: #666666; font-weight: bold;">&gt;</span> <span style="color: #db5821; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span>
        ccDrawCircle<span style="color: #000000;">&#40;</span>previous, <span style="color: #db5821; font-weight:bold;">2.2</span>, M_PI, <span style="color: #db5821; font-weight:bold;">6</span>, <span style="color: #db5821; font-weight: bold;">false</span><span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
        ccDrawLine<span style="color: #000000;">&#40;</span>previous, ccp <span style="color: #000000;">&#40;</span>x_, y_<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
        previous <span style="color: #666666; font-weight: bold;">=</span> ccp<span style="color: #000000;">&#40;</span>x_, y_<span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span>
&nbsp;
        t <span style="color: #666666; font-weight: bold;">+=</span> <span style="color: #000000;">&#40;</span><span style="color: #db5821; font-weight: bold;">float</span><span style="color: #000000;">&#41;</span> <span style="color: #db5821; font-weight:bold;">1</span> <span style="color: #666666; font-weight: bold;">/</span> segments<span style="color: #666666; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

</div>
<p>Note that to draw filled circles in Cocos2d and Cocos2d-x you may have to edit the drawCircle method in CCDrawingPrimitives, editing the line:</p>
<div class="as-code">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">glDrawArrays<span style="color: #000000;">&#40;</span>GL_LINE_STRIP, <span style="color: #db5821; font-weight:bold;">0</span>, <span style="color: #000000;">&#40;</span>GLsizei<span style="color: #000000;">&#41;</span> segments<span style="color: #666666; font-weight: bold;">+</span>additionalSegment<span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span></pre></td></tr></table></div>

</div>
<p>To read:</p>
<div class="as-code">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">glDrawArrays<span style="color: #000000;">&#40;</span>GL_TRIANGLE_FAN, <span style="color: #db5821; font-weight:bold;">0</span>, <span style="color: #000000;">&#40;</span>GLsizei<span style="color: #000000;">&#41;</span> segments<span style="color: #666666; font-weight: bold;">+</span>additionalSegment<span style="color: #000000;">&#41;</span><span style="color: #666666; font-weight: bold;">;</span></pre></td></tr></table></div>

</div>
<p>In LibGDX we use the ShapeRenderer class:</p>
<div class="as-code">

<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
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> drawQuadBezier <span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">float</span> x1, <span style="color: #000066; font-weight: bold;">float</span> y1, <span style="color: #000066; font-weight: bold;">float</span> cx, <span style="color: #000066; font-weight: bold;">float</span> cy, <span style="color: #000066; font-weight: bold;">float</span> x2, <span style="color: #000066; font-weight: bold;">float</span> y2, <span style="color: #000066; font-weight: bold;">int</span> segments<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">float</span> t <span style="color: #339933;">=</span> 0.0f<span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">float</span> x_<span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">float</span> y_<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">float</span> previousX <span style="color: #339933;">=</span> x1<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">float</span> previousY <span style="color: #339933;">=</span> y1<span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> segments <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		x_ <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">-</span> t, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> x1 <span style="color: #339933;">+</span> 2.0f <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">-</span> t<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> t <span style="color: #339933;">*</span> cx <span style="color: #339933;">+</span> t <span style="color: #339933;">*</span> t <span style="color: #339933;">*</span> x2<span style="color: #339933;">;</span>
		y_ <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">-</span> t, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> y1 <span style="color: #339933;">+</span> 2.0f <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">-</span> t<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> t <span style="color: #339933;">*</span> cy <span style="color: #339933;">+</span> t <span style="color: #339933;">*</span> t <span style="color: #339933;">*</span> y2<span style="color: #339933;">;</span>
&nbsp;
		shapeRenderer.<span style="color: #006633;">begin</span><span style="color: #009900;">&#40;</span>ShapeType.<span style="color: #003399;">Line</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		shapeRenderer.<span style="color: #006633;">line</span><span style="color: #009900;">&#40;</span>previousX, previousY, x_, y_<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		shapeRenderer.<span style="color: #006633;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			shapeRenderer.<span style="color: #006633;">begin</span><span style="color: #009900;">&#40;</span>ShapeType.<span style="color: #006633;">FilledCircle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			shapeRenderer.<span style="color: #006633;">filledCircle</span><span style="color: #009900;">&#40;</span>previousX, previousY, 2.2f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			shapeRenderer.<span style="color: #006633;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		previousX <span style="color: #339933;">=</span> x_<span style="color: #339933;">;</span>
		previousY <span style="color: #339933;">=</span> y_<span style="color: #339933;">;</span>
&nbsp;
		t <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">/</span> segments<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</div>
<p>The object shapeRenderer is created when I set up the camera. Note that in more recent versions of LibGDX the calls to state changes in ShapeRenderer may have changed. Also, there is the addition of a shapeRenderer.curve method, but for some reason it only covers the cubic bezier.</p>
<p>And the result? Here it is in LibGDX:</p>
<p><img class ="content-img" style="margin:10px auto;" src="http://www.rengelbert.com/blog/wp-content/themes/donewithcomputers/images/caps2.jpg"/></p>
<p>Without the logic and with the logic.</p>
]]></content:encoded>
			<wfw:commentRss>http://rengelbert.com/blog/moonherder-the-line-hack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=rengelbert&amp;popout=1&amp;url=http%3A%2F%2Frengelbert.com%2Fblog%2Fmoonherder-the-line-hack%2F&amp;language=en_GB&amp;category=text&amp;title=MoonHerder%3A+the+line+hack&amp;description=TweetThere+is+a+tiny+bit+of+an+issue+when+drawing+curved+lines+in+OpenGL%2C+because+of+the+way+the+line+%26%238220%3Bcaps%26%238221%3B+are+drawn.+Of+course+the+issue+is+only+an...&amp;tags=Actionscript%2CC%2B%2B+Tutorial%2CCocos2D+Tutorial%2CCocos2D-x+Tutorial%2CFlash%2CFlash+Games%2CGame+Development%2CiOS+Game+Development%2CJava+Tutorial%2CLibGDX+Tutorial%2CObjective-C+Tutorial%2CProgramming%2Cblog%2Cflash%2Cactionscript%2Cgame+development%2Cios%2Ciphone%2Cipad%2Candroid%2Clibgdx%2Ccocos2d%2Ccocos2d-x%2Csparrow%2Cprogramming%2Cnd2d%2Cstarling%2Cbox2d%2Cgames%2Ctutorials" type="text/html" />
	</item>
		<item>
		<title>MoonHerder: Cocos2d &amp; Cocos2d-x</title>
		<link>http://rengelbert.com/blog/moonherder-cocos2d-cocos2d-x/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=moonherder-cocos2d-cocos2d-x</link>
		<comments>http://rengelbert.com/blog/moonherder-cocos2d-cocos2d-x/#comments</comments>
		<pubDate>Thu, 07 Feb 2013 00:01:00 +0000</pubDate>
		<dc:creator>roger</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[C++ Tutorial]]></category>
		<category><![CDATA[Cocos2D Tutorial]]></category>
		<category><![CDATA[Cocos2D-x Tutorial]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[iOS Game Development]]></category>
		<category><![CDATA[Objective-C Tutorial]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://rengelbert.com/blog/moonherder-cocos2d-cocos2d-x/</guid>
		<description><![CDATA[
<img class ="entry-img" src="http://www.rengelbert.com/blog/wp-content/themes/donewithcomputers/images/entries/img_moonherderx.png"/>

<p><span class="drop-cap">H</span>ey! What's up. Me again. I just finished converting the game Moon Herder to Cocos2d-x, and I changed the syntax to make it more Cocos2d-ish. The reason being that I want to take this same code to JavaScript and see what happens. But more on that later.</p>
]]></description>
				<content:encoded><![CDATA[<div id="tweetbutton305" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Frengelbert.com%2Fblog%2Fmoonherder-cocos2d-cocos2d-x%2F&amp;text=MoonHerder%3A%20Cocos2d%20%26%23038%3B%20Cocos2d-x&amp;related=&amp;lang=en&amp;count=none&amp;counturl=http%3A%2F%2Frengelbert.com%2Fblog%2Fmoonherder-cocos2d-cocos2d-x%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://rengelbert.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><img class ="entry-img" src="http://www.rengelbert.com/blog/wp-content/themes/donewithcomputers/images/entries/img_moonherderx.png"/></p>
<p><span class="drop-cap">H</span>ey! What&#8217;s up. Me again. I just finished converting the game Moon Herder to Cocos2d-x, and I changed the syntax to make it more Cocos2d-ish. The reason being that I want to take this same code to JavaScript and see what happens. But more on that later.</p>
<p>So I also converted the old Cocos2d code to be more Cocos2d-ish as well. Now both versions are 2.0+, still no ARC, and I was finally able to compare the two in terms of building an universal app. The conclusion I arrived at is this: Cocos2d-x beats Cocos2d hands down. In terms of managing your graphics and scaling game logic, Cocos2d-x is better than Cocos2d. There. I said it.</p>
<p>Now I realize I have to follow this with a gigantic post on managing graphics for universal apps, and multi-platform apps since we are also dealing with C++. And that will come too, soon, I hope. So for now, sure, go ahead and don&#8217;t take my word for it.</p>
<p>But there are differences in how each framework handles that side of things. Especially regarding the 4 inch screen iPhone.</p>
<p>I also removed preloading, as I finally got to optimizing the graphics and the game dropped 1000% in size.</p>
<p>So here they are:</p>
<p><a href="https://github.com/rengelbert/moon-herder-cocos2d" target="_blank"> The Cocos2d code</a></p>
<p><a href="https://github.com/rengelbert/moon-herder-cocos2d-x" target="_blank"> The Cocos2d-x code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://rengelbert.com/blog/moonherder-cocos2d-cocos2d-x/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=rengelbert&amp;popout=1&amp;url=http%3A%2F%2Frengelbert.com%2Fblog%2Fmoonherder-cocos2d-cocos2d-x%2F&amp;language=en_GB&amp;category=text&amp;title=MoonHerder%3A+Cocos2d+%26%23038%3B+Cocos2d-x&amp;description=Tweet+Hey%21+What%26%238217%3Bs+up.+Me+again.+I+just+finished+converting+the+game+Moon+Herder+to+Cocos2d-x%2C+and+I+changed+the+syntax+to+make+it+more+Cocos2d-ish.+The+reason+being+that...&amp;tags=Actionscript%2CC%2B%2B+Tutorial%2CCocos2D+Tutorial%2CCocos2D-x+Tutorial%2CFlash%2CFlash+Games%2CGame+Development%2CiOS+Game+Development%2CObjective-C+Tutorial%2CProgramming%2Cblog%2Cflash%2Cactionscript%2Cgame+development%2Cios%2Ciphone%2Cipad%2Candroid%2Clibgdx%2Ccocos2d%2Ccocos2d-x%2Csparrow%2Cprogramming%2Cnd2d%2Cstarling%2Cbox2d%2Cgames%2Ctutorials" type="text/html" />
	</item>
		<item>
		<title>Fussball3D: 2D Collision Logic</title>
		<link>http://rengelbert.com/blog/fussball3d-2d-collision-logic/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fussball3d-2d-collision-logic</link>
		<comments>http://rengelbert.com/blog/fussball3d-2d-collision-logic/#comments</comments>
		<pubDate>Tue, 27 Nov 2012 00:01:00 +0000</pubDate>
		<dc:creator>roger</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Away3D]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[iOS 3D Game Development]]></category>
		<category><![CDATA[iOS Game Development]]></category>
		<category><![CDATA[ISGL3D Tutorial]]></category>
		<category><![CDATA[Objective-C Tutorial]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://rengelbert.com/blog/fussball3d-2d-collision-logic/</guid>
		<description><![CDATA[<p><span class="drop-cap">S</span>o how did I create the 2D logic for the Fussball demo? The answer is quite simple really. The foot of the player can connect with the ball only if the foot is at the right distance from the table, right? Because the player rotates, it can only hit the ball if the foot is pointing down. In the code I implemented this logic like this:
]]></description>
				<content:encoded><![CDATA[<div id="tweetbutton298" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Frengelbert.com%2Fblog%2Ffussball3d-2d-collision-logic%2F&amp;text=Fussball3D%3A%202D%20Collision%20Logic&amp;related=&amp;lang=en&amp;count=none&amp;counturl=http%3A%2F%2Frengelbert.com%2Fblog%2Ffussball3d-2d-collision-logic%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://rengelbert.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><span class="drop-cap">S</span>o how did I create the 2D logic for the Fussball demo? The answer is quite simple really. The foot of the player can connect with the ball only if the foot is at the right distance from the table, right? Because the player rotates, it can only hit the ball if the foot is pointing down. In the code I implemented this logic like this:</p>
<div class="flash-game" style="width:550px;margin:auto;">

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_fussball_rotation_652805212"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="http://www.rengelbert.com/blog/swf/fussball_rotation.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.rengelbert.com/blog/swf/fussball_rotation.swf"
			name="fm_fussball_rotation_652805212"
			width="550"
			height="400">
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
</div>
<p>When the player presses and drags the mouse on a bar, I rotate a 2D point based on the mouse position. In the movie above this point is represented by the orange ball. It rotates around a pivot point. The blue ball represents the Y displacement of the point and the green ball the X displacement.</p>
<p>The weird rectangle with the arrow represents the player sprite in &#8220;3D&#8221;. The arrow is the head. So you can see that collision can only happen depending on the Y displacement, in other words the blue ball must be a certain positive distance from the pivot point, this means the foot is close enough to the table to hit the ball.</p>
<p>Of course there are ways to build the same logic without using actual rotation. But with this, I get the extra bonus of knowing how much rotation should be applied to the 3D sprite.</p>
<p>And that&#8217;s it really. I told you it was simple. This 2D logic translates to this weird looking thing:</p>
<div class="flash-game" style="width:450px;margin:auto;">

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_fussball_2d_1881416858"
			class="flashmovie"
			width="450"
			height="600">
	<param name="movie" value="http://www.rengelbert.com/blog/swf/fussball_2d.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.rengelbert.com/blog/swf/fussball_2d.swf"
			name="fm_fussball_2d_1881416858"
			width="450"
			height="600">
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
</div>
<p>You will notice I added a slight gravity to the ball, depending on wich side of the table the ball is. I removed this logic in the 3D version. The code for this 2D demo is otherwise the same found in the final source code with some minor adjustments to speed and controls sensitivity.</p>
<p>Again, the code can be found in my GitHub account:</p>
<p><a href="https://github.com/rengelbert/fussball3d-away3d" target="_blank">- The Away3D version</a></p>
<p><a href="https://github.com/rengelbert/fussball3d-isgl3d" target="_blank">- The Isgl3D version</a></p>
]]></content:encoded>
			<wfw:commentRss>http://rengelbert.com/blog/fussball3d-2d-collision-logic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=rengelbert&amp;popout=1&amp;url=http%3A%2F%2Frengelbert.com%2Fblog%2Ffussball3d-2d-collision-logic%2F&amp;language=en_GB&amp;category=text&amp;title=Fussball3D%3A+2D+Collision+Logic&amp;description=TweetSo+how+did+I+create+the+2D+logic+for+the+Fussball+demo%3F+The+answer+is+quite+simple+really.+The+foot+of+the+player+can+connect+with+the+ball+only+if...&amp;tags=Actionscript%2CAway3D%2CFlash%2CFlash+Games%2CGame+Development%2CiOS+3D+Game+Development%2CiOS+Game+Development%2CISGL3D+Tutorial%2CObjective-C+Tutorial%2CProgramming%2Cblog%2Cflash%2Cactionscript%2Cgame+development%2Cios%2Ciphone%2Cipad%2Candroid%2Clibgdx%2Ccocos2d%2Ccocos2d-x%2Csparrow%2Cprogramming%2Cnd2d%2Cstarling%2Cbox2d%2Cgames%2Ctutorials" type="text/html" />
	</item>
	</channel>
</rss>

<!-- Dynamic page generated in 4.862 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2013-05-23 08:20:05 -->
