<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Emanuele Feronato &#187; iPhone</title>
	<atom:link href="http://www.emanueleferonato.com/category/iphone/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emanueleferonato.com</link>
	<description>italian geek and PROgrammer</description>
	<lastBuildDate>Mon, 06 Feb 2012 14:28:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>One game, many frameworks</title>
		<link>http://www.emanueleferonato.com/2012/01/03/one-game-many-frameworks/</link>
		<comments>http://www.emanueleferonato.com/2012/01/03/one-game-many-frameworks/#comments</comments>
		<pubDate>Tue, 03 Jan 2012 16:23:07 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Users contributions]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=5037</guid>
		<description><![CDATA[Ever wanted to build a mobile game using a framework? <strong>Roger Engelbert</strong> built a guide to do it with <strong>four</strong> different frameworks!]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s no doubt 2012 will be the year of the frameworks. <strong><strong>Roger Engelbert</strong></strong> in his blog <a href="http://www.rengelbert.com/" target="_blank">rengelbert.com</a> show us how to do a mobile Frogger game using four different frameworks: <a href="http://www.starling-framework.org/" target="_blank">Starling</a>, <a href="http://www.sparrow-framework.org/" target="_blank">Sparrow</a>, <a href="http://cocos2d.org/" target="_blank">Cocos2D</a> and <a href="http://libgdx.badlogicgames.com/" target="_blank">LibGDX</a>.</p>
<p>This is the game involved in the experiment:</p>
<p><center><embed src="/wp-content/uploads/2012/01/FroggerAS3.swf" menu="false" quality="high" width="320" height="480" wmode="direct" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></center></p>
<p>Use arrow keys to move the frog, or click on the frog controller in the bottom right corner of the game.</p>
<p>Here is a brief intro by Roger:</p>
<p>I will first explain something of the logic controlling the game. It is very important to point out that the logic itself has nothing to do with any of the frameworks I&#8217;ll use. The frameworks for the most part only influence the VIEW part of the logic: the actual rendering of textures on screen in place of all the rectangle objects the logic handles.</p>
<p>So these tutorials are not meant to be on how to build a game like Frogger, but how to blitt stuff to the screen.</p>
<p>Having said that, I thought I might as well explain enough of the game logic so that you can separate that logic from the actual framework code.</p>
<p><strong>The Game Engine</strong></p>
<p>The engine manages a series of Screen objects, but displays only one screen at a time. So you can build an Intro Screen, a How to Play Screen, a High Scores Screen, a Game Screen&#8230; The Engine then updates the currently displayed Screen object within a loop.</p>
<p>For this example I created two screens, one called a MenuScreen that shows the instructions on how to play the game, and one screen called GameScreen where the actual game runs.</p>
<p>The main Game object contains exposed references to GameData, the TextureAtlas and Sounds. Every object in the game receives a reference to the Game object. I&#8217;ve decide to use composition and drop the Singletons.</p>
<p><strong>Controls</strong></p>
<p>For the Flash version of the game I will use two sets of controls: Key presses and Mouse clicks. Later when porting to mobile devices I will drop the Keyboard input logic and just keep the touch events.</p>
<p><strong>The Game Logic</strong></p>
<p>The game logic is very simple. The screen is divided into 13 rows, I called them Tiers. Each Tier has a bunch of sprites inside it, they can be cars, or turtles or tree logs.</p>
<p>The frog sprite, controlled by the player, can have only 13 possible values for its Y position, one value per Tier. So with every UP or DOWN movement the player Y value changes to either the next Tier or the previous one.</p>
<p>Collision is run only with the objects belonging to the Tier the frog is currently at.</p>
<p>Each Tier has the responsibility to update the positions of the sprites it contains.</p>
<p><strong>The Skin Property</strong></p>
<p>As I talked about earlier, the different frameworks used will be in charge of the SKIN property of the Sprites used in the game as well as the various Texture objects a skin may use (if it&#8217;s animated for instance.) It is here that the Framework logic and syntax can be found.</p>
<p>However when I update the position of sprites in the game I do this through the wrappers and not directly to the Image objects. This might seem like extra work for you, and you&#8217;re free to change this. I simply find this way to be the easiest when taking the same logic to different platforms as I only need to change the logic controlling rendering of the skins.</p>
<p>Now, you are ready to ready one of the most interesting tutorials I&#8217;ve ever seen.</p>
<p><a href="http://www.rengelbert.com/tutorial.php?id=163" target="_blank">http://www.rengelbert.com/tutorial.php?id=163</a> (using Starling)<br />
<a href="http://www.rengelbert.com/tutorial.php?id=167" target="_blank">http://www.rengelbert.com/tutorial.php?id=167</a> (using Sparrow)<br />
<a href="http://www.rengelbert.com/tutorial.php?id=171" target="_blank">http://www.rengelbert.com/tutorial.php?id=171</a> (using Cocos2D)<br />
<a href="http://www.rengelbert.com/tutorial.php?id=175" target="_blank">http://www.rengelbert.com/tutorial.php?id=175</a> (using LibGDX)</p>
<p>I am looking forward for playing and messing around with frameworks and publish my thoughts.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2012/01/03/one-game-many-frameworks/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>iPhone String Avoider prototype with Adobe Flash Professional CS5.5</title>
		<link>http://www.emanueleferonato.com/2011/12/21/iphone-string-avoider-prototype-with-adobe-flash-professional-cs5-5/</link>
		<comments>http://www.emanueleferonato.com/2011/12/21/iphone-string-avoider-prototype-with-adobe-flash-professional-cs5-5/#comments</comments>
		<pubDate>Wed, 21 Dec 2011 10:49:42 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=4994</guid>
		<description><![CDATA[An experiment to port Stringy engine to iPhone using Adobe Flash Professional CS5.5]]></description>
			<content:encoded><![CDATA[<p>I am considering to port <a href="http://www.emanueleferonato.com/2011/12/16/play-stringy-my-latest-flash-game/">Stringy</a> on the iPhone and after seeing the <a href="http://www.emanueleferonato.com/2011/12/19/develop-a-string-avoider-prototype-with-corona-sdk/">Corona SDK example</a> I tried to convert the <a href="http://www.emanueleferonato.com/2011/10/13/develop-a-flash-game-like-string-avoider-as3-version-and-more/">AS3 String Avoider prototype</a> for the iPhone, using the guidelines in the post <a href="http://www.emanueleferonato.com/2011/09/22/creation-of-an-iphone-app-with-flash-and-without-a-mac-for-all-windows-lovers/">Creation of an iPhone App with Flash and without a Mac</a>.</p>
<p>So, I wrote this code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.geom</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Point</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> final <span style="color: #9900cc; font-weight: bold;">class</span> Main <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> static const tailLenght<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> static const tailNodes<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">300</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> static const head<span style="color: #000066; font-weight: bold;">:</span>Head=<span style="color: #0033ff; font-weight: bold;">new</span> Head<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> static const tailCanvas<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span>=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> static const bg<span style="color: #000066; font-weight: bold;">:</span>Background=<span style="color: #0033ff; font-weight: bold;">new</span> Background<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> nodes<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>Point<span style="color: #000066; font-weight: bold;">&gt;</span>=<span style="color: #0033ff; font-weight: bold;">new</span> Vector<span style="color: #000066; font-weight: bold;">.&lt;</span>Point<span style="color: #000066; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> followTheMouse<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>=<span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>bg<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			bg<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">cacheAsBitmap</span>=<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>head<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			head<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">cacheAsBitmap</span>=<span style="color: #0033ff; font-weight: bold;">true</span>
			head<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span>=<span style="color: #000000; font-weight:bold;">160</span><span style="color: #000066; font-weight: bold;">;</span>
			head<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span>=<span style="color: #000000; font-weight:bold;">240</span><span style="color: #000066; font-weight: bold;">;</span>
			head<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_DOWN</span><span style="color: #000066; font-weight: bold;">,</span>followMouse<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>tailCanvas<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">&lt;</span>tailNodes<span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				nodes<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>head<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span>head<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span>update<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_UP</span><span style="color: #000066; font-weight: bold;">,</span>stopMouse<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> followMouse<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			followTheMouse=<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
			head<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">visible</span>=<span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> stopMouse<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			followTheMouse=<span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span>
			head<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">visible</span>=<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>followTheMouse<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				head<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span>=<span style="color: #004993;">mouseX</span><span style="color: #000066; font-weight: bold;">;</span>
				head<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span>=<span style="color: #004993;">mouseY</span><span style="color: #000066; font-weight: bold;">;</span>
				tailCanvas<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
				tailCanvas<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">,</span>0xffffff<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
				tailCanvas<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">moveTo</span><span style="color: #000000;">&#40;</span>head<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span>head<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
				nodes<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span>=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>head<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span>head<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">&lt;</span>tailNodes<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #6699cc; font-weight: bold;">var</span> nodeAngle<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">atan2</span><span style="color: #000000;">&#40;</span>nodes<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>y<span style="color: #000066; font-weight: bold;">-</span>nodes<span style="color: #000000;">&#91;</span>i<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">,</span>nodes<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>x<span style="color: #000066; font-weight: bold;">-</span>nodes<span style="color: #000000;">&#91;</span>i<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					nodes<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>nodes<span style="color: #000000;">&#91;</span>i<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">+</span>tailLenght<span style="color: #000066; font-weight: bold;">*</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">cos</span><span style="color: #000000;">&#40;</span>nodeAngle<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span>nodes<span style="color: #000000;">&#91;</span>i<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">+</span>tailLenght<span style="color: #000066; font-weight: bold;">*</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">sin</span><span style="color: #000000;">&#40;</span>nodeAngle<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					tailCanvas<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineTo</span><span style="color: #000000;">&#40;</span>nodes<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">,</span>nodes<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #009900; font-style: italic;">// do other stuff in the game</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Which is basically the same as the <a href="http://www.emanueleferonato.com/2011/10/13/develop-a-flash-game-like-string-avoider-as3-version-and-more/">original prototype</a>, just dragging the string when the finger (mouse) is down and showing a circle where to tap (click) to start dragging again if the player leaves the finger (releases mouse button).</p>
<p>Play with the result:</p>
<p><center><embed src="/wp-content/uploads/2011/12/iStringy.swf" menu="false" quality="high" width="320" height="480" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></center></p>
<p>Drag the white circle to move the string.</p>
<p>I also optimized a bit the code defining the class as <code>final</code> since it&#8217;s not meant for being extended and its methods aren&#8217;t meant for being overridden. <code>final</code> classes should execute faster than other classes, and since I am looking for the best performance improvement, I used this trick.</p>
<p>Using <code>static</code> in some variable declarations also should increase the speed. <code>static</code> specifies that a variable (or a constant) belongs to the class rather than to the instances of the class.</p>
<p>Finally, <code>cacheAsBitmap</code> method allows me to render the Display Objects as bitmaps rather than vectors. You may say &#8220;just use a PNG image then&#8221;, and you&#8217;re right, but I wanted to keep the code as much similar to the original as I can.</p>
<p>This is the final example of the app working on my Iphone4:</p>
<p><iframe width="640" height="360" src="http://www.youtube.com/embed/zeOleAzv6K0" frameborder="0" allowfullscreen></iframe></p>
<p>It works quite well although I am considering using a sequence of dots rather than a thin line, I will keep you updated.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/12/21/iphone-string-avoider-prototype-with-adobe-flash-professional-cs5-5/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Develop a string avoider prototype with Corona SDK</title>
		<link>http://www.emanueleferonato.com/2011/12/19/develop-a-string-avoider-prototype-with-corona-sdk/</link>
		<comments>http://www.emanueleferonato.com/2011/12/19/develop-a-string-avoider-prototype-with-corona-sdk/#comments</comments>
		<pubDate>Mon, 19 Dec 2011 10:23:57 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=4987</guid>
		<description><![CDATA[Mobile version of the string avoider prototype made with Corona by <strong>Christian Wisniewski</strong>]]></description>
			<content:encoded><![CDATA[<p>As you know, <strong><a href="http://www.emanueleferonato.com/2011/12/16/play-stringy-my-latest-flash-game/">Stringy</a></strong> game was built starting from this prototype: <a href="http://www.emanueleferonato.com/2011/10/13/develop-a-flash-game-like-string-avoider-as3-version-and-more/">Develop a Flash game like String Avoider – AS3 version – and more!</a>.</p>
<p><strong>Christian Wisniewski</strong> made a Corona version of the prototype to be played on mobile devices as you can see here:</p>
<p><iframe width="640" height="480" src="http://www.youtube.com/embed/b3kz0CP3Yaw" frameborder="0" allowfullscreen></iframe></p>
<p>With this main.lua file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
</pre></td><td class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Declaring Variables</span>
<span style="color: #808080; font-style: italic;">-- Stage Width and Height</span>
<span style="color: #b1b100;">local</span> _W,_H <span style="color: #66cc66;">=</span> display.contentWidth,display.contentHeight
&nbsp;
<span style="color: #b1b100;">local</span> tailLength <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">5</span>
<span style="color: #b1b100;">local</span> tailNodes <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">200</span>
<span style="color: #b1b100;">local</span> head <span style="color: #66cc66;">=</span> display.newCircle<span style="color: #66cc66;">&#40;</span>_W<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>,_H<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">local</span> nodes <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">for</span> i<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>,tailNodes,<span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">do</span>
	nodes<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
	nodes<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.x <span style="color: #66cc66;">=</span> head.x
	nodes<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.y <span style="color: #66cc66;">=</span> head.y
<span style="color: #b1b100;">end</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- Creating Display Groups</span>
<span style="color: #b1b100;">local</span> tailGroup <span style="color: #66cc66;">=</span> display.newGroup<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #b1b100;">local</span> <span style="color: #b1b100;">function</span> startDrag<span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>
	<span style="color: #808080; font-style: italic;">-- Store the Target (the Head) in a variable</span>
	<span style="color: #b1b100;">local</span> t <span style="color: #66cc66;">=</span> event.target
	<span style="color: #b1b100;">if</span> event.phase <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;began&quot;</span> <span style="color: #b1b100;">then</span>
		<span style="color: #808080; font-style: italic;">-- When the touch started, set the Focus on the Head</span>
		display.getCurrentStage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:setFocus<span style="color: #66cc66;">&#40;</span> t <span style="color: #66cc66;">&#41;</span>
		t.isFocus <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">true</span>
		<span style="color: #808080; font-style: italic;">-- Store initial Position of the Head</span>
		t.x0 <span style="color: #66cc66;">=</span> event.x - t.x
        	t.y0 <span style="color: #66cc66;">=</span> event.y - t.y
	<span style="color: #b1b100;">elseif</span> t.isFocus <span style="color: #b1b100;">then</span>
		<span style="color: #b1b100;">if</span> event.phase <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;moved&quot;</span> <span style="color: #b1b100;">then</span>
			<span style="color: #808080; font-style: italic;">-- While moving the head</span>
			<span style="color: #808080; font-style: italic;">-- remove every drawn line from the group</span>
			<span style="color: #b1b100;">for</span> i<span style="color: #66cc66;">=</span>tailGroup.numChildren,<span style="color: #cc66cc;">1</span>,-<span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">do</span>
				tailGroup:<span style="color: #b1b100;">remove</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>
			<span style="color: #b1b100;">end</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">-- Move the Head</span>
			t.x <span style="color: #66cc66;">=</span> event.x - t.x0
			t.y <span style="color: #66cc66;">=</span> event.y - t.y0
&nbsp;
			<span style="color: #808080; font-style: italic;">-- Create starting line and insert it into the group</span>
			<span style="color: #b1b100;">local</span> line <span style="color: #66cc66;">=</span> display.newLine<span style="color: #66cc66;">&#40;</span>head.x,head.y,nodes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>.x,nodes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>.y<span style="color: #66cc66;">&#41;</span>
			tailGroup:insert<span style="color: #66cc66;">&#40;</span>line<span style="color: #66cc66;">&#41;</span>
			line.width <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">20</span>
&nbsp;
			nodes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>.x <span style="color: #66cc66;">=</span> head.x
			nodes<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>.y <span style="color: #66cc66;">=</span> head.y
&nbsp;
			<span style="color: #808080; font-style: italic;">-- Loop through every tailNode and save new positions in table</span>
			<span style="color: #808080; font-style: italic;">-- Note: LUA tables start with an index of 1 and not 0!</span>
			<span style="color: #b1b100;">for</span> i<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">2</span>,tailNodes,<span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">do</span>
				<span style="color: #b1b100;">local</span> nodeAngle <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">math.atan2</span><span style="color: #66cc66;">&#40;</span>nodes<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.y-nodes<span style="color: #66cc66;">&#91;</span>i-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>.y,nodes<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.x-nodes<span style="color: #66cc66;">&#91;</span>i-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>.x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
				nodes<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.x <span style="color: #66cc66;">=</span> nodes<span style="color: #66cc66;">&#91;</span>i-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>.x+tailLength<span style="color: #66cc66;">*</span><span style="color: #b1b100;">math.cos</span><span style="color: #66cc66;">&#40;</span>nodeAngle<span style="color: #66cc66;">&#41;</span>
				nodes<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.y <span style="color: #66cc66;">=</span> nodes<span style="color: #66cc66;">&#91;</span>i-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>.y+tailLength<span style="color: #66cc66;">*</span><span style="color: #b1b100;">math.sin</span><span style="color: #66cc66;">&#40;</span>nodeAngle<span style="color: #66cc66;">&#41;</span>
&nbsp;
				<span style="color: #808080; font-style: italic;">-- You could use</span>
				<span style="color: #808080; font-style: italic;">-- line:append(nodes[i].x,nodes[i].y)</span>
				<span style="color: #808080; font-style: italic;">-- but this wouldn't allow you to alter each segment (fading the color, reducing linewidth etc.)</span>
&nbsp;
				<span style="color: #808080; font-style: italic;">-- Creating new line segments</span>
				<span style="color: #b1b100;">local</span> nLine <span style="color: #66cc66;">=</span> display.newLine<span style="color: #66cc66;">&#40;</span>nodes<span style="color: #66cc66;">&#91;</span>i-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>.x,nodes<span style="color: #66cc66;">&#91;</span>i-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>.y,nodes<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.x,nodes<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.y<span style="color: #66cc66;">&#41;</span>
				<span style="color: #808080; font-style: italic;">-- c will store the colorvalue - starting from 255 and ending with 0 (white to black)</span>
				<span style="color: #b1b100;">local</span> c <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">255</span>-i<span style="color: #66cc66;">/</span>tailNodes<span style="color: #66cc66;">*</span><span style="color: #cc66cc;">255</span>
				nLine:setColor<span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">&#41;</span>
				<span style="color: #808080; font-style: italic;">-- width will start at 20 and fade out to 0</span>
				nLine.width <span style="color: #66cc66;">=</span> line.width-i<span style="color: #66cc66;">/</span>tailNodes<span style="color: #66cc66;">*</span>line.width
&nbsp;
				<span style="color: #808080; font-style: italic;">-- Important: insert the new Line segments into the table!</span>
				tailGroup:insert<span style="color: #66cc66;">&#40;</span>nLine<span style="color: #66cc66;">&#41;</span>
			<span style="color: #b1b100;">end</span>
		<span style="color: #b1b100;">elseif</span> event.phase <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;ended&quot;</span> <span style="color: #b1b100;">or</span> event.phase <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;cancelled&quot;</span> <span style="color: #b1b100;">then</span>
			<span style="color: #808080; font-style: italic;">-- Remove the focus</span>
			display.getCurrentStage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:setFocus<span style="color: #66cc66;">&#40;</span> t, <span style="color: #b1b100;">nil</span> <span style="color: #66cc66;">&#41;</span>
			t.isFocus <span style="color: #66cc66;">=</span> <span style="color: #b1b100;">false</span>
		<span style="color: #b1b100;">end</span>
	<span style="color: #b1b100;">end</span>
<span style="color: #b1b100;">end</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- Create touch Eventlistener</span>
head:addEventListener<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;touch&quot;</span>,startDrag<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>During next days I will improve the prototype, maybe this could be a start for a mobile version of Stringy game.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/12/19/develop-a-string-avoider-prototype-with-corona-sdk/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Develop iPhone games with Corona SDK &#8211; handling normal and retina display</title>
		<link>http://www.emanueleferonato.com/2011/11/22/develop-iphone-games-with-corona-sdk-handling-normal-and-retina-display/</link>
		<comments>http://www.emanueleferonato.com/2011/11/22/develop-iphone-games-with-corona-sdk-handling-normal-and-retina-display/#comments</comments>
		<pubDate>Tue, 22 Nov 2011 11:08:21 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=4861</guid>
		<description><![CDATA[Introducing Corona SDK to develop iPhone games, handling both normal and retina displays]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;ll start to use a new tool to develop iPhone games: <a href="http://www.anscamobile.com/corona/" target="_blank">Corona SDK</a>.</p>
<p>Built on top of OpenGL, OpenAL, Box2D, and Lua, Corona lets you develop games which run run at native speeds  using native iOS features like multitouch, GPS, accelerometer, gyroscope, camera, Google Maps, WebKit and software keyboards.</p>
<p><a href="http://www.anscamobile.com/corona/" target="_blank"><img src="/wp-content/uploads/2011/11/corona.jpg" alt="" title="corona" width="640" height="254" class="alignnone size-full wp-image-4866" /></a></p>
<p>Moreover, Corona supports an <a href="http://developer.anscamobile.com/reference" target="_blank">huge list of APIs</a> which will let you add to your games a lot of interesting features like ads, virtual currency, databases and so on.</p>
<p>In this first step, I&#8217;ll try to port my old Flash game <a href="http://www.emanueleferonato.com/2008/12/21/play-red-flowers/">Red Flowers</a> to iPhone, handling the retina display of iPhone 4 models.</p>
<p>We will deal with two different screen sizes: 320&#215;480 and 640&#215;960. So the first thing to do is to design two  different splash pages, one for normal iPhones and one for retina-powered.</p>
<p><img src="/wp-content/uploads/2011/11/twosizes.jpg" /></p>
<p>On the left, the 480&#215;320 splash screen for normal iPhones, on the right, the 960&#215;640 HD splash for retina displays.</p>
<p>I saved them in a folder, calling them respectively <code>splash.png</code> and <code>splash_hd.png</code>.</p>
<p>Note the HD image has the same name as the normal image, just with an <code>_hd</code> suffix.</p>
<p>Now, the very basic Corona project has three files: <code>build.settings</code> which includes duild-time properties, <code>config.lua</code> which contains runtime properties, and <code>main.lua</code> which is the main file itself.</p>
<p>As you can see looking at <code>lua</code> extensions, Corona SDK uses <a href="http://www.lua.org/" target="_blank">Lua</a> as programming language. Probably some of you already met it when playing with World of Warcraft macros and interfaces. Anyway, it&#8217;s not that hard and not that different than AS3.</p>
<p>Let&#8217;s look at <code>build.settings</code> file:</p>

<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="lua" style="font-family:monospace;">settings <span style="color: #66cc66;">=</span>
<span style="color: #66cc66;">&#123;</span>
	orientation <span style="color: #66cc66;">=</span>
	<span style="color: #66cc66;">&#123;</span>
		default <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;landscapeRight&quot;</span>,
	<span style="color: #66cc66;">&#125;</span>,
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Here I just set the orientation as landscape, as the game is meant to be played in landscape mode.</p>
<p>This is <code>config.lua</code> file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="lua" style="font-family:monospace;">application <span style="color: #66cc66;">=</span>
<span style="color: #66cc66;">&#123;</span>
	content <span style="color: #66cc66;">=</span>
	<span style="color: #66cc66;">&#123;</span>
		width <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">320</span>,
		height <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">480</span>,
		scale <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;letterbox&quot;</span>,
		imageSuffix <span style="color: #66cc66;">=</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;_hd&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span>,
		<span style="color: #66cc66;">&#125;</span>,
	<span style="color: #66cc66;">&#125;</span>,
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Another easy and intuitive settings: I am defining game width and height, and setting scale mode as &#8220;letterbox&#8221;.</p>
<p>These are the possible dynamic scale settings:</p>
<p>* <strong>none</strong> &#8211; turns off dynamic content scaling</p>
<p>* <strong>letterbox</strong> &#8211; uniformly scales up content as much as possible, while still showing all content on the screen. This is similar to watching a widescreen DVD on a non-widescreen TV set, except that you can use offstage content to fill the &#8220;bleed&#8221; areas rather than displaying black bars.</p>
<p>* <strong>zoomEven</strong> &#8211; uniformly scales up content to fill the screen, while preserving aspect ratio. Some content may appear offscreen, if the new screen has a different aspect ratio</p>
<p>* <strong>zoomStretch</strong> &#8211; non-uniformly scales up content to fill the screen. All content will remain onscreen, but it may be stretched vertically or horizontally.</p>
<p>Then, we defined on <code>imageSuffix</code> the suffix to be used when the images need to be multiplied by 2 to match device resolution. Do you remember the <code>_hd</code> suffix we added to our HD splash screen? Here it is.</p>
<p>Finally, let&#8217;s code <code>main.lua</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="lua" style="font-family:monospace;">display.setStatusBar<span style="color: #66cc66;">&#40;</span>display.HiddenStatusBar<span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">local</span> bg <span style="color: #66cc66;">=</span> display.newImageRect<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;splash.png&quot;</span>,<span style="color: #cc66cc;">480</span>,<span style="color: #cc66cc;">320</span><span style="color: #66cc66;">&#41;</span>
bg.x <span style="color: #66cc66;">=</span> bg.contentWidth<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>
bg.y <span style="color: #66cc66;">=</span> bg.contentHeight<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span></pre></td></tr></table></div>

<p><strong>Line 1</strong>: hides the status bar</p>
<p><strong>Line 2</strong>: defines a variable called <code>bg</code> assigning it the path of the <strong>low resolution</strong> splash screen, along with its size. At this time the image is automatically shown, with its origin (the center) aligned with the top left corner of the device.</p>
<p><strong>Line 3</strong>: centers horizontally the splash according to its width</p>
<p><strong>Line 4</strong>: same thing with the height.</p>
<p>Notice how <code>x</code> and <code>y</code> properties work the same way as AS3, while <code>contentWidth</code> and <code>contentHeight</code> work like AS3 <code>width</code> and <code>height</code>.</p>
<p>Once all files are saved in the same folder, it&#8217;s time to run the simulator in iPhone and iPhone 4 mode</p>
<p><img src="/wp-content/uploads/2011/11/simulator.jpg" /></p>
<p>And finally we have our splash screen up and working. Next time, I&#8217;ll add interactivity.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/11/22/develop-iphone-games-with-corona-sdk-handling-normal-and-retina-display/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Creation of an iPhone App with Flash and without a Mac (for all Windows lovers)</title>
		<link>http://www.emanueleferonato.com/2011/09/22/creation-of-an-iphone-app-with-flash-and-without-a-mac-for-all-windows-lovers/</link>
		<comments>http://www.emanueleferonato.com/2011/09/22/creation-of-an-iphone-app-with-flash-and-without-a-mac-for-all-windows-lovers/#comments</comments>
		<pubDate>Thu, 22 Sep 2011 09:52:40 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=4636</guid>
		<description><![CDATA[Detailed tutorial about Flash development for iPhone, oriented to Windows users. Step by step explanation with 29 screenshots. You can't fail.]]></description>
			<content:encoded><![CDATA[<p>It seems you can&#8217;t call yourself a developer if you did not build at least one iPhone application. For most developers, the biggest problem of getting started with iPhone development is they don&#8217;t have a Mac. Windows based notebooks are a lot less expensive, and having both a Windows notebook and a MacBook is not for everyone.</p>
<p>Moreover, a lot of readers of this blog are skilled with Flash and ActionsScript3, but they don&#8217;t feel comfortable with Objective-C, the programming language used to create native iPhone applications.</p>
<p>Well, I am going to show you a step by step detailed guide to help you publishing an iPhone application just with your Windows computer, thanks to <a href="http://www.adobe.com/cfusion/tdrc/index.cfm?product=flash" target="_blank">Flash Professional CS5.5</a>.</p>
<p>This will be a very long tutorial but once finished, you will be able to create a Flash application and make it run on your iPhone as a native application.</p>
<p><strong>1 &#8211; CREATING THE FLASH APPLICATION</strong></p>
<p>This is the easiest part, as you only have to do what you are really used to do: create a Flash application. Just remember to select <strong>AIR for iOS</strong>. Leaving all settings with their default values, you will create a 320&#215;480 pixels document, which is the size of the non-retina iPhone display.</p>
<p><img src="/wp-content/uploads/2011/09/cert27.jpg" /></p>
<p>We will start with something simple, with this script:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Main <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> circle<span style="color: #000066; font-weight: bold;">:</span>circleMc<span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			circle= <span style="color: #0033ff; font-weight: bold;">new</span> circleMc<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>circle<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span>moveCircle<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> moveCircle<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			circle<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span>=<span style="color: #004993;">mouseX</span><span style="color: #000066; font-weight: bold;">;</span>
			circle<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span>=<span style="color: #004993;">mouseY</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Where <code>circleMc</code> is just a small red circle. It&#8217;s quite obvious what this script is supposed to do: clicking (later: tapping) on the stage, you will move the circle under the mouse pointer (later: your finger).</p>
<p>Test the movie, and once you are sure everything works, it&#8217;s time to create the developer certificate</p>
<p><strong>2 &#8211; APPLYING TO APPLE IOS DEVELOPER PROGRAM</strong></p>
<p>If you never developed an iPhone application, probably you aren&#8217;t part of Apple iOS Developer Program, so you must enroll to it. This is a paid service which costs $99/year, or €79/year if you live in the Euro zone. Actually, the change is a bit unfair since the Euro certification may cost up to $10 more than the Dollar certification.</p>
<p>Anyway, everything starts from <a href="http://developer.apple.com/programs/ios/" target="_blank">this page</a>, click on <strong>Enroll Now</strong> and follow the steps filling forms with your information.</p>
<p><img src="/wp-content/uploads/2011/09/cert28.jpg" /></p>
<p>As you proceed with the enrollment, you will notice a warning:</p>
<p>&#8220;You must have an Intel-based Mac running Mac OS X Snow Leopard or later to develop Mac OS X and iOS apps for the App Store&#8221;.</p>
<p>Ignore it, as you won&#8217;t need any Mac.</p>
<p>According to Apple&#8217;s suggestions, if you intend to enroll in a paid Developer Program for business purposes, you may prefer to create a new Apple ID that is dedicated to your business transactions and used for accounting purposes with Apple, and your Apple ID is associated with an existing iTunes Connect account you should create a new Apple ID to avoid accounting and reporting issues.</p>
<p>They are right, I tried to upgrade my iTunes account, and my Developer certification was kept on hold because of some difference between my living and billing addresses, which is quite common if you work both at home and in a studio.</p>
<p>So I recommend to create a brand new account.</p>
<p><img src="/wp-content/uploads/2011/09/cert29.jpg" /></p>
<p>Once the (huge) process is done, you will finally see this screen:</p>
<p><img src="/wp-content/uploads/2011/09/cert01.jpg" /></p>
<p>Enter the activation code you should have received in your email after the purchase, and you are now an iPhone developer!!</p>
<p><img src="/wp-content/uploads/2011/09/cert09.png" /></p>
<p>In some cases, Apple will keep your certification on hold anyway, to ensure the (big) amount of data you entered is correct. It will be asked you to send a fax with a notarized copy of a government issued identification and wait &#8220;several working days&#8221; to get the certification. Before doing it, go to the <a href="http://developer.apple.com/contact/" target="_blank">contact page</a> and send a form explaining you made everything right. In most cases, common sense will win and you will have your account approved in less than 24 hours.</p>
<p><strong>3 &#8211; GETTING A DEVELOPMENT CERTIFICATE</strong></p>
<p>Once you registered as developer, you need to acquire a Development Certificate, as well as register your Apple devices and applications.</p>
<p>Go to <a href="https://developer.apple.com/devcenter/ios/index.action" target = "blank">iOS Dev Center</a>, log in and enter the <strong>iOS Provisioning Portal</strong>:</p>
<p><img src="/wp-content/uploads/2011/09/cert02.jpg" /></p>
<p>Then click on <strong>Certificates</strong>:</p>
<p><img src="/wp-content/uploads/2011/09/cert03.jpg" /></p>
<p>And since you are new to the iOS Provisioning Portal and don&#8217;t have any certificate, click on <strong>Request Certificate</strong>:</p>
<p><img src="/wp-content/uploads/2011/09/cert04.jpg" /></p>
<p>You will find yourself in front of a page where you are asked to choose a file and submit it to the page:</p>
<p><img src="/wp-content/uploads/2011/09/cert05.jpg" /></p>
<p>Don&#8217;t click anything at the moment, as you don&#8217;t have any file yet. We have to create it.</p>
<p>It&#8217;s time to play a bit with <a href="http://en.wikipedia.org/wiki/Transport_Layer_Security" target="_blank">SSL protocols</a>.</p>
<p>Go to the <a href="http://www.slproweb.com/products/Win32OpenSSL.html" target="_blank">Win32 OpenSSL Installation Project</a> and download and install both <a href="http://www.slproweb.com/download/Win32OpenSSL_Light-1_0_0e.exe" target="_blank">Win32 OpenSSL v1.0.0e Light</a> and <a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&#038;id=29" target="_blank">Microsoft Visual C++ 2008 Redistributable Package</a>.</p>
<p><img src="/wp-content/uploads/2011/09/cert06.png" /></p>
<p>First install Microsoft&#8217;s package, then Win32 OpenSSL which will probably install in <code>C:\OpenSSL-Win32</code> path. During this tutorial, I will refer to this path, but obviously you should change yours if different.</p>
<p>Open the command prompt window by entering <code>cmd</code> into Start-Run or through <code>Start->All Programs->Accessories</code>, and write:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">cd \OpenSSL-Win32\bin</pre></div></div>

<p>Remember I am assuming OpenSSL has been installed on <code>C:\OpenSSL-Win32</code>. Anyway, access to <code>bin</code> folder inside your installation.</p>
<p>Once you&#8217;re in the <code>bin</code> folder, write:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">openssl</pre></div></div>

<p>To change the prompt from <code>C:\OpenSSL-Win32\bin></code> to <code>OpenSSL></code>.</p>
<p>If you get a warning you can&#8217;t open the config file, don&#8217;t worry at the moment. Write:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">genrsa -out thekey.key 2048</pre></div></div>

<p>To generate a file called <code>thekey.key</code> in <code>bin</code> folder. Feel free to change the file name if you want. I&#8217;ll assume you will use the same filename I used. Then write:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">req -new -key thekey.key -out certificate.csr  -subj &quot;/emailAddress=info@emanueleferonato.com, CN=Emanuele Feronato, C=IT&quot; -config &quot;openssl.cfg&quot;</pre></div></div>

<p>Which will create a file called <code>certificate.csr</code> in bin <code>folder</code>. Just change <code>thekey.key</code> with your key filename, <code>info@emanueleferonato.com</code> with the email address you used during the registration, <code>Emanuele Feronato</code> with the name you used during the registration and <code>IT</code> with the country code of the Country you live in. <code>-config "openssl.cfg"</code> is necessary <strong>only</strong> if you previously got the warning you couldn&#8217;t open the config file.</p>
<p>This is how my command prompt window looked like once I&#8217;ve finished (sorry for the italian text, it&#8217;s a real example):</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">Microsoft Windows [Versione 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. Tutti i diritti riservati.
&nbsp;
C:\Users\User&gt;cd \OpenSSL-Win32\bin
&nbsp;
C:\OpenSSL-Win32\bin&gt;openssl
WARNING: can't open config file: /usr/local/ssl/openssl.cnf
OpenSSL&gt; genrsa -out thekey.key 2048
Loading 'screen' into random state - done
Generating RSA private key, 2048 bit long modulus
.....................+++
........+++
e is 65537 (0x10001)
OpenSSL&gt; req -new -key thekey.key -out certificate.csr  -subj &quot;/emailAddress=info@emanueleferonato.com, CN=Emanuele Feronato, C=IT&quot; -config &quot;openssl.cfg&quot;
Loading 'screen' into random state - done
OpenSSL&gt;</pre></div></div>

<p>Finally you can upload the file <code>.csr</code> in the certificate page, and your certificate will be in <strong>Pending Issuance</strong> status.  </p>
<p><img src="/wp-content/uploads/2011/09/cert07.png" /></p>
<p>Refresh in a couple of minutes, and your certificate will be issued! Download it (its name should be <code>developer_identity.cer</code>) and for the sake of simplicity save it in the same <code>bin</code> folder where you created the other files, as the process is not over.</p>
<p><img src="/wp-content/uploads/2011/09/cert08.png" /></p>
<p>Connect your iPhone to iTunes and get the UDID, you will find in the iPhone page and probably you will have to click on the series number in order to get it.</p>
<p><img src="/wp-content/uploads/2011/09/cert10.jpg" /></p>
<p>Return to the Provisioning Portal and click on <strong>Devices</strong>, then since you shouldn&#8217;t have any device installed, click on &#8220;Add Devices&#8221;.</p>
<p><img src="/wp-content/uploads/2011/09/cert11.jpg" /></p>
<p>Add a device name and the UDID you just got from iTunes, then click on &#8220;Submit&#8221;:</p>
<p><img src="/wp-content/uploads/2011/09/cert12.jpg" /></p>
<p>Your iPhone should be successfully registered now.</p>
<p><img src="/wp-content/uploads/2011/09/cert13.jpg" /></p>
<p>Time to register your app now. Go to <strong>App IDs</strong> page and click on New App ID.</p>
<p><img src="/wp-content/uploads/2011/09/cert14.jpg" /></p>
<p>You will be asked to enter a name, you will find a precompiled App ID prefix and you will be asked to enter the APP ID suffix. You can enter anything you want but you&#8217;d better follow Apple guidelines using the reverse domain name style. Click on &#8220;Submit&#8221;:</p>
<p><img src="/wp-content/uploads/2011/09/cert15.jpg" /></p>
<p>And now your App is registered.</p>
<p><img src="/wp-content/uploads/2011/09/cert16.jpg" /></p>
<p>One last effort and go to <strong>Provisioning</strong> to create a Development Provisioning Profile and click on &#8220;New Profile&#8221;:</p>
<p><img src="/wp-content/uploads/2011/09/cert17.jpg" /></p>
<p>Fill the form (it shouldn&#8217;t be hard at this time) and click on &#8220;Submit&#8221;</p>
<p><img src="/wp-content/uploads/2011/09/cert18.jpg" /></p>
<p>Your Provisioning Profile will appear in Pending mode:</p>
<p><img src="/wp-content/uploads/2011/09/cert19.jpg" /></p>
<p>And just like before, in a few minutes it will be <strong>Active</strong>. Download it (it should call <code>Test_Profile.mobileprovision</code> if you named the Provisioning Profile as Test Profile) and save it in your friend <code>bin</code> folder.</p>
<p><img src="/wp-content/uploads/2011/09/cert20.jpg" /></p>
<p>Open a new command prompt window, enter the old same</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">cd \OpenSSL-Win32\bin</pre></div></div>

<p>and</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">openssl</pre></div></div>

<p>and from <code>OpenSSL></code> prompt write:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">x509 -in developer_identity.cer -inform DER -out developer_identity.pem -outform PEM</pre></div></div>

<p>to create a file called <code>developer_identity.pem</code>, then</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">pkcs12 -export -inkey thekey.key -in developer_identity.pem -out iphone_dev.p12</pre></div></div>

<p>to create an <code>iphone_dev.p12</code> file. You will be asked for a password, enter it (and possibly, remember it).</p>
<p>This is how my prompt command window looked like:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">Microsoft Windows [Versione 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. Tutti i diritti riservati.
&nbsp;
C:\Users\User&gt;cd \OpenSSL-Win32\bin
&nbsp;
C:\OpenSSL-Win32\bin&gt;openssl
OpenSSL&gt; x509 -in developer_identity.cer -inform DER -out developer_identity.pem -outform PEM
OpenSSL&gt; pkcs12 -export -inkey thekey.key -in developer_identity.pem -out iphone_dev.p12
Loading 'screen' into random state - done
Enter Export Password:
Verifying - Enter Export Password:
OpenSSL&gt;</pre></div></div>

<p>Now finally we have everything we need.</p>
<p><strong>4 &#8211; PUBLISHING WITH FLASH FOR IPHONE</strong></p>
<p>Back to Flash Professional CS5.5, in your publish settings, add the certificate file, the password you entered before, the provisioning profile, and select the iOS development type as &#8220;Quick publishing for device testing&#8221;.</p>
<p><img src="/wp-content/uploads/2011/09/cert21.jpg" /></p>
<p>Publish the application. It will take up to some minutes, unlike regular Flash movies publishing. And finally the App is ready to be uploaded to your iPhone.</p>
<p><img src="/wp-content/uploads/2011/09/cert22.jpg" /></p>
<p>This should be the content of your Flash application folder: look at the <code>iTest.ipa</code> file. It&#8217;s your App!!!</p>
<p><img src="/wp-content/uploads/2011/09/cert23.jpg" /></p>
<p>Go to iTunes connect the iPhone, and install the provisioning profile, using <code>File->Add To Library</code>. Then, synchronize your iPhone to have it installed on it. To check if everything worked fine, in your iPhone select <code>Settings->General->Profiles</code> and look for your profile.</p>
<p><img src="/wp-content/uploads/2011/09/cert24.jpg" /></p>
<p>With the profile installed, add your application with <code>File->Add To Library</code>, and you will find it in your <strong>Sync Apps</strong> page:</p>
<p><img src="/wp-content/uploads/2011/09/cert25.jpg" /></p>
<p>Synchronize your iPhone again, and you will find the working application on your device:</p>
<p><img src="/wp-content/uploads/2011/09/cert26.jpg" /></p>
<p>Congratulations, you made it. Now, let&#8217;s port your Flash blockbusters on iPhone</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/09/22/creation-of-an-iphone-app-with-flash-and-without-a-mac-for-all-windows-lovers/feed/</wfw:commentRss>
		<slash:comments>41</slash:comments>
		</item>
		<item>
		<title>Playing with javascript, iPhone and devicemotion event listener</title>
		<link>http://www.emanueleferonato.com/2011/09/05/playing-with-javascript-iphone-and-devicemotion-event-listener/</link>
		<comments>http://www.emanueleferonato.com/2011/09/05/playing-with-javascript-iphone-and-devicemotion-event-listener/#comments</comments>
		<pubDate>Mon, 05 Sep 2011 14:58:27 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=4591</guid>
		<description><![CDATA[How to create an iPhone accelerometer monitor and an "highest iPhone point" arrow using javascript's <code>devicemotion</code> event listener.]]></description>
			<content:encoded><![CDATA[<p>As you all know, the iPhone has an accelerometer which can help you to detect phone movements and tilts.</p>
<p>With javascript&#8217;s <code>devicemotion</code> event you can add a listener to your iPhone browser and have your x, y and z acceleration values ready to be used in your scripts.</p>
<p>Before we see any script, let&#8217;s see what these acceleration values mean: if you place your iPhone on a table, that&#8217;s what you can get: </p>
<p><strong>X</strong>: roll, that is the rotation around the vertical axis, from the home button to the earpiece.</p>
<p><strong>Y</strong>: pitch, that is the rotation around the horizontal axis.</p>
<p><strong>Z</strong>: yaw&#8230; no&#8230; surprise! It&#8217;s not the yaw, it&#8217;s just a face up/face down detection. This means you can toss your iPhone in the air and play &#8220;head or tail&#8221; just like you are used to do with coins.</p>
<p>(warning: tossing your iPhone in the air can cause damage to the phone itself and injuries to nearby people)</p>
<p>Even more interesting, with the iPhone in your hand you can determine where is the &#8220;sky&#8221;, that is the iPhone highest point, just combining X and Y values and some trigonometry. Useful if you are buried under an avalanche.</p>
<p>(warning: getting buried under an avalanche may reduce life expectancy)</p>
<p>So this is what you can do with a little jQuery Mobile script and a touch of HTML5 canvas:</p>
<p><img src="/wp-content/uploads/2011/09/rollpitch.png" /></p>
<p>This is the code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt; 
&lt;html&gt; 
&lt;head&gt; 
&nbsp;
     &lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=utf-8&quot;&gt; 
     &lt;meta name=&quot;viewport&quot; content=&quot;user-scalable=no, width=device-width&quot; /&gt;
&nbsp;
     &lt;link rel=&quot;stylesheet&quot; href=&quot;http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.css&quot; /&gt;
&nbsp;
     &lt;script type=&quot;text/javascript&quot; src = &quot;http://code.jquery.com/jquery-1.6.2.min.js&quot;&gt;&lt;/script&gt;
     &lt;script type=&quot;text/javascript&quot; src = &quot;http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.js&quot;&gt;&lt;/script&gt;
&nbsp;
	<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
		$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><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>
			window.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;devicemotion&quot;</span><span style="color: #339933;">,</span>onDeviceMotion<span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</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: #339933;">;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">function</span> onDeviceMotion<span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> ctx <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;2d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> accel <span style="color: #339933;">=</span> event.<span style="color: #660066;">accelerationIncludingGravity</span><span style="color: #339933;">;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sliderX&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>accel.<span style="color: #660066;">x</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slider</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;refresh&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sliderY&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>accel.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slider</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;refresh&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sliderZ&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>accel.<span style="color: #660066;">z</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slider</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;refresh&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> angle <span style="color: #339933;">=</span> Math.<span style="color: #660066;">atan2</span><span style="color: #009900;">&#40;</span>accel.<span style="color: #660066;">y</span><span style="color: #339933;">,</span>accel.<span style="color: #660066;">x</span><span style="color: #009900;">&#41;</span>  
			ctx.<span style="color: #660066;">clearRect</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><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span><span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			ctx.<span style="color: #660066;">beginPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			ctx.<span style="color: #660066;">arc</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">50</span><span style="color: #339933;">,</span><span style="color: #CC0000;">50</span><span style="color: #339933;">,</span><span style="color: #CC0000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>Math.<span style="color: #660066;">PI</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			ctx.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">50</span><span style="color: #339933;">,</span><span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">50</span><span style="color: #339933;">-</span><span style="color: #CC0000;">50</span><span style="color: #339933;">*</span>Math.<span style="color: #660066;">cos</span><span style="color: #009900;">&#40;</span>angle<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">50</span><span style="color: #339933;">+</span><span style="color: #CC0000;">50</span><span style="color: #339933;">*</span>Math.<span style="color: #660066;">sin</span><span style="color: #009900;">&#40;</span>angle<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			ctx.<span style="color: #660066;">stroke</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;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&nbsp;
&lt;/head&gt; 
&lt;body&gt; 
&nbsp;
&lt;div data-role=&quot;page&quot; id = &quot;intropage&quot;&gt;
&nbsp;
	&lt;div data-role=&quot;header&quot;&gt;
		&lt;h1&gt;Accelerometer&lt;/h1&gt;
	&lt;/div&gt;
&nbsp;
	&lt;div data-role=&quot;content&quot;&gt;	   
     	&lt;label for=&quot;sliderX&quot;&gt;X Acceleration (Roll)&lt;/label&gt;
		&lt;input type=&quot;range&quot; name=&quot;sliderX&quot; id=&quot;sliderX&quot; value=&quot;0&quot; min=&quot;-10&quot; max=&quot;10&quot; data-theme=&quot;a&quot; /&gt;
&nbsp;
          &lt;label for=&quot;sliderY&quot;&gt;Y Acceleration (Pitch)&lt;/label&gt;
          &lt;input type=&quot;range&quot; name=&quot;sliderY&quot; id=&quot;sliderY&quot; value=&quot;0&quot; min=&quot;-10&quot; max=&quot;10&quot; data-theme=&quot;b&quot; /&gt;
&nbsp;
          &lt;label for=&quot;sliderZ&quot;&gt;Z Acceleration (&lt;strike&gt;Yaw&lt;/strike&gt; Face up/down)&lt;/label&gt;
          &lt;input type=&quot;range&quot; name=&quot;sliderZ&quot; id=&quot;sliderZ&quot; value=&quot;0&quot; min=&quot;-10&quot; max=&quot;10&quot; data-theme=&quot;c&quot; /&gt;
     &lt;/div&gt;	
&nbsp;
	&lt;div style = &quot;text-align:center;margin-top:10px;&quot;&gt;
		&lt;canvas id=&quot;c&quot; width=&quot;100&quot; height=&quot;100&quot;&gt;&lt;/canvas&gt;
	&lt;/div&gt;
&nbsp;
&lt;/div&gt;
&nbsp;
&lt;/body&gt; 
&lt;/html&gt;</pre></td></tr></table></div>

<p>The core lies at <strong>line 15</strong> (the listener) and <strong>20</strong> (getting acceleration data).</p>
<p>You can see it in action pointing your iPhone browser at this link:</p>
<p><a href="http://www.emanueleferonato.com/stuff/iaccelerometer/" target="_blank">http://www.emanueleferonato.com/stuff/iaccelerometer/</a></p>
<p>or using this QRCode:</p>
<p><img src="http://chart.apis.google.com/chart?cht=qr&#038;chs=150x150&#038;chl=http%3A//www.emanueleferonato.com/stuff/iaccelerometer/&#038;chld=H|0" alt="" /></p>
<p>And that&#8217;s it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/09/05/playing-with-javascript-iphone-and-devicemotion-event-listener/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Play GuessNext for iPhone, a web app game powered by jQuery Mobile</title>
		<link>http://www.emanueleferonato.com/2011/08/17/play-guessnext-for-iphone-a-web-app-game-powered-by-jquery-mobile/</link>
		<comments>http://www.emanueleferonato.com/2011/08/17/play-guessnext-for-iphone-a-web-app-game-powered-by-jquery-mobile/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 12:21:01 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Monetize]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=4544</guid>
		<description><![CDATA[My first web app game for iPhone is GuessNext, powered by jQuery Mobile. Play it and give feedback.]]></description>
			<content:encoded><![CDATA[<p>Do you want to play a web app game for iPhone? Go to <a href="http://www.emanueleferonato.com/stuff/guessnext/">www.emanueleferonato.com/stuff/guessnext</a> or use this QR Code</p>
<p><img src="/wp-content/uploads/2011/08/qrguessnext.png" /></p>
<p>and play <strong><a href="http://www.emanueleferonato.com/stuff/guessnext/">GuessNext for iPhone</a></strong>!</p>
<p><a href="http://www.emanueleferonato.com/stuff/guessnext/"><img src="/wp-content/uploads/2011/08/guessnext.jpg" /></a></p>
<p>It&#8217;s the web remake of an <a href="http://www.emanueleferonato.com/2007/12/30/guessnext-complete-flash-game-with-highscores/">old game I published on Kongregate</a> more than three years ago, and it&#8217;s the perfect kind of game if you want to take the first steps into iPhone web app development.</p>
<p>During the next days I will explain in detail every (interesting) line of code, meanwhile have a look at the features:</p>
<p>* Powered by <a href="http://jquerymobile.com/" target="_blank">jQuery Mobile</a><br />
* Optimized to be launched from Home<br />
* Loading screen (only if played from Home)<br />
* Custom icon when adding it to Home<br />
* Reminder to add it to Home if the game is not launched from Home<br />
* Smooth transitions between pages<br />
* Best score saved locally<br />
* In game Adsense ads<br />
* Views and plays tracking powered by <a href="http://playtomic.com/" target="_blank">Playtomic</a></p>
<p>The game was built starting from the tutorials <a href="http://www.emanueleferonato.com/2011/06/30/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store/" target="_blank">Making a game with HTML + jQuery Mobile and publishing it to Apple App Store step 1</a> and <a href="http://www.emanueleferonato.com/2011/07/20/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store-step-2/" target="_blank">step 2</a>, reading those tutorials will give you a lot of clues about the development of this game.</p>
<p>Your feedback is needed to improve a bit the game, to finally convert it into a native iPhone app. Also, I would like to know your best scores.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/08/17/play-guessnext-for-iphone-a-web-app-game-powered-by-jquery-mobile/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Making a game with HTML + jQuery Mobile and publishing it to Apple App Store &#8211; step 2</title>
		<link>http://www.emanueleferonato.com/2011/07/20/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store-step-2/</link>
		<comments>http://www.emanueleferonato.com/2011/07/20/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store-step-2/#comments</comments>
		<pubDate>Wed, 20 Jul 2011 17:47:22 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[Html]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=4473</guid>
		<description><![CDATA[Second step of the iPhone web app creation, this time featuring splash screen, status bar appearance and saving data locally.]]></description>
			<content:encoded><![CDATA[<p>Some days ago I showed you the <a href="http://www.emanueleferonato.com/2011/06/30/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store/">basics of an HTML game optimized for the iPhone</a>.</p>
<p>Now we are going to improve the game with three exciting new features:</p>
<p>* Showing a splash/preloader screen<br />
* Changing iPhone top bar appearance<br />
* Saving data locally</p>
<p>To show a preloader we just need to add a line in the header of the page:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;apple-touch-startup-image&quot; href=&quot;splash.png&quot; /&gt;</pre></div></div>

<p>This will work only on iOS 3.0 and later, and <code>splash.png</code> (which you will change with any other path) is the splash image that is displayed while your web application launches, replacing the default screenshot of the web application taken the last time it was used.</p>
<p>Your image must have a size of 320&#215;460 pixels.</p>
<p>In the following picture, on the left the picture I used, on the right how it is shown on the iPhone.</p>
<p><img src="/wp-content/uploads/2011/07/iphone01.png" /></p>
<p>You can also change the status bar with just one line in the header:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;meta name=&quot;apple-mobile-web-app-status-bar-style&quot; content=&quot;black&quot; /&gt;</pre></div></div>

<p>This will work only if your app is launched in full screen mode (refer to <a href="http://www.emanueleferonato.com/2011/06/30/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store/">step 1</a> to see how to launch a web app in full screen mode).</p>
<p>content attribute may have three values:</p>
<p>default: displays the status bar as you are used to see.</p>
<p>black: the status bar will have a black background.</p>
<p>black-translucent: the status bar is black and translucent. In this case, the web content is displayed even below the status bar, partially obscured by the status bar.</p>
<p>This is how my app looks before and after changing the status bar:</p>
<p><img src="/wp-content/uploads/2011/07/iphone02.png" /></p>
<p>As you can see, the status bar is black.</p>
<p>To save data locally, <code>localStorage</code> comes into our help as it acts like a browser local database, or something like a cookie, if you prefer.</p>
<p>That&#8217;s why in this script:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><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> currentNumber <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> score <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>localStorage.<span style="color: #660066;">topScore</span><span style="color: #339933;">==</span>undefined<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		localStorage.<span style="color: #660066;">topScore</span><span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#topscore&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Top score: &quot;</span><span style="color: #339933;">+</span>localStorage.<span style="color: #660066;">topScore</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#number&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>currentNumber<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Click higher or lower&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#higher&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click tap&quot;</span><span style="color: #339933;">,</span><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> newNumber <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>newNumber <span style="color: #339933;">&gt;=</span> currentNumber<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			score<span style="color: #339933;">++;</span>	
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>score<span style="color: #339933;">&gt;</span>localStorage.<span style="color: #660066;">topScore</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				localStorage.<span style="color: #660066;">topScore</span><span style="color: #339933;">=</span>score<span style="color: #339933;">;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#topscore&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Top score: &quot;</span><span style="color: #339933;">+</span>localStorage.<span style="color: #660066;">topScore</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Good! score: &quot;</span><span style="color: #339933;">+</span>score<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>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Bad! score: &quot;</span><span style="color: #339933;">+</span>score<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		currentNumber <span style="color: #339933;">=</span> newNumber<span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#number&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>currentNumber<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;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#lower&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click tap&quot;</span><span style="color: #339933;">,</span><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> newNumber <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>newNumber <span style="color: #339933;">&lt;=</span> currentNumber<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			score<span style="color: #339933;">++;</span>	
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>score<span style="color: #339933;">&gt;</span>localStorage.<span style="color: #660066;">topScore</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				localStorage.<span style="color: #660066;">topScore</span><span style="color: #339933;">=</span>score<span style="color: #339933;">;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#topscore&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Top score: &quot;</span><span style="color: #339933;">+</span>localStorage.<span style="color: #660066;">topScore</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Good! score: &quot;</span><span style="color: #339933;">+</span>score<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>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Bad! score: &quot;</span><span style="color: #339933;">+</span>score<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		currentNumber <span style="color: #339933;">=</span> newNumber<span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#number&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>currentNumber<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;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>At lines <strong>4-7</strong>, <strong>14-17</strong> and <strong>30-33</strong> we are using a variable called <code>topScore</code> saved into <code>localStorage</code> accessing it just like an ordinary variable.</p>
<p>And that&#8217;s enough for this step, no we have everything we need to create a complete game with high scores, which will be shown during next step. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/07/20/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store-step-2/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Controlling an iPhone mobile game with swipes using jQuery</title>
		<link>http://www.emanueleferonato.com/2011/07/06/controlling-an-iphone-mobile-game-with-swipes-using-jquery/</link>
		<comments>http://www.emanueleferonato.com/2011/07/06/controlling-an-iphone-mobile-game-with-swipes-using-jquery/#comments</comments>
		<pubDate>Wed, 06 Jul 2011 08:28:35 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[Html]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=4417</guid>
		<description><![CDATA[Learn how to control characters and content in your web applications using  <strong><a href="http://labs.skinkers.com/touchSwipe/" target="_blank">TouchSwipe</a></strong> jQuery plugin.]]></description>
			<content:encoded><![CDATA[<p>Now that I&#8217;m trying to <a href="http://www.emanueleferonato.com/2011/06/30/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store/">create an iPhone game using HTML + jQuery</a>, one of the most unusual things to develop in a web app is the swipe management.</p>
<p>Normally when you design web pages you don&#8217;t need to handle swipes because we expect the final user to interact with it with the mouse or the trackpad.</p>
<p>On mobile phones, people expect to use their fingers to interact with the content, and we have to meet their wishes if we want our app to be successful.</p>
<p>I am about to show you how easy it is: let&#8217;s start with the <a href="http://www.emanueleferonato.com/2010/06/14/creation-of-a-sokoban-game-with-jquery/">jQuery Sokoban prototype</a> I published about a year ago, and change keyboard input with swipe input.</p>
<p>Have a look at the code in the <a href="http://www.emanueleferonato.com/2011/06/30/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store/">original version</a>, then look at this one:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;
	&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot;&gt;
&lt;head&gt;
     &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no&quot;/&gt;
     &lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
     &lt;title&gt;jQuery Swipe Sokoban by Emanuele Feronato&lt;/title&gt;
     &lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
     &lt;script type=&quot;text/javascript&quot; src=&quot;touchswipe.js&quot;&gt;&lt;/script&gt; 
     <span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
     $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><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> map <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	map<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	map<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	map<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	map<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	map<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	map<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<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><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</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: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</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>map.<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>
     		<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>j<span style="color: #339933;">&lt;</span>map<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     			<span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>map<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     				<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">:</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class = &quot;wall&quot; style = &quot;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     				<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">:</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class = &quot;goal&quot; style = &quot;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     				<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">:</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id = &quot;c'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">+</span><span style="color: #3366CC;">'_'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot; class = &quot;crate&quot; style = &quot;z-index:1000;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     				<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">:</span>
     					map<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
     					<span style="color: #003366; font-weight: bold;">var</span> player_pos <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span>j<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id =&quot;player&quot; style = &quot;z-index:1000;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     				<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">:</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class = &quot;goal&quot; style = &quot;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id = &quot;c'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">+</span><span style="color: #3366CC;">'_'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot; class = &quot;crate&quot; style = &quot;z-index:1000;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     				<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">:</span>
     					map<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
     					<span style="color: #003366; font-weight: bold;">var</span> player_pos <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span>j<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class = &quot;goal&quot; style = &quot;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id =&quot;player&quot; style = &quot;z-index:1000;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     			<span style="color: #009900;">&#125;</span>
     		<span style="color: #009900;">&#125;</span>
     	<span style="color: #009900;">&#125;</span>
     	<span style="color: #003366; font-weight: bold;">function</span> possible_move<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     		<span style="color: #003366; font-weight: bold;">var</span> tile <span style="color: #339933;">=</span> map<span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>y<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>x<span style="color: #009900;">&#93;</span>
     		<span style="color: #003366; font-weight: bold;">var</span> far_tile <span style="color: #339933;">=</span> map<span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>y<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>x<span style="color: #009900;">&#93;</span>
     		<span style="color: #003366; font-weight: bold;">var</span> can_move <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
     		<span style="color: #003366; font-weight: bold;">var</span> solved <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
     		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>tile<span style="color: #339933;">==</span><span style="color: #CC0000;">0</span> <span style="color: #339933;">||</span> tile<span style="color: #339933;">==</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    can_move <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                    solved <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</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>
                    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>tile<span style="color: #339933;">==</span><span style="color: #CC0000;">3</span> <span style="color: #339933;">||</span> tile<span style="color: #339933;">==</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>far_tile<span style="color: #339933;">==</span><span style="color: #CC0000;">0</span> <span style="color: #339933;">||</span> far_tile<span style="color: #339933;">==</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                         map<span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>y<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>x<span style="color: #009900;">&#93;</span><span style="color: #339933;">-=</span><span style="color: #CC0000;">3</span><span style="color: #339933;">;</span>
                         map<span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>y<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>x<span style="color: #009900;">&#93;</span><span style="color: #339933;">+=</span><span style="color: #CC0000;">3</span><span style="color: #339933;">;</span>
                         $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#c&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;_&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>x<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                              left<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;+=&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                              top<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;+=&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>y<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #009900;">&#41;</span>
                         <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">100</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;">for</span><span style="color: #009900;">&#40;</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>map.<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>
                                   <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>j<span style="color: #339933;">&lt;</span>map<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                                        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>map<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                                             solved <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;">break</span><span style="color: #339933;">;</span>
                                        <span style="color: #009900;">&#125;</span>
                                   <span style="color: #009900;">&#125;</span>
                              <span style="color: #009900;">&#125;</span>
                              <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>solved<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                                   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;SOLVED&quot;</span><span style="color: #009900;">&#41;</span>
                              <span style="color: #009900;">&#125;</span>
                         <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;_&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>x<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                         can_move <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: #009900;">&#125;</span>	
               <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>can_move<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+=</span>y<span style="color: #339933;">;</span>
                    player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+=</span>x<span style="color: #339933;">;</span>
     			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#player&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
     				left<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;+=&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                         top<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;+=&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>y<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #009900;">&#41;</span>
     			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
               <span style="color: #009900;">&#125;</span>
     	<span style="color: #009900;">&#125;</span>
        	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#player&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">swipe</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>swipe<span style="color: #339933;">:</span>swipe<span style="color: #339933;">,</span>threshold<span style="color: #339933;">:</span><span style="color: #CC0000;">50</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        	<span style="color: #003366; font-weight: bold;">function</span> swipe<span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span>direction<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>direction<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     			<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">&quot;left&quot;</span><span style="color: #339933;">:</span>
     				possible_move<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
     				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     			<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">&quot;up&quot;</span><span style="color: #339933;">:</span>
     				possible_move<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
     				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     			<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">&quot;right&quot;</span><span style="color: #339933;">:</span>
     				possible_move<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
     				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     			<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">&quot;down&quot;</span><span style="color: #339933;">:</span>
     				possible_move<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
     				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     		<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
     &lt;style type=&quot;text/css&quot;&gt;
     .wall{
     	background-color:#000000;
     	width:32px;
     	height:32px;
     	position:absolute;
     }
&nbsp;
     .goal{
     	background-color:#ff0000;
     	width:32px;
     	height:32px;
     	position:absolute;	
     }
&nbsp;
     .crate{
     	border:10px solid #00ff00;
     	width:12px;
     	height:12px;
     	position:absolute;	
     }
&nbsp;
     #player{
     	border:10px solid #0000ff;
     	width:12px;
     	height:12px;
     	position:absolute;	
     }
     &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
     &lt;div id = &quot;sokoban&quot;&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>At <strong>line 5</strong> we have the <code>viewport</code> meta declaration as explained in <a href="http://www.emanueleferonato.com/2011/06/30/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store/">this post</a> but the core lies at <strong>line 9</strong> when I call <strong><a href="http://labs.skinkers.com/touchSwipe/" target="_blank">TouchSwipe</a></strong>, a jQuery plugin for touch devices.</p>
<p>Everything is managed by this fantastic plugin, since among its features you can find:</p>
<p>* Detects swipes in 4 directions, &#8220;up&#8221;, &#8220;down&#8221;, &#8220;left&#8221; and &#8220;right&#8221;<br />
* Supports single finger or double finger touch events<br />
* Supports click events both on the touchSwipe object and its child objects<br />
* Definable threshold to determin when a gesture is actually a swipe<br />
* Events triggered for swipe &#8220;start&#8221;,&#8221;move&#8221;,&#8221;end&#8221; and &#8220;cancel&#8221;<br />
* End event can be triggered either on touch release, or as soon as threshold is met<br />
* Allows swiping and page scrolling</p>
<p>So WASD key interaction you can find in the <a href="http://www.emanueleferonato.com/2010/06/14/creation-of-a-sokoban-game-with-jquery/">original version</a> has been replaced by swipe interaction at <strong>lines 90-106</strong>, where every swipe longer than 50 pixels is translated into a movement.</p>
<p>To test it, point your mobile browser to <a href="http://www.emanueleferonato.com/stuff/touchban/" target="_blank">this page</a>, put your finger on the hero (the blue square) and move it with a swipe.</p>
<p>That wasy? Yes, that easy. The App store is getting closer and closer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/07/06/controlling-an-iphone-mobile-game-with-swipes-using-jquery/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Making a game with HTML + jQuery Mobile and publishing it to Apple App Store</title>
		<link>http://www.emanueleferonato.com/2011/06/30/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store/</link>
		<comments>http://www.emanueleferonato.com/2011/06/30/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store/#comments</comments>
		<pubDate>Thu, 30 Jun 2011 08:39:55 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[Html]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=4379</guid>
		<description><![CDATA[The first attempt to create an iPhone game starting from a web page.]]></description>
			<content:encoded><![CDATA[<p>During these days I am playing with HTML to build an iPhone web app and publish it on the <a href="http://www.apple.com/iphone/apps-for-iphone/" target = "_blank">Apple App Store</a>.</p>
<p>It will be a long journey, so let&#8217;s start with something simple: a jQuery version of <a href="http://www.emanueleferonato.com/2009/03/19/magormin-an-old-php-based-game/">MagOrMin &#8211; an old php based game</a>, whose concept have been applied for the creation of <a href="http://www.emanueleferonato.com/2007/12/30/guessnext-complete-flash-game-with-highscores/">GuessNext</a> Flash game.</p>
<p>The first step in making an HTML app for iPhone is making an HTML page.</p>
<p>And this is main, the most basic possible:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&nbsp;
&lt;head&gt;
&nbsp;
	&lt;script src = &quot;jquery.js&quot;&gt;&lt;/script&gt;
	&lt;script src = &quot;game.js&quot;&gt;&lt;/script&gt;
&nbsp;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&nbsp;
&lt;div id = &quot;number&quot;&gt;&lt;/div&gt;
&lt;div id = &quot;higher&quot;&gt;higher&lt;/div&gt;
&lt;div id = &quot;lower&quot;&gt;lower&lt;/div&gt;
&lt;div id = &quot;comment&quot;&gt;&lt;/div&gt;
&nbsp;
&lt;/body&gt;	
&nbsp;
&lt;/html&gt;</pre></td></tr></table></div>

<p>Where jquery.js contains the latest <a href="http://docs.jquery.com/Downloading_jQuery" target = "_blank">jQuery distribution</a> and game.js is this script:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><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> currentNumber <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> score <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#number&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>currentNumber<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Click higher or lower&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#higher&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><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> newNumber <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>newNumber <span style="color: #339933;">&gt;=</span> currentNumber<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			score<span style="color: #339933;">++;</span>	
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Good! score: &quot;</span><span style="color: #339933;">+</span>score<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>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Bad! score: &quot;</span><span style="color: #339933;">+</span>score<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		currentNumber <span style="color: #339933;">=</span> newNumber<span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#number&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>currentNumber<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: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#lower&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><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> newNumber <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>newNumber <span style="color: #339933;">&lt;=</span> currentNumber<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			score<span style="color: #339933;">++;</span>	
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Good! score: &quot;</span><span style="color: #339933;">+</span>score<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>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Bad! score: &quot;</span><span style="color: #339933;">+</span>score<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		currentNumber <span style="color: #339933;">=</span> newNumber<span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#number&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>currentNumber<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;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Explaining HTML and jQuery is beyond the scope of this post, just notice there are four divs:</p>
<p><strong>number</strong> contains a random integer number from 0 to 9</p>
<p><strong>higher</strong> is a clickable div and when the player clicks on it then he guesses next random number will be higher (or equal) than the current one</p>
<p><strong>lower</strong> follows the same concept as <code>higher</code> but the guess assumes next random number will be lower or equal than the current one</p>
<p><strong>comment</strong> contains in-game comments, such as &#8220;Good!&#8221;, &#8220;Bad!&#8221; and the current score.</p>
<p>At this stage the game has no limit, no game over, no high scores, nothing. But it&#8217;s a good start.</p>
<p>This is what you get if you upload the scripts on a page and browse it with your iPhone:</p>
<p><img src="/wp-content/uploads/2011/06/iphone01.jpg" /></p>
<p>You can even play it, but the text is so tiny!</p>
<p>This happens because unless you tell it otherwise, Safari on the iPhone is going to assume that your page width is 980px.</p>
<p>So we are going to add this line between <code>&lt;head&gt;</code> and <code>&lt;\head&gt;</code> to specify the viewport:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;meta name=&quot;viewport&quot; content=&quot;user-scalable=no, width=device-width&quot; /&gt;</pre></div></div>

<p>This is how the same page looks with the correct <code>viewport</code> meta data:</p>
<p><img src="/wp-content/uploads/2011/06/iphone02.jpg" /></p>
<p>Much better now&#8230; and more playable since the &#8220;buttons&#8221; are bigger.</p>
<p>But it&#8217;s obvious we won&#8217;t go so far without a library designed with mobile devices in mind.</p>
<p>This is when <a href="http://jquerymobile.com/" target = "_blank">jQuery Mobile</a> comes into play. It&#8217;s an unified user interface system across all popular mobile device platforms, built on the rock-solid jQuery and jQuery UI foundation, and that&#8217;s what we need.</p>
<p>Now it&#8217;s time to heavily change our html:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html&gt; 
&nbsp;
&lt;html&gt;
&nbsp;
&lt;head&gt;
&nbsp;
	&lt;meta name=&quot;viewport&quot; content=&quot;user-scalable=no, width=device-width&quot; /&gt;
&nbsp;
	&lt;link rel=&quot;stylesheet&quot; href=&quot;jquery.mobile-1.0b1.min.css&quot; /&gt;
&nbsp;
	&lt;script src = &quot;jquery.js&quot;&gt;&lt;/script&gt;
	&lt;script src=&quot;jquery.mobile-1.0b1.min.js&quot;&gt;&lt;/script&gt;
	&lt;script src = &quot;game.js&quot;&gt;&lt;/script&gt;
&nbsp;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&nbsp;
&lt;div data-role=&quot;page&quot;&gt;
&nbsp;
	&lt;div data-role=&quot;header&quot;&gt;
		&lt;h1&gt;Page Title&lt;/h1&gt;
	&lt;/div&gt;
&nbsp;
	&lt;div data-role=&quot;content&quot;&gt;	
		&lt;div id = &quot;number&quot;&gt;&lt;/div&gt;
		&lt;div id = &quot;higher&quot;&gt;higher&lt;/div&gt;
		&lt;div id = &quot;lower&quot;&gt;lower&lt;/div&gt;
		&lt;div id = &quot;comment&quot;&gt;&lt;/div&gt;		
	&lt;/div&gt;
&nbsp;
	&lt;div data-role=&quot;footer&quot;&gt;
		&lt;h4&gt;Page Footer&lt;/h4&gt;
	&lt;/div&gt;
&lt;/div&gt;
&nbsp;
&lt;/body&gt;
&nbsp;
&lt;/html&gt;</pre></td></tr></table></div>

<p><strong>Line 9</strong>: imports jQuery mobile stylesheet.</p>
<p><strong>Line12</strong>: imports jQuery Mobile library. Both the library and the stylesheet can be downloaded from <a href="http://jquerymobile.com/download/" target = "_blank">this page</a></p>
<p><strong>Line 19</strong>: we are meeting our first <code>data-role</code> attribute. This will tell jQuery Mobile the role of the div in the mobile page. With <code>data-role="page"</code> we tell jQM it&#8217;s the beginning of a mobile page.</p>
<p><strong>Line 21</strong>: <code>data-role="header"</code> tells us we are working wit the header of the page.</p>
<p><strong>Line 25</strong>: <code>data-role="content"</code> tells us we are working wit the content of the page.</p>
<p><strong>Line 32</strong>: <code>data-role="footer"</code> tells us we are working wit the footer of the page.</p>
<p>Once the browser is pointed on the page, jQM automatically triggers all HTML5 <code>data-role</code> attributes to render the page with all necessary widgets. That&#8217;s what we have now:</p>
<p><img src="/wp-content/uploads/2011/06/iphone03.jpg" /></p>
<p>This is not that good yet, as the footer isn&#8217;t at the bottom of the page but just when the page itself ends. To have the footer (and the header) fixed on the bottom (and on the top) of the page, change <strong>lines 21</strong> and <strong>32</strong> respectively with:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>21
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div data-role=&quot;header&quot; data-position=&quot;fixed&quot;&gt;</pre></td></tr></table></div>

<p>and</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>32
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div data-role=&quot;footer&quot; data-position=&quot;fixed&quot;&gt;</pre></td></tr></table></div>

<p>and now your header and footer will be fixed at the very top and at the very bottom of the page, this way:</p>
<p><img src="/wp-content/uploads/2011/06/iphone04.jpg" /></p>
<p>Anyway, we are greedy and we want our page to go in full screen mode, without showing the address bar at the top and the toolbar at the bottom.</p>
<p>This will be made in two steps: first, we have to insert this line between <code>&lt;head&gt;</code> and <code>&lt;\head&gt;</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;meta name=&quot;apple-mobile-web-app-capable&quot; content=&quot;yes&quot; /&gt;</pre></div></div>

<p>Then, we have to launch the web page from Home, that is the iPhone &#8220;desktop&#8221;. Add the page to home: a custom icon will be generated with the thumbnail of the page:</p>
<p><img src="/wp-content/uploads/2011/06/iphone06.jpg" /></p>
<p>and once you call it directly from the icon you will get your full page navigation.</p>
<p><img src="/wp-content/uploads/2011/06/iphone07.jpg" /></p>
<p>The last thing we will see in this first part is how to create two mobile friendly buttons to control &#8220;higher&#8221; and &#8220;lower&#8221; guesses.</p>
<p>Change your content block this way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>25
26
27
28
29
30
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div data-role=&quot;content&quot;&gt;	
	&lt;div id = &quot;number&quot;&gt;&lt;/div&gt;
	&lt;a href=&quot;#&quot; data-role=&quot;button&quot; id = &quot;higher&quot;&gt;Higher&lt;/a&gt;
	&lt;a href=&quot;#&quot; data-role=&quot;button&quot; id = &quot;lower&quot;&gt;Lower&lt;/a&gt;
	&lt;div id = &quot;comment&quot;&gt;&lt;/div&gt;		
&lt;/div&gt;</pre></td></tr></table></div>

<p>Button divs have been changed to anchor texts with <code>data-role="button"</code>. This will make jQuery Mobile render the links as buttons.</p>
<p>This is how the page looks now:</p>
<p><img src="/wp-content/uploads/2011/06/iphone05.jpg" /></p>
<p>But to interact with them we have to change a bit the javascript changing<strong> lines 6</strong> and <strong>18</strong> from respectively</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#higher&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><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></pre></td></tr></table></div>

<p>and</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>18
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#lower&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><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></pre></td></tr></table></div>

<p>to respectively</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#higher&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click tap&quot;</span><span style="color: #339933;">,</span><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></pre></td></tr></table></div>

<p>and</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>18
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#lower&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click tap&quot;</span><span style="color: #339933;">,</span><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></pre></td></tr></table></div>

<p>And finally you will be able to play again using the new buttons.</p>
<p>This is where this tutorial ends, but there&#8217;s still a lot to do. During next step we&#8217;ll build a preloader, save high scores and enhance the visual appeal of the game.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/06/30/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced
Database Caching 10/18 queries in 0.079 seconds using disk: basic

Served from: www.emanueleferonato.com @ 2012-02-10 04:51:18 -->
