tag:blogger.com,1999:blog-77691409746614489942024-03-18T21:41:35.919-07:00Stefans Dev PlaygroundBlog about developing small games for iOS devicesStefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.comBlogger40125tag:blogger.com,1999:blog-7769140974661448994.post-17427120749880734532016-04-09T04:12:00.001-07:002016-05-13T14:11:25.120-07:00Apple Watch as Gaming Platform - Use the Digital Crown to control sprite movement for a Pong like game<script>
window.location="http://developerplayground.net/applewatch-digitalcrown-control-sprite/";
</script>
<h2>
Use the Digital Crown to control sprite movement</h2>
<div>
<div style="text-align: justify;">
Welcome to my new WatchKit tutorial. Today I'll show that it is possible to use the Apple Watch as a gaming platform. First let's look back to the first WatchKit versions. They have been very limited in terms of possible input controls. A typical game was usually built with a number of <span style="font-family: "courier new" , "courier" , monospace;">WkInterfaceButtons</span> and looked like this:</div>
<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQujQKI4CsPWZjtXSin1iDQWD8leRVQNpKjviJ5Qq8lN9YDDu44PHxBsimdU9PjRZK_yezpfdNJsCg0EC_1Cs1GKSlKzdd_MkfwGbvTTFqhhG-8EyvrYidAHQ32lRHeTBBqy40rwNpbiw/s1600/Simulator+Screen+Shot+23+Apr+2016+09.40.38.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="15 Puzzle AppleWatch" border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQujQKI4CsPWZjtXSin1iDQWD8leRVQNpKjviJ5Qq8lN9YDDu44PHxBsimdU9PjRZK_yezpfdNJsCg0EC_1Cs1GKSlKzdd_MkfwGbvTTFqhhG-8EyvrYidAHQ32lRHeTBBqy40rwNpbiw/s200/Simulator+Screen+Shot+23+Apr+2016+09.40.38.png" title="15 Puzzle AppleWatch" width="160" /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfCcSXEYfDIDlopEjFuSsKuAVwk3BsyghjIABn11amJgFh9qCRUNZGIwTbQyIv8DgyNyadXyXXPsTragwn9Pmp-Agy2W73mk5Eab9pRsamvgVyLGOksLxY4j4HwSB0F1gbBXu78JrUG9g/s1600/Simulator+Screen+Shot+23+Apr+2016+09.40.29.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="15 Puzzle AppleWatch" border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfCcSXEYfDIDlopEjFuSsKuAVwk3BsyghjIABn11amJgFh9qCRUNZGIwTbQyIv8DgyNyadXyXXPsTragwn9Pmp-Agy2W73mk5Eab9pRsamvgVyLGOksLxY4j4HwSB0F1gbBXu78JrUG9g/s200/Simulator+Screen+Shot+23+Apr+2016+09.40.29.png" title="15 Puzzle AppleWatch" width="160" /></a></div>
<br />
<br />
<br />
<br /></div>
Since WatchKit 2 it is possible to use the <span style="font-family: "courier new" , "courier" , monospace;">Digital Crown</span> for custom apps. This provides way more possibilities for great games, like this <a href="https://itunes.apple.com/app/watch-ping-pong/id1039082864?l=en&mt=8">Watch Ping Pong</a> version created by me.<br />
<span style="text-align: justify;">I'll publish the Source Code as OpenSource in my GitHub repository, once I have reached 1000 downloads. Currently only 986 to go ;-)</span><br />
<span style="text-align: justify;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://itunes.apple.com/app/watch-ping-pong/id1039082864?l=en&mt=8"><img alt="Pong AppleWatch" border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOk2e6l4shRGXPUejBytKZquWdUGHtcszMaE1OlCLoxo8K8hlJl6FJq9Yqim_YkVJ7AINUfbyvP7wuJZ32K05C9YZdgYIWV38ity9O9nOeYjGF8jKMkiRbtxOgo3dWCiaUBREBy1RN2Cs/s200/WatchPingPongGif.gif" title="Pong AppleWatch" width="160" /></a></div>
<br />
<br />
<a name='more'></a><br /><br />
<span style="text-align: justify;">I have a quick calculation for you, i</span><span style="text-align: justify;">f you think 0.99$ is expensive:</span><br />
<span style="text-align: justify;">It took me around 30 hours to write the game. </span><span style="text-align: justify;">14 downloads generate around 13,86$ earnings. </span><span style="text-align: justify;">Apple takes one third means</span><span style="text-align: justify;"> 9,70$ remaining for me. </span><br />
<span style="text-align: justify;"><b>=> Whoa! I've earned 0,32$ per hour ;-)</b></span><br />
<span style="text-align: justify;"><br /></span>
<span style="text-align: justify;">Don't get me wrong. I'm not expecting to get rich with my Apps. I'm writing this Blog and and my games only for fun.</span><br />
<span style="text-align: justify;"><br /></span>
<br />
<h3>
<span style="text-align: justify;"><b>But now, let's start with the tutorial: </b></span></h3>
<span style="text-align: justify;">Goal is to create the racket at the left side and use the digital crown to move it up and down.</span><br />
<span style="text-align: justify;"><br /></span>
<span style="text-align: justify;"><br /></span>
<br />
<h4>
<span style="text-align: justify;">1. </span>Open XCode and create a new WatchKit project:</h4>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjN79NK8UTULE90dRKQyObfztJfMF-Ja-yx5ZWPA1X1H7qaoT-sAuxN4DRNsLJPfHTNZXbyyFwrFvqs0miFlfzSJHAhKNOm51ZODlI9IKnn9TjesHDsnMKIkpddg3EIZe3dJEL_bKBB5cQ/s1600/WatchGame1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjN79NK8UTULE90dRKQyObfztJfMF-Ja-yx5ZWPA1X1H7qaoT-sAuxN4DRNsLJPfHTNZXbyyFwrFvqs0miFlfzSJHAhKNOm51ZODlI9IKnn9TjesHDsnMKIkpddg3EIZe3dJEL_bKBB5cQ/s320/WatchGame1.png" width="320" /></a></div>
<div>
<br /></div>
Disable the checkmark on 'Include Notification Scene':<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHe2aYYjRdlmsWk2wkM85wt7adNswJSOzULX1XTOYUoe1wocGFT9B4nBYsM5O4ECtCXby_Ygafh6PNsUgGX3bS_ELPlYb_GBuT8X2w9RRiCeMp5BgKwlkYRlo8IL62vMUn0RAyC-2E0U8/s1600/WatchGameBlog2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="226" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHe2aYYjRdlmsWk2wkM85wt7adNswJSOzULX1XTOYUoe1wocGFT9B4nBYsM5O4ECtCXby_Ygafh6PNsUgGX3bS_ELPlYb_GBuT8X2w9RRiCeMp5BgKwlkYRlo8IL62vMUn0RAyC-2E0U8/s320/WatchGameBlog2.png" width="320" /></a></div>
<br />
<br />
<h4>
2. Create the UserInterface:</h4>
Open the WatchKit Storyboard:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5EVteI8sdQcu-NKoEFqYP4gM-UVSAmBkk_OOxUxYvSdqcG4_l8EWrWhKZCaL-7WyMQt8RLpNmeJlYP6ZVqq9FabDuIzMq4S7ENqTYPrQjamWk1g2YTRoHc2z4-HWXfLksb1EmnZQcE6Y/s1600/Screen+Shot+2016-04-09+at+14.00.44.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="144" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5EVteI8sdQcu-NKoEFqYP4gM-UVSAmBkk_OOxUxYvSdqcG4_l8EWrWhKZCaL-7WyMQt8RLpNmeJlYP6ZVqq9FabDuIzMq4S7ENqTYPrQjamWk1g2YTRoHc2z4-HWXfLksb1EmnZQcE6Y/s320/Screen+Shot+2016-04-09+at+14.00.44.png" width="320" /></a></div>
<br />
<br />
Add a <span style="font-family: "courier new" , "courier" , monospace;">WKInterfaceGroup</span> and below a <span style="font-family: "courier new" , "courier" , monospace;">WKInterfaceImage</span> and a <span style="font-family: "courier new" , "courier" , monospace;">WKInterfacePicker</span> to the <span style="font-family: "courier new" , "courier" , monospace;">InterfaceController</span>:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmjO4EiqsAglcP5ICZ9u0AlfQdtN_cZx5_hlzpsYYXDNPnbLmSfUS0zb54wb5pRpLjau6d5-A4-g_Ff2qKlPm-3zeX90TNp8phiEeK517WR1plPyx86RwgZLKNQ0hkv2oRsbYkttqv-7I/s1600/Screen+Shot+2016-04-09+at+14.00.17.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="149" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmjO4EiqsAglcP5ICZ9u0AlfQdtN_cZx5_hlzpsYYXDNPnbLmSfUS0zb54wb5pRpLjau6d5-A4-g_Ff2qKlPm-3zeX90TNp8phiEeK517WR1plPyx86RwgZLKNQ0hkv2oRsbYkttqv-7I/s320/Screen+Shot+2016-04-09+at+14.00.17.png" width="320" /></a></div>
<br />
<br />
Change the size of the group and the Image to fullscreen:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5hifDhW80VmUIx_fupOCec7C6U5BbzrIjz4S1z-UHJp-SNQl8E50SRujoE0Td-jZFuJXzft8xxaP8qODf_GE0ijZDdrKbLrIdV5UX4psEgsQP2UrYrVNxTFOIHtNKR3nZlyfsL-cTSC4/s1600/WatchGameBlog5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="119" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5hifDhW80VmUIx_fupOCec7C6U5BbzrIjz4S1z-UHJp-SNQl8E50SRujoE0Td-jZFuJXzft8xxaP8qODf_GE0ijZDdrKbLrIdV5UX4psEgsQP2UrYrVNxTFOIHtNKR3nZlyfsL-cTSC4/s320/WatchGameBlog5.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcYB06if9eVFqvsNYAdquTYjfVugQzX1mm-GndqJH0J6pucZtR4_I4uXlT0ytw5O5iNFqFOplS4S7piGqNTcJLGqwi7Lxq6HXWfGHAyZM3Ywztv1IhvEWeLHRCdQigzrxDXOZyevrr3B4/s1600/WatchGameBlog6.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="99" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcYB06if9eVFqvsNYAdquTYjfVugQzX1mm-GndqJH0J6pucZtR4_I4uXlT0ytw5O5iNFqFOplS4S7piGqNTcJLGqwi7Lxq6HXWfGHAyZM3Ywztv1IhvEWeLHRCdQigzrxDXOZyevrr3B4/s320/WatchGameBlog6.png" width="320" /></a></div>
<br />
<div>
Change the radius of the group to 0:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAIy84UC0z-MFl-6YQPOmzMDd0LE09vLNWvXZNP1TMdga8oXwhDNA0AY9D4biQykMjmiLNFAO2b_XRwcP-odgazxorymZJeJoYXbWbMyva4n8aJH3qIhE7C0q4h_D0M4IAfL-EC5xRnj8/s1600/WatchGameBlog7.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="98" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAIy84UC0z-MFl-6YQPOmzMDd0LE09vLNWvXZNP1TMdga8oXwhDNA0AY9D4biQykMjmiLNFAO2b_XRwcP-odgazxorymZJeJoYXbWbMyva4n8aJH3qIhE7C0q4h_D0M4IAfL-EC5xRnj8/s320/WatchGameBlog7.png" width="320" /></a></div>
<br />
<br />
The <span style="font-family: "courier new" , "courier" , monospace;">WKInterfacePicker</span> will only be used to connect our interface with the digital crown. It is not necessary that it is visible on the screen.<br />
<br />
<h3>
3. Connect the UI Classes with the InterfaceController:</h3>
Create an <span style="font-family: "courier new" , "courier" , monospace;">IBOutlet</span> for 'Image' and 'Picker':<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhi7b-7f1dr7L2RMFhpj0fDWr5dhqcSnAkjFyr0q9KTNou5a-HtiCDyKSHMNQX7TNBYqdUamoPDro2CIzv2o7hyqRGxNnrE8BsYWvA3JTmPuw32TJOocYIttLnej4yKzh3iJyxaWuEvv4E/s1600/WatchGameBlog8.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="166" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhi7b-7f1dr7L2RMFhpj0fDWr5dhqcSnAkjFyr0q9KTNou5a-HtiCDyKSHMNQX7TNBYqdUamoPDro2CIzv2o7hyqRGxNnrE8BsYWvA3JTmPuw32TJOocYIttLnej4yKzh3iJyxaWuEvv4E/s320/WatchGameBlog8.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaaMJyw6soagqg3edqBkYAfhWhiPonc1wrUh0CQmrlt2KZ0t1yzPBajeSIoHSyA32muJL8AhTFQCJqQcmWS68CcvKvuQEKaxjixrolmebzWssiqSs3vfBzlbgJVtexc2qjs7n1CW3AYEA/s1600/WatchGameBlog9.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="148" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaaMJyw6soagqg3edqBkYAfhWhiPonc1wrUh0CQmrlt2KZ0t1yzPBajeSIoHSyA32muJL8AhTFQCJqQcmWS68CcvKvuQEKaxjixrolmebzWssiqSs3vfBzlbgJVtexc2qjs7n1CW3AYEA/s320/WatchGameBlog9.png" width="320" /></a></div>
<br />
<br />
<br />
Create an <span style="font-family: "courier new" , "courier" , monospace;">IBAction</span> for the 'Picker':<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjjMjLE2lzeqGtd0MyPvH7xh284EeVs3tMKDFsw1yiLF-r18xkCIbgCavt4MqwKpf23JFguC3xik4KfGWjQD5cSmanhfPfQSVG2ng0-MTHYEIwr4kl5_2mIsEQ7JIwojgUAWUabUV37_s/s1600/WatchGameBlog10.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="163" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjjMjLE2lzeqGtd0MyPvH7xh284EeVs3tMKDFsw1yiLF-r18xkCIbgCavt4MqwKpf23JFguC3xik4KfGWjQD5cSmanhfPfQSVG2ng0-MTHYEIwr4kl5_2mIsEQ7JIwojgUAWUabUV37_s/s320/WatchGameBlog10.png" width="320" /></a></div>
<br />
<br />
<br />
The result in your code should look like this:<br />
<br />
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">@IBOutlet</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> picker: </span><span style="font-variant-ligatures: no-common-ligatures;">WKInterfacePicker</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">!</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">@IBOutlet</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> image: </span><span style="font-variant-ligatures: no-common-ligatures;">WKInterfaceImage</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">!</span></div>
<br />
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">@IBAction</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span><span style="font-variant-ligatures: no-common-ligatures;"> pick(value: </span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Int</span><span style="font-variant-ligatures: no-common-ligatures;">) {}</span></div>
<br /></div>
<br />
<h3>
4. Implement the logic:</h3>
<div style="text-align: justify;">
Now everything is setup and we can start implementing the logic. The <span style="font-family: "courier new" , "courier" , monospace;">picker</span> outlet/action will provide the access to the digital crown. It must be initialized with some values first. Add this code snippet to the method <span style="font-family: "courier new" , "courier" , monospace;">willActivate()</span>: </div>
<br />
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span><span style="font-variant-ligatures: no-common-ligatures;"> willActivate() {</span></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">// This method is called when watch view controller is about to be visible to user</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">super</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">willActivate</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="line-height: normal; min-height: 13px;">
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> // Initialize picker with 10 items to detect state changes of the digital crown</span></div>
</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span><span style="font-variant-ligatures: no-common-ligatures;"> pickerItems: [</span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">WKPickerItem</span><span style="font-variant-ligatures: no-common-ligatures;">] = []</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">for</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">_</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span><span style="font-variant-ligatures: no-common-ligatures;"> 0 ..< </span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">10</span><span style="font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> pickerItem = </span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">WKPickerItem</span><span style="font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> pickerItem.</span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">title</span><span style="font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">""</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> pickerItem.</span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">caption</span><span style="font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">""</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> pickerItems.</span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">append</span><span style="font-variant-ligatures: no-common-ligatures;">(pickerItem)</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">picker</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">setItems</span><span style="font-variant-ligatures: no-common-ligatures;">(pickerItems)</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">picker</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">setSelectedItemIndex</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">(4)</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"><br /></span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<br /></div>
<span style="text-align: justify;">Move the racket overtime the picker changes:</span><br />
<span style="text-align: justify;"><br /></span>
<br />
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">@IBAction</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span><span style="font-variant-ligatures: no-common-ligatures;"> pick(value: </span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Int</span><span style="font-variant-ligatures: no-common-ligatures;">) {</span></div>
<div style="font-family: Menlo; line-height: normal; min-height: 13px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="color: #008f00; font-family: Menlo; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">// Create the graphics context</span></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> size = </span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGSizeMake</span><span style="font-variant-ligatures: no-common-ligatures;">(100, 100)</span></div>
<div style="color: #3495af; font-family: Menlo; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">UIGraphicsBeginImageContext</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">(size)</span></div>
<div style="color: #3495af; font-family: Menlo; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> context = </span><span style="font-variant-ligatures: no-common-ligatures;">UIGraphicsGetCurrentContext</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; line-height: normal; min-height: 13px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="color: #008f00; font-family: Menlo; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">// Setup for the path style</span></div>
<div style="color: #3495af; font-family: Menlo; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">CGContextSetStrokeColorWithColor</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">(context, </span><span style="font-variant-ligatures: no-common-ligatures;">UIColor</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures;">whiteColor</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">().</span><span style="font-variant-ligatures: no-common-ligatures;">CGColor</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGContextSetLineWidth</span><span style="font-variant-ligatures: no-common-ligatures;">(context, 2.0)</span></div>
<div style="font-family: Menlo; line-height: normal; min-height: 13px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="color: #008f00; font-family: Menlo; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">// Draw racket</span></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGContextBeginPath</span><span style="font-variant-ligatures: no-common-ligatures;"> (context);</span></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGContextMoveToPoint</span><span style="font-variant-ligatures: no-common-ligatures;">(context, 80, </span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span><span style="font-variant-ligatures: no-common-ligatures;">(value + 1) * 10);</span></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGContextAddLineToPoint</span><span style="font-variant-ligatures: no-common-ligatures;">(context, 80, </span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span><span style="font-variant-ligatures: no-common-ligatures;">(value) * 10)</span></div>
<div style="color: #3495af; font-family: Menlo; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">CGContextStrokePath</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">(context);</span></div>
<div style="font-family: Menlo; line-height: normal; min-height: 13px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="color: #008f00; font-family: Menlo; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">// Convert to an UIImage</span></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> cgimage = </span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGBitmapContextCreateImage</span><span style="font-variant-ligatures: no-common-ligatures;">(context);</span></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> uiimage = </span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIImage</span><span style="font-variant-ligatures: no-common-ligatures;">(CGImage: cgimage!)</span></div>
<div style="font-family: Menlo; line-height: normal; min-height: 13px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="color: #008f00; font-family: Menlo; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">// End the graphics context</span></div>
<div style="color: #3495af; font-family: Menlo; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">UIGraphicsEndImageContext</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; line-height: normal; min-height: 13px;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="color: #008f00; font-family: Menlo; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">// Assign on WKInterfaceImage</span></div>
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">image</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">setImage</span><span style="font-variant-ligatures: no-common-ligatures;">(uiimage)</span></div>
<br />
<div style="font-family: Menlo; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> }</span></div>
</div>
<div>
<span style="font-variant-ligatures: no-common-ligatures;"><br /></span></div>
<br />
Now you can run the App and play with the crown and the racket. The result should look like this:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiU-xWDCyHHZcNvia1KfsbCJ0B7RBZOeFuFLxvlF205Bc06D2gXFN41fD8lsyq8yWPuwPJyxk72f1Sg4WjTxMqSpv-vTBaD9Nl4B6gRObV3_fpdMR0OyxHmVk18cirMzXtzcxPqmEVpD5A/s1600/WatchGameDemoGif.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiU-xWDCyHHZcNvia1KfsbCJ0B7RBZOeFuFLxvlF205Bc06D2gXFN41fD8lsyq8yWPuwPJyxk72f1Sg4WjTxMqSpv-vTBaD9Nl4B6gRObV3_fpdMR0OyxHmVk18cirMzXtzcxPqmEVpD5A/s320/WatchGameDemoGif.gif" width="254" /></a></div>
<br />
<br />
<br />
That's all for today.<br />
<br />
<div style="text-align: justify;">
If you want to support me, please download my Apps from the Apple AppStore. </div>
<br />
<a href="https://itunes.apple.com/developer/stefan-josten/id949662361" style="font-family: '"helvetica neue light"'; text-align: center;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a><br />
<br />
<div>
Cheers,<br />
Stefan</div>
<div style="text-align: center;">
<br /></div>
<br />
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com0tag:blogger.com,1999:blog-7769140974661448994.post-6410193501473537022015-11-20T08:54:00.003-08:002016-05-13T14:11:54.206-07:00Quick Tip: Endless scrolling with SpriteKit and SWIFT (Part 2 of 2)<script>
window.location="http://developerplayground.net/quick-tip-endless-scrolling-spritekit-swift-part-2-4/";
</script>
<div>
<h2>
Natural endless scrolling with easing</h2>
<div>
<div>
Welcome to my tutorial series about scrolling:</div>
<div>
<ul>
<li><a href="http://stefansdevplayground.blogspot.de/2015/11/quicktip-endless-scrolling-with.html">Part 1</a>: Endless scrolling with background tiles</li>
<li><a href="http://stefansdevplayground.blogspot.de/2015/11/quick-tip-endless-scrolling-with-easing.html">Part 2</a>: Natural endless scrolling with easing</li>
</ul>
</div>
</div>
<div>
<br /></div>
<br />
In <a href="http://stefansdevplayground.blogspot.de/2015/11/quicktip-endless-scrolling-with.html">part one</a> I showed how to implement endless scrolling. This is working fine, but there is still room for improvements. The scrolling starts immediately with full speed and also stops directly after the touch ends. A more natural movement would be to increase and decrease the speed slowly, till the target speed is reached or the movement is stopped. These is also known as <a href="https://developers.google.com/web/fundamentals/design-and-ui/animations/the-basics-of-easing">ease in or ease out</a> animation.<br />
Thank you to hamobi who helped me with my <a href="http://stackoverflow.com/questions/33721183/ease-out-animation-for-skspritenode">Stackoverflow question</a> for finding the right solution.<br />
<br />
To give you an impression compare the two videos:<br />
<br />
<b>Without Easing:</b></div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen="1" frameborder="0" height="400" src="https://youtube.com//embed/uYVqG8Y8rLU" width="600"></iframe>
</div>
<br />
<br />
<b>With Easing:</b>
<br />
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen="1" frameborder="0" height="400" src="https://youtube.com//embed/k1m1JH7yYAw" width="600"></iframe>
</div>
<br />
<br />
<br />
<a name='more'></a><br />
<br />
<h3>
First repeat the steps from <a href="http://stefansdevplayground.blogspot.de/2015/11/quicktip-endless-scrolling-with.html">Part 1 </a></h3>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div>
The main difference is the calculation of the speed which is done in the scroll function:<br />
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
// Calculate the new speed with the easing function (New speed is influence by current speed)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff;">let</span> newSpeed = (<span style="color: #3495af;">currentSpeed</span> + (speed - <span style="color: #3495af;">currentSpeed</span>) * <span style="color: #3495af;">easeOutfactor</span>)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
</div>
<br />
<br /></div>
<h3>
Open GameScene.swift:</h3>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgse5Z-ZGn30jeqhRjDEaix6H9dPP3vdtpmRkd60vmTtC2hX1A36BrupPCotLL03vZXlCZA_a90olVy3QH6MdW8EKfjtc8y3sIZVqvYf6HX1waIAgCXIyArSlS5nCZZZxQ5Y6iBAUqOcy8/s1600/Screen+Shot+2015-11-13+at+18.14.43.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Tutorial: Endless scrolling with SpriteKit and SWIFT" border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgse5Z-ZGn30jeqhRjDEaix6H9dPP3vdtpmRkd60vmTtC2hX1A36BrupPCotLL03vZXlCZA_a90olVy3QH6MdW8EKfjtc8y3sIZVqvYf6HX1waIAgCXIyArSlS5nCZZZxQ5Y6iBAUqOcy8/s1600/Screen+Shot+2015-11-13+at+18.14.43.png" title="Endless scrolling with SpriteKit and SWIFT" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<h4>
Replace the complete code with this snippet: </h4>
(For explanation check the comments inside the code snippet)<br />
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
//</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
// GameScene.swift</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
// EndlessScrollingDemo</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
//</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
// Created by STEFAN JOSTEN on 13/11/15.</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
// Copyright (c) 2015 Stefan. All rights reserved.</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
//</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">import</span> SpriteKit</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">class</span> GameScene: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKScene</span> {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Some global variables to preserve the state and store the touch positions</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> lastUpdateTime: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CFTimeInterval</span>?</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> currentSpeed: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span> = 0.0</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> nodeTileWidth: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span> = 0.0</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> xTouchCurrentPosition: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span> = 0.0</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> xTouchStartPosition: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span> = 0.0</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> xTouchDistance: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span> = 0.0</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Some global constants to configure the speed</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> speedFactor:<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span> = 5.0</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> easeOutfactor: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span> = 0.04</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Declare the globaly needed sprite kit nodes</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> worldNode: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>?</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> spriteNode: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>?</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> didMoveToView(view: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKView</span>) {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Setup static background</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> backgroundNode = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>(imageNamed: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Background"</span>)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
backgroundNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGSize</span>(width: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">frame</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">width</span>, height: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">frame</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">height</span>)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
backgroundNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">anchorPoint</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: 0, y: 0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
backgroundNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">zPosition</span> = -10</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addChild</span>(backgroundNode)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Setup dynamic background tiles</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>worldNode<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>SKSpriteNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>addChild<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>worldNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">!)</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Create the dynamic background tiles. Image of left and right node must be identical</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> leftNode = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>(imageNamed: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"LeftTile"</span>)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> middleNode = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>(imageNamed: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"RightTile"</span>)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> rightNode = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>(imageNamed: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"LeftTile"</span>)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// store this value globaly to avoid recalculations during each update call</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>nodeTileWidth<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = leftNode.</span>frame<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>width</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
leftNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">anchorPoint</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: 0, y: 0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
leftNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: 0, y: 0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
middleNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">anchorPoint</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: 0, y: 0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
middleNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">nodeTileWidth</span>, y: 0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
rightNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">anchorPoint</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: 0, y: 0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
rightNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">nodeTileWidth</span> * 2, y: 0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Add the tiles to worldNode. worldNode is used to realize the scrolling</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">worldNode</span>!.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addChild</span>(leftNode)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">worldNode</span>!.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addChild</span>(rightNode)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">worldNode</span>!.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addChild</span>(middleNode)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Setup spaceship sprite</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">spriteNode</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>(imageNamed: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Spaceship"</span>)</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>spriteNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>CGPoint<span style="color: black; font-variant-ligatures: no-common-ligatures;">(x:</span>CGRectGetMidX<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>frame<span style="color: black; font-variant-ligatures: no-common-ligatures;">), y:</span>CGRectGetMidY<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>frame<span style="color: black; font-variant-ligatures: no-common-ligatures;">))</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">spriteNode</span>?.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">xScale</span> = 0.1</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">spriteNode</span>?.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">yScale</span> = 0.1</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>spriteNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>zPosition<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = 10</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>addChild<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>spriteNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">!)</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Store the start touch position</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> touchesBegan(touches: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Set</span><<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UITouch</span>>, withEvent event: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIEvent</span>?) {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">for</span> touch <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span> touches {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>xTouchStartPosition<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = touch.</span>locationInNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">).</span>x</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Calculate the distance of the toch movement</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> touchesMoved(touches: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Set</span><<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UITouch</span>>, withEvent event: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIEvent</span>?) {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">for</span> touch <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span> touches {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>xTouchCurrentPosition<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = touch.</span>locationInNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">).</span>x</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>xTouchDistance<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>xTouchStartPosition<span style="color: black; font-variant-ligatures: no-common-ligatures;"> - </span>xTouchCurrentPosition</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Reset all movement states</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> touchesEnded(touches: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Set</span><<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UITouch</span>>, withEvent event: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIEvent</span>?) {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>xTouchCurrentPosition<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = 0.0</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">xTouchDistance</span> = 0.0</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>xTouchStartPosition<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = 0.0</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// SpriteKits gameloop function</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> update(currentTime: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CFTimeInterval</span>) {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Scroll the background</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">scroll</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">xTouchDistance</span>, currentTime: currentTime)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
<br /></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Rotate sprite depending on direction</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">xTouchDistance</span> > 0 {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>spriteNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>zRotation<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>CGFloat<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>M_PI<span style="color: black; font-variant-ligatures: no-common-ligatures;">/2.0)</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">xTouchDistance</span> < 0 {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>spriteNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>zRotation<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = -</span>CGFloat<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>M_PI<span style="color: black; font-variant-ligatures: no-common-ligatures;">/2.0)</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Implement the scrolling</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> scroll(speed: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span>, currentTime: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CFTimeInterval</span>) {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">lastUpdateTime</span> != <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span> {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Calculate the new speed with the easing function (New speed is influence by current speed)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> newSpeed = (<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">currentSpeed</span> + (speed - <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">currentSpeed</span>) * <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">easeOutfactor</span>)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">currentSpeed</span> = newSpeed</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Set the new x position depending on the timeframe since the last calls.</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// This is needed because spritekit cannot guarantee that the timeframe is allways the same</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>worldNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">!.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>x<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>worldNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">!.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>x<span style="color: black; font-variant-ligatures: no-common-ligatures;"> + newSpeed * </span>CGFloat<span style="color: black; font-variant-ligatures: no-common-ligatures;">((currentTime - </span>lastUpdateTime<span style="color: black; font-variant-ligatures: no-common-ligatures;">!)) * </span>speedFactor</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Check if right end is reached</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>worldNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">!.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>x<span style="color: black; font-variant-ligatures: no-common-ligatures;"> < -(2 * </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>nodeTileWidth<span style="color: black; font-variant-ligatures: no-common-ligatures;">) {</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">worldNode</span>!.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">x</span> = 0.0</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Check if left end is reached</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">worldNode</span>!.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">x</span> > 0 {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>worldNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">!.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>x<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = -(2 * </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>nodeTileWidth<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">lastUpdateTime</span> = currentTime</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div>
<br /></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
<br /></div>
</div>
<div>
You can download the complete sample from my <a href="https://github.com/stfnjstn/EndlessScrollingDemo/releases/tag/v0.2">Github repository</a>.</div>
<br />
If you want to support me, please download my Apps from the Apple AppStore:<br />
<br />
<a href="https://itunes.apple.com/developer/stefan-josten/id949662361" style="font-family: '"helvetica neue light"'; text-align: center;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a><br />
<br />
That's all for today.<br />
<br />
<div>
Cheers,<br />
Stefan</div>
<div style="text-align: center;">
<br /></div>
<br />
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com0tag:blogger.com,1999:blog-7769140974661448994.post-18323136845663512132015-11-12T14:07:00.001-08:002016-05-13T14:15:06.445-07:00Quick Tip: Endless scrolling with SpriteKit and SWIFT (Part 1 of 2)<script>
window.location="http://developerplayground.net/quick-tip-endless-scrolling-with-spritekit-and-swift/";
</script>
<h2>
Endless scrolling with background tiles</h2>
<div>
Welcome to my new tutorial series about scrolling:</div>
<div>
<ul>
<li><a href="http://stefansdevplayground.blogspot.de/2015/11/quicktip-endless-scrolling-with.html">Part 1</a>: Endless scrolling with background tiles</li>
<li><a href="http://stefansdevplayground.blogspot.de/2015/11/quick-tip-endless-scrolling-with-easing.html">Part 2</a>: Natural endless scrolling with easing</li>
</ul>
</div>
<div>
<br />
<b>This video gives an impression what I'll show today:</b></div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen="1" frameborder="0" height="400" src="https://youtube.com//embed/uYVqG8Y8rLU" width="600"></iframe>
</div>
<br />
<br />
<h3>
1. About the algorithm and the background tiles</h3>
<br />
<h4>
Creating the image tiles for the scrolling parts of the background:</h4>
<br />
First we need a background image:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhE_fwJw0eBqcnRhdYIO0Z3Yi43Vo4ZOBZtXdWlHvffG55OgwcCpgCnqr-ESQP7vEIgOwa0dl0eu5PDLaT0r8PbiyaWrhCrLzoc_3JpBhGBAMpmuKJDx9gr548Wf2yAOOLdixTE_urp8vI/s1600/Tiles1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="84" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhE_fwJw0eBqcnRhdYIO0Z3Yi43Vo4ZOBZtXdWlHvffG55OgwcCpgCnqr-ESQP7vEIgOwa0dl0eu5PDLaT0r8PbiyaWrhCrLzoc_3JpBhGBAMpmuKJDx9gr548Wf2yAOOLdixTE_urp8vI/s200/Tiles1.png" width="200" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
Let's mirror this image and add the new one at the right side:</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLwA6Od5Vgi8B4flhCSa8HGi6o1s2EUx5x7PlIoc2oGhTIFim7pgxurX6ghWQfVrlSzr-zkt9oqRTx1zyUDqR6E7skh9NBKOcVUupGl34n6h5aXeI7geBR7hELe0xgnqzReavixY5L0_c/s1600/Tiles2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="" border="0" height="67" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLwA6Od5Vgi8B4flhCSa8HGi6o1s2EUx5x7PlIoc2oGhTIFim7pgxurX6ghWQfVrlSzr-zkt9oqRTx1zyUDqR6E7skh9NBKOcVUupGl34n6h5aXeI7geBR7hELe0xgnqzReavixY5L0_c/s320/Tiles2.png" title="Endless scrolling" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<a name='more'></a>Copy the original image and append it at the right side. In our app we will have three sprite nodes, one for each image tile:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0YnP0hsY8g1dneGQyEdsjpvjv89kJEhKaIU_i_CO5cO4eEaGkgy9LVgqdffdm1FP5Pt2XkXVuH5XsY9IxA6VOKy6x3DSgrvIxuKr27GRWMk7Dv19LOE5XS7ArYZgqEV1belgWbA6x-AQ/s1600/Tiles3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="" border="0" height="45" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0YnP0hsY8g1dneGQyEdsjpvjv89kJEhKaIU_i_CO5cO4eEaGkgy9LVgqdffdm1FP5Pt2XkXVuH5XsY9IxA6VOKy6x3DSgrvIxuKr27GRWMk7Dv19LOE5XS7ArYZgqEV1belgWbA6x-AQ/s320/Tiles3.png" title="Endless scrolling" width="320" /></a></div>
<br />
Scrolling will start at the left side:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtnI-_wjrJ4MZ_3m3I6HxKw4OQJm2VuxXe-8xJrEDpUXwZlJEDlqnQkpMMfkabKemdlQ7FkgTS9u-Mmm-qK66v3COmS0GIWUDBJROS8jJRY98WHcsClVLHHEtvnYWjK6xh6RqAOUS3qOQ/s1600/Tiles4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="" border="0" height="85" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtnI-_wjrJ4MZ_3m3I6HxKw4OQJm2VuxXe-8xJrEDpUXwZlJEDlqnQkpMMfkabKemdlQ7FkgTS9u-Mmm-qK66v3COmS0GIWUDBJROS8jJRY98WHcsClVLHHEtvnYWjK6xh6RqAOUS3qOQ/s320/Tiles4.png" title="Endless scrolling" width="320" /></a></div>
<br />
Let's add a static background image:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibE2I9WSXib26ykcCgvcX6nVpD_Dz2Ry2_MnDA0PvPqkQSz-1z-efr9cw7eUei8roS_RJoRLaI92uI3zMUbE0PSyI8HIgU79fFzdE0bsIZHxpOY__Yw3auuM2sHwDiftNjEcQKvKZApS8/s1600/Tiles5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="" border="0" height="85" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibE2I9WSXib26ykcCgvcX6nVpD_Dz2Ry2_MnDA0PvPqkQSz-1z-efr9cw7eUei8roS_RJoRLaI92uI3zMUbE0PSyI8HIgU79fFzdE0bsIZHxpOY__Yw3auuM2sHwDiftNjEcQKvKZApS8/s320/Tiles5.png" title="Endless scrolling" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
To scroll right, move the background tiles in the left direction:</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixCpsIBWfc5buA9dXJl2C6GIbsAcgh733TVJOyElOOeRMO6ShYbJ3RoM08Myys3MNqJQO9gDvFazYXLMB6up9Ojp1Ngm2JaWi1-U0WbjqQ7ekS4YMUtXpFSr0dNTnfZuzku89wsbQd-9Q/s1600/Tiles6.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="" border="0" height="85" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixCpsIBWfc5buA9dXJl2C6GIbsAcgh733TVJOyElOOeRMO6ShYbJ3RoM08Myys3MNqJQO9gDvFazYXLMB6up9Ojp1Ngm2JaWi1-U0WbjqQ7ekS4YMUtXpFSr0dNTnfZuzku89wsbQd-9Q/s320/Tiles6.png" title="Endless scrolling" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
If the end at the right side is reached:</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhw8iygbGlG0q-j1P-bXSm7TrSX31cUOA9Uu_lvODcv3aXuivxin4v6z5C090d64JHatYCoP5qSWKqvvruSbDi0aqkrRgWmGFnEDTfSc1Eey0h6rAdbjr1fpj2BYQs7Ou6bNxwsOJdDldQ/s1600/Tiles7.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="" border="0" height="124" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhw8iygbGlG0q-j1P-bXSm7TrSX31cUOA9Uu_lvODcv3aXuivxin4v6z5C090d64JHatYCoP5qSWKqvvruSbDi0aqkrRgWmGFnEDTfSc1Eey0h6rAdbjr1fpj2BYQs7Ou6bNxwsOJdDldQ/s320/Tiles7.png" title="Endless scrolling" width="320" /></a></div>
<br />
Move the background tiles back to the start position:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSjg7z1xwc7YZQB0ydru5MdXhkvmDJHgjs7rR5bcVndT_uVanlqBzKGL1FYY7_dzO1zc3ez8bxMSnJk7iJPt4e6fooak1Bvhb5_cnirEMZaB0UwEVteuM3EfJIVYjYwNkgybi_0NbJh4A/s1600/Tiles8.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="" border="0" height="107" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSjg7z1xwc7YZQB0ydru5MdXhkvmDJHgjs7rR5bcVndT_uVanlqBzKGL1FYY7_dzO1zc3ez8bxMSnJk7iJPt4e6fooak1Bvhb5_cnirEMZaB0UwEVteuM3EfJIVYjYwNkgybi_0NbJh4A/s320/Tiles8.png" title="Endless scrolling" width="320" /></a></div>
<br />
<br />
<br />
<b>The SpriteKits object hierarchy will be created this way:</b><br />
<ul>
<li>scene (SKScene)</li>
<ul>
<li>backgroundNode (SKSpriteNode)</li>
<li>worldNode (SKNode)</li>
<ul>
<li>leftTileNode (SKSpriteNode)</li>
<li>middleTileNode (SKSpriteNode)</li>
<li>rightTileNode (SKSpriteNode)</li>
</ul>
<li>spriteNode (SKSpriteNode)</li>
</ul>
</ul>
<div>
For scrolling the worldNode will be moved to implement the scrolling.</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<h3>
2. Create the SWIFT project:</h3>
</div>
<div>
<br /></div>
<div>
<h4>
Create a new Sprite Kit project:</h4>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgupMKyahYJFo-oalkdOnYL3maNMqF9MEXayL5RaNpjiX3ZBtadKxNVMVD5jS7QBj_M8OwXN6ZzJ_VMOyLA7aL7SEeDm2Szs07n_Jo07ImbXeR8kObZIrdq7TfcIBZf9vMNTtBhuo-yfTU/s1600/Screen+Shot+2015-11-13+at+18.13.14.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Tutorial: Endless scrolling with SpriteKit and SWIFT" border="0" height="227" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgupMKyahYJFo-oalkdOnYL3maNMqF9MEXayL5RaNpjiX3ZBtadKxNVMVD5jS7QBj_M8OwXN6ZzJ_VMOyLA7aL7SEeDm2Szs07n_Jo07ImbXeR8kObZIrdq7TfcIBZf9vMNTtBhuo-yfTU/s320/Screen+Shot+2015-11-13+at+18.13.14.png" title="Endless scrolling" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitV81YdkTGj4aTbz46KxIL86fj5wMbIqcep4rb07UpK3rHwSJZJDjAkulGvKAtoqRP4d2ml47wfvbG2Ni5cArN4iM2lrLY5Nn9nw2Sn0HRg3wk3ciqxka3zi4u_l0nd05KJM-RXj0oCH4/s1600/Screen+Shot+2015-11-13+at+18.14.00.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Tutorial: Endless scrolling with SpriteKit and SWIFT" border="0" height="229" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitV81YdkTGj4aTbz46KxIL86fj5wMbIqcep4rb07UpK3rHwSJZJDjAkulGvKAtoqRP4d2ml47wfvbG2Ni5cArN4iM2lrLY5Nn9nw2Sn0HRg3wk3ciqxka3zi4u_l0nd05KJM-RXj0oCH4/s320/Screen+Shot+2015-11-13+at+18.14.00.png" title="Endless scrolling" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div>
<br /></div>
<h4>
Open Asset Catalogue and add three images (Background, LeftTile, rightTile)</h4>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidt5ygrvAiWvHYPLVVaSBVrgSSaWYMagmey7-cnbI7dseCXAAGyLqiQ-eJIs7E_6Z7lgG1EqElZp1WIG0oITLzTYTApUZB7r1y1gmFWpall7xw8xH_DWORnmdBfGQ0hHl2D7C29NdBcp0/s1600/Screen+Shot+2015-11-13+at+18.30.51.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Tutorial: Endless scrolling with SpriteKit and SWIFT" border="0" height="81" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidt5ygrvAiWvHYPLVVaSBVrgSSaWYMagmey7-cnbI7dseCXAAGyLqiQ-eJIs7E_6Z7lgG1EqElZp1WIG0oITLzTYTApUZB7r1y1gmFWpall7xw8xH_DWORnmdBfGQ0hHl2D7C29NdBcp0/s320/Screen+Shot+2015-11-13+at+18.30.51.png" title="Endless scrolling with SpriteKit and SWIFT" width="320" /></a></div>
<div>
<br /></div>
<h4>
Open GameScene.swift:</h4>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgse5Z-ZGn30jeqhRjDEaix6H9dPP3vdtpmRkd60vmTtC2hX1A36BrupPCotLL03vZXlCZA_a90olVy3QH6MdW8EKfjtc8y3sIZVqvYf6HX1waIAgCXIyArSlS5nCZZZxQ5Y6iBAUqOcy8/s1600/Screen+Shot+2015-11-13+at+18.14.43.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Tutorial: Endless scrolling with SpriteKit and SWIFT" border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgse5Z-ZGn30jeqhRjDEaix6H9dPP3vdtpmRkd60vmTtC2hX1A36BrupPCotLL03vZXlCZA_a90olVy3QH6MdW8EKfjtc8y3sIZVqvYf6HX1waIAgCXIyArSlS5nCZZZxQ5Y6iBAUqOcy8/s1600/Screen+Shot+2015-11-13+at+18.14.43.png" title="Endless scrolling with SpriteKit and SWIFT" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<h4>
Replace the complete code with this snippet: </h4>
(For explanation check the comments inside the code snippet)<br />
<br />
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
//</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
// GameScene.swift</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
// EndlessScrollingDemo</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
//</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
// Created by STEFAN on 13/11/15.</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
// Copyright (c) 2015 Stefan. All rights reserved.</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
//</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">import</span> SpriteKit</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">class</span> GameScene: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKScene</span> {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Declare the globaly needed sprite kit nodes</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> worldNode: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKNode</span>?</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> spriteNode: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>?</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
<br /></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// store the width of the NodeTiles</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> nodeTileWidth: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span> = 0.0</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// store the start position of the movement</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> xOrgPosition: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span> = 0</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> didMoveToView(view: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKView</span>) {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Setup static background</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> backgroundNode = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>(imageNamed: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Background"</span>)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
backgroundNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGSize</span>(width: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">frame</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">width</span>, height: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">frame</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">height</span>)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
backgroundNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">anchorPoint</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: 0, y: 0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
backgroundNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">zPosition</span> = -10</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addChild</span>(backgroundNode)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Setup world</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>worldNode<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>SKNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>addChild<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>worldNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">!)</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Setup dynamic background tiles</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Image of left and right node must be identical</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> leftNode = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>(imageNamed: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"LeftTile"</span>)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> middleNode = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>(imageNamed: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"RightTile"</span>)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> rightNode = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>(imageNamed: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"LeftTile"</span>)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>nodeTileWidth<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = leftNode.</span>frame<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>width</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
leftNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">anchorPoint</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: 0, y: 0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
leftNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: 0, y: 0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
middleNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">anchorPoint</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: 0, y: 0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
middleNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">nodeTileWidth</span>, y: 0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
rightNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">anchorPoint</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: 0, y: 0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
rightNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">nodeTileWidth</span> * 2, y: 0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Add tiles to worldNode. worldNode is used to realize the scrolling</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">worldNode</span>!.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addChild</span>(leftNode)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">worldNode</span>!.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addChild</span>(rightNode)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">worldNode</span>!.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addChild</span>(middleNode)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Setup sprite</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">spriteNode</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>(imageNamed: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Spaceship"</span>)</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>spriteNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>CGPoint<span style="color: black; font-variant-ligatures: no-common-ligatures;">(x:</span>CGRectGetMidX<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>frame<span style="color: black; font-variant-ligatures: no-common-ligatures;">), y:</span>CGRectGetMidY<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>frame<span style="color: black; font-variant-ligatures: no-common-ligatures;">))</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">spriteNode</span>?.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">xScale</span> = 0.1</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">spriteNode</span>?.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">yScale</span> = 0.1</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>spriteNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>zPosition<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = 10</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>addChild<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>spriteNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">!)</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Implement the scrolling, triggered by swipe gestures</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> touchesMoved(touches: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Set</span><<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UITouch</span>>, withEvent event: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIEvent</span>?) {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">for</span> touch <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span> touches {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Touch position</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> xTouchPosition = touch.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">locationInNode</span>(<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>).<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">x</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">xOrgPosition</span> != 0.0 {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// calculate the new position</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> xNewPosition = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">worldNode</span>!.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">x</span> + (<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">xOrgPosition</span> - xTouchPosition)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Check if right end is reached</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> xNewPosition <= -(2 * <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">nodeTileWidth</span>) {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">worldNode</span>!.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: 0, y: 0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">print</span>(<span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Right end reached"</span>)</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Check if left end is reached</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> xNewPosition >= 0 {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">worldNode</span>!.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: -(2 * <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">nodeTileWidth</span>), y: 0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">print</span>(<span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Left end reached"</span>)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">worldNode</span>!.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: xNewPosition, y: 0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Rotate sprite depending on direction</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> xTouchPosition < <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">xOrgPosition</span> {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">spriteNode</span>?.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">zRotation</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">M_PI</span>/2.0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">spriteNode</span>?.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">zRotation</span> = -<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">M_PI</span>/2.0)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Store the current touch position to calculate the delta in the next iteration</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">xOrgPosition</span> = xTouchPosition</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> touchesEnded(touches: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Set</span><<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UITouch</span>>, withEvent event: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIEvent</span>?) {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Reset value for the next swipe gesture</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">xOrgPosition</span> = 0</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> update(currentTime: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CFTimeInterval</span>) {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>/* Called before each frame is rendered */</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<br />
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div>
<br /></div>
<br />
<br /></div>
<div>
You can download the complete sample from my <a href="https://github.com/stfnjstn/EndlessScrollingDemo/releases/tag/v0.1">Github repository</a>.</div>
<br />
I'll show an improved version with a smoother scrolling in <a href="http://stefansdevplayground.blogspot.de/2015/11/quick-tip-endless-scrolling-with-easing.html">part 2</a>.<br />
<br />
You can support me by downloading my Apps from the Apple AppStore:<br />
<br />
<a href="https://itunes.apple.com/developer/stefan-josten/id949662361" style="font-family: '"helvetica neue light"'; text-align: center;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a><br />
<br />
That's all for today.<br />
<br />
<div>
Cheers,<br />
Stefan</div>
<div style="text-align: center;">
<br /></div>
<br />
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com0tag:blogger.com,1999:blog-7769140974661448994.post-36199527180288124812015-10-01T12:14:00.003-07:002016-05-17T08:45:43.077-07:00Quick Tip: How to use the Apple TV remote to control a sprite in your TVOS Apps<script>
window.location="http://developerplayground.net/quick-tip-how-to-use-the-apple-tv-remote-to-control-a-sprite-in-your-tvos-apps/";
</script>
<h2>
How to use the Apple TV remote to control a sprite in your TVOS Apps</h2>
<div>
Since my Apple TV Developer kit arrived one week ago I had plenty of time to try certain things. I will blog about some of my prototypes in the next days.<br />
<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6xdZnRtFQqV-lpQLADQbcjJFGF1V8q69RMQMWqTTrLoXaHvQr_y0IbOCOB56c6zWof3JWqieeAN-0HTxg4LIJnM2fHSDl-N3QlylM9wfiVg_ED0pFGw-WZNcz5FvkFd30MKjUSmf9TnI/s1600/Screen+Shot+2015-10-01+at+20.12.48.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="212" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6xdZnRtFQqV-lpQLADQbcjJFGF1V8q69RMQMWqTTrLoXaHvQr_y0IbOCOB56c6zWof3JWqieeAN-0HTxg4LIJnM2fHSDl-N3QlylM9wfiVg_ED0pFGw-WZNcz5FvkFd30MKjUSmf9TnI/s320/Screen+Shot+2015-10-01+at+20.12.48.png" width="320" /></a></div>
<br />
<br />
<div style="text-align: justify;">
Today I'll will focus again on the remote control. It provides, especially in combination with SpriteKit, a simple and very natural way to control a sprite on the screen. For a natural sprite movement the approach of my todays post is much better, the my <a href="http://stefansdevplayground.blogspot.de/2015/09/quick-tip-how-to-use-remote-control-in.html">article about gesture recognition</a> on the AppleTV. </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Let's start...</div>
<br />
<h4>
<b>1. Download the XCode 7.1 Beta from the <a href="https://developer.apple.com/xcode/download/">Apple Developer Portal</a>:</b></h4>
<b><br /></b>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUsSbnrxWalfZu-yYQQuPmZqvySJG7-imtY3DXSH0MZUrdw_mF522g7fQN6pypUAFlAXMHQEedhJwcj2lNW0-Ds4L3s6UVcRsj0OTXU7na5WgngKeUQinSuIfHlPKoqxGJwi3-9BGG9Jw/s1600/Screen+Shot+2015-09-11+at+00.01.17.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="124" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUsSbnrxWalfZu-yYQQuPmZqvySJG7-imtY3DXSH0MZUrdw_mF522g7fQN6pypUAFlAXMHQEedhJwcj2lNW0-Ds4L3s6UVcRsj0OTXU7na5WgngKeUQinSuIfHlPKoqxGJwi3-9BGG9Jw/s320/Screen+Shot+2015-09-11+at+00.01.17.png" width="320" /></a></div>
<br />
<h4>
<b>2. Create a new project:</b></h4>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRurP7UbiEZPn6Q4jzgpodUfIrNfssbyhqojpC5ZeQdiWbEi54gYKGfc1aW5xG18b4BhPhgfXqux2f3FPT3lsPK0Xf51dEphU91ZuzO1jCAh10FAJa6MGEsYRw-RZPH9FPSUdWEPWuUNY/s1600/Screen+Shot+2015-09-10+at+23.59.18.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRurP7UbiEZPn6Q4jzgpodUfIrNfssbyhqojpC5ZeQdiWbEi54gYKGfc1aW5xG18b4BhPhgfXqux2f3FPT3lsPK0Xf51dEphU91ZuzO1jCAh10FAJa6MGEsYRw-RZPH9FPSUdWEPWuUNY/s320/Screen+Shot+2015-09-10+at+23.59.18.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgW_JBqRQPUCaVRlunhTffaLHOXB3nbB-4iPn9Wfrc-IvMTMSWZow2bK8eTMO08fzvrHp-pxgM0A20Hlc4wmSzo3oFHjBURwVZ-H9Yr40HMzUcEwqsL0ie22UfsmdMNn53Ct3y2XnFQmL4/s1600/Screen+Shot+2015-10-01+at+20.16.29.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="228" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgW_JBqRQPUCaVRlunhTffaLHOXB3nbB-4iPn9Wfrc-IvMTMSWZow2bK8eTMO08fzvrHp-pxgM0A20Hlc4wmSzo3oFHjBURwVZ-H9Yr40HMzUcEwqsL0ie22UfsmdMNn53Ct3y2XnFQmL4/s320/Screen+Shot+2015-10-01+at+20.16.29.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<a name='more'></a><br />
<br />
<h4>
<b>3. Open '<span style="font-family: Courier New, Courier, monospace;">GameViewController.swift'</span> </b></h4>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfy4eDnE3X-H04vOQtEQ2N7IvkCxWyAiRE_n3FcxT410q9iNNc3EGKXof9DI102HVxKk-3WZ08ekoowCvGyo9UwkbZ6qACtv1p7Q-EZkjcEsXCeGsYbBFshUKbMaNzIfJJOSJ5dNVsvVY/s1600/Screen+Shot+2015-10-01+at+20.39.10.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfy4eDnE3X-H04vOQtEQ2N7IvkCxWyAiRE_n3FcxT410q9iNNc3EGKXof9DI102HVxKk-3WZ08ekoowCvGyo9UwkbZ6qACtv1p7Q-EZkjcEsXCeGsYbBFshUKbMaNzIfJJOSJ5dNVsvVY/s1600/Screen+Shot+2015-10-01+at+20.39.10.png" /></a></div>
<div>
<b><br /></b></div>
<span style="font-weight: normal;">Change the scale mode from in <span style="font-family: Courier New, Courier, monospace;">viewDidLoad</span> from</span><br />
<span style="font-weight: normal;"><br /></span>
<br />
<div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
scene.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">scaleMode</span> = .<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">AspectFill</span></div>
</div>
<br />
to<br />
<br />
<div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;">scene.</span>scaleMode<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = .</span>ResizeFill</div>
</div>
<h4>
<b>4. Open '<span style="font-family: Courier New, Courier, monospace;">GameScene.swift</span>' </b></h4>
<span style="font-weight: normal;">Override the <span style="font-family: Courier New, Courier, monospace;">didMoveToView</span> method with this snippet to create a sprite in the middle of the screen:</span><br />
<b></b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg682E-bKArUozz4JJS-afNDdLl2T1z97x__rh8qxZWhdGDr67rUmPkijUTKiFTIBFVvY27gU84DXFaQK8C60jXpu67PnUHPt1c-vbpoj0yKqnfh8NxGfB8Vu2kuKclz45wDJgmchaK79E/s1600/Screen+Shot+2015-10-01+at+20.16.46.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg682E-bKArUozz4JJS-afNDdLl2T1z97x__rh8qxZWhdGDr67rUmPkijUTKiFTIBFVvY27gU84DXFaQK8C60jXpu67PnUHPt1c-vbpoj0yKqnfh8NxGfB8Vu2kuKclz45wDJgmchaK79E/s1600/Screen+Shot+2015-10-01+at+20.16.46.png" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<b><br /></b>
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Global property to store the sprite object</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> sprite = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>(imageNamed:<span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Spaceship"</span>)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> didMoveToView(view: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKView</span>) {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">sprite</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">yScale</span> = 0.2</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">sprite</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">xScale</span> = 0.2</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>sprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>CGPoint<span style="color: black; font-variant-ligatures: no-common-ligatures;">(x:</span>CGRectGetMidX<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>frame<span style="color: black; font-variant-ligatures: no-common-ligatures;">), y:</span>CGRectGetMidY<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>frame<span style="color: black; font-variant-ligatures: no-common-ligatures;">))</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>addChild<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>sprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<h4>
5. Add the code to handle the sprite movement</h4>
Replace the <span style="font-family: Courier New, Courier, monospace;">touchesBegan</span> method with this snippet to persist the initial touch position:<br />
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Persist the initial touch position of the remote</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> touchPositionX: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span> = 0.0</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> touchPositionY: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span> = 0.0</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> touchesBegan(touches: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Set</span><<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UITouch</span>>, withEvent event: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIEvent</span>?) {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">for</span> touch <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span> touches {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>touchPositionX<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = touch.</span>locationInNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">).</span>x</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>touchPositionY<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = touch.</span>locationInNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">).</span>y</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<br />
The <span style="font-family: Courier New, Courier, monospace;">touchedMoved</span> method implements the logic to move the sprite on the screen:<br />
<span style="font-weight: normal;"><br /></span>
<br />
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> touchesMoved(touches: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Set</span><<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UITouch</span>>, withEvent event: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIEvent</span>?) {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">for</span> touch <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span> touches {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> location = touch.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">locationInNode</span>(<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">touchPositionX</span> != 0.0 && <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">touchPositionY</span> != 0.0 {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Calculate the movement on the remote</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> deltaX = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">touchPositionX</span> - location.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">x</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> deltaY = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">touchPositionY</span> - location.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">y</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Calculate the new Sprite position</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> x = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">sprite</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">x</span> - deltaX</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> y = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">sprite</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">y</span> - deltaY</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Check if the sprite will leave the screen</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> x < 0 {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
x = 0</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> x > <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">frame</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">width</span> {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
x = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">frame</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">width</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> y < 0 {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
y = 0</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> y > <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">frame</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">height</span> {</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
y = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">frame</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">height</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Move the sprite</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">sprite</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: x, y: y)</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Persist latest touch position</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">touchPositionY</span> = location.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">y</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">touchPositionX</span> = location.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">x</span></div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; line-height: normal;">
}</div>
<h4>
</h4>
<h4>
6. Now let's move the sprite around and start the App</h4>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP_g0eA_WdHf3fFtO5RJ-1qgpE5d3aBjzTMWUl9oSRlRnsqm8YHOSzeMYNwgUkW9upO4cBIGhM4ggQaaXE7MwG1pov0OvrTLblwU-l6KsEg7e1HWRrvrBhG9EKFsPA6DnD6uZ-f6NMEBo/s1600/Screen+Shot+2015-09-11+at+00.08.58.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="193" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP_g0eA_WdHf3fFtO5RJ-1qgpE5d3aBjzTMWUl9oSRlRnsqm8YHOSzeMYNwgUkW9upO4cBIGhM4ggQaaXE7MwG1pov0OvrTLblwU-l6KsEg7e1HWRrvrBhG9EKFsPA6DnD6uZ-f6NMEBo/s320/Screen+Shot+2015-09-11+at+00.08.58.png" width="320" /></a></div>
<div>
<br /></div>
<div>
<br />
<span style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px;">If you try this in the Simulator you have to press the option key (alt) on you Mac keyboard and move the mouse around on the touch area of the remote control window. No mouse clicks!!!</span></div>
<div>
<br /></div>
</div>
<br />
That's all for today.<br />
<br />
<div>
Cheers,<br />
Stefan</div>
<div style="text-align: center;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><a href="https://itunes.apple.com/developer/stefan-josten/id949662361"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></span></span></span></div>
<br />
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com0tag:blogger.com,1999:blog-7769140974661448994.post-88810579358146701132015-09-10T15:16:00.004-07:002016-05-17T08:46:24.657-07:00Quick Tip: How to use the remote control in your TVOS Apps for Apple TV in SWIFT<script>
window.location="http://developerplayground.net/quick-tip-how-to-use-the-remote-control-in-your-tvos-apps-for-apple-tv-in-swift/";
</script>
<h2>
How to use the remote control in your tvOS Apps for Apple TV in SWIFT</h2>
<div style="text-align: justify;">
There are already dozens of 'Hello world' tutorials published for the new Apple tvOS, so let's do something different. I'll show how to use the remote control to move a sprite on Apple TV. It was surprisingly easy and took me only 10 minutes to implement:</div>
<br />
<h4>
<b>1. Download the XCode 7.1 Beta from the <a href="https://developer.apple.com/xcode/download/">Apple Developer Portal</a>:</b></h4>
<b><br /></b>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUsSbnrxWalfZu-yYQQuPmZqvySJG7-imtY3DXSH0MZUrdw_mF522g7fQN6pypUAFlAXMHQEedhJwcj2lNW0-Ds4L3s6UVcRsj0OTXU7na5WgngKeUQinSuIfHlPKoqxGJwi3-9BGG9Jw/s1600/Screen+Shot+2015-09-11+at+00.01.17.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="124" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUsSbnrxWalfZu-yYQQuPmZqvySJG7-imtY3DXSH0MZUrdw_mF522g7fQN6pypUAFlAXMHQEedhJwcj2lNW0-Ds4L3s6UVcRsj0OTXU7na5WgngKeUQinSuIfHlPKoqxGJwi3-9BGG9Jw/s320/Screen+Shot+2015-09-11+at+00.01.17.png" width="320" /></a></div>
<br />
<h4>
<b>2. Create a new project:</b></h4>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRurP7UbiEZPn6Q4jzgpodUfIrNfssbyhqojpC5ZeQdiWbEi54gYKGfc1aW5xG18b4BhPhgfXqux2f3FPT3lsPK0Xf51dEphU91ZuzO1jCAh10FAJa6MGEsYRw-RZPH9FPSUdWEPWuUNY/s1600/Screen+Shot+2015-09-10+at+23.59.18.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRurP7UbiEZPn6Q4jzgpodUfIrNfssbyhqojpC5ZeQdiWbEi54gYKGfc1aW5xG18b4BhPhgfXqux2f3FPT3lsPK0Xf51dEphU91ZuzO1jCAh10FAJa6MGEsYRw-RZPH9FPSUdWEPWuUNY/s320/Screen+Shot+2015-09-10+at+23.59.18.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2kg8Lzr3ebABy6-1UMQ7oyHxztmdcA5y4zbbc3plAr2KcvPY6AB07H35uziAJdfIBmd1FrX-5RJchbKo_G9TZhXq1eWMMZCM_YggpBckYFkL7s3jTQZgSzY-p7Mmce6GbL1uTu56EKrM/s1600/Screen+Shot+2015-09-11+at+00.06.33.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="228" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2kg8Lzr3ebABy6-1UMQ7oyHxztmdcA5y4zbbc3plAr2KcvPY6AB07H35uziAJdfIBmd1FrX-5RJchbKo_G9TZhXq1eWMMZCM_YggpBckYFkL7s3jTQZgSzY-p7Mmce6GbL1uTu56EKrM/s320/Screen+Shot+2015-09-11+at+00.06.33.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<a name='more'></a><br />
<br />
<h4>
<b>3. Open '<span style="font-family: Courier New, Courier, monospace;">GameScene.swift</span>':</b></h4>
<b></b><br />
<div class="separator" style="clear: both; text-align: center;">
<b><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghD1DoxNrgIm2wxQcLGPYanhDPy0m3f1SxB_x64j5JZ7Ic9rRmeaUUN4YyATsLakaN6DIHDQjhFgDjjyX38d0DUQ8V5T7qC5V-tPAN1jf0sVpkrFTH2v7nUc6vJcIDm-t9gezKv3jR2So/s1600/Screen+Shot+2015-09-11+at+00.03.18.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghD1DoxNrgIm2wxQcLGPYanhDPy0m3f1SxB_x64j5JZ7Ic9rRmeaUUN4YyATsLakaN6DIHDQjhFgDjjyX38d0DUQ8V5T7qC5V-tPAN1jf0sVpkrFTH2v7nUc6vJcIDm-t9gezKv3jR2So/s1600/Screen+Shot+2015-09-11+at+00.03.18.png" /></a></b></div>
<br />
<b><br /></b>
<b><br /></b>
<br />
<h4>
<b>4. Replace the complete code with this snippet:</b></h4>
<b><br /></b>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">import</span> SpriteKit</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">class</span> GameScene: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKScene</span> {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> sprite = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>(imageNamed:<span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Spaceship"</span>)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> didMoveToView(view: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKView</span>) {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>/* Setup your scene here */</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Add Sprite</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">sprite</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">xScale</span> = 0.5</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">sprite</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">yScale</span> = 0.5</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>sprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>CGPoint<span style="color: black; font-variant-ligatures: no-common-ligatures;">(x:</span>CGRectGetMidX<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>frame<span style="color: black; font-variant-ligatures: no-common-ligatures;">), y:</span>CGRectGetMidY<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>frame<span style="color: black; font-variant-ligatures: no-common-ligatures;">))</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>addChild<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>sprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Register Swipe Events</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> swipeRight:</span>UISwipeGestureRecognizer<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>UISwipeGestureRecognizer<span style="color: black; font-variant-ligatures: no-common-ligatures;">(target: </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, action: </span>Selector<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"swipedRight:"</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">))</span></div>
<div style="font-family: Menlo; font-size: 11px;">
swipeRight.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">direction</span> = .Right</div>
<div style="font-family: Menlo; font-size: 11px;">
view.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addGestureRecognizer</span>(swipeRight)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> swipeLeft:</span>UISwipeGestureRecognizer<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>UISwipeGestureRecognizer<span style="color: black; font-variant-ligatures: no-common-ligatures;">(target: </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, action: </span>Selector<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"swipedLeft:"</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">))</span></div>
<div style="font-family: Menlo; font-size: 11px;">
swipeLeft.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">direction</span> = .Left</div>
<div style="font-family: Menlo; font-size: 11px;">
view.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addGestureRecognizer</span>(swipeLeft)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> swipeUp:</span>UISwipeGestureRecognizer<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>UISwipeGestureRecognizer<span style="color: black; font-variant-ligatures: no-common-ligatures;">(target: </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, action: </span>Selector<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"swipedUp:"</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">))</span></div>
<div style="font-family: Menlo; font-size: 11px;">
swipeUp.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">direction</span> = .Up</div>
<div style="font-family: Menlo; font-size: 11px;">
view.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addGestureRecognizer</span>(swipeUp)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> swipeDown:</span>UISwipeGestureRecognizer<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>UISwipeGestureRecognizer<span style="color: black; font-variant-ligatures: no-common-ligatures;">(target: </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, action: </span>Selector<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"swipedDown:"</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">))</span></div>
<div style="font-family: Menlo; font-size: 11px;">
swipeDown.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">direction</span> = .Down</div>
<div style="font-family: Menlo; font-size: 11px;">
view.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addGestureRecognizer</span>(swipeDown)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Handle Swipe Events</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> swipedRight(sender:<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UISwipeGestureRecognizer</span>){</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>sprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>CGPoint<span style="color: black; font-variant-ligatures: no-common-ligatures;">(x: </span>sprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>x<span style="color: black; font-variant-ligatures: no-common-ligatures;"> + 10, y: </span>sprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>y<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> swipedLeft(sender:<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UISwipeGestureRecognizer</span>){</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>sprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>CGPoint<span style="color: black; font-variant-ligatures: no-common-ligatures;">(x: </span>sprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>x<span style="color: black; font-variant-ligatures: no-common-ligatures;"> - 10, y: </span>sprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>y<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> swipedUp(sender:</span>UISwipeGestureRecognizer<span style="color: black; font-variant-ligatures: no-common-ligatures;">){</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>sprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>CGPoint<span style="color: black; font-variant-ligatures: no-common-ligatures;">(x: </span>sprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>x<span style="color: black; font-variant-ligatures: no-common-ligatures;">, y: </span>sprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>y<span style="color: black; font-variant-ligatures: no-common-ligatures;">+10)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> swipedDown(sender:<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UISwipeGestureRecognizer</span>){</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>sprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>CGPoint<span style="color: black; font-variant-ligatures: no-common-ligatures;">(x: </span>sprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>x<span style="color: black; font-variant-ligatures: no-common-ligatures;">, y: </span>sprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>y<span style="color: black; font-variant-ligatures: no-common-ligatures;">-10)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> update(currentTime: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CFTimeInterval</span>) {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>/* Called before each frame is rendered */</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<br />
<h4>
5. Start the Simulator:</h4>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3NrS5Y5SKOyNClfo9Ftr8rENOE_ReVDDy8Te5vDhXsjUzPBDoxouzAiWB8qiitur9A0PGHwV5Tm5ZvJpHrspp48SfeQG0EqvpBBLdUxwD0dI0W0Sig5IX0_6SzJTty3MqrAVBfmBquy4/s1600/Screen+Shot+2015-09-11+at+00.06.44.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3NrS5Y5SKOyNClfo9Ftr8rENOE_ReVDDy8Te5vDhXsjUzPBDoxouzAiWB8qiitur9A0PGHwV5Tm5ZvJpHrspp48SfeQG0EqvpBBLdUxwD0dI0W0Sig5IX0_6SzJTty3MqrAVBfmBquy4/s1600/Screen+Shot+2015-09-11+at+00.06.44.png" /></a></div>
<br />
<br />
<h4>
6. Show the remote control:</h4>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg558k2z2culuIuKr0yEpDK-vH61ZSiQ0z-K3m_F7SCPFj1TQ0-SBwfoMH9nVg-sqwCU1SPVnL02y2DRoc3cK2G_YqsUrtgsFDjCjyEzpzEcM1oNbsKYOqIWRepiTDtL9s13dM1cKNkjJg/s1600/Screen+Shot+2015-09-11+at+00.08.32.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg558k2z2culuIuKr0yEpDK-vH61ZSiQ0z-K3m_F7SCPFj1TQ0-SBwfoMH9nVg-sqwCU1SPVnL02y2DRoc3cK2G_YqsUrtgsFDjCjyEzpzEcM1oNbsKYOqIWRepiTDtL9s13dM1cKNkjJg/s1600/Screen+Shot+2015-09-11+at+00.08.32.png" /></a></div>
<br />
<br />
<h4>
7. Move the sprite around: </h4>
This was the most confusing part and costs me some minutes. You have to press the option key (alt) on you Mac keyboard and move the mouse around on the touch area of the remote control window. No mouse clicks!!!<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiRjegCxGcwAnIRPxtothQzw6T2sVCwn1diKArAuLgaTDhYrMT-4NpG1seLTRgBQ97P3faK2ojAikMJr1gvuqFXiVY6SzCQU24eXtPHLEGZ7rtv0Hf-EOz52rJvx4_puARmauwyWNjX7OE/s1600/Screen+Shot+2015-09-11+at+00.08.58.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="193" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiRjegCxGcwAnIRPxtothQzw6T2sVCwn1diKArAuLgaTDhYrMT-4NpG1seLTRgBQ97P3faK2ojAikMJr1gvuqFXiVY6SzCQU24eXtPHLEGZ7rtv0Hf-EOz52rJvx4_puARmauwyWNjX7OE/s320/Screen+Shot+2015-09-11+at+00.08.58.png" width="320" /></a></div>
<br />
<br />
<br />
That's all for today.<br />
<br />
<div>
Cheers,<br />
Stefan</div>
<div style="text-align: center;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><a href="https://itunes.apple.com/us/app/yet-another-watch-puzzle-game/id997514879?ls=1&mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></span></span></span></div>
<br />
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com1tag:blogger.com,1999:blog-7769140974661448994.post-70810436115908934262015-08-29T10:22:00.001-07:002016-05-17T08:47:06.692-07:00Quick Tip: Implement the new Google AdMob / Adsense Cookie Policy<script>
window.location="http://developerplayground.net/quick-tip-implement-the-new-google-admob-adsense-cookie-policy/";
</script>
In the last days Google sent out emails to App developers which uses their AdSense and AdMob frameworks. Google urges them to inform their users about the usage of the advertising cookies:<br />
<br />
<b>From Google:</b><br />
<blockquote class="tr_bq">
<div style="color: #444444; font-family: arial, sans-serif; font-size: 13px; line-height: 20px; margin-bottom: 10px;">
European laws require that digital publishers give visitors to their sites and apps information about their use of cookies and other forms of local storage. In many cases these laws also require that consent be obtained.</div>
<div>
<br /></div>
</blockquote>
In this tutorial I'll show how you can implement an information alert about the cookie usage in SWIFT. The basic idea is to detect, if the app is started the first time:<br />
<ul>
<li>If yes show an alert with a message about the cookie usage. </li>
<li>After clicking 'OK' store a key in NSUserDefault. </li>
<li>Next time the application is started, this key will be found and therefore no alert will be shown.</li>
</ul>
<b><br /></b>
<b>IMPORTANT: </b>I'm not a lawyer. So no guarantees. You have to decide on your own if this is sufficient.<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDKfPs6obYLbjA20vad_CO7S6m2FVCCIdoVaE5NEEeo1zqLKuL4D8JNdjVbqhxSB2P8ho-0gqEs1RTs5r49nguU_r2o2Dg4hjBshCTLhiCwEuwUDm3M46HmCXp8AafpsZ-TFURPvWTC6M/s1600/iOS+Simulator+Screen+Shot+29+Aug+2015+19.02.15.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDKfPs6obYLbjA20vad_CO7S6m2FVCCIdoVaE5NEEeo1zqLKuL4D8JNdjVbqhxSB2P8ho-0gqEs1RTs5r49nguU_r2o2Dg4hjBshCTLhiCwEuwUDm3M46HmCXp8AafpsZ-TFURPvWTC6M/s320/iOS+Simulator+Screen+Shot+29+Aug+2015+19.02.15.png" width="180" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">AdMob Cookie Usage</td></tr>
</tbody></table>
<br />
<a name='more'></a><br /><br />
<h4>
The Code snippet for showing the AdMob Cookie usage is simple:</h4>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// AdMob Cookie handling:</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// Warning: I'm not a lawyer. You have to decide on your own, if this is sufficient</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// Google gives more hints on: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"><a href="http://www.cookiechoices.org/">http://www.cookiechoices.org</a></span></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> adMobTitle = <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Cookie usage:"</span></div>
<div style="color: #b4261a; font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> adMobCookieText = </span>"We use device identifiers to personalise content and ads, to provide social media features and to analyse our traffic. We also share such identifiers and other information from your device with our social media, advertising and analytics partners."</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<span style="color: #0433ff;">let</span> userDefaults : <span style="color: #3495af;">NSUserDefaults</span> = <span style="color: #3495af;">NSUserDefaults</span>.<span style="color: #3495af;">standardUserDefaults</span>()</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> !userDefaults.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">boolForKey</span>(<span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"termsAccepted"</span>) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> alert = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertController</span>(title: adMobTitle, message: adMobCookieText, preferredStyle: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertControllerStyle</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Alert</span>)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> alert.</span>addAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>UIAlertAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(title: </span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"OK"</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, style: </span>UIAlertActionStyle<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>Default<span style="color: black; font-variant-ligatures: no-common-ligatures;">) { </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">_</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span></div>
<div style="font-family: Menlo; font-size: 11px;">
userDefaults.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">setBool</span>(<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span>, forKey: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"termsAccepted"</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
})</div>
<div style="font-family: Menlo; font-size: 11px;">
presentingViewController.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">presentViewController</span>(alert, animated: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span>, completion: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
I've also updated the sample code in my <a href="https://github.com/stfnjstn/iAdAdMobDemo">GitHub repository</a> from the <a href="http://stefansdevplayground.blogspot.de/2015/07/quick-tip-combine-iad-and-admob-ads-for.html">iAd & AdMob tutorial</a>.</div>
<div>
<br /></div>
<div>
Google gives more details and shows how to implement this in ObjectiveC at <a href="http://www.cookiechoices.org/">http://www.cookiechoices.org</a>. Further details about the Google AdMob SDK can be found <a href="https://developers.google.com/admob/ios/interstitial">here</a>.</div>
<br />
That's all for today.<br />
<br />
<div>
Cheers,<br />
Stefan</div>
<div style="text-align: center;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><a href="https://itunes.apple.com/us/app/yet-another-watch-puzzle-game/id997514879?ls=1&mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></span></span></span></div>
<br />
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com0tag:blogger.com,1999:blog-7769140974661448994.post-14632174519832997672015-07-10T06:14:00.003-07:002016-05-17T08:47:48.666-07:00Quick Tip: Combine iAd and AdMob Ads for iOS in SWIFT<script>
window.location="http://developerplayground.net/quick-tip-combine-iad-and-admob-ads-for-ios-in-swift/";
</script>
<h2>
Use iAd and AdMob ads in the same app</h2>
<div>
<div style="text-align: justify;">
Today I'll show how to use iAd together with AdMob ads. If you use the interstitial ads provided by Apples iAd frequently, you might have seen that the fill rate is not always 100 percent:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUKJslXxeuoSgnNn02RJZh8GSBtTF6r625jIiuxPI8VfZC1ANJEMBPt1KzvrgOXXhzHVv_rwyxLixTE-iJDd89rP1_M-OD2Irbhg16fhVI0kA_dwGhv1_9V1VqQBqgchYwtbBzd5gfswQ/s1600/Screen+Shot+2015-07-10+at+14.20.17.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="59" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUKJslXxeuoSgnNn02RJZh8GSBtTF6r625jIiuxPI8VfZC1ANJEMBPt1KzvrgOXXhzHVv_rwyxLixTE-iJDd89rP1_M-OD2Irbhg16fhVI0kA_dwGhv1_9V1VqQBqgchYwtbBzd5gfswQ/s320/Screen+Shot+2015-07-10+at+14.20.17.png" width="320" /></a></div>
<br />
<br />
One reason could be that there was just not enough time to load the new content. This tutorial shows how to improve the fill rate by requesting a Google AdMob ad in parallel. Depending on the availability, the iAd or the AdMob ad will be shown.<br />
<br /></div>
</div>
<a name='more'></a><br />
<h4>
Prerequisites for this tutorial:</h4>
<br />
<ul>
<li>You have a subscription for the Apple iAds program. For details please check my previous article about <a href="http://stefansdevplayground.blogspot.de/2015/02/how-to-implement-space-shooter-with.html">iAD integration</a>.</li>
<li>You are registered for the Google <a href="https://www.google.com/admob/">AdMob</a><span id="goog_559191462"></span><span id="goog_559191463"></span><a href="https://www.blogger.com/"></a> program</li>
<li>You have downloaded the Google <a href="https://developers.google.com/admob/ios/download">AdMob SDK for iOS</a></li>
</ul>
<br />
<br />
<br />
<br />
<h3>
1. Let's create a sample project:</h3>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1jtzDB964JCNsB3IPwk1vEFtj-XvpKf6j0gPQjzg28I9T-AKXIJ2t4xnAAFfdS60jyQEajB3yrGPzIwdWKlafaGadv_ozsysXCkA6BsnY3-3dczzF0PzIES0QsNeB3vrmAB3q5vOKlJk/s1600/Screen+Shot+2015-07-10+at+14.28.16.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="185" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1jtzDB964JCNsB3IPwk1vEFtj-XvpKf6j0gPQjzg28I9T-AKXIJ2t4xnAAFfdS60jyQEajB3yrGPzIwdWKlafaGadv_ozsysXCkA6BsnY3-3dczzF0PzIES0QsNeB3vrmAB3q5vOKlJk/s320/Screen+Shot+2015-07-10+at+14.28.16.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg84ZufzPSoeAw9e-svMVH70gzc1tYZjeR6PlUT0Q1mr_cgDxmFZqJdSQscvGdBxFkBJtb3h98WjiUZCIepF7cMZgNQVqta1_6gWWrhwmtfu5Iw_faT0bUADL8KJuyBPhBTVJEeXJAbNMw/s1600/Screen+Shot+2015-07-10+at+14.28.35.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="186" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg84ZufzPSoeAw9e-svMVH70gzc1tYZjeR6PlUT0Q1mr_cgDxmFZqJdSQscvGdBxFkBJtb3h98WjiUZCIepF7cMZgNQVqta1_6gWWrhwmtfu5Iw_faT0bUADL8KJuyBPhBTVJEeXJAbNMw/s320/Screen+Shot+2015-07-10+at+14.28.35.png" width="320" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<h3>
2. Ad the iAD framework to your project:</h3>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVuhR3HmmKSgmwMrV58PUzwm5_Vfbcxs78CjqCHf-SJ-EBRD0mPvjREVB2fOzIPiE98gz7wTUx0UMh792w80FJuh507LXyk8W205Ic01t0-TRVloNiBEkrwq4Gokh9Ms5_d06fZv76gNg/s1600/Screen+Shot+2015-07-08+at+21.57.04.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="160" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVuhR3HmmKSgmwMrV58PUzwm5_Vfbcxs78CjqCHf-SJ-EBRD0mPvjREVB2fOzIPiE98gz7wTUx0UMh792w80FJuh507LXyk8W205Ic01t0-TRVloNiBEkrwq4Gokh9Ms5_d06fZv76gNg/s320/Screen+Shot+2015-07-08+at+21.57.04.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggNbWRS093HGEwl_KUvr-VIMeGAyjFcGsPyc22KklCeUZQVIK9p75ExHFbXSTqsFBalY2z_txljpn7ypkVqi4jRFqD3KSqm7zgGNN2cqpFoRjU6xCBh5e9YCJJafhjnSoFtXA_9PMcJk4/s1600/Screen+Shot+2015-07-08+at+21.57.19.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggNbWRS093HGEwl_KUvr-VIMeGAyjFcGsPyc22KklCeUZQVIK9p75ExHFbXSTqsFBalY2z_txljpn7ypkVqi4jRFqD3KSqm7zgGNN2cqpFoRjU6xCBh5e9YCJJafhjnSoFtXA_9PMcJk4/s320/Screen+Shot+2015-07-08+at+21.57.19.png" width="274" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdtY0wTGA3lzMkptwftQHaJz9yec-OIVsNPAb5g_nPkRubanSr8k1yq8RHfWDthYJUlDwKdv0j3d4c7fH9anvAQTkXEjSH7ouCN_DFc8GZk8SIXqApLYXVoDuaNHRcd6Y-SVl7-Cou7-w/s1600/Screen+Shot+2015-07-08+at+21.57.29.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="141" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdtY0wTGA3lzMkptwftQHaJz9yec-OIVsNPAb5g_nPkRubanSr8k1yq8RHfWDthYJUlDwKdv0j3d4c7fH9anvAQTkXEjSH7ouCN_DFc8GZk8SIXqApLYXVoDuaNHRcd6Y-SVl7-Cou7-w/s320/Screen+Shot+2015-07-08+at+21.57.29.png" width="320" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<br />
<h3>
3. Ad the Google AdMob SDK to your project:</h3>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqnmalcmslNZc8hLssCPugxvQI9XJREujUPYUZZXmv4PjCFJywnCDJIIEQiZ36KQG1lAkBwgrKecGHKshh01qqo-VrMICh34Sdk1eO6dQ2uwLDGDUO0c0yCEhdpx6K_5F6-bjqFChz5zQ/s1600/Screen+Shot+2015-07-10+at+14.33.12.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqnmalcmslNZc8hLssCPugxvQI9XJREujUPYUZZXmv4PjCFJywnCDJIIEQiZ36KQG1lAkBwgrKecGHKshh01qqo-VrMICh34Sdk1eO6dQ2uwLDGDUO0c0yCEhdpx6K_5F6-bjqFChz5zQ/s1600/Screen+Shot+2015-07-10+at+14.33.12.png" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHjumntJIt6SFleXzZdIuHn8FXSAnqMdhyphenhyphenEthS2v2Czi43uK4bhTDoqevDoF94VzMzDQjOtb3tO8CDODE9cXF85aGF8RrMCBh22_epHfXTvSDsI1o3oyfuZ9qloTjaS5j-pxlOxTuTJuE/s1600/Screen+Shot+2015-07-10+at+14.33.51.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHjumntJIt6SFleXzZdIuHn8FXSAnqMdhyphenhyphenEthS2v2Czi43uK4bhTDoqevDoF94VzMzDQjOtb3tO8CDODE9cXF85aGF8RrMCBh22_epHfXTvSDsI1o3oyfuZ9qloTjaS5j-pxlOxTuTJuE/s320/Screen+Shot+2015-07-10+at+14.33.51.png" width="296" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghtqsGwLVZC895hhl1Zc6sLtcaHQtcEXTG6BOqnxcShhkUhBHXtwBeO2NqaUSdvHjmnZ0cCHKUjXt5WjQpc_96yFsdAF2CAiXoKnT02171KAsQ6p_w29mJ02cYEZbF5kukjMnCVaWUpTI/s1600/Screen+Shot+2015-07-10+at+14.34.03.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="53" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghtqsGwLVZC895hhl1Zc6sLtcaHQtcEXTG6BOqnxcShhkUhBHXtwBeO2NqaUSdvHjmnZ0cCHKUjXt5WjQpc_96yFsdAF2CAiXoKnT02171KAsQ6p_w29mJ02cYEZbF5kukjMnCVaWUpTI/s320/Screen+Shot+2015-07-10+at+14.34.03.png" width="320" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<h3>
4. Create the UI:</h3>
<h4>
Open the storyboard and add a button to the screen:</h4>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBUU5H85uxl-l665QsPmDmvRplvH1RHEz3V4WdiMskXcIZwQNzdsEz_IP6tA4Ulu9Yua5poC7YZs0X6qp4RA8zxTuIhhcpbMiOpjekGBCl58bIv0VFZua3PmkeBorqHbulkVovxrzuaok/s1600/Screen+Shot+2015-07-10+at+14.36.02.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="179" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBUU5H85uxl-l665QsPmDmvRplvH1RHEz3V4WdiMskXcIZwQNzdsEz_IP6tA4Ulu9Yua5poC7YZs0X6qp4RA8zxTuIhhcpbMiOpjekGBCl58bIv0VFZua3PmkeBorqHbulkVovxrzuaok/s320/Screen+Shot+2015-07-10+at+14.36.02.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<h4>
Use Autolayout to center the button on the screen:</h4>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3_5H9R6azZOJQCnNyji8nttranBkgjjoYLPBqMVHxFhEMm77vCfuXWqWLixL8HoH0hOsQ4Wk3cPXf8_gKy8S6lScJpYw0OpuFtiWMzm6i-MMBJt4pbLBMIVO6T1saVNGRfMFee7vyQwA/s1600/Screen+Shot+2015-07-10+at+14.36.21.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="235" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3_5H9R6azZOJQCnNyji8nttranBkgjjoYLPBqMVHxFhEMm77vCfuXWqWLixL8HoH0hOsQ4Wk3cPXf8_gKy8S6lScJpYw0OpuFtiWMzm6i-MMBJt4pbLBMIVO6T1saVNGRfMFee7vyQwA/s320/Screen+Shot+2015-07-10+at+14.36.21.png" width="320" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<h4>
Create an IBAction method to handle touch events for the button:</h4>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtynIS6DRFhMMrY8NrPNUv-Jq4PJ4Ye6o7YwCvjAU6aCN-zsv_PGRjXSwGTJ8_fKsAVlSXozKdHJ2Zfdvk4WQaewS2J8F28SexSNQxnu0uZuzYpPqYOt5E1o0VwHkpUPGckaChAA0W0zI/s1600/Screen+Shot+2015-07-10+at+14.41.05.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="117" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtynIS6DRFhMMrY8NrPNUv-Jq4PJ4Ye6o7YwCvjAU6aCN-zsv_PGRjXSwGTJ8_fKsAVlSXozKdHJ2Zfdvk4WQaewS2J8F28SexSNQxnu0uZuzYpPqYOt5E1o0VwHkpUPGckaChAA0W0zI/s320/Screen+Shot+2015-07-10+at+14.41.05.png" width="320" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<h3>
5. Create the AdHelper class </h3>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTA68lSkMOkarEF416XRK6-RiUMsQn3VkgD0TjlutSxl6Lpwxz2Q8WDYYF78U1Rf7PicUuubh_LL2oG-DleJsYYCHLN_AXpm2k92XKPQ5tPgJjWRnvKwyS1RyLD2j1Lx_JFhNDAciLDMQ/s1600/Screen+Shot+2015-07-10+at+14.51.05.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="59" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTA68lSkMOkarEF416XRK6-RiUMsQn3VkgD0TjlutSxl6Lpwxz2Q8WDYYF78U1Rf7PicUuubh_LL2oG-DleJsYYCHLN_AXpm2k92XKPQ5tPgJjWRnvKwyS1RyLD2j1Lx_JFhNDAciLDMQ/s320/Screen+Shot+2015-07-10+at+14.51.05.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhr_A9q7zg_3c2_pPmlM8DbAgLa4GSxEpak8mnNs4StqLKxcaf7ppCaEpH6iFd7S8oaQ983a_7N3xLw3_YU5pfl6YG1VpyuyK4e1Zijdq5tuWTchxW4y9usFde7Fkzv05of6UgBjqDNuuk/s1600/Screen+Shot+2015-07-10+at+14.51.14.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="187" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhr_A9q7zg_3c2_pPmlM8DbAgLa4GSxEpak8mnNs4StqLKxcaf7ppCaEpH6iFd7S8oaQ983a_7N3xLw3_YU5pfl6YG1VpyuyK4e1Zijdq5tuWTchxW4y9usFde7Fkzv05of6UgBjqDNuuk/s320/Screen+Shot+2015-07-10+at+14.51.14.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4YOSqhF6ZyZCEbmjczmDb8bQaeK8s5I6Zpgw16MDcVwUEIxahYWb6xTSarqOTqaevAGMoQxXw7RDzoBqZNICh1UAbfm72wkT1r-C8xGtBU8R_sWof2rBycuBO3DMp3iixmbEVlilVNdY/s1600/Screen+Shot+2015-07-10+at+14.51.26.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="189" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj4YOSqhF6ZyZCEbmjczmDb8bQaeK8s5I6Zpgw16MDcVwUEIxahYWb6xTSarqOTqaevAGMoQxXw7RDzoBqZNICh1UAbfm72wkT1r-C8xGtBU8R_sWof2rBycuBO3DMp3iixmbEVlilVNdY/s320/Screen+Shot+2015-07-10+at+14.51.26.png" width="320" /></a></div>
<div>
<br /></div>
<h4>
Paste this code snippet into the newly created file:</h4>
<div>
<br /></div>
<div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// Helper class to show iAd and Google AdMob interstitial ads. Default is the iAd.</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// If a new iAD add is not available an Google AdMob ad will be shown</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">import</span> UIKit</div>
<div style="color: #0433ff; font-family: Menlo; font-size: 11px;">
import<span style="color: black; font-variant-ligatures: no-common-ligatures;"> iAd</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">import</span> GoogleMobileAds</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">class</span> AdHelper: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">NSObject</span> {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">private</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> iterationsTillPresentAd = 0 </span>// Can be used to show an ad only after a fixed number of iterations</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">private</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> adMobKey = </span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">""</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Stores the key provided by google</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">private</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> counter = 0</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">private</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> adMobInterstitial: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">GADInterstitial</span>!</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Initialize iAd and AdMob interstitials ads</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">init</span>(presentingViewController: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIViewController</span>, googleAdMobKey: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">String</span>, iterationsTillPresentInterstitialAd: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Int</span>) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">iterationsTillPresentAd</span> = iterationsTillPresentInterstitialAd</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">adMobKey</span> = googleAdMobKey</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>adMobInterstitial<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>GADInterstitial<span style="color: black; font-variant-ligatures: no-common-ligatures;">(adUnitID: </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>adMobKey<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> presentingViewController.</span>interstitialPresentationPolicy<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>ADInterstitialPresentationPolicy<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>Manual</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Present the interstitial ads</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> showAds(presentingViewController: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIViewController</span>) {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Check if ad should be shown</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">counter</span>++</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>counter<span style="color: black; font-variant-ligatures: no-common-ligatures;"> >= </span>iterationsTillPresentAd<span style="color: black; font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Try if iAd ad is available</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> presentingViewController.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">requestInterstitialAdPresentation</span>() {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">counter</span> = 0</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> presentingViewController.</span>interstitialPresentationPolicy<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>ADInterstitialPresentationPolicy<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>None</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// The ad was used. Prefetch the next one</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">preloadIAdInterstitial</span>(presentingViewController)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Try if the AdMob is available</div>
<div style="font-family: Menlo; font-size: 11px;">
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">adMobInterstitial</span> == <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span> {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// In case the disableAd was called</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>adMobInterstitial<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>GADInterstitial<span style="color: black; font-variant-ligatures: no-common-ligatures;">(adUnitID: </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>adMobKey<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Present the AdMob ad, if available</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> (<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">adMobInterstitial</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">isReady</span>) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">counter</span> = 0</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">adMobInterstitial</span>!.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">presentFromRootViewController</span>(presentingViewController)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">adMobInterstitial</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">GADInterstitial</span>(adUnitID: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">adMobKey</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Prefetch the next ads</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">preloadIAdInterstitial</span>(presentingViewController)</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>preloadAdMobInterstitial<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Disable ads</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> disableAd(presentingViewController: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIViewController</span>) {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> presentingViewController.</span>interstitialPresentationPolicy<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>ADInterstitialPresentationPolicy<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>None</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>adMobInterstitial<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Prefetch AdMob ads</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">private</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> preloadAdMobInterstitial() {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> request = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">GADRequest</span>()</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> request.</span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">testDevices</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> = [</span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"kGADSimulatorID"</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">] </span>// Needed to show Ads in the simulator</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>adMobInterstitial<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>loadRequest<span style="color: black; font-variant-ligatures: no-common-ligatures;">(request)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Prefetch iAd ads</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">private</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> preloadIAdInterstitial(presentingViewController: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIViewController</span>) {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> presentingViewController.</span>interstitialPresentationPolicy<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>ADInterstitialPresentationPolicy<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>Manual</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>UIViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>prepareInterstitialAds<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
</div>
<div>
<br /></div>
<h3>
6. Call the AdHelper class</h3>
<h4>
Open <span style="font-family: Courier New, Courier, monospace;">ViewController.swift</span>:</h4>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgaDrMEAUkgoXXQiHcEZwmL2FwljSlzQPoko0svBeeAZOHaf5GdpRd6L9CyZtACkOX9ey5hqoooKBikEcharXU1GamU5qVtoGqvDvOZMOQOrajSqiZK8zKyx6QnlUwrYPuJ9eb9VEfdH4/s1600/Screen+Shot+2015-07-10+at+15.01.41.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="151" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgaDrMEAUkgoXXQiHcEZwmL2FwljSlzQPoko0svBeeAZOHaf5GdpRd6L9CyZtACkOX9ey5hqoooKBikEcharXU1GamU5qVtoGqvDvOZMOQOrajSqiZK8zKyx6QnlUwrYPuJ9eb9VEfdH4/s320/Screen+Shot+2015-07-10+at+15.01.41.png" width="320" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<h4>
Add a property which hold the instance of <span style="font-family: Courier New, Courier, monospace;">AdHelper</span> and initialise it in the <span style="font-family: Courier New, Courier, monospace;">ViewDidLoad</span> method: </h4>
<div>
<br /></div>
<div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">import</span> UIKit</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">class</span> ViewController: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIViewController</span> {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="background-color: yellow;"><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> adHelper: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">AdHelper</span>!</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> viewDidLoad() {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">super</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">viewDidLoad</span>()</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Do any additional setup after loading the view, typically from a nib.</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="background-color: yellow;"> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">adHelper</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">AdHelper</span>(presentingViewController: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>, googleAdMobKey: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"PASTE YOU ADMOB ID HERE"</span>, iterationsTillPresentInterstitialAd: 1)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
</div>
<div>
<br /></div>
<div>
<br /></div>
<h4>
Enter the code to call the ad each time the button is pressed:</h4>
<div>
<br /></div>
<div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">@IBAction</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> showAd(sender: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">AnyObject</span>) {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> <span style="background-color: yellow;"> </span></span><span style="background-color: yellow;">adHelper<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>showAds<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
</div>
<div>
<br /></div>
<div>
<br /></div>
<h4>
Now you can test this on a device:</h4>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVy97hZ7XRTUiDJhrSKMwo60SbDCV6c7QWXmX-72jm1OucBjFb1yVXkVa2gmBnedW3OYt5dy9qxYcml9BHVH-RgtwyVPPRoHiziQDVZ6thAdKdTOBrBTyVcuhywGHyEPnBxFe20oL0xls/s1600/Screen+Shot+2015-07-10+at+15.14.21.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="181" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVy97hZ7XRTUiDJhrSKMwo60SbDCV6c7QWXmX-72jm1OucBjFb1yVXkVa2gmBnedW3OYt5dy9qxYcml9BHVH-RgtwyVPPRoHiziQDVZ6thAdKdTOBrBTyVcuhywGHyEPnBxFe20oL0xls/s320/Screen+Shot+2015-07-10+at+15.14.21.png" width="320" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<br />
<br />
That's all for today.<br />
<br />
You can download the Sample from my GitHub <a href="https://github.com/stfnjstn/iAdAdMobDemo">repository</a>. Further details about the Google AdMob SDK can be found <a href="https://developers.google.com/admob/ios/interstitial">here</a>.<br />
<br />
<div>
Cheers,<br />
Stefan</div>
<div style="text-align: center;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><a href="https://itunes.apple.com/us/app/yet-another-watch-puzzle-game/id997514879?ls=1&mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></span></span></span></div>
<br />
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com0tag:blogger.com,1999:blog-7769140974661448994.post-18857946583647991672015-07-08T13:28:00.002-07:002016-05-17T08:48:32.691-07:00Quick Tip: Integrate iAd banners in your iOS App with two lines of code<script>
window.location="http://developerplayground.net/quick-tip-integrate-iad-banner-ads-to-your-ios-app-with-two-lines-of-code/";
</script>
<h2>
The easiest way to add an iAD banner to your app</h2>
<div>
<br /></div>
<div>
<div style="text-align: justify;">
Today I'll show a very simple way to integrate an ad banner to your iOS app. Most tutorials (including mine) explain the complex version by implementing the <span style="font-family: Courier New, Courier, monospace;">ADBannerViewDelegate</span> protocol. If you don't care about error handling, animations, positioning or the maximum number of allowed banner instances (10!): There is a much easier way which requires only two lines of code in SWIFT.</div>
</div>
<a name='more'></a><br />
For details how to subscribe for the Apple iAds program, please check my previous article about <a href="http://stefansdevplayground.blogspot.de/2015/02/how-to-implement-space-shooter-with.html">iAD integration</a>.<br />
<br />
<h3>
1. Create the sample project:</h3>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1HuKPhaPIsxD3QfRgR9LpU-UKwsqYqboK2JuniaxKh_o3rqvCzroIHlhAdwK_-G2xaZZHEJ7UfR9p9__Nt2C5jvOkjRMhU3mCBWLZ8I_PbneRF1ISNJa1Vwo1x9MQd9-hGYwp9ywZg9Q/s1600/Screen+Shot+2015-07-08+at+21.55.35.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="189" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1HuKPhaPIsxD3QfRgR9LpU-UKwsqYqboK2JuniaxKh_o3rqvCzroIHlhAdwK_-G2xaZZHEJ7UfR9p9__Nt2C5jvOkjRMhU3mCBWLZ8I_PbneRF1ISNJa1Vwo1x9MQd9-hGYwp9ywZg9Q/s320/Screen+Shot+2015-07-08+at+21.55.35.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitdPlSD0NCjMWdcuPuP3uPXCl0HhWiTbNBXr_tNR5Rht2Y4-yduzTRrHjPHuSSfC5ii5kBPElIKarcyiF-EOySRCrXLhVFw_Bgx7WPwJZREytuYM967XCEmJkcXWR2rZCcI0NPWzSeDbw/s1600/Screen+Shot+2015-07-08+at+21.55.52.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="189" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitdPlSD0NCjMWdcuPuP3uPXCl0HhWiTbNBXr_tNR5Rht2Y4-yduzTRrHjPHuSSfC5ii5kBPElIKarcyiF-EOySRCrXLhVFw_Bgx7WPwJZREytuYM967XCEmJkcXWR2rZCcI0NPWzSeDbw/s320/Screen+Shot+2015-07-08+at+21.55.52.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<h3>
2. Ad the iAD framework to your project:</h3>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjO6uhB4-l2_gfN7PUuZgR-P4tdnYbNjc_1UrwL3hyphenhyphen9nrX7JCSHzrq28slhy2d-BxIp1O5EbQEUBDNf8QWezhPlIp2FCXQCn4-T9FkE5bAU3t5lq-hiAUgCZokpCza4HnR1CIT1PjpvAtw/s1600/Screen+Shot+2015-07-08+at+21.57.04.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="160" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjO6uhB4-l2_gfN7PUuZgR-P4tdnYbNjc_1UrwL3hyphenhyphen9nrX7JCSHzrq28slhy2d-BxIp1O5EbQEUBDNf8QWezhPlIp2FCXQCn4-T9FkE5bAU3t5lq-hiAUgCZokpCza4HnR1CIT1PjpvAtw/s320/Screen+Shot+2015-07-08+at+21.57.04.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3fzXs1fSOKVCa5vqWwS5AHdUD3rIhXLgv4_ZE6szwO1GRVH641E_eSg5B5je7_6ElXemYUr0LnewpzhE_vWWUcZ5NhbLPopyCoigxkWTfbKnsJiRfjbnF-dD2WnmWjAutkZ4hk23riOc/s1600/Screen+Shot+2015-07-08+at+21.57.19.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3fzXs1fSOKVCa5vqWwS5AHdUD3rIhXLgv4_ZE6szwO1GRVH641E_eSg5B5je7_6ElXemYUr0LnewpzhE_vWWUcZ5NhbLPopyCoigxkWTfbKnsJiRfjbnF-dD2WnmWjAutkZ4hk23riOc/s320/Screen+Shot+2015-07-08+at+21.57.19.png" width="274" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBqorsSm35A7NnX6yi5N8h7m2ZKx0eIS1itufFpJKhe7oVNOzr5RMCGoO25TO_h24frpiCKmOneubNymwdFmnaZq3BIsZNuV296pFJx-CRD4PzFZNS0pjdpNGMU3iB-VV9tpZ-xgMshsM/s1600/Screen+Shot+2015-07-08+at+21.57.29.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="141" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBqorsSm35A7NnX6yi5N8h7m2ZKx0eIS1itufFpJKhe7oVNOzr5RMCGoO25TO_h24frpiCKmOneubNymwdFmnaZq3BIsZNuV296pFJx-CRD4PzFZNS0pjdpNGMU3iB-VV9tpZ-xgMshsM/s320/Screen+Shot+2015-07-08+at+21.57.29.png" width="320" /></a></div>
<br />
<br />
<h3>
3. Add these two lines of code to your <span style="font-family: Courier New, Courier, monospace;">ViewController</span> class:</h3>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">import</span> UIKit</div>
<div style="color: #0433ff; font-family: Menlo; font-size: 11px;">
<span style="background-color: yellow;">import<span style="color: black; font-variant-ligatures: no-common-ligatures;"> iAd</span></span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">class</span> ViewController: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIViewController</span> {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> viewDidLoad() {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">super</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">viewDidLoad</span>()</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Do any additional setup after loading the view, typically from a nib.</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="background-color: yellow;"><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>canDisplayBannerAds<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span></span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
Thats all! Apple takes care about ads loading and error handling and decides, if the ad is ready to show. Apple also handles orientation changes and the different sizes for iPhone and iPad.<br />
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPIhZOGYZLPbGEJ8oeeGvSRA1GXV7AITZB-GcamFkBCff4HTwE2qDbI6RMWojD3Y77oOrCtOtqybUs4VDmGTZTdB4WOCj2JNQlEv6D52CuDvK6X2CoGlDOk5D1D2UJxOVFRLQebsce1ow/s1600/Screen+Shot+2015-07-08+at+22.01.17.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="179" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPIhZOGYZLPbGEJ8oeeGvSRA1GXV7AITZB-GcamFkBCff4HTwE2qDbI6RMWojD3Y77oOrCtOtqybUs4VDmGTZTdB4WOCj2JNQlEv6D52CuDvK6X2CoGlDOk5D1D2UJxOVFRLQebsce1ow/s320/Screen+Shot+2015-07-08+at+22.01.17.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhra_2_uwHxddR6KoHX6EmBHhTahBdiKoVMVwnZjQW1RfxThyj9fSt-OJe8oWfQG1i5b76OOsKuU3spkO-iG856Z2Y9CwPKTJHa-miph9NqvPyAir5R2VLKiU96Ya33UiefKwnnsfpyNic/s1600/Screen+Shot+2015-07-08+at+22.01.36.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhra_2_uwHxddR6KoHX6EmBHhTahBdiKoVMVwnZjQW1RfxThyj9fSt-OJe8oWfQG1i5b76OOsKuU3spkO-iG856Z2Y9CwPKTJHa-miph9NqvPyAir5R2VLKiU96Ya33UiefKwnnsfpyNic/s320/Screen+Shot+2015-07-08+at+22.01.36.png" width="179" /></a></div>
<div>
<br /></div>
<div>
That's all for today.<br />
<br />
Cheers,<br />
Stefan</div>
<div style="text-align: center;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></span></span></span></div>
<br />
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com1tag:blogger.com,1999:blog-7769140974661448994.post-11487384512428520122015-07-08T10:45:00.003-07:002015-07-08T11:50:41.742-07:00I've published an update for my (Watch) puzzle game in the Apple App Store<h3 style="text-align: center;">
Yet Another (Watch) Puzzle Game:</h3>
<div class="separator" style="clear: both; text-align: center;">
Simple puzzle game inspired by the <a href="https://en.wikipedia.org/wiki/15_puzzle">15 Puzzle</a> for Apple Watch, iPhone and iPad.</div>
<div class="separator" style="clear: both; text-align: center;">
New in version 1.3: Puzzle mode - slide image tiles instead of numbers.</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://developerplayground.net/wp-content/uploads/2015/04/iOS-Simulator-Screen-Shot-Apple-Watch-21-May-2015-22.18.26.png"><img alt="iOS Simulator Screen Shot - Apple Watch 21 May 2015 22.18.26" class="aligncenter wp-image-730" src="http://developerplayground.net/wp-content/uploads/2015/04/iOS-Simulator-Screen-Shot-Apple-Watch-21-May-2015-22.18.26-240x300.png" height="150" width="120" />
</a><a href="http://developerplayground.net/wp-content/uploads/2015/04/iOS-Simulator-Screen-Shot-Apple-Watch-21-May-2015-22.20.20.png"><img alt="iOS Simulator Screen Shot - Apple Watch 21 May 2015 22.20.20" class="aligncenter wp-image-731" src="http://developerplayground.net/wp-content/uploads/2015/04/iOS-Simulator-Screen-Shot-Apple-Watch-21-May-2015-22.20.20-240x300.png" height="150" width="120" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<img alt="iOS Simulator Screen Shot - Apple Watch 21 May 2015 22.36.29" class="aligncenter wp-image-732" src="http://developerplayground.net/wp-content/uploads/2015/04/iOS-Simulator-Screen-Shot-Apple-Watch-21-May-2015-22.36.29-240x300.png" height="150" width="120" /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpO_gY2xbH95L6_5HLLlIsoG4AwK5YY99NNhh_GMKxJr9pn4ufqbt3P2cKQplum1NvBayUd9j96Upb3CYIMc0UId8XOhul2gySLO_2ZvKyMy9J5dYjofOhO7zDhzc37Lo1JBPUmEOcYhI/s1600/iOS+Simulator+Screen+Shot+23+Jun+2015+21.31.50.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="213" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpO_gY2xbH95L6_5HLLlIsoG4AwK5YY99NNhh_GMKxJr9pn4ufqbt3P2cKQplum1NvBayUd9j96Upb3CYIMc0UId8XOhul2gySLO_2ZvKyMy9J5dYjofOhO7zDhzc37Lo1JBPUmEOcYhI/s320/iOS+Simulator+Screen+Shot+23+Jun+2015+21.31.50.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyTQDre01iKWr8mIs9rbeiWbgoZcuL0Gyf2S_KXHPqkjtxRN-asbWO8pJDfdi_eT5qIC5HyjLZcih6GOuGfnplWf6EjyGYbXuM6QxsrLTaOid0NFmPtdX_I-vVMCqYVj_plEYyRYPXPcE/s1600/iOS+Simulator+Screen+Shot+01+Jul+2015+23.49.38.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="179" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyTQDre01iKWr8mIs9rbeiWbgoZcuL0Gyf2S_KXHPqkjtxRN-asbWO8pJDfdi_eT5qIC5HyjLZcih6GOuGfnplWf6EjyGYbXuM6QxsrLTaOid0NFmPtdX_I-vVMCqYVj_plEYyRYPXPcE/s320/iOS+Simulator+Screen+Shot+01+Jul+2015+23.49.38.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://itunes.apple.com/us/app/yet-another-watch-puzzle-game/id997514879?ls=1&mt=8"><img alt="AppStore" class=" size-full wp-image-152 aligncenter" src="http://developerplayground.net/wp-content/uploads/2015/04/AppStore1.png" height="40" width="135" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<h3 style="text-align: center;">
<a href="http://developerplayground.net/wp-content/uploads/2015/04/iOS-Simulator-Screen-Shot-08-Feb-2015-22.43.20.png">
</a></h3>
Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com0tag:blogger.com,1999:blog-7769140974661448994.post-17027298876710115562015-06-13T16:36:00.004-07:002015-07-08T11:51:39.878-07:00Quick Tip: Implement fullscreen (interstitial) Ads for iOS in SWIFT<h2>
How to implement an Interstitial Ad combined with a counter</h2>
<div>
<div style="text-align: justify;">
Today I'll show how to implement a often used pattern in free games: Usually you can play free games a certain time, till a fullscreen ad is shown. For example after each third game over an ad is shown. Implementing this behaviour for the iOS platform requires only few lines of code in SWIFT.</div>
</div>
<a name='more'></a><br />
For details how to subscribe for the Apple iAds program, please check my previous article about <a href="http://stefansdevplayground.blogspot.de/2015/02/how-to-implement-space-shooter-with.html">iAD integration</a>.<br />
<br />
<h3>
1. Create the sample project:</h3>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiRxO9X3Ivn9BYttTG2QqeZELeGiPcUbz1fDWhzZjKtgowd990JFopKY5-3bjfY5jjbo1FcDc8ZsK06su0QsjPJi2-KB168IxeU4EzHzxNvzrYcpvdGAPwdKRAldMFP_36BRF9Uwk1-8m8/s1600/iAD1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="188" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiRxO9X3Ivn9BYttTG2QqeZELeGiPcUbz1fDWhzZjKtgowd990JFopKY5-3bjfY5jjbo1FcDc8ZsK06su0QsjPJi2-KB168IxeU4EzHzxNvzrYcpvdGAPwdKRAldMFP_36BRF9Uwk1-8m8/s320/iAD1.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhdPyYI820VSUHHJN-MFPqidJt4UY2GfWUHJnDs_9W3m8vKCT1hLk_rpHBwY5UR-78Czug5gSe7oORDHG_aeLDOR2jWOldLWXcwNuJqpoVaiSg8ylqMPk0RQWkWHNNbDRscQIw3d5P636o/s1600/iAD2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="189" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhdPyYI820VSUHHJN-MFPqidJt4UY2GfWUHJnDs_9W3m8vKCT1hLk_rpHBwY5UR-78Czug5gSe7oORDHG_aeLDOR2jWOldLWXcwNuJqpoVaiSg8ylqMPk0RQWkWHNNbDRscQIw3d5P636o/s320/iAD2.png" width="320" /></a></div>
<br />
<h3>
2. Create the sample program flow:</h3>
Add a new <span style="font-family: Courier New, Courier, monospace;">UIViewController</span> class to your project and name it <span style="font-family: Courier New, Courier, monospace;">TargetViewController</span>:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirpZgNsW-zWcsF49Jjd-H9yubjkt-AlV7zN_IfjRM_Hvfd76YndTQ513lJBZTt4R_uaJXk-KrgkESVcGDaqUNdpw4adYk42qpwNjI224ybVmYFGjg4YLSagFWZtH0WS7xREG4fEDwfDlw/s1600/iAD3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="133" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirpZgNsW-zWcsF49Jjd-H9yubjkt-AlV7zN_IfjRM_Hvfd76YndTQ513lJBZTt4R_uaJXk-KrgkESVcGDaqUNdpw4adYk42qpwNjI224ybVmYFGjg4YLSagFWZtH0WS7xREG4fEDwfDlw/s320/iAD3.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1Bd4UuXWMLb2eoNj4wN2nXSaQH02JEh32c9qI6tGVHd0i40Mg4zIBqf1XcR5J3oIIYUd-DE-c1kX1mPm_8FH20EwtLUE3wJR28Caazy5CKUpY_FDUjVQpNxlkaSRM0LI-Jz6dUKqJVsQ/s1600/iAD4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="187" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1Bd4UuXWMLb2eoNj4wN2nXSaQH02JEh32c9qI6tGVHd0i40Mg4zIBqf1XcR5J3oIIYUd-DE-c1kX1mPm_8FH20EwtLUE3wJR28Caazy5CKUpY_FDUjVQpNxlkaSRM0LI-Jz6dUKqJVsQ/s320/iAD4.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjX6F40ItTTeD1fCWw5VY-H3ua2DYepyInAwEJB-vkrcvoQEi0ceB4frqxSkyG2_acTQ1Yii___g-8WlmXlEF_u-0yEdrIe97T3k2Sa_Qya06rTY8N06KCIx34z3mTZB23jOsgxUceCXU/s1600/iAD5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="190" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjX6F40ItTTeD1fCWw5VY-H3ua2DYepyInAwEJB-vkrcvoQEi0ceB4frqxSkyG2_acTQ1Yii___g-8WlmXlEF_u-0yEdrIe97T3k2Sa_Qya06rTY8N06KCIx34z3mTZB23jOsgxUceCXU/s320/iAD5.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuawVeFDcoxK8-ZjduQ1jeQKyMqkzpQRrKq3oXFKwucMzvITWfooXkiz5-XFNNFdVku9fCr_rVvhcacULcGojpPNVY68hCgt2fN4uZsJMt-B5MH38JhKy4goQNudtgA0TcudZzj332Dlk/s1600/iAD6.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuawVeFDcoxK8-ZjduQ1jeQKyMqkzpQRrKq3oXFKwucMzvITWfooXkiz5-XFNNFdVku9fCr_rVvhcacULcGojpPNVY68hCgt2fN4uZsJMt-B5MH38JhKy4goQNudtgA0TcudZzj332Dlk/s1600/iAD6.png" /></a></div>
<br />
<br />
Place a new <span style="font-family: Courier New, Courier, monospace;">UIViewController</span> on the <span style="font-family: Courier New, Courier, monospace;">Storyboard </span>and add a button to each ViewController<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgK4v4KOI0XC5Dh5NiQaxt4nRTjoW2EmwY9nUXH9lf9Hs7trvB5SE-o5lRJGOdPIE5HCE9gzfmkFWq5P2SkaD6SVD38h0xB5evewspRedwa25OSQJrmlEKbXljUt_fovIQec8_4uAqz68w/s1600/iAd7.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="148" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgK4v4KOI0XC5Dh5NiQaxt4nRTjoW2EmwY9nUXH9lf9Hs7trvB5SE-o5lRJGOdPIE5HCE9gzfmkFWq5P2SkaD6SVD38h0xB5evewspRedwa25OSQJrmlEKbXljUt_fovIQec8_4uAqz68w/s320/iAd7.png" width="320" /></a></div>
<br />
Add constraints to the buttons to centre them on the screen for each form factor, orientation and resolution:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOdQxZrSGYo9woma6cOnC5_UxMjdQcAJna_2nkg_PRbQZKTL9qk4bwb6LFk_tP1xAY17zDwlFFJfW8rcqYXanaD9aLc_ZQFhjdrNV1EOGG3Fqif62L4aLyv1iCzPrOo6yRYP0xvt9CQio/s1600/iAD8.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="288" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOdQxZrSGYo9woma6cOnC5_UxMjdQcAJna_2nkg_PRbQZKTL9qk4bwb6LFk_tP1xAY17zDwlFFJfW8rcqYXanaD9aLc_ZQFhjdrNV1EOGG3Fqif62L4aLyv1iCzPrOo6yRYP0xvt9CQio/s320/iAD8.png" width="320" /></a></div>
<br />
<br />
Change type of the new view controller to <span style="font-family: Courier New, Courier, monospace;">TargetViewController</span>:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEieHy16oE-6-XvkP67LmsbcnAhxhDSBE2fqj3G-TEC2jVaYuJIszQF48QzxXQJBTTMYhMfVsw9-Q3P1KhWOQ6iSe6ZHJSSX1vhlV0At06gAQyhNDuqDRKO3SFaO02UrxdKRkDANecGOflU/s1600/iAD9.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="63" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEieHy16oE-6-XvkP67LmsbcnAhxhDSBE2fqj3G-TEC2jVaYuJIszQF48QzxXQJBTTMYhMfVsw9-Q3P1KhWOQ6iSe6ZHJSSX1vhlV0At06gAQyhNDuqDRKO3SFaO02UrxdKRkDANecGOflU/s320/iAD9.png" width="320" /></a></div>
<br />
<br />
Add a <span style="font-family: Courier New, Courier, monospace;">unwindFromTargetViewController</span> method to your <span style="font-family: Courier New, Courier, monospace;">ViewController </span>class:<br />
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">import</span> UIKit</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">class</span> ViewController: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIViewController</span> {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> viewDidLoad() {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">super</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">viewDidLoad</span>()</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Do any additional setup after loading the view, typically from a nib.</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> didReceiveMemoryWarning() {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">super</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>didReceiveMemoryWarning<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Dispose of any resources that can be recreated.</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="background-color: yellow;"> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">@IBAction</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> unwindFromTargetViewController(segue: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIStoryboardSegue</span>) {</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<span style="background-color: yellow;"> </span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="background-color: yellow;"> }</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<b>Now implement the navigation between the ViewControllers:</b><br />
<br />
Control Drag the first button, move it to <span style="font-family: Courier New, Courier, monospace;">TargetViewController</span> and create a segue to implement the navigation from <span style="font-family: Courier New, Courier, monospace;">ViewController</span> to <span style="font-family: Courier New, Courier, monospace;">TargetViewController</span>:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhN0hKBm30Ss2-ZDHWUrUod9201AZjNmbg1t-YJYUH7_9xNeGjOS11_kwEwV8YVVNRsDU1BKetihIR3ilY_hWLDlHUVOvvrBQX4dIfpRpV-GAGjNbrplx-nDMG4izn3u6j3kesgp6PeoXQ/s1600/iAD10.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="152" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhN0hKBm30Ss2-ZDHWUrUod9201AZjNmbg1t-YJYUH7_9xNeGjOS11_kwEwV8YVVNRsDU1BKetihIR3ilY_hWLDlHUVOvvrBQX4dIfpRpV-GAGjNbrplx-nDMG4izn3u6j3kesgp6PeoXQ/s320/iAD10.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
Control Drag the button in <span style="font-family: Courier New, Courier, monospace;">TargetViewController</span> and move the mouse pointer to the <span style="font-family: Courier New, Courier, monospace;">Exit</span> box of <span style="font-family: Courier New, Courier, monospace;">ViewController:</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAC2V7YE3SKycc1WWbTGXuwmdE7RX35zh-AimcjrPaNIjOmDGVbhIe2rc6rXDsaqfxdBR1Z_Y_MDFvzPgnaI8xWNCYTPXWDyhPmk2Ie7Ag3WiY9PI2_yRe7pd3tRIOJei_eeKrCDgUuC0/s1600/iAD11.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="161" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAC2V7YE3SKycc1WWbTGXuwmdE7RX35zh-AimcjrPaNIjOmDGVbhIe2rc6rXDsaqfxdBR1Z_Y_MDFvzPgnaI8xWNCYTPXWDyhPmk2Ie7Ag3WiY9PI2_yRe7pd3tRIOJei_eeKrCDgUuC0/s320/iAD11.png" width="320" /></a></div>
<br />
Select the <span style="font-family: Courier New, Courier, monospace;">unwindFromTargetViewController</span> method:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdNCyl0YxUJwiV8nl9qCOu9-290Cb135ta7hXoOtvkEgXBS6IMKKkHjcPyv1ullzHDaq2QxwsSUQCF7mpzXt_OMZCp8ZTLnAVHCpCL1dg6ZZiQTA4qezC2fGJGwxuiEtAS9ptO_EQwQbA/s1600/iAD12.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="299" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdNCyl0YxUJwiV8nl9qCOu9-290Cb135ta7hXoOtvkEgXBS6IMKKkHjcPyv1ullzHDaq2QxwsSUQCF7mpzXt_OMZCp8ZTLnAVHCpCL1dg6ZZiQTA4qezC2fGJGwxuiEtAS9ptO_EQwQbA/s320/iAD12.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
As a result you see a segue under each ViewController:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-75WKw6IqR4kUtEAaLZhoUWr7en_rAB1Q1N6pKN2mbMQkt9VVvsOTSCcEHJ9NxKjviiFlnfpElh_LQNcHVQZjdYhDnerN9cIAfi53BH86zTgzgdE72jA8oqTBtr9whx0-LFD53PG_RWA/s1600/iAD13.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-75WKw6IqR4kUtEAaLZhoUWr7en_rAB1Q1N6pKN2mbMQkt9VVvsOTSCcEHJ9NxKjviiFlnfpElh_LQNcHVQZjdYhDnerN9cIAfi53BH86zTgzgdE72jA8oqTBtr9whx0-LFD53PG_RWA/s320/iAD13.png" width="304" /></a></div>
<br />
More on segues in my previous tutorial about <a href="http://stefansdevplayground.blogspot.de/2014/02/howto-add-view-controllers-to.html">View Navigation</a>.<br />
<br />
<br />
<h3>
3. Implement the iAd Integration</h3>
If you start the project you can navigate from the start screen to the second screen and back. Now I'll implement the logic to show a fullscreen (<span style="font-family: Courier New, Courier, monospace;">interstitial)</span> ad after each third navigation to the second screen.<br />
<h4>
Add the iAD framework to your project: </h4>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZBZ1ipNsySMYEDDNxZBU83hHZPwXN72uhplvXvcV_TKn0qBTpbBTlDW_dYlpHTgNuEoBPtve1ik-e_Qqhwwr47X9yPdTKOn-ZbQ8uEKAB4Wk_F8Q7Cf3pjJWLjFMy53e9KjO2snQKnHU/s1600/iAD14.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="224" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZBZ1ipNsySMYEDDNxZBU83hHZPwXN72uhplvXvcV_TKn0qBTpbBTlDW_dYlpHTgNuEoBPtve1ik-e_Qqhwwr47X9yPdTKOn-ZbQ8uEKAB4Wk_F8Q7Cf3pjJWLjFMy53e9KjO2snQKnHU/s320/iAD14.png" width="320" /></a></div>
<br />
<h4>
Open <span style="font-family: Courier New, Courier, monospace;">ViewController.swift</span>:</h4>
At the top import the iAD framework and define a global counter:<br />
<br />
<div style="color: #0433ff; font-family: Menlo; font-size: 11px;">
import UIKit<br />
<span style="background-color: yellow;">import<span style="color: black; font-variant-ligatures: no-common-ligatures;"> iAd</span></span><br />
<div style="min-height: 13px;">
<span style="background-color: yellow;"><br /></span></div>
<span style="background-color: yellow;">var counter = 0</span><br />
<div style="min-height: 13px;">
<br /></div>
<br />
class ViewController: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIViewController</span> {<br />
<div>
<br /></div>
<br /></div>
Add this code to the <span style="font-family: Courier New, Courier, monospace;">viewDidLoad</span> method to initialise the ads:<br />
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> viewDidLoad() {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">super</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">viewDidLoad</span>()</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Do any additional setup after loading the view, typically from a nib.</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Initialize the Ad</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="background-color: yellow;">UIViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>prepareInterstitialAds<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></span></div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
Implement the logic to open the ad after each third third navigation to the second screen inside of the <span style="font-family: Courier New, Courier, monospace;">prepareForSegue</span> method:<br />
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> prepareForSegue(segue: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIStoryboardSegue</span>, sender: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">AnyObject</span>?) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">counter</span>++</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">counter</span> == 2 {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">counter</span> = 0</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> destination = segue.</span>destinationViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">as</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">! </span>UIViewController</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> destination.</span>interstitialPresentationPolicy<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>ADInterstitialPresentationPolicy<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>Automatic</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
Thats all! Apple takes care about ads loading and error handling and decides, if the ad is ready to show. It can happen that no ad is shown. For example, if you click to fast in this sample project no new ad is available.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjitCDMS-WFDD15IJJzstOoMP7ZcaEX-i8Y67xis9R0I_DqKnwFI4FoscLSp5AjY5A6a7Bj7D4pGqfcT0CmCeQlPcO9y5DLEhanKejXp9p-jLo1viTme4Nr5l5zikqvk5UE-qwUXOCXYFM/s1600/iAD15.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjitCDMS-WFDD15IJJzstOoMP7ZcaEX-i8Y67xis9R0I_DqKnwFI4FoscLSp5AjY5A6a7Bj7D4pGqfcT0CmCeQlPcO9y5DLEhanKejXp9p-jLo1viTme4Nr5l5zikqvk5UE-qwUXOCXYFM/s320/iAD15.png" width="180" /></a></div>
<br />
<br />
You can download the sample code from my <a href="https://github.com/stfnjstn/iAdSample">GitHub repository</a>.<br />
<br />
That's all for today.<br />
<br />
Cheers,<br />
Stefan<br />
<br />
<br />
<br />
<br />
<div style="text-align: center;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></span></span></span></div>
<br />
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>
Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com4tag:blogger.com,1999:blog-7769140974661448994.post-28064350222995174142015-06-04T14:41:00.000-07:002015-08-28T14:44:23.885-07:00How to implement In-App Purchase for your iOS App in SWIFT<h1>
Adding In-App Purchases:</h1>
Welcome to part 8 of my swift programming tutorial. Today I'll show how to implement <strong>In-App Purchases:</strong>
<br />
<ul>
<li>Create In-App Purchases in iTunesConnect</li>
<li>Implement In-App Purchases</li>
<li>Test and upload to iTunesConnect</li>
</ul>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLqFN9x6IDznLDI81K8PxKVXQTr0i5j4AdfnrzTFQOLfRKclz43B79pUnKq3mCjquVR_T3drz06yRAkh69y33EY6wVPoIb6J5SP7x8Nhzt_YTmZirV5S8SYkJOqcGxGK-68M1oV6B53Q8/s1600/InApp01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="191" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLqFN9x6IDznLDI81K8PxKVXQTr0i5j4AdfnrzTFQOLfRKclz43B79pUnKq3mCjquVR_T3drz06yRAkh69y33EY6wVPoIb6J5SP7x8Nhzt_YTmZirV5S8SYkJOqcGxGK-68M1oV6B53Q8/s320/InApp01.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzqLb0fWdnSHyFuJGkIsqOWGU9UWufOHEHf6_w3we8TgeHIIaxPdx8Trog5QT2T_OR-HQmmN8pTYJGUqiTT7MZtw7I3fkImg3x3YM1ougGDh0DDOcVWAHeXybd_vXMNwYqqtvzwIEaXic/s1600/InApp02.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="177" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzqLb0fWdnSHyFuJGkIsqOWGU9UWufOHEHf6_w3we8TgeHIIaxPdx8Trog5QT2T_OR-HQmmN8pTYJGUqiTT7MZtw7I3fkImg3x3YM1ougGDh0DDOcVWAHeXybd_vXMNwYqqtvzwIEaXic/s320/InApp02.png" width="320" /></a></div>
<br />
<br />
As a starting point you can download the sample project from my GitHub <a href="https://github.com/stfnjstn/MySecondGame/releases/tag/v0.7">repository</a>.
<br />
<a name='more'></a><h3>
Tutorial Overview:</h3>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with.html">Part 1</a>: Initial project setup, sprite creation and movement using </span><span style="font-family: 'Courier New', Courier, monospace;">SKAction</span><span style="font-family: 'Helvetica Neue Light';"> and </span><span style="font-family: 'Courier New', Courier, monospace;">SKConstraint</span></span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_11.html">Part 2</a>: Adding enemies, bullets and shooting with </span></span><span style="color: #323333; font-family: 'Courier New', Courier, monospace;">SKAction</span><span style="color: #323333; font-family: 'Helvetica Neue Light';"> and </span><span style="color: #323333; font-family: 'Courier New', Courier, monospace;">SKConstraint</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_19.html">Part 3</a>: Adding a HUD with </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">SKLabelNode</span><span style="color: #323333; font-family: 'Helvetica Neue Light';"> and </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">SKSpriteNode</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_27.html">Part 4</a>: Adding basic game logic and collision detection</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/12/how-to-implement-space-shooter-with.html">Part 5</a>: Adding particles and sound </span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2015/01/how-to-implement-space-shooter-with.html">Part 6</a>: GameCenter integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2015/02/how-to-implement-space-shooter-with.html">Part 7</a>: iAd integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2015/04/how-to-implement-in-app-purchase-for.html">Part 8</a>: In-App Purchases</span></li>
</ul>
<br />
<h2>
Let's start:</h2>
<div>
<br /></div>
<h3>
<a href="https://www.blogger.com/blogger.g?blogID=7769140974661448994" id="InApp1"></a>1. Create In-App Purchases in iTunes Connect</h3>
<div style="text-align: justify;">
You need a paid Apple Developer Account to execute the next steps. For details about the process to upload Apps to iTunes Connect check tutorial <a href="http://stefansdevplayground.blogspot.de/2015/01/how-to-implement-space-shooter-with.html">part 6</a>.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
</div>
Browse to <a href="https://itunesconnect.apple.com/">iTunes Connect</a> and open your App:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_B_ZFEoBGEp9Pb7hCIOuo55VzfLr7voUFoLHaQMCmHPkv1aAOYT-Fi6Y6q7ab98iWNhOhyacjvvnK4J3ZxvIpz0NgOzjqXQVvqOh5ew23y4RzOPwkTj_62FDLY6DAS1yXBDuO42rh6Bo/s1600/InApp2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="158" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_B_ZFEoBGEp9Pb7hCIOuo55VzfLr7voUFoLHaQMCmHPkv1aAOYT-Fi6Y6q7ab98iWNhOhyacjvvnK4J3ZxvIpz0NgOzjqXQVvqOh5ew23y4RzOPwkTj_62FDLY6DAS1yXBDuO42rh6Bo/s320/InApp2.png" width="320" /></a></div>
<br />
<br />
Choose <strong>In-App Purchases</strong> and click on <strong>Create New</strong>:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicEhbEUJ3w6DJ9hH_q1aPqqgbD6Vzd3-GRe5zoFtLhM977VuRo_TOUa8WwPaTMRfoxb9eMaEHYV-Ph45GI9ErDFPJqcTpQ5Wxpgz9WFYNdIffWQAG9bdThmVOptmyU84vnPo9pbmJ-ZD8/s1600/InApp3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="71" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEicEhbEUJ3w6DJ9hH_q1aPqqgbD6Vzd3-GRe5zoFtLhM977VuRo_TOUa8WwPaTMRfoxb9eMaEHYV-Ph45GI9ErDFPJqcTpQ5Wxpgz9WFYNdIffWQAG9bdThmVOptmyU84vnPo9pbmJ-ZD8/s320/InApp3.png" width="320" /></a></div>
<br />
<br />
You see several different types of possible purchases. In this tutorial I'll show a '<strong>Consumable</strong>' and a '<strong>Non-Consumable</strong>' purchase.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQxe1N0CJXm4kZ3iPFbRP9iLJ0aMaM-XQK0oEOt5jIXwLqqmNcFQW3FX4f8iVDElWMEp8JGlWMnXzo0GWePaVCMsH6qLiyRcjfnvqmmoGgS9H9Sey6c1BIIidTkoQB-FOE0z7sae9FhvM/s1600/InApp4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="250" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQxe1N0CJXm4kZ3iPFbRP9iLJ0aMaM-XQK0oEOt5jIXwLqqmNcFQW3FX4f8iVDElWMEp8JGlWMnXzo0GWePaVCMsH6qLiyRcjfnvqmmoGgS9H9Sey6c1BIIidTkoQB-FOE0z7sae9FhvM/s320/InApp4.png" width="320" /></a></div>
<br />
<br />
Create the <strong>Consumable</strong> purchase:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9ritSQYfyJPzCh9TY86QQfi7VumtxvRAn8yaXJYvxri9D7rG2uH6FUrrBLbYcPWTSQaCNCqWjE-_L9ZFNrM-hC-WEY4BK59Ku-39pSKdmJ-x6Iror0eYy-BO6J84oyV5qmJGEoJeJBuk/s1600/InApp5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="49" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9ritSQYfyJPzCh9TY86QQfi7VumtxvRAn8yaXJYvxri9D7rG2uH6FUrrBLbYcPWTSQaCNCqWjE-_L9ZFNrM-hC-WEY4BK59Ku-39pSKdmJ-x6Iror0eYy-BO6J84oyV5qmJGEoJeJBuk/s320/InApp5.png" width="320" /></a></div>
<br />
<br />
Enter <strong>Reference Name</strong>, <strong>Product ID</strong> and <strong>Price Tier</strong>:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiD8mdtMihFV0maJnzVzYF5Y5rqDmMLqaNHqHcF3b1fIwE4ETQceDQGBZX-4HrIqtrJLaJ5YhgWU3e2ZdTFwcQiwd7x69eeM9nKfyqs2oBfARFXcKeRh0iZvothiORMyF2thbkg4q4t7-c/s1600/InApp6.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="185" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiD8mdtMihFV0maJnzVzYF5Y5rqDmMLqaNHqHcF3b1fIwE4ETQceDQGBZX-4HrIqtrJLaJ5YhgWU3e2ZdTFwcQiwd7x69eeM9nKfyqs2oBfARFXcKeRh0iZvothiORMyF2thbkg4q4t7-c/s320/InApp6.png" width="320" /></a></div>
<br />
<br />
<br />
Add <strong>Display Name</strong> and <strong>Description</strong> at least for one language:
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXo0G0YQjWkiOu8w3_06aZMtYB7Dr3jVn5JUDMszKjxJ872KRwLtgQrE04n8n2XSBFFr7SrH4GqRIRPN0htZUharzlDHX3QjnzLVUSBMt9iiqwJo9eSzwFZZNH5yunbjb0aanaTJu9jgo/s1600/InApp7.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="104" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXo0G0YQjWkiOu8w3_06aZMtYB7Dr3jVn5JUDMszKjxJ872KRwLtgQrE04n8n2XSBFFr7SrH4GqRIRPN0htZUharzlDHX3QjnzLVUSBMt9iiqwJo9eSzwFZZNH5yunbjb0aanaTJu9jgo/s320/InApp7.png" width="320" /></a></div>
<br />
<br />
Additionally a screenshot is needed for the review team:<br />
<a href="http://developerplayground.net/wp-content/uploads/2015/04/InApp8.png"></a><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqnhl3LaWvIWU6hnaLcQUzedP89yU96DoCdJSGMXP0o-u7Q255ExURV1Wv-Su3zD8aPN4YhakmEDAr7lm3lF4bzwmK8XPH38sR7zJiwZwXRWOFVfBdgj5DS1zrU7Mq-moiwUoHdUciLls/s1600/InApp8.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="78" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqnhl3LaWvIWU6hnaLcQUzedP89yU96DoCdJSGMXP0o-u7Q255ExURV1Wv-Su3zD8aPN4YhakmEDAr7lm3lF4bzwmK8XPH38sR7zJiwZwXRWOFVfBdgj5DS1zrU7Mq-moiwUoHdUciLls/s320/InApp8.png" width="320" /></a></div>
<br />
<br />
<br />
Now do the same for the <strong>Non-Consumable</strong> purchase:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaGkg99HUPM9z0HxQ74v3kwRBY4_83Z8oRFl27vPA6_w6UgCLIyfcUOA1U192EJ9iU0eClAjBZJeN_83DDxtJ_6PosGRAwiiQI6eD0UaY4ZdSjxsiDKhWXZPtyUXDrxyrZjjXCsMgd_sw/s1600/InApp9.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="45" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaGkg99HUPM9z0HxQ74v3kwRBY4_83Z8oRFl27vPA6_w6UgCLIyfcUOA1U192EJ9iU0eClAjBZJeN_83DDxtJ_6PosGRAwiiQI6eD0UaY4ZdSjxsiDKhWXZPtyUXDrxyrZjjXCsMgd_sw/s320/InApp9.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitvnDuhSGeSLlvB39nPIMqcpLgdntIITumO76BjXTX81La6aaXtLzcxWuaRHMFC7wr83c-GAqXKibWF1k_B0hV8NoJiL1RdmBAZoDOfgXqmPv1TlfN7GBWj97NLYCV9HK5rztPb5H-nR8/s1600/InApp14.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="175" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitvnDuhSGeSLlvB39nPIMqcpLgdntIITumO76BjXTX81La6aaXtLzcxWuaRHMFC7wr83c-GAqXKibWF1k_B0hV8NoJiL1RdmBAZoDOfgXqmPv1TlfN7GBWj97NLYCV9HK5rztPb5H-nR8/s320/InApp14.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<br />
The final result should look like this:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtlMX9OBDKWOAAWZuVmQNuDfjH3BMR4N6AFGH4XDEdtR_FWOkRFXwtll6iAo63_0P4_hDv3pR9-PuD9EyeFg3H8RhDxPMOqqemzAqa6o7yM6oIzRBpn5CAZ-a5t4x3WvoHuCyichLLEJE/s1600/InApp15.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="184" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtlMX9OBDKWOAAWZuVmQNuDfjH3BMR4N6AFGH4XDEdtR_FWOkRFXwtll6iAo63_0P4_hDv3pR9-PuD9EyeFg3H8RhDxPMOqqemzAqa6o7yM6oIzRBpn5CAZ-a5t4x3WvoHuCyichLLEJE/s320/InApp15.png" width="320" /></a></div>
<br />
<br />
<h3>
<a href="https://www.blogger.com/blogger.g?blogID=7769140974661448994" id="InApp2"></a>2. Implement In-App Purchases</h3>
Open your project in XCode (sample project is available <a href="https://github.com/stfnjstn/MySecondGame/releases/tag/v0.7">here</a>), navigate to the <strong>Capabilities</strong> configuration page and enable <strong>In-App Purchases</strong>:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioBRnw9a7_UfO_FRxVY8trK0tDLTkxDEewVTGnMyQ1WtL0NSncBzxC7FYyCSQCHvgrASplgwX393ftDDguU7ou6oOhXsGIhHzymgOUfu-AflC-8X_PpMXninh0PDXKV-kf10YaTx_epFY/s1600/InApp12.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="132" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioBRnw9a7_UfO_FRxVY8trK0tDLTkxDEewVTGnMyQ1WtL0NSncBzxC7FYyCSQCHvgrASplgwX393ftDDguU7ou6oOhXsGIhHzymgOUfu-AflC-8X_PpMXninh0PDXKV-kf10YaTx_epFY/s320/InApp12.png" width="320" /></a></div>
<br />
<br />
<br />
Xcode will include the <strong>StoreKit</strong> framework and add the <strong>In-App Purchase</strong> entitlement automatically.
<br />
<h4>
2.1. Add a button to trigger the purchases:</h4>
Open the <strong>createHUD</strong> method in <strong>GameScene.swift</strong> and add this code snippet to add a '$$$' button to the header:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyX9s4tnNjzkTgcvJA-hSwb0efUID2trnoS6XDM-bySP-cBGHRptJaYKAV8g_9MhsaVeSaKAiu4sX7toIm_lTIBH1c0C1r-n-gWadL1A0VGaTO9d1Cqg08PRffbQTI2IuQso4RAZRVVHg/s1600/InApp13.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="12" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyX9s4tnNjzkTgcvJA-hSwb0efUID2trnoS6XDM-bySP-cBGHRptJaYKAV8g_9MhsaVeSaKAiu4sX7toIm_lTIBH1c0C1r-n-gWadL1A0VGaTO9d1Cqg08PRffbQTI2IuQso4RAZRVVHg/s320/InApp13.png" width="320" /></a></div>
<br />
<br />
<div style="font-family: Menlo; font-size: 11px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"><br /></span>
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> createHUD() {</div>
<div style="font-family: Menlo; font-size: 11px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
...</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="text-align: justify;">
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Add a $ Button for In-App Purchases:</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"> var</span> purchaseButton = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKLabelNode</span>()</div>
<div style="font-family: Menlo; font-size: 11px;">
purchaseButton.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPointMake</span>(hud.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">width</span>/1.5, 1)</div>
<div style="font-family: Menlo; font-size: 11px;">
purchaseButton.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">text</span>=<span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"$$$"</span></div>
<div style="font-family: Menlo; font-size: 11px;">
purchaseButton.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">fontSize</span>=hud.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">height</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> purchaseButton.</span>horizontalAlignmentMode<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>SKLabelHorizontalAlignmentMode<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>Center</div>
<div style="font-family: Menlo; font-size: 11px;">
purchaseButton.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">name</span>=<span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"PurchaseButton"</span></div>
<br />
Add these two lines to <strong>touchesBegan</strong> to call <strong>inAppPurchase</strong>:
<br />
<div style="text-align: justify;">
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"><br /></span>
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"> override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> touchesBegan(touches: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Set</span><<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">NSObject</span>>, withEvent event: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIEvent</span>) {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>/* Called when a touch begins */</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"> for</span> touch <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span> (touches <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">as</span>! <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Set</span><<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UITouch</span>>) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"> var</span> location = touch.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">locationInNode</span>(<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"> var</span> node = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">nodeAtPoint</span>(location)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"> if</span> (node.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">name</span> == <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"PauseButton"</span>) || (node.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">name</span> == <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"PauseButtonContainer"</span>) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;"> showPauseAlert</span>()</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="background-color: yellow; color: red;"> } else if (node.name == "PurchaseButton") {</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="background-color: yellow; color: red;"> inAppPurchase()</span></div>
<div style="font-family: Menlo; font-size: 11px;">
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> {</div>
<div style="font-family: Menlo; font-size: 11px;">
...</div>
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;"><br /></span></span>
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;">Add an empty inAppPurchase method:</span></span>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<br />
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> inAppPurchase() {</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<h4>
2.2. Implement Puchase</h4>
</div>
</div>
All changes will be done in GameScene.swift.
Import the StoreKit framework:
<span style="color: #0433ff; font-family: Menlo; font-size: 11px;">import</span><span style="font-family: Menlo; font-size: 11px;"> StoreKit</span>
Add the StoreKit protocols:
<br />
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"><br /></span>
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">class</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> GameScene: </span>SKScene<span style="color: black; font-variant-ligatures: no-common-ligatures;">, </span>SKPhysicsContactDelegatee<span style="color: black; font-variant-ligatures: no-common-ligatures;">, </span><span style="background-color: yellow;">SKPaymentTransactionObserver<span style="color: black; font-variant-ligatures: no-common-ligatures;">, </span>SKProductsRequestDelegate</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> {</span></div>
<br />
Add these lines at the end of the <strong>didMoveToView</strong> method to initialise the purchase objects and to check if the new feature is already purchased:
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<br />
// In-App Purchase</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: #3495af;">initInAppPurchases</span><span style="color: black;">()</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
checkAndActivateGreenShip<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<br />
I've documented the missing methods in the code itself:
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"><br /></span>
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// ---------------------------------</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// ---- Handle In-App Purchases ----</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// ---------------------------------</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">private</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> request : <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKProductsRequest</span>!</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">private</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> products : [</span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKProduct</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">] = [] </span>// List of available purchases</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">private</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> greenShipPurchased = </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span> // Used to enable/disable the 'green ship' feature</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="background-color: yellow;">// Open a menu with the available purchases</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> inAppPurchase() {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> alert = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertController</span>(title: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"In App Purchases"</span>, message: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">""</span>, preferredStyle: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertControllerStyle</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Alert</span>)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gamePaused</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Add an alert action for each available product</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">for</span> (<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> i = 0; i < <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">products</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">count</span>; i++) {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> currentProduct = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">products</span>[i]</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> !(currentProduct.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">productIdentifier</span> == <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"MySecondGameGreenShip"</span> && <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">greenShipPurchased</span>) {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Get the localized price</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> numberFormatter = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">NSNumberFormatter</span>()</div>
<div style="font-family: Menlo; font-size: 11px;">
numberFormatter.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">numberStyle</span> = .<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CurrencyStyle</span></div>
<div style="font-family: Menlo; font-size: 11px;">
numberFormatter.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">locale</span> = currentProduct.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">priceLocale</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Add the alert action</div>
<div style="font-family: Menlo; font-size: 11px;">
alert.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addAction</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertAction</span>(title: currentProduct.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">localizedTitle</span> + <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">" "</span> + numberFormatter.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">stringFromNumber</span>(currentProduct.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">price</span>)!, style: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertActionStyle</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Default</span>) { <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">_</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Perform the purchase</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">buyProduct</span>(currentProduct)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gamePaused</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span></div>
<div style="font-family: Menlo; font-size: 11px;">
})</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Offer the restore option only if purchase info is not available</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>greenShipPurchased<span style="color: black; font-variant-ligatures: no-common-ligatures;"> == </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">) {</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> alert.</span>addAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>UIAlertAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(title: </span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Restore"</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, style: </span>UIAlertActionStyle<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>Default<span style="color: black; font-variant-ligatures: no-common-ligatures;">) { </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">_</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>restorePurchasedProducts<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gamePaused</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span></div>
<div style="font-family: Menlo; font-size: 11px;">
})</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> alert.</span>addAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>UIAlertAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(title: </span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Cancel"</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, style: </span>UIAlertActionStyle<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>Default<span style="color: black; font-variant-ligatures: no-common-ligatures;">) { </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">_</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gamePaused</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span></div>
<div style="font-family: Menlo; font-size: 11px;">
})</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Show the alert</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>view<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>window<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>rootViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>presentViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;">(alert, animated: </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, completion: </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="background-color: yellow;">// Initialize the App Purchases</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> initInAppPurchases() {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>SKPaymentQueue<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>defaultQueue<span style="color: black; font-variant-ligatures: no-common-ligatures;">().</span>addTransactionObserver<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Get the list of possible purchases</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">request</span> == <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span> {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">request</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKProductsRequest</span>(productIdentifiers: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Set</span>([<span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"MySecondGameGreenShip"</span>,<span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"MySecondGameDonate"</span>]))</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">request</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">delegate</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">request</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">start</span>()</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="background-color: yellow;">// Request a purchase</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> buyProduct(product: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKProduct</span>) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> payment = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKPayment</span>(product: product)</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>SKPaymentQueue<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>defaultQueue<span style="color: black; font-variant-ligatures: no-common-ligatures;">().</span>addPayment<span style="color: black; font-variant-ligatures: no-common-ligatures;">(payment)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="background-color: yellow;">// Restore purchases</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> restorePurchasedProducts() {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>SKPaymentQueue<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>defaultQueue<span style="color: black; font-variant-ligatures: no-common-ligatures;">().</span>restoreCompletedTransactions<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="background-color: yellow;">// StoreKit protocoll method. Called when the AppStore responds</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> productsRequest(request: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKProductsRequest</span>!, didReceiveResponse response: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKProductsResponse</span>!) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">products</span> = response.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">products</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">as</span>! [<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKProduct</span>]</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">request</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="background-color: yellow;">// StoreKit protocoll method. Called when an error happens in the communication with the AppStore</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> request(request: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKRequest</span>!, didFailWithError error: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">NSError</span>!) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">println</span>(error)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">request</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="background-color: yellow;">// StoreKit protocoll method. Called after the purchase</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> paymentQueue(queue: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKPaymentQueue</span>!, updatedTransactions transactions: [<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">AnyObject</span>]!) {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">for</span> transaction <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span> transactions <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">as</span>! [<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKPaymentTransaction</span>] {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">switch</span> (transaction.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">transactionState</span>) {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">case</span> .Purchased:</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> transaction.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">payment</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">productIdentifier</span> == <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"MySecondGameGreenShip"</span> {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>handleGreenShipPurchased<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
queue.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">finishTransaction</span>(transaction)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">case</span> .Restored:</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> transaction.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">payment</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">productIdentifier</span> == <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"MySecondGameGreenShip"</span> {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>handleGreenShipPurchased<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
queue.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">finishTransaction</span>(transaction)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">case</span> .Failed:</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">println</span>(<span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Payment Error: %@"</span>, transaction.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">error</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
queue.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">finishTransaction</span>(transaction)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">default</span>:</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">println</span>(<span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Transaction State: %@"</span>, transaction.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">transactionState</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="background-color: yellow;">// Called after the purchase to provide the 'green ship' feature</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> handleGreenShipPurchased() {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>greenShipPurchased<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>checkAndActivateGreenShip<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// persist the purchase locally</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>NSUserDefaults<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>standardUserDefaults<span style="color: black; font-variant-ligatures: no-common-ligatures;">().</span>setBool<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, forKey: </span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"MySecondGameGreenShip"</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="background-color: yellow;">// Called after applicattion start to check if the 'green ship' feature was purchased</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> checkAndActivateGreenShip() {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> NSUserDefaults<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>standardUserDefaults<span style="color: black; font-variant-ligatures: no-common-ligatures;">().</span>boolForKey<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"MySecondGameGreenShip"</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">) {</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>greenShipPurchased<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>color<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>UIColor<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>greenColor<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>colorBlendFactor<span style="color: black; font-variant-ligatures: no-common-ligatures;">=0.8</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div>
</div>
<h3>
<a href="https://www.blogger.com/blogger.g?blogID=7769140974661448994" id="AppStore3"></a>Test and upload to iTunes Connect</h3>
You need a Sandbox Test User to test the purchases. I've described the steps to create one in <a href="http://stefansdevplayground.blogspot.de/2015/01/how-to-implement-space-shooter-with.html">part 6</a>.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEnaNSRIC_tXXw4aPqXyM56yHl3RRRCku2xePFKP6G7jN2pwaMG5MECeQGXuAJV4XGbPsE2vroE2bsaFBFXpyGp_Ihye8edrBlp7dSckBheGpB_HFD12Ylvoqk2G-jOG9zg7kBpnAD6Gs/s1600/InApp16.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="158" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEnaNSRIC_tXXw4aPqXyM56yHl3RRRCku2xePFKP6G7jN2pwaMG5MECeQGXuAJV4XGbPsE2vroE2bsaFBFXpyGp_Ihye8edrBlp7dSckBheGpB_HFD12Ylvoqk2G-jOG9zg7kBpnAD6Gs/s320/InApp16.png" width="320" /></a></div>
<br />
<br />
<div style="text-align: justify;">
<br />
Testing is only possible on a real device. If you try a purchase in the simulator, you'll receive an error message: AppStore is not available.<br />
<br />
And don't forget to set the checkmarks on your purchases to include them to your them to your application bundle, before submitting a new version in iTunesConnect.
<br />
<div style="text-align: justify;">
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;"><br /></span></span>
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;">That's all for today. The SourceCode of this tutorial is available at <a href="https://github.com/stfnjstn/MySecondGame/releases/tag/v0.8">GitHub</a>. </span></span><span style="color: #323333; font-family: 'Helvetica Neue Light';">To see <strong>In-App Purchase</strong> in action you can download my free <a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8">game</a> in the AppStore. The <strong>In-App Purchase </strong>update will be released in the next days.</span></div>
<div style="text-align: justify;">
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: center;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></span></span></span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">
</span></span></span>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">Cheers, </span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">Stefan
<br />
</span><br />
</span></span>
<br />
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>
</div>
Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com0tag:blogger.com,1999:blog-7769140974661448994.post-87404254312030149512015-04-30T13:57:00.005-07:002015-08-28T14:45:31.286-07:00How to debug on a real Apple Watch<h2>
Today my Apple Watch was delivered :-)</h2>
<div style="text-align: justify;">
I immediately tried to deploy my prototypes to the Apple Watch, to see how the behave on real hardware. The simulator gives only a first impression. </div>
<strong><br /></strong>
<strong><br /></strong>
<strong>Here are my learnings: </strong>
Basically you need to perform two steps:
<br />
<ul>
<li>Add the UDID of your apple watch to the developer portal</li>
<li>Update your provisioning profile to support the watch</li>
</ul>
<br />
Luckily XCode can manage the whole process for you.<br />
<br />
<a name='more'></a><br />
<br />
First you need a sample project like this from NatashaTheRobot: <a href="https://github.com/NatashaTheRobot/WatchKitTableDemo">WatchKitTableDemo</a><br />
<strong><br /></strong>
<strong>1. Deploy the App to your iPhone/iPad</strong><br />
<strong><br /></strong>
<a href="http://developerplayground.net/wp-content/uploads/2015/04/Screen-Shot-2015-04-29-at-22.53.24.png" rel="attachment wp-att-703"><img alt="Debug Apple Watch part1" class="alignnone wp-image-703 size-medium" src="http://developerplayground.net/wp-content/uploads/2015/04/Screen-Shot-2015-04-29-at-22.53.24-300x177.png" height="177" width="300" /></a><br />
<br />
<strong><br /></strong>
<strong><br /></strong>
<strong>2. Start the WatchKit App also on your iPhone/iPad (not in the simulator)</strong><br />
<strong><br /></strong>
<a href="http://developerplayground.net/wp-content/uploads/2015/04/Screen-Shot-2015-04-29-at-22.55.14.png" rel="attachment wp-att-704"><img alt="Debug Apple Watch part2" class="alignnone wp-image-704 size-medium" src="http://developerplayground.net/wp-content/uploads/2015/04/Screen-Shot-2015-04-29-at-22.55.14-300x119.png" height="119" width="300" /></a>
<br />
<br />
<strong>3. Choose Fix Issue, when this error message appears:</strong><br />
<br />
<a href="http://developerplayground.net/wp-content/uploads/2015/04/watch1.png" rel="attachment wp-att-707"><img alt="WatchKit Error Message" class="alignnone wp-image-707 size-medium" src="http://developerplayground.net/wp-content/uploads/2015/04/watch1-300x137.png" height="137" width="300" /></a>
<a href="http://developerplayground.net/wp-content/uploads/2015/04/watch2.png" rel="attachment wp-att-708"><img alt="Select team from Apple Developer Portal" class="alignnone wp-image-708 size-medium" src="http://developerplayground.net/wp-content/uploads/2015/04/watch2-300x118.png" height="118" width="300" /></a>
<strong>4. </strong><br />
<strong><br /></strong>
<strong><br /></strong>
<strong>Select your Development Team:</strong><br />
<br />
<a href="http://developerplayground.net/wp-content/uploads/2015/04/watch3.png" rel="attachment wp-att-709" title="XCode Watch Kit select team"><img alt="watch3" class=" size-medium wp-image-709 alignnone" src="http://developerplayground.net/wp-content/uploads/2015/04/watch3-300x117.png" height="117" width="300" /></a>
<br />
<br />
That's all. After a short time XCode has created and downloaded a new profile and added your Apple Watch to the devices section in the Apple Developer Portal.<br />
<br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">Cheers, </span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">Stefan
</span>
</span></span>
<br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
This tutorial is also available on <a href="http://developerplayground.net/?p=699">developerplayground.net</a><br />
<div>
<br />
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>
</div>
Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com0tag:blogger.com,1999:blog-7769140974661448994.post-75577252188590278552015-04-24T15:38:00.000-07:002015-08-28T14:46:15.485-07:00SWIFT 1.2I've updated all my SWIFT tutorials to SWIFT 1.2 and XCode 6.3. You can find them <a href="http://developerplayground.net/?page_id=101">here</a>.<br />
<br />
<br />Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com0tag:blogger.com,1999:blog-7769140974661448994.post-22918466616836763562015-04-20T14:18:00.001-07:002015-05-30T04:45:32.057-07:00New Blog Domain<h3>
<span style="font-weight: normal;">I've moved my blog to a new domain: <a href="http://developerplayground.net/">developerplayground.net</a></span></h3>
<div style="background-color: white; color: #333333; font-family: Lato, Helvetica, sans-serif; font-size: 16px; line-height: 24px;">
Unfortunately it looks like Google has lost it's interest in Blogspot. There have been no major updates in the last two years.<br />
<br />
It also looks like that it is nearly impossible to get AdSense running with a Blogspot domain.</div>
<span style="background-color: white; color: #333333; font-family: Lato, Helvetica, sans-serif; font-size: 16px; line-height: 24px;"><br /></span>
<span style="background-color: white; color: #333333; font-family: Lato, Helvetica, sans-serif; font-size: 16px; line-height: 24px;">I have moved my blog to a new domain. I'll continue posting links and copies of my articles also here for a while. </span><br />
<br />Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com0tag:blogger.com,1999:blog-7769140974661448994.post-55385795760344299312015-03-04T11:54:00.002-08:002015-08-28T14:50:18.881-07:00How to add Social Media Integration to your iOS App<div style="text-align: justify;">
Welcome to my new SWIFT tutorial. Today I'll show you how to implement social media integration for Facebook and Twitter. Additionally I'll explain Email integration and how to embed a direct AppStore link to provide a convenient way for players to rate an app.</div>
<br />
My latest <a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8">game</a> in the AppStore contains a sample:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmNt2DQWEAmWTunbUQgCav_L2jrwEeBi5jYHTMSMkinS4FWcZd-YyQ0n4Nj9P8iwVB23bTUD_JEj5V-NnfLxvQ4xf9myLZZQ2VgO6PHZ5On3DbGZ6IKd4IX704ty6tzPuZdnpjTnpImQU/s1600/IMG_8919.PNG" width="320" /></a><span id="goog_242148030"></span><span id="goog_242148031"></span><a href="https://www.blogger.com/"></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
The four buttons at the bottom of the screen trigger the social actions:</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgas4gqFV5ksX-F7ejxUNDlQJkQgbOVE6cCmQZGMic22AOg5WRyfvOoCnCwtqxDQ6_ZyRNflnxXKvjXIbR4G2fdsG9RSkp58SJfxPdHPhh2LmNTrP1BX3L4N13_h5YtCUZ01bkxr2qakCk/s1600/Screen+Shot+2015-02-26+at+23.12.45.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="226" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgas4gqFV5ksX-F7ejxUNDlQJkQgbOVE6cCmQZGMic22AOg5WRyfvOoCnCwtqxDQ6_ZyRNflnxXKvjXIbR4G2fdsG9RSkp58SJfxPdHPhh2LmNTrP1BX3L4N13_h5YtCUZ01bkxr2qakCk/s1600/Screen+Shot+2015-02-26+at+23.12.45.png" width="320" /></a></div>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<a name='more'></a><br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<br />
<span style="font-size: large;">1. Create a sample project:</span><br />
<br />
Start XCode and create a new Single View Application:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHM_FzzDsVx1zWhFAGu0fPCGUpBCdbIQAXsYGFpUR_0RJ5fNBBzG62k2RR_rlcp8_n13BYeG_eXeBrhsDTnrLYDnkIJskrhRE47wjagCM24RoZHiO7l2qSFXZgen3P5_xJgIuEjfbgXJM/s1600/Screen+Shot+2015-03-02+at+20.32.21.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="190" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHM_FzzDsVx1zWhFAGu0fPCGUpBCdbIQAXsYGFpUR_0RJ5fNBBzG62k2RR_rlcp8_n13BYeG_eXeBrhsDTnrLYDnkIJskrhRE47wjagCM24RoZHiO7l2qSFXZgen3P5_xJgIuEjfbgXJM/s1600/Screen+Shot+2015-03-02+at+20.32.21.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiU8xkdGcBax95jJmrUSyXvWgSX5XZThogegXmLewZnaPLlk8IgPsxXpkuSUT1VgVxDaWahr3frumXbbutAuW960JjjSQLYRL162TNbSBx1_NSsSA5JJOfGjzpFxTJssP3k8r2hpZI9F04/s1600/Screen+Shot+2015-03-02+at+20.32.38.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="191" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiU8xkdGcBax95jJmrUSyXvWgSX5XZThogegXmLewZnaPLlk8IgPsxXpkuSUT1VgVxDaWahr3frumXbbutAuW960JjjSQLYRL162TNbSBx1_NSsSA5JJOfGjzpFxTJssP3k8r2hpZI9F04/s1600/Screen+Shot+2015-03-02+at+20.32.38.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
Open the <span style="font-family: Courier New, Courier, monospace;">Main.storyboard</span> file and add four buttons to the <span style="font-family: Courier New, Courier, monospace;">ViewController</span>:</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXgVjNFt_vxfQ9vf6k7upHuJ3QSrwKB0SEaoik-0U-VKU8C7RSPeRN0cfHF3B5Xj5nyqDjp2sSiTwGNi_B4FtkZwdl3aW4gGvbLrIzQ9uPrk7K1GYmoggnjM7-uW6TaR_EKB5BP7Lrgc0/s1600/Screen+Shot+2015-03-02+at+20.33.34.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXgVjNFt_vxfQ9vf6k7upHuJ3QSrwKB0SEaoik-0U-VKU8C7RSPeRN0cfHF3B5Xj5nyqDjp2sSiTwGNi_B4FtkZwdl3aW4gGvbLrIzQ9uPrk7K1GYmoggnjM7-uW6TaR_EKB5BP7Lrgc0/s1600/Screen+Shot+2015-03-02+at+20.33.34.png" width="311" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
For a proper layout on all form factors and orientations add the needed auto layout constraints:</div>
<div class="separator" style="clear: both; text-align: left;">
</div>
<ul>
<li>Select the four buttons</li>
<li>Press the <span style="font-family: Courier New, Courier, monospace;">Pin</span> button</li>
<li>Enter in the <span style="font-family: Courier New, Courier, monospace;">spacing to nearest neighbour </span>section 20 to the bottom and 10 to the right</li>
</ul>
<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAU7T0SXhp4eW3wh1CMwZyuoGcJ4pMrX71JAsOtnouFHDyTEEK__S-JSFbDxdkIG4cBksLOweLfeXgCIox-NqwumDcmkNI3URIlg_SIUw98SVZA3al-hE2FNJLhdAyRBM_Gb0c7PsIG7A/s1600/Screen+Shot+2015-03-02+at+20.34.55.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="289" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAU7T0SXhp4eW3wh1CMwZyuoGcJ4pMrX71JAsOtnouFHDyTEEK__S-JSFbDxdkIG4cBksLOweLfeXgCIox-NqwumDcmkNI3URIlg_SIUw98SVZA3al-hE2FNJLhdAyRBM_Gb0c7PsIG7A/s1600/Screen+Shot+2015-03-02+at+20.34.55.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
XCode shows a warning, that the current layout does not match the constraints (the yellow lines). To fix this click the <span style="font-family: Courier New, Courier, monospace;">Resolve Auto Layout Issues</span> button and choose <span style="font-family: Courier New, Courier, monospace;">Update Frames</span>:</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEio8HDnq3QrAdNpfjPFT7DeHP3wpaNXsL6XazIR0z0kMKH3bX-dA4AQW2BEa9xI7fZB8rBntVQ1Zg6Y0nXWR0o8t6CRUMTWg77LeED1bbpPPBpGqwdS5C3zqijaMuIzFKqKh55zY_SZoHM/s1600/Screen+Shot+2015-03-02+at+20.35.11.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEio8HDnq3QrAdNpfjPFT7DeHP3wpaNXsL6XazIR0z0kMKH3bX-dA4AQW2BEa9xI7fZB8rBntVQ1Zg6Y0nXWR0o8t6CRUMTWg77LeED1bbpPPBpGqwdS5C3zqijaMuIzFKqKh55zY_SZoHM/s1600/Screen+Shot+2015-03-02+at+20.35.11.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
The result should look like this:</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEih-HKKfzn_pCW2ZhYhVfO7e6SUlaU2SZBjxq8JoibbwZhov1UUsprtB6w03Uf30_LpvCQyKLEjV3tutzQpeUZ7vuZiURQipK7lQdg2mlmhyphenhyphenvOR73QupFrh6H1V-NIN7VtKesFmauIUJ50/s1600/Screen+Shot+2015-03-02+at+20.35.22.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEih-HKKfzn_pCW2ZhYhVfO7e6SUlaU2SZBjxq8JoibbwZhov1UUsprtB6w03Uf30_LpvCQyKLEjV3tutzQpeUZ7vuZiURQipK7lQdg2mlmhyphenhyphenvOR73QupFrh6H1V-NIN7VtKesFmauIUJ50/s1600/Screen+Shot+2015-03-02+at+20.35.22.png" width="306" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0ZnsoVV6CPI07DSVIG_YLkayezi3cJ8r7BjWJdqQe6-GHJb4Mo2ZGZKVCvcnCsPoq1-CXfPkgUTz442aOFn7y9eRT55kj4HBmOqivYMReoPiSvMXGgZgPA4ZpEpCE6sTMSBYzRMtGYIg/s1600/Screen+Shot+2015-03-02+at+20.36.22.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="69" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0ZnsoVV6CPI07DSVIG_YLkayezi3cJ8r7BjWJdqQe6-GHJb4Mo2ZGZKVCvcnCsPoq1-CXfPkgUTz442aOFn7y9eRT55kj4HBmOqivYMReoPiSvMXGgZgPA4ZpEpCE6sTMSBYzRMtGYIg/s1600/Screen+Shot+2015-03-02+at+20.36.22.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: justify;">
Change the background colour of the view to black:</div>
<div class="separator" style="clear: both; text-align: justify;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUbWAzYTsyI9AeVJqZKA_fC7zLVO8OawcPAIgAXGCZYYwJBMFoPXyIT-woLItw3b4D_11ImXtR6vMgv3rRfuswCYMNHORo3_ilun_8p4_q2dOTtamw6mNLo8Fo8JKn_uhL0YIonrPcJPY/s1600/Screen+Shot+2015-03-03+at+22.32.43.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUbWAzYTsyI9AeVJqZKA_fC7zLVO8OawcPAIgAXGCZYYwJBMFoPXyIT-woLItw3b4D_11ImXtR6vMgv3rRfuswCYMNHORo3_ilun_8p4_q2dOTtamw6mNLo8Fo8JKn_uhL0YIonrPcJPY/s1600/Screen+Shot+2015-03-03+at+22.32.43.png" /></a></div>
<div class="separator" style="clear: both; text-align: justify;">
<br /></div>
<div class="separator" style="clear: both; text-align: justify;">
Add an <span style="font-family: Courier New, Courier, monospace;">IBAction</span> method for every button (Select CTRL, press mouse and move the mouse pointer to the view controller file)</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYYppBDVA9Q8hXbINLVI-oaMSw0p-PFhhIYSa8J5beRY_7Ri8NVPaR8aLjmMbXAjR7S_CbinOQiBKOXuI2IrO8TRtyd0kvaTRjkTBC1z7z3UK7Z27Vq-WfF5sKUvKVTmAa0w725heJlkI/s1600/Screen+Shot+2015-03-02+at+23.12.52.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="210" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYYppBDVA9Q8hXbINLVI-oaMSw0p-PFhhIYSa8J5beRY_7Ri8NVPaR8aLjmMbXAjR7S_CbinOQiBKOXuI2IrO8TRtyd0kvaTRjkTBC1z7z3UK7Z27Vq-WfF5sKUvKVTmAa0w725heJlkI/s1600/Screen+Shot+2015-03-02+at+23.12.52.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
The <span style="font-family: Courier New, Courier, monospace;">ViewController.swift</span> file should look like this:</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">import</span> UIKit</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">class</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> ViewController: </span>UIViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> viewDidLoad() {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">super</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">viewDidLoad</span>()</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Do any additional setup after loading the view, typically from a nib.</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">@IBAction</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> handleAppStore(sender: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">AnyObject</span>) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff;"> </span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">@IBAction</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> handleFacebook(sender: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">AnyObject</span>) {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">@IBAction</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> handleTwitter(sender: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">AnyObject</span>) {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">@IBAction</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> handleMail(sender: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">AnyObject</span>) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span><span style="color: #0433ff;"> </span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div class="separator" style="clear: both;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<div class="separator" style="clear: both; text-align: justify;">
Add an image to <span style="font-family: Courier New, Courier, monospace;">image.assets</span> which can be attached to the tweets, posts ands mail.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEja7MS6l7WTupxw7tElahxQhgrESEUB4oElod8_kzXk7n0uPYKw1SdfI-ylb_S3A7bj3YDcoBO30ATQnKdjT0Z8_F3AHUbf4aIYO2aAY2UuwOftL5Rb0IwfALWoVa3ygMw0sl4x4IGxic8/s1600/Screen+Shot+2015-03-04+at+20.36.33.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="99" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEja7MS6l7WTupxw7tElahxQhgrESEUB4oElod8_kzXk7n0uPYKw1SdfI-ylb_S3A7bj3YDcoBO30ATQnKdjT0Z8_F3AHUbf4aIYO2aAY2UuwOftL5Rb0IwfALWoVa3ygMw0sl4x4IGxic8/s1600/Screen+Shot+2015-03-04+at+20.36.33.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<h3>
2. Implement AppStore Integration</h3>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlBa9F_tGspZjpxSkrlzf6dJzhtzHD3VP5vbQD1mZ9yth0UmJVzut1kTiPrGwLhjWLvkEdUeIkBPaKn2CszPiZ4wJcX8pVSH0lC400CwZMbfAE3hexi61KPqCtv83RIdm5twU1UEqx_og/s1600/IMG_8941.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlBa9F_tGspZjpxSkrlzf6dJzhtzHD3VP5vbQD1mZ9yth0UmJVzut1kTiPrGwLhjWLvkEdUeIkBPaKn2CszPiZ4wJcX8pVSH0lC400CwZMbfAE3hexi61KPqCtv83RIdm5twU1UEqx_og/s1600/IMG_8941.PNG" width="112" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<div style="text-align: justify;">
Implement the <span style="font-family: Courier New, Courier, monospace;">handleAppStore</span> method. Show an alert where the user can choose to navigate to the AppStore:</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">@IBAction</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> handleAppStore(sender: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">AnyObject</span>) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> alert = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertController</span>(title: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Rate"</span>, message: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Rate my App"</span>, preferredStyle: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertControllerStyle</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Alert</span>)</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> alert.</span>addAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>UIAlertAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(title: </span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Rate"</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, style: </span>UIAlertActionStyle<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>Default<span style="color: black; font-variant-ligatures: no-common-ligatures;">) { </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">_</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Open App in AppStore</div>
<div style="color: #b4261a; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> iLink = </span>"https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>UIApplication<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>sharedApplication<span style="color: black; font-variant-ligatures: no-common-ligatures;">().</span>openURL<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>NSURL<span style="color: black; font-variant-ligatures: no-common-ligatures;">(string: iLink)!)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
} )</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> alert.</span>addAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>UIAlertAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(title: </span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Cancel"</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, style: </span>UIAlertActionStyle<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>Default<span style="color: black; font-variant-ligatures: no-common-ligatures;">, handler: </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">))</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">presentViewController</span>(alert, animated: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span>, completion: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>)</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<br />
<div style="text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpUkkwKaIwXa_JzLf49F9Rg2uCKic7WxGHjvMIWtwUOPG0hTGSL8WdNPTNzmMu45bokrIkxHe7S3Qi_mG8hTQvlLc0BdaSwqsemU8b3Apogo92aSxDJn5L2aPSplP3ye4fHcAD59CT2dY/s1600/IMG_8942.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpUkkwKaIwXa_JzLf49F9Rg2uCKic7WxGHjvMIWtwUOPG0hTGSL8WdNPTNzmMu45bokrIkxHe7S3Qi_mG8hTQvlLc0BdaSwqsemU8b3Apogo92aSxDJn5L2aPSplP3ye4fHcAD59CT2dY/s1600/IMG_8942.PNG" width="112" /></a></div>
<br />
<h3>
3. Implement Mail Integration</h3>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitfjo8o5AhYRcrlLrV9iLzlVds-vpebCxI-04C9BCLXMUWqFuNvWcfH94IoyoAjNrSSsTx9HxNGQ3wTqOwamAa3EzVtCEcXoJgey8sB2BKI7N6nuHthTLjzjFJA9HJIXCoOWDa1XRtk08/s1600/IMG_8948.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitfjo8o5AhYRcrlLrV9iLzlVds-vpebCxI-04C9BCLXMUWqFuNvWcfH94IoyoAjNrSSsTx9HxNGQ3wTqOwamAa3EzVtCEcXoJgey8sB2BKI7N6nuHthTLjzjFJA9HJIXCoOWDa1XRtk08/s1600/IMG_8948.PNG" width="112" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: justify;">
<span style="text-align: start;">Import <span style="font-family: Courier New, Courier, monospace;">MessageUI</span> and add the <span style="font-family: Courier New, Courier, monospace;">MFMailComposeViewControllerDelegate</span> to the class definition</span><span style="text-align: start;">:</span></div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">import</span> MessageUI</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<br />
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">class</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> ViewController: </span>UIViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;">, </span>MFMailComposeViewControllerDelegate<span style="color: black; font-variant-ligatures: no-common-ligatures;"> {</span></div>
<br />
Implement the <span style="font-family: Courier New, Courier, monospace;">handleMail</span> and <span style="font-family: Courier New, Courier, monospace;">mailComposeController</span> methods:<br />
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">@IBAction</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> handleMail(sender: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">AnyObject</span>) {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Check if Mail is available</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>MFMailComposeViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>canSendMail<span style="color: black; font-variant-ligatures: no-common-ligatures;">()){</span></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Create the mail message</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> mail = </span>MFMailComposeViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; font-size: 11px;">
mail.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">mailComposeDelegate</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span></div>
<div style="font-family: Menlo; font-size: 11px;">
mail.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">setSubject</span>(<span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"New App"</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
mail.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">setMessageBody</span>(<span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"I want to share this App: "</span>, isHTML: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span>)</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Attach the image</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> imageData = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIImagePNGRepresentation</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIImage</span>(named: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"shareImage"</span>))</div>
<div style="font-family: Menlo; font-size: 11px;">
mail.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addAttachmentData</span>(imageData, mimeType: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"image/png"</span>, fileName: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Image"</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">presentViewController</span>(mail, animated: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span>, completion: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Mail not available. Show a warning</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> alert = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertController</span>(title: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Email"</span>, message: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Email not available"</span>, preferredStyle: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertControllerStyle</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Alert</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
alert.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addAction</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertAction</span>(title: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"OK"</span>, style: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertActionStyle</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Default</span>, handler: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>))</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">presentViewController</span>(alert, animated: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span>, completion: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// Required by interface MFMailComposeViewControllerDelegate</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> mailComposeController(controller: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">MFMailComposeViewController</span>!, didFinishWithResult result: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">MFMailComposeResult</span>, error: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">NSError</span>!) {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Close the mail dialog</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>dismissViewControllerAnimated<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, completion: </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<br />
<h3>
4. Implement Twitter Integration</h3>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWDBH4jLtC_9058MU_Fm-RQMDE4o2XG5vdvWbjMhVaqsSR0nNNq6eXaT1n2qW5llWAhQp0gqDoKOhJWIKWkSMXnw8ufsEn03S2CxFS74y1-9ji4FCKxKjXzq5fLwlkImPGp1O6S4vC63U/s1600/IMG_8940.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWDBH4jLtC_9058MU_Fm-RQMDE4o2XG5vdvWbjMhVaqsSR0nNNq6eXaT1n2qW5llWAhQp0gqDoKOhJWIKWkSMXnw8ufsEn03S2CxFS74y1-9ji4FCKxKjXzq5fLwlkImPGp1O6S4vC63U/s1600/IMG_8940.PNG" width="112" /></a></div>
<br />
<br />
Import the <span style="font-family: Courier New, Courier, monospace;">Social</span>:<br />
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">import</span> Social</div>
<br />
Implement the <span style="font-family: Courier New, Courier, monospace;">handleTwitter</span> method:<br />
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">@IBAction</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> handleTwitter(sender: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">AnyObject</span>) {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Check if Twitter is available</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>SLComposeViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>isAvailableForServiceType<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>SLServiceTypeTwitter<span style="color: black; font-variant-ligatures: no-common-ligatures;">)) {</span></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Create the tweet</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> tweet = </span>SLComposeViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;">(forServiceType: </span>SLServiceTypeTwitter<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="color: #b4261a; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> tweet.</span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">setInitialText</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>"I want to share this App: "<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
tweet.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addImage</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIImage</span>(named: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"shareImage"</span>))</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">presentViewController</span>(tweet, animated: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span>, completion: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Twitter not available. Show a warning</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> alert = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertController</span>(title: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Twitter"</span>, message: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Twitter not available"</span>, preferredStyle: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertControllerStyle</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Alert</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
alert.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addAction</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertAction</span>(title: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"OK"</span>, style: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertActionStyle</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Default</span>, handler: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>))</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">presentViewController</span>(alert, animated: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span>, completion: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<br />
<h3>
5. Implement Facebook Integration</h3>
<br />
Implement the <span style="font-family: Courier New, Courier, monospace;">handleFacebook</span> method:<br />
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">@IBAction</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> handleFacebook(sender: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">AnyObject</span>) {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Check if Facebook is available</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> (</span>SLComposeViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>isAvailableForServiceType<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>SLServiceTypeFacebook<span style="color: black; font-variant-ligatures: no-common-ligatures;">))</span></div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Create the post</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> post = </span>SLComposeViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;">(forServiceType: (</span>SLServiceTypeFacebook<span style="color: black; font-variant-ligatures: no-common-ligatures;">))</span></div>
<div style="color: #b4261a; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> post.</span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">setInitialText</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>"I want to share this App: "<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
post.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addImage</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIImage</span>(named: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"shareImage"</span>))</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">presentViewController</span>(post, animated: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span>, completion: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Facebook not available. Show a warning</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> alert = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertController</span>(title: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Facebook"</span>, message: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Facebook not available"</span>, preferredStyle: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertControllerStyle</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Alert</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
alert.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addAction</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertAction</span>(title: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"OK"</span>, style: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertActionStyle</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Default</span>, handler: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>))</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">presentViewController</span>(alert, animated: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span>, completion: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<br />
<br />
Instead of the text you should show icons. I cannot add them to the SourceCode because of copyright reasons. You can download them from Facebook and Twitter homepage.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXIPvqp_kN4NA-18axcWGVE-kG-8zEy0_Xie37-rd7i4OcWD-DsxAnkP2lOUjl5Eiw3CNi_ba9jc1L0RikQvvilHPVDZ6NwiaGt4sH7W40GxQeJilkgmRemo5iBaFE5JRCSlGKJkXb4Bc/s1600/Screen+Shot+2015-02-26+at+23.09.39.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="54" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXIPvqp_kN4NA-18axcWGVE-kG-8zEy0_Xie37-rd7i4OcWD-DsxAnkP2lOUjl5Eiw3CNi_ba9jc1L0RikQvvilHPVDZ6NwiaGt4sH7W40GxQeJilkgmRemo5iBaFE5JRCSlGKJkXb4Bc/s1600/Screen+Shot+2015-02-26+at+23.09.39.png" width="200" /></a><br />
<br />
<div style="text-align: justify;">
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;"><br /></span></span>
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;"><br /></span></span>
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;">That's all for today. The SourceCode of this tutorial is available at <a href="https://github.com/stfnjstn/SocialMediaSample">GitHub</a>. For my next post I'm planning something about In App Purchase. </span></span><span style="color: #323333; font-family: 'Helvetica Neue Light';">To see the social media integration in action you can download my free game in the AppStore:</span></div>
<div style="text-align: justify;">
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></div>
<div style="text-align: center;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></span></span></span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">
</span></span></span>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">Cheers, </span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">Stefan
<br />
</span><br />
</span></span>
<br />
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>
<div>
<br /></div>
<div>
<br /></div>
Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com3tag:blogger.com,1999:blog-7769140974661448994.post-70760774922898692462015-02-17T23:09:00.000-08:002015-06-07T11:49:05.677-07:00How to implement a space shooter with SpriteKit and SWIFT is in the AppStore<h3 style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: bolder; margin: 0.83em 0px; padding: 0px; position: relative; text-align: justify;">
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 14px;">The sample game developed in my SpriteKit Tutorial is in the AppStore:</span></h3>
<div>
<a href="https://itunes.apple.com/us/app/mysecondgame/id956647245?ls=1&mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></div>
<div>
<br /></div>
<div>
SourceCode: <a href="https://github.com/stfnjstn/MySecondGame">here</a></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
A more sophisticate game also based on the tutorial:</div>
<div>
<br /></div>
<div>
<a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8" style="font-size: 19px; font-weight: bold; text-align: center;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<h3 style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: bolder; margin: 0.83em 0px; padding: 0px; position: relative; text-align: justify;">
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 14px;">Tutorial Overview:</span></h3>
<div style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-weight: normal; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with.html">Part 1</a>: Initial project setup, sprite creation and movement using </span><span style="font-family: 'Courier New', Courier, monospace;">SKAction</span><span style="font-family: 'Helvetica Neue Light';"> and </span><span style="font-family: 'Courier New', Courier, monospace;">SKConstraint</span></span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_11.html">Part 2</a>: Adding enemies, bullets and shooting with </span></span><span style="color: #323333; font-family: 'Courier New', Courier, monospace;">SKAction</span><span style="color: #323333; font-family: 'Helvetica Neue Light';"> and </span><span style="color: #323333; font-family: 'Courier New', Courier, monospace;">SKConstraint</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_19.html">Part 3</a>: Adding a HUD with </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">SKLabelNode</span><span style="color: #323333; font-family: 'Helvetica Neue Light';"> and </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">SKSpriteNode</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_27.html">Part 4</a>: Adding basic game logic and collision detection</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/12/how-to-implement-space-shooter-with.html">Part 5</a>: Adding particles and sound </span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2015/01/how-to-implement-space-shooter-with.html">Part 6</a>: GameCenter integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2015/02/how-to-implement-space-shooter-with.html">Part 7</a>: iAd integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2015/04/how-to-implement-in-app-purchase-for.html">Part 8</a>: In-App Purchases</span></li>
</ul>
<div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><br /></span></div>
</div>
Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com0tag:blogger.com,1999:blog-7769140974661448994.post-52093056340589924352015-02-09T22:33:00.005-08:002015-08-30T09:59:21.703-07:00How to implement a space shooter with SpriteKit and SWIFT - Part 7: iAd integration<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 24px; text-align: justify;">Adding iAd integration</span><br />
<h3 style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: bolder; margin: 0.83em 0px; padding: 0px; position: relative; text-align: justify;">
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<iframe allowfullscreen="1" frameborder="0" height="400" src="//www.youtube.com/embed/8d8MH_gXt84" width="600"></iframe></div>
<div style="font-weight: normal;">
<br /></div>
<div style="text-align: center;">
<a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></div>
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<br /></div>
</h3>
<div style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-weight: normal; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<div>
<br />
<div style="text-align: justify;">
<span style="color: #323333; font-family: Helvetica Neue Light;">
Welcome to part 7 of my swift programming tutorial. In the previous parts we've created <a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_11.html">sprites, added movement, enemies with a follow behaviour, bullets & shooting</a>, a <a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_19.html">HUD</a>, <a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_27.html">collision detection</a>, <a href="http://stefansdevplayground.blogspot.de/2014/12/how-to-implement-space-shooter-with.html">sound & particle effects</a> and a </span><span style="color: #323333; font-family: 'Helvetica Neue Light';">global leaderboard using <a href="http://stefansdevplayground.blogspot.de/2015/01/how-to-implement-space-shooter-with.html">Game Center</a></span><span style="color: #323333; font-family: 'Helvetica Neue Light';">. Today I'll show how to integrate the Apple Advertising Framework <b>iAD</b>:
</span><br />
<ul><span style="color: #323333; font-family: 'Helvetica Neue Light';">
<li>Enable iAd in iTunesConnect</li>
<li>Add a Banner Ad (at the bottom of the screen)</li>
<li>Add a Fullscreen Ad (after game over)</li>
</span></ul>
</div>
<br />
<a name='more'></a><br />
<h3 style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: bolder; margin: 0.83em 0px; padding: 0px; position: relative; text-align: justify;">
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 14px;">Tutorial Overview:</span></h3>
<div style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-weight: normal; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with.html">Part 1</a>: Initial project setup, sprite creation and movement using </span><span style="font-family: 'Courier New', Courier, monospace;">SKAction</span><span style="font-family: 'Helvetica Neue Light';"> and </span><span style="font-family: 'Courier New', Courier, monospace;">SKConstraint</span></span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_11.html">Part 2</a>: Adding enemies, bullets and shooting with </span></span><span style="color: #323333; font-family: 'Courier New', Courier, monospace;">SKAction</span><span style="color: #323333; font-family: 'Helvetica Neue Light';"> and </span><span style="color: #323333; font-family: 'Courier New', Courier, monospace;">SKConstraint</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_19.html">Part 3</a>: Adding a HUD with </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">SKLabelNode</span><span style="color: #323333; font-family: 'Helvetica Neue Light';"> and </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">SKSpriteNode</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_27.html">Part 4</a>: Adding basic game logic and collision detection</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/12/how-to-implement-space-shooter-with.html">Part 5</a>: Adding particles and sound </span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2015/01/how-to-implement-space-shooter-with.html">Part 6</a>: GameCenter integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2015/02/how-to-implement-space-shooter-with.html">Part 7</a>: iAd integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2015/04/how-to-implement-in-app-purchase-for.html">Part 8</a>: In-App Purchases</span></li>
</ul>
</div>
<div>
<h2>
<span style="color: #323333; font-family: Helvetica Neue Light;"><b><br /></b></span></h2>
<h2>
<span style="color: #323333; font-family: Helvetica Neue Light;"><b>Let's start:</b></span></h2>
</div>
</div>
<div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><b><br /></b></span></div>
</div>
<h3>
<span style="color: #323333; font-family: Helvetica Neue Light;">1. Enable iAD support in iTunes Connect</span></h3>
<div>
<span style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;">You need a paid Apple Developer Account to execute the next steps. </span></div>
<div>
<span style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"><br /></span></div>
<div>
<b><span style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;">Open </span><a href="https://itunesconnect.apple.com/" style="font-family: 'Helvetica Neue Light'; text-align: justify;"><span style="-webkit-text-stroke-color: rgb(4, 46, 238); color: #551a8b;">iTunes Connect</span></a><span style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"> and navigate to the Agreements, Tax and Banking section:</span></b></div>
<div>
<span style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRIP_SUgKd1a-w37k_TAURfFjYoluBdD0nXTxHh4bmUXp6VMNd1k49XWt5R773eRsZCaF0hoSRzWD4IIAmaQgDruqT9btIFxOCAdaGTTM8Bolb9pSGJTc5BAkwbg7FW8_YadiYezwZwW4/s1600/Screen+Shot+2015-02-05+at+22.10.19.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRIP_SUgKd1a-w37k_TAURfFjYoluBdD0nXTxHh4bmUXp6VMNd1k49XWt5R773eRsZCaF0hoSRzWD4IIAmaQgDruqT9btIFxOCAdaGTTM8Bolb9pSGJTc5BAkwbg7FW8_YadiYezwZwW4/s1600/Screen+Shot+2015-02-05+at+22.10.19.png" /></a></div>
<div>
<span style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"><br /></span></div>
<div>
<h3>
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: small; text-align: justify;">Request an iAd Agreement:</span></h3>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7mXzWIQhpVxrDB9dSL_Ixvhe40yf3vCbZ1HhfXUgGhMhb2lkj66T3Ism9mH0_DWnSwWX5eDYtJT2VhaWIBN_0twM0wrRlscM80hTbweUIJ1EotHz2-OaXBElNX-zNByiVeUw0ZJ7DyjU/s1600/iad.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="85" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7mXzWIQhpVxrDB9dSL_Ixvhe40yf3vCbZ1HhfXUgGhMhb2lkj66T3Ism9mH0_DWnSwWX5eDYtJT2VhaWIBN_0twM0wrRlscM80hTbweUIJ1EotHz2-OaXBElNX-zNByiVeUw0ZJ7DyjU/s1600/iad.png" width="320" /></a></div>
<h3>
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: small; font-weight: normal; text-align: justify;"> </span></h3>
<h3>
<span style="color: #323333; font-family: Helvetica Neue Light;">2. Add a Banner Ad</span></h3>
</div>
<div>
<span style="text-align: justify;"><span style="color: #323333; font-family: Helvetica Neue Light;">In this section I'll show how to add a banner add at the bottom of the screen. As a starting point you can download the code from part 6 <a href="https://github.com/stfnjstn/MySecondGame/releases/tag/v0.6">here</a>.</span></span></div>
<div>
<br /></div>
<div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyzeaX0bl9mOPaoNAraV1L9OdMMZxt9Wlqic7MycsJEigPxhWyiz6axB6CJPqG40xIglLBbieXbu-ZvZ4N48-zLWpJV0XyErwJMg13Id1KQmrj2u0JdCxnKd55ZshcomGqYRG5s5CYeqQ/s1600/Screen+Shot+2015-02-08+at+16.46.48.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyzeaX0bl9mOPaoNAraV1L9OdMMZxt9Wlqic7MycsJEigPxhWyiz6axB6CJPqG40xIglLBbieXbu-ZvZ4N48-zLWpJV0XyErwJMg13Id1KQmrj2u0JdCxnKd55ZshcomGqYRG5s5CYeqQ/s1600/Screen+Shot+2015-02-08+at+16.46.48.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="color: #323333; text-align: justify;"><b><span style="font-family: Helvetica Neue Light;">Import the </span><span style="font-family: Courier New, Courier, monospace;">iAd</span><span style="font-family: Helvetica Neue Light;"> framework and implement the </span><span style="font-family: Courier New, Courier, monospace;">ADBannerViewDelegate </span></b></span><b style="color: #323333; text-align: justify;"><span style="font-family: Helvetica Neue Light;">protocol</span></b><b style="color: #323333; text-align: justify;"><span style="font-family: Helvetica Neue Light;">:</span></b></div>
<div style="color: #0433ff; font-family: Menlo; font-size: 11px; font-weight: normal;">
<br /></div>
<div style="color: #0433ff; font-family: Menlo; font-size: 11px; font-weight: normal;">
import<span style="color: black; font-variant-ligatures: no-common-ligatures;"> iAd</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
<br /></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">class</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> GameViewController: </span>UIViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;">, </span>ADBannerViewDelegate<span style="color: black; font-variant-ligatures: no-common-ligatures;">, </span>GKGameCenterControllerDelegate<span style="color: black; font-variant-ligatures: no-common-ligatures;">, </span>GameSceneDelegate<span style="color: black; font-variant-ligatures: no-common-ligatures;"> {</span></div>
<b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"><br /></b>
<b style="text-align: justify;"><span style="color: #323333; font-family: Helvetica Neue Light;">Add two properties to </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">GameViewController</span><span style="color: #323333; font-family: Helvetica Neue Light;"> to keep a reference to the banner object and the visibility state:</span></b>
<br />
<b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"><br /></b>
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// Properties for Banner Ad</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> iAdBanner = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">ADBannerView</span>()</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> bannerVisible = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span></div>
<b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"><br /></b>
<b style="text-align: justify;"><span style="color: #323333; font-family: Helvetica Neue Light;">Add the code to initialise the banner at the end of </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">viewDidAppear</span><span style="color: #323333; font-family: Helvetica Neue Light;">:</span></b><br />
<b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"><br /></b>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> viewDidAppear(animated: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Bool</span>) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">super</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">viewDidAppear</span>(animated)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> ...</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Prepare banner Ad</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>iAdBanner<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>frame<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>CGRectMake<span style="color: black; font-variant-ligatures: no-common-ligatures;">(0, </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>view<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>frame<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>height<span style="color: black; font-variant-ligatures: no-common-ligatures;">, </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>view<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>frame<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>width<span style="color: black; font-variant-ligatures: no-common-ligatures;">, 50)</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>iAdBanner<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>delegate<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>bannerVisible<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"><br /></b>
<b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;">Implement the protocol method </b><b style="color: #323333; text-align: justify;"><span style="font-family: Courier New, Courier, monospace;">bannerViewDidLoadAd</span></b><b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"> to move the banner on the screen, if an ad is loaded:</b><br />
<b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"><br /></b>
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// Show banner, if Ad is successfully loaded.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> bannerViewDidLoadAd(banner: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">ADBannerView</span>!) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">bannerVisible</span> == <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span>) {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Add banner Ad to the view</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">iAdBanner</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">superview</span> == <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>) {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>view<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>addSubview<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>iAdBanner<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Move banner into visible screen frame:</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIView</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">beginAnimations</span>(<span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"iAdBannerShow"</span>, context: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
banner.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">frame</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGRectOffset</span>(banner.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">frame</span>, 0, <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">-</span>banner.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">frame</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">height</span>)</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>UIView<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>commitAnimations<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">bannerVisible</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"><br /></b>
<b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;">Implement the protocol method </b><b style="color: #323333; text-align: justify;"><span style="font-family: Courier New, Courier, monospace;">bannerView</span></b><b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"> to remove the banner from screen, if an ad is not available:</b><br />
<b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"><br /></b>
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// Hide banner, if Ad is not loaded.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> bannerView(banner: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">ADBannerView</span>!, didFailToReceiveAdWithError error: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">NSError</span>!) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">bannerVisible</span> == <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span>) {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Move banner below screen frame:</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIView</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">beginAnimations</span>(<span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"iAdBannerHide"</span>, context: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
banner.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">frame</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGRectOffset</span>(banner.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">frame</span>, 0, banner.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">frame</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">height</span>)</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>UIView<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>commitAnimations<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">bannerVisible</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"><br /></b>
<br />
<h3>
<span style="color: #323333; font-family: Helvetica Neue Light;">3. Add a Fullscreen Ad</span></h3>
</div>
<br />
<b style="text-align: justify;"><span style="color: #323333; font-family: Helvetica Neue Light;">Add this code to prepare a Fullscreen Ad at the end of </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">viewDidAppear</span><span style="color: #323333; font-family: Helvetica Neue Light;">:</span></b><br />
<b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"><br /></b>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff;">override</span> <span style="color: #0433ff;">func</span> viewDidAppear(animated: <span style="color: #3495af;">Bool</span>) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff;">super</span>.<span style="color: #3495af;">viewDidAppear</span>(animated)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> ...</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>// Prepare fullscreen Ad</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>UIViewController<span style="color: black;">.</span>prepareInterstitialAds<span style="color: black;">()</span> </div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<span style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"><br /></span>
<br />
<b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;">Add a new method </b><b style="color: #323333; text-align: justify;"><span style="font-family: Courier New, Courier, monospace;">openAds</span></b><b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"> to show a Action Sheet where the player can chose to open the Ad or play again:</b><br />
<br />
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// Open a fullscreen Ad</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> openAds(sender: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">AnyObject</span>) {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Create an alert</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> alert = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertController</span>(title: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">""</span>, message: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Play again?"</span>, preferredStyle: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertControllerStyle</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Alert</span>)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Play again option</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> alert.</span>addAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>UIAlertAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(title: </span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Yes"</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, style: </span>UIAlertActionStyle<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>Default<span style="color: black; font-variant-ligatures: no-common-ligatures;">) { </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">_</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">scene</span>!.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gameOver</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span></div>
<div style="font-family: Menlo; font-size: 11px;">
})</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Show fullscreen Ad option</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> alert.</span>addAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>UIAlertAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(title: </span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Watch Ad"</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, style: </span>UIAlertActionStyle<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>Default<span style="color: black; font-variant-ligatures: no-common-ligatures;">) { </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">_</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>interstitialPresentationPolicy<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>ADInterstitialPresentationPolicy<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>Manual</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>requestInterstitialAdPresentation<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">scene</span>!.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gameOver</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span></div>
<div style="font-family: Menlo; font-size: 11px;">
})</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">presentViewController</span>(alert, animated: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span>, completion: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgq9Dsn9IwxzERfWtjZNvdGCEoZNHsc9oI2SSHDj9SOa7hMai6reJk48ajgltnkMMpbLlEnzChwyXMjCm63FYzIJXNPiqQHEPWbqZBX5WTxdyDxddtMXTlMZZwRQalpzWV_zbUAxTaOOP4/s1600/Screen+Shot+2015-02-08+at+22.20.11.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="179" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgq9Dsn9IwxzERfWtjZNvdGCEoZNHsc9oI2SSHDj9SOa7hMai6reJk48ajgltnkMMpbLlEnzChwyXMjCm63FYzIJXNPiqQHEPWbqZBX5WTxdyDxddtMXTlMZZwRQalpzWV_zbUAxTaOOP4/s1600/Screen+Shot+2015-02-08+at+22.20.11.png" width="320" /></a></div>
<br />
<br />
<b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;">Add the call of the action sheet method in </b><b style="color: #323333; text-align: justify;"><span style="font-family: Courier New, Courier, monospace;">gameOver</span></b><b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"> and </b><b style="color: #323333; text-align: justify;"><span style="font-family: Courier New, Courier, monospace;">gameCenterViewControllerDidFinish</span></b><b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;">:</b><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// Show game center leaderboard</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> gameOver() {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff;">...</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Show leaderboard</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>GKLocalPlayer<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>localPlayer<span style="color: black; font-variant-ligatures: no-common-ligatures;">().</span>authenticated<span style="color: black; font-variant-ligatures: no-common-ligatures;"> == </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">presentViewController</span>(gcViewController, animated: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span>, completion: {</div>
<div style="font-family: Menlo; font-size: 11px;">
})</div>
<div style="font-family: Menlo; font-size: 11px;">
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Show fullscreen Ad</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: red;"><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">openAds</span>(<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<b style="color: #323333; font-family: 'Helvetica Neue Light'; text-align: justify;"><br /></b>
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// Continue the game after GameCenter is closed</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> gameCenterViewControllerDidFinish(gameCenterViewController: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">GKGameCenterViewController</span>!) {</div>
<div style="font-family: Menlo; font-size: 11px;">
gameCenterViewController.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">dismissViewControllerAnimated</span>(<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span>, completion: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Show fullscreen Ad</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: red;"><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">openAds</span>(<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgd8GW0WYGQWGIoL3hRZ31P9QWYGfYRCpD_-39ald_xdPMmpE-fE2YuqrgHyUZL6Nv28KvseXNZQiAYr2mJhVt63P3uaAzEa-qpJoCJG0mKG8o-pO9zCXXKzsUICCT1a2o5ZuScaPgYPsw/s1600/Screen+Shot+2015-02-08+at+22.20.56.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgd8GW0WYGQWGIoL3hRZ31P9QWYGfYRCpD_-39ald_xdPMmpE-fE2YuqrgHyUZL6Nv28KvseXNZQiAYr2mJhVt63P3uaAzEa-qpJoCJG0mKG8o-pO9zCXXKzsUICCT1a2o5ZuScaPgYPsw/s1600/Screen+Shot+2015-02-08+at+22.20.56.png" width="177" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">
</span></span></span>
<br />
<div style="text-align: justify;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">That's all for today. You can download the code from GitHub: <a href="https://github.com/stfnjstn/MySecondGame/releases/tag/v0.7">Part 7</a> or the <a href="https://github.com/stfnjstn/MySecondGame/tree/master">latest version</a>. </span></span></span></div>
</div>
<br />
<div style="text-align: justify;">
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;">I've submitted this version to the Apple AppStore. </span></span><span style="color: #323333; font-family: 'Helvetica Neue Light';">In one of my next posts I'll show how to implement social media integration. If you want to get an idea what is coming: </span>
</div>
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;">A more sophisticated game based on this tutorial series is available in the AppStore: </span></span><br />
<div style="text-align: center;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></span></span></span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">
</span></span></span>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">Cheers, </span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">Stefan
<br />
</span><br />
</span></span>
<br />
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>
Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com4tag:blogger.com,1999:blog-7769140974661448994.post-60078680135290916802015-01-14T15:01:00.001-08:002015-08-30T10:01:52.743-07:00How to implement a space shooter with SpriteKit and SWIFT - Part 6: Game Center integration<h2>
Adding Game Center integration
</h2>
<h3 style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: bolder; margin: 0.83em 0px; padding: 0px; position: relative; text-align: justify;">
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<iframe allowfullscreen="1" frameborder="0" height="400" src="//www.youtube.com/embed/8d8MH_gXt84" width="600"></iframe></div>
<div style="font-weight: normal;">
<br /></div>
<div style="text-align: center;">
<a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></div>
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<br /></div>
</h3>
<div style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-weight: normal; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<div>
<br />
<div style="text-align: justify;">
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;">
Welcome to part 6 of my swift programming tutorial. In the previous parts we've created sprites, added movement, created enemies which follow our sprite, added bullets, a HUD, collision detection, sound and particle effects. Today I'll show how to integrate Game Center to add a global leaderboard.</span></span>
</div>
<br />
<div style="text-align: justify;">
<span style="color: #323333; font-family: 'Helvetica Neue Light';"><br /></span></div>
<div style="text-align: justify;">
<span style="color: #323333; font-family: 'Helvetica Neue Light';">The most complex part is doing configuration stuff in </span><a href="https://itunesconnect.apple.com/" style="font-family: 'Helvetica Neue Light';"><span style="-webkit-text-stroke-color: rgb(4, 46, 238); color: #551a8b;">iTunes Connect</span></a><span style="color: #323333; font-family: 'Helvetica Neue Light';">. 3 from 4 steps of this tutorial are related to that. You need a paid Apple Developer Account to execute the next steps:</span></div>
</div>
</div>
<div>
<br />
<ul>
<li><span style="color: #323333; font-family: 'Helvetica Neue Light';">Upload our App to iTunes Connect </span></li>
<li><span style="color: #323333; font-family: Helvetica Neue Light;">Create a Leaderboard in iTunes </span><span style="color: #323333; font-family: 'Helvetica Neue Light';">Connect </span></li>
<li><span style="color: #323333; font-family: 'Helvetica Neue Light';">Create a Sandbox Test User in iTunes Connect</span></li>
<li><span style="color: #323333; font-family: 'Helvetica Neue Light';">Coding </span></li>
</ul>
<br />
<a name='more'></a><br />
<h3 style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: bolder; margin: 0.83em 0px; padding: 0px; position: relative; text-align: justify;">
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 14px;">Tutorial Overview:</span></h3>
<div style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-weight: normal; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with.html">Part 1</a>: Initial project setup, sprite creation and movement using </span><span style="font-family: 'Courier New', Courier, monospace;">SKAction</span><span style="font-family: 'Helvetica Neue Light';"> and </span><span style="font-family: 'Courier New', Courier, monospace;">SKConstraint</span></span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_11.html">Part 2</a>: Adding enemies, bullets and shooting with </span></span><span style="color: #323333; font-family: 'Courier New', Courier, monospace;">SKAction</span><span style="color: #323333; font-family: 'Helvetica Neue Light';"> and </span><span style="color: #323333; font-family: 'Courier New', Courier, monospace;">SKConstraint</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_19.html">Part 3</a>: Adding a HUD with </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">SKLabelNode</span><span style="color: #323333; font-family: 'Helvetica Neue Light';"> and </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">SKSpriteNode</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_27.html">Part 4</a>: Adding basic game logic and collision detection</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/12/how-to-implement-space-shooter-with.html">Part 5</a>: Adding particles and sound </span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2015/01/how-to-implement-space-shooter-with.html">Part 6</a>: GameCenter integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2015/02/how-to-implement-space-shooter-with.html">Part 7</a>: iAd integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2015/04/how-to-implement-in-app-purchase-for.html">Part 8</a>: In-App Purchases</span></li>
</ul>
</div>
<br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<br />
<h4>
<span style="color: #323333; font-family: Helvetica Neue Light;"><b>Let's start:
</b></span></h4>
</div>
<div>
<br />
<h3>
<span style="color: #323333; font-family: Helvetica Neue Light;">
1. Upload App to iTunes Connect
</span></h3>
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;">
Check, if XCode is set up properly with your Apple account data:</span></span><br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgr0izUSzn3nW994zac1Y9XTSLjLu23nFCkVcfAhIxHaYxH04YzcTjBfqwPrIet1DcChWLBvVspT3LJNpxQYMxaURMJul4NqrqR4V7R45UOm8V-FBIJAokcenHg7TgcXmxdUSZuiq-t5ns/s1600/Screen+Shot+2015-01-08+at+23.15.57.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="114" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgr0izUSzn3nW994zac1Y9XTSLjLu23nFCkVcfAhIxHaYxH04YzcTjBfqwPrIet1DcChWLBvVspT3LJNpxQYMxaURMJul4NqrqR4V7R45UOm8V-FBIJAokcenHg7TgcXmxdUSZuiq-t5ns/s1600/Screen+Shot+2015-01-08+at+23.15.57.png" width="320" /></a></div>
<span style="color: #323333;"><span style="font-weight: normal;"><br /><span style="font-family: Helvetica Neue Light;"> </span><br /><span style="font-family: Helvetica Neue Light;">
Before submitting your app add appropriate app icons to your project. Otherwise </span><span style="font-family: Courier New, Courier, monospace;">iTunes Connect</span><span style="font-family: Helvetica Neue Light;"> will reject your upload: </span></span></span><br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3eQIMqZ4-wH7YLio-qNmnZg6cgetdEtWeLNMadNB_j7BZHuXVDw6jKOXHFZySqg-avP8UUkpY7fS8oEOKiJ-2Z5hR9NWOUe-Kxl7CQDgl6TLThW1v6oPEQHe02C5LwleLe5D2vdjMPo4/s1600/Screen+Shot+2015-01-08+at+23.57.32.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="108" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3eQIMqZ4-wH7YLio-qNmnZg6cgetdEtWeLNMadNB_j7BZHuXVDw6jKOXHFZySqg-avP8UUkpY7fS8oEOKiJ-2Z5hR9NWOUe-Kxl7CQDgl6TLThW1v6oPEQHe02C5LwleLe5D2vdjMPo4/s1600/Screen+Shot+2015-01-08+at+23.57.32.png" width="320" /></a></div>
<span style="color: #323333;"><span style="font-weight: normal;"><br /><span style="font-family: Helvetica Neue Light;"> </span><br /><span style="font-family: Helvetica Neue Light;">
Enable </span><span style="font-family: Courier New, Courier, monospace;">Game Center</span><span style="font-family: Helvetica Neue Light;"> in the Capabilities Tab of your project. Select your team profile and wait a few seconds till XCode has updated the project settings. </span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFI9UEphLovB_Z0eo0ujCyma-1t-ipE9wBUIVqDLBU1KVNbOREHjY5nvGpyAx4z0d1TpmGxcZ9QdxaulhbbnKcxqrtc77PkfhHEBuzhh9tdAk43j9b_ZfLk6ssbKdkwT0yZkbKxitXIMA/s1600/Screen+Shot+2015-01-08+at+23.05.49.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="66" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFI9UEphLovB_Z0eo0ujCyma-1t-ipE9wBUIVqDLBU1KVNbOREHjY5nvGpyAx4z0d1TpmGxcZ9QdxaulhbbnKcxqrtc77PkfhHEBuzhh9tdAk43j9b_ZfLk6ssbKdkwT0yZkbKxitXIMA/s1600/Screen+Shot+2015-01-08+at+23.05.49.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light; font-weight: normal;"><br /></span><br /><span style="font-family: Helvetica Neue Light; font-weight: normal;">
Create an </span><span style="font-family: Courier New, Courier, monospace; font-weight: normal;">Archive</span><span style="font-family: Helvetica Neue Light; font-weight: normal;"> of your app:
</span></span><br />
<div class="separator" style="clear: both; text-align: center;">
<span style="color: #323333;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtneApUKXgQgwrAWoEj0LIndwcitodakgGyxEdz04lXhXmO9qkJkPOl3hnMfbW92IK5BeJc_LDD16NbKzBbTv-3XPmIdHKQLxk4hfSBki4oNFDPItB7lg8e0KvBkG_OdyM68huvcC_F9c/s1600/Screen+Shot+2015-01-08+at+22.50.14.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtneApUKXgQgwrAWoEj0LIndwcitodakgGyxEdz04lXhXmO9qkJkPOl3hnMfbW92IK5BeJc_LDD16NbKzBbTv-3XPmIdHKQLxk4hfSBki4oNFDPItB7lg8e0KvBkG_OdyM68huvcC_F9c/s1600/Screen+Shot+2015-01-08+at+22.50.14.png" width="208" /></a></span></div>
<div style="text-align: center;">
<span style="color: #323333;"><br /></span></div>
<span style="color: #323333;"><br /><span style="font-family: Helvetica Neue Light; font-weight: normal;">
Open the </span><span style="font-family: Courier New, Courier, monospace; font-weight: normal;">Organizer</span><span style="font-family: Helvetica Neue Light; font-weight: normal;"> window: </span></span><br />
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light; font-weight: normal;"><br /></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTnTVHAtx4eyJdTNM8cripCJHS1HYSW9yvXb4-fJQHHFVph6F1xJCKtUk3Us5raomgphROm2boj-nASqEU85x6ZZ41bCi9wP8v8x-wUmj-1RCzaDEfHCtx18nc345y6nqnGpA5xytSIr0/s1600/Screen+Shot+2015-01-08+at+22.51.44.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="210" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTnTVHAtx4eyJdTNM8cripCJHS1HYSW9yvXb4-fJQHHFVph6F1xJCKtUk3Us5raomgphROm2boj-nASqEU85x6ZZ41bCi9wP8v8x-wUmj-1RCzaDEfHCtx18nc345y6nqnGpA5xytSIr0/s1600/Screen+Shot+2015-01-08+at+22.51.44.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHXa1mq181474ax7qM4y-ma6pKCfWPh85ZPESUslFoW2sWYmWWy_jFyrlggg8LTKisFOgUHGOvrSpa6xRaaEJKgDFbivGXA_FkxM5xEvkTfDqVADcxw6upcTi58BKC15SeffEUKKq4Vp0/s1600/Screen+Shot+2015-01-09+at+08.08.24.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="182" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHXa1mq181474ax7qM4y-ma6pKCfWPh85ZPESUslFoW2sWYmWWy_jFyrlggg8LTKisFOgUHGOvrSpa6xRaaEJKgDFbivGXA_FkxM5xEvkTfDqVADcxw6upcTi58BKC15SeffEUKKq4Vp0/s1600/Screen+Shot+2015-01-09+at+08.08.24.png" width="320" /></a></div>
<div style="text-align: center;">
<br /></div>
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light; font-weight: normal;">
Click </span><span style="font-family: Courier New, Courier, monospace; font-weight: normal;">Submit</span><span style="font-family: Helvetica Neue Light; font-weight: normal;"> choose your profile and submit your application to </span><span style="font-family: Courier New, Courier, monospace; font-weight: normal;">iTunesConnect</span><span style="font-family: Helvetica Neue Light; font-weight: normal;">
</span></span><br />
<div class="separator" style="clear: both; text-align: center;">
<span style="color: #323333;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6QB1Cf21jS1YxjJVQfgWxoSXAxhbup3UdE9m_6_VFzl_MdMr5bjyTiWYYDWyA4YCNCrCB16caXN-5TxDKgO14-__9fWYPmdXYbGV1zE1YhqkzBfDefszIQE7xaydWW8jrC3QSuQw0wfM/s1600/Screen+Shot+2015.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="198" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6QB1Cf21jS1YxjJVQfgWxoSXAxhbup3UdE9m_6_VFzl_MdMr5bjyTiWYYDWyA4YCNCrCB16caXN-5TxDKgO14-__9fWYPmdXYbGV1zE1YhqkzBfDefszIQE7xaydWW8jrC3QSuQw0wfM/s1600/Screen+Shot+2015.png" width="320" /></a></span></div>
<div style="text-align: center;">
<span style="color: #323333;"><br /></span></div>
<span style="color: #323333;"><br /><span style="font-family: Helvetica Neue Light; font-weight: normal;">
If you see this error message you haven't created an </span><span style="font-family: Courier New, Courier, monospace; font-weight: normal;">AppId</span><span style="font-family: Helvetica Neue Light; font-weight: normal;"> for your app in </span><span style="font-family: Courier New, Courier, monospace; font-weight: normal;">iTunesConnect</span><span style="font-family: Helvetica Neue Light; font-weight: normal;">:
</span></span><br />
<div class="separator" style="clear: both; text-align: center;">
<span style="color: #323333;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8GDckrjpqQfy2S1JySXnxbb0Xtsc5q-VZViOynZsir8rY6sc8nJtZmM8MOeDkULi4EYeZc8U-Bb_GsQTzlHeHHUJat2hTZ1Tv2VC14rslvyqxHnKI4I-R4Zt62uJGY6PYLYV6Qu2Um1Q/s1600/Screen+Shot+2015-01-08+at+22.56.24.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="193" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8GDckrjpqQfy2S1JySXnxbb0Xtsc5q-VZViOynZsir8rY6sc8nJtZmM8MOeDkULi4EYeZc8U-Bb_GsQTzlHeHHUJat2hTZ1Tv2VC14rslvyqxHnKI4I-R4Zt62uJGY6PYLYV6Qu2Um1Q/s1600/Screen+Shot+2015-01-08+at+22.56.24.png" width="320" /></a></span></div>
<span style="color: #323333;">
</span>
<br />
<div style="text-align: center;">
<span style="color: #323333;"><br /></span></div>
<span style="color: #323333;">
<br /><span style="font-family: Helvetica Neue Light; font-weight: normal;">
Open <a href="https://itunesconnect.apple.com/">iTunesConnect</a> in your browser, select </span><span style="font-family: Courier New, Courier, monospace; font-weight: normal;">MyApps</span><span style="font-family: Helvetica Neue Light; font-weight: normal;">, add a new app and fill out the required fields:
</span><br /><span style="font-family: Helvetica Neue Light; font-weight: normal;">
</span></span><br />
<div class="separator" style="clear: both; text-align: center;">
<span style="color: #323333;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKCrAg4zV4G_RGpm3owh8rqhHJq6Yj-yNOf9bLDen7jFCMXxVOhLWFKy_MZWEEM67Mi-shGn47zAnfqO43BQxKJ97zhv_rSMJHWy7jE4PqBlPGVwcmVGRznd-ZFwMB3aTmS9_afAZGeGM/s1600/Screen+Shot+2015-01-08+at+23.18.26.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKCrAg4zV4G_RGpm3owh8rqhHJq6Yj-yNOf9bLDen7jFCMXxVOhLWFKy_MZWEEM67Mi-shGn47zAnfqO43BQxKJ97zhv_rSMJHWy7jE4PqBlPGVwcmVGRznd-ZFwMB3aTmS9_afAZGeGM/s1600/Screen+Shot+2015-01-08+at+23.18.26.png" /></a></span></div>
<span style="color: #323333;">
<br />
</span><br />
<div class="separator" style="clear: both; text-align: center;">
<span style="color: #323333;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCLAs8kizl3IDni5cIb2TbvssqLH4RYfBYiLzdnlOQpVhAZtYu_JGuEsHVzP-L_4AaFoD1JH1hf4JE2GapkQ7wcnYcf4qwAS4vo8hMp8W3GkVlUvyXrLUaihW4kSyKdjWIWvfVaZx5_Cc/s1600/Screen+Shot+2015-01-08+at+23.18.38.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCLAs8kizl3IDni5cIb2TbvssqLH4RYfBYiLzdnlOQpVhAZtYu_JGuEsHVzP-L_4AaFoD1JH1hf4JE2GapkQ7wcnYcf4qwAS4vo8hMp8W3GkVlUvyXrLUaihW4kSyKdjWIWvfVaZx5_Cc/s1600/Screen+Shot+2015-01-08+at+23.18.38.png" /></a></span></div>
<span style="color: #323333;">
<br />
</span><br />
<div class="separator" style="clear: both; text-align: center;">
<span style="color: #323333;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbjgU6bLm5Ws6avQlrZ1gOwFI0vd2AcZ-FgUWScblkJdj_-pTOYjyGiTJz03xMg4tfi6x5iVVaDWUtQOVliQNt9xJ-gj74ogDxgyonuyTvOH4UZIN73C7B956eVj1TbE2pPJXnuTsdzZc/s1600/Screen+Shot+2015-01-08+at+23.20.30.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="155" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbjgU6bLm5Ws6avQlrZ1gOwFI0vd2AcZ-FgUWScblkJdj_-pTOYjyGiTJz03xMg4tfi6x5iVVaDWUtQOVliQNt9xJ-gj74ogDxgyonuyTvOH4UZIN73C7B956eVj1TbE2pPJXnuTsdzZc/s1600/Screen+Shot+2015-01-08+at+23.20.30.png" width="320" /></a></span></div>
<span style="color: #323333;">
<br />
</span><br />
<div class="separator" style="clear: both; text-align: center;">
<span style="color: #323333;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi82hj4_5seahCH0EzZcADm_oUQGo6TTM2pfLpHOCcIcUZnBimq8fzGCBKtzpDIfOY7fd0Fh3P8tzJX-TJrlSZK1j_iH82mmACP_NKVZU8Xrs9Fyr6gX91hSqT4uaAcMmhZL0UVCcwbCsk/s1600/Screen+Shot+2015-01-08+at+23.20.44.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="101" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi82hj4_5seahCH0EzZcADm_oUQGo6TTM2pfLpHOCcIcUZnBimq8fzGCBKtzpDIfOY7fd0Fh3P8tzJX-TJrlSZK1j_iH82mmACP_NKVZU8Xrs9Fyr6gX91hSqT4uaAcMmhZL0UVCcwbCsk/s1600/Screen+Shot+2015-01-08+at+23.20.44.png" width="320" /></a></span></div>
<span style="color: #323333;">
</span>
<br />
<div style="text-align: center;">
<span style="color: #323333;"><br /></span></div>
<span style="color: #323333;">
<span style="font-family: Helvetica Neue Light; font-weight: normal;">
Go back to XCode, open the Organizer window and submit your app again:
</span><br /><span style="font-family: Helvetica Neue Light; font-weight: normal;">
</span><span style="font-family: Helvetica Neue Light; font-weight: normal;">
</span><br />
</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizQynYnMlO8inCIxWfaOmDgnzHDyd1jo_cV0tPxJN0NLxrkBb9jVNY9nMc3AxRx4X4SDFpe_L0zPkz6iwzAInmnhMeZJod0p3wpolnRJLrznP3-MxJiiJTfIQp5kTOwmLIUaEG34t8Pug/s1600/Screen+Shot+2015-01-08+at+23.58.37.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="191" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizQynYnMlO8inCIxWfaOmDgnzHDyd1jo_cV0tPxJN0NLxrkBb9jVNY9nMc3AxRx4X4SDFpe_L0zPkz6iwzAInmnhMeZJod0p3wpolnRJLrznP3-MxJiiJTfIQp5kTOwmLIUaEG34t8Pug/s1600/Screen+Shot+2015-01-08+at+23.58.37.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAQ0IL3OK4QvtBE3Bg6q9M7cqv01fNUEEJXX2Lt0frwGeQoHna4i1aZKlfJDjzL1kXP30Gcu3vH3fODQeH9Bdb4nHVRVSE3rK2ST9XTqP3w3OLVf5_gR_zrCxaqYxZDWp5H7NpyQGbgJ8/s1600/Screen+Shot+2015-01-09+at+00.00.03.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="196" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAQ0IL3OK4QvtBE3Bg6q9M7cqv01fNUEEJXX2Lt0frwGeQoHna4i1aZKlfJDjzL1kXP30Gcu3vH3fODQeH9Bdb4nHVRVSE3rK2ST9XTqP3w3OLVf5_gR_zrCxaqYxZDWp5H7NpyQGbgJ8/s1600/Screen+Shot+2015-01-09+at+00.00.03.png" width="320" /></a></div>
<div style="text-align: center;">
<span style="color: #323333;"><br /></span></div>
</div>
<div>
<br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<br />
<h3>
<span style="color: #323333; font-family: Helvetica Neue Light;">
2. Create a Leaderboard in iTunesConnect </span></h3>
<br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;">Open <a href="https://itunesconnect.apple.com/">iTunesConnect</a> and select your app: </span></span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixMRzImDVkr_owIla_NuUZeZvOF3hOScM2DnVrZH4aMrBSCgAUVlxZ141_5vJLZqcFr2pTkYfA2xdc0xZKB9yCjTLnClImSKRsOMTtW5Kiih9rn1HLbVCrwGpuh986CLr_nW4iblr-ykw/s1600/Screen+Shot+2015-01-09+at+08.20.11.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixMRzImDVkr_owIla_NuUZeZvOF3hOScM2DnVrZH4aMrBSCgAUVlxZ141_5vJLZqcFr2pTkYfA2xdc0xZKB9yCjTLnClImSKRsOMTtW5Kiih9rn1HLbVCrwGpuh986CLr_nW4iblr-ykw/s1600/Screen+Shot+2015-01-09+at+08.20.11.png" width="223" /></a></div>
<div style="text-align: center;">
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><br /><span style="font-family: Helvetica Neue Light;">
Click on </span><span style="font-family: Courier New, Courier, monospace;">Game Center</span><span style="font-family: Helvetica Neue Light;">: </span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyDQsKOeOLg5F4BlT7TP9Te6SoRSzGrk8NPOH-td4STW8gnHZAVqa1kVj9y2lMVkWvwHPO61nB8lFnTzTmUyiUqthzH6Kied438__AYsTGzjjYHRegy2TrrdCNcpBrNDNEq6LAbGJ7Ujk/s1600/Screen+Shot+2015-01-09+at+08.20.21.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="100" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyDQsKOeOLg5F4BlT7TP9Te6SoRSzGrk8NPOH-td4STW8gnHZAVqa1kVj9y2lMVkWvwHPO61nB8lFnTzTmUyiUqthzH6Kied438__AYsTGzjjYHRegy2TrrdCNcpBrNDNEq6LAbGJ7Ujk/s1600/Screen+Shot+2015-01-09+at+08.20.21.png" width="320" /></a></div>
<div style="text-align: center;">
<span style="color: #323333; font-family: 'Helvetica Neue Light';"><br /></span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">
Select </span><span style="font-family: Courier New, Courier, monospace;">Single Game</span><span style="font-family: Helvetica Neue Light;">: </span></span></span><br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO3Hy3OqRvF8LguAfZJ9PVLC90tix-xVX9Wx89WXxDiIUtddKiJb_CrS-X2-L5eMMYcl2rtcpUzlunXFjTQOnijlbVr4k9R6SURT0oBVvhMYVq5gUlmvhmriTW213z_Bw-6npwWtlEgzo/s1600/Screen+Shot+2015-01-09+at+08.20.30.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="126" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO3Hy3OqRvF8LguAfZJ9PVLC90tix-xVX9Wx89WXxDiIUtddKiJb_CrS-X2-L5eMMYcl2rtcpUzlunXFjTQOnijlbVr4k9R6SURT0oBVvhMYVq5gUlmvhmriTW213z_Bw-6npwWtlEgzo/s1600/Screen+Shot+2015-01-09+at+08.20.30.png" width="320" /></a></div>
<div style="text-align: center;">
<span style="color: #323333; font-family: 'Helvetica Neue Light';"><br /></span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">
Select </span><span style="font-family: Courier New, Courier, monospace;">Add Leaderboard</span><span style="font-family: Helvetica Neue Light;">: </span></span></span><br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFxiML-aRz32CDBzMHhaL2PBS7w2dEb5KcbUcfWftmlX66XxmCVf7lz5hKk2LSvtreF9rMvYQA1g_ribk4glJcjX5dwtk3biJp9VutjOS4vth6g5vk2-BDwnSKJUWDhjjjbX7XAqnzark/s1600/Screen+Shot+2015-01-09+at+22.07.40.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="183" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFxiML-aRz32CDBzMHhaL2PBS7w2dEb5KcbUcfWftmlX66XxmCVf7lz5hKk2LSvtreF9rMvYQA1g_ribk4glJcjX5dwtk3biJp9VutjOS4vth6g5vk2-BDwnSKJUWDhjjjbX7XAqnzark/s1600/Screen+Shot+2015-01-09+at+22.07.40.png" width="320" /></a></div>
<div style="text-align: center;">
<span style="color: #323333; font-family: 'Helvetica Neue Light';"><br /></span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">
Choose </span><span style="font-family: Courier New, Courier, monospace;">Single Leaderboard</span><span style="font-family: Helvetica Neue Light;">: </span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjX0fgrLuH0U3RWcQ1hofh_3DWgJ_dAQ5ykHLMq8-86F8-cHVZUP1wmfJOdfkL5I_BShpL_mzZVKG1KnidHzarWXHaU0of0HgL1oW16omDw0rX0y7_wVS_e226Fd_s7tSGk2d0PfgRJRGg/s1600/Screen+Shot+2015-01-09+at+22.12.31.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="102" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjX0fgrLuH0U3RWcQ1hofh_3DWgJ_dAQ5ykHLMq8-86F8-cHVZUP1wmfJOdfkL5I_BShpL_mzZVKG1KnidHzarWXHaU0of0HgL1oW16omDw0rX0y7_wVS_e226Fd_s7tSGk2d0PfgRJRGg/s1600/Screen+Shot+2015-01-09+at+22.12.31.png" width="320" /></a></div>
<div style="text-align: center;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br />
Configure your Leaderboard: </span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxmrUQS40DzIa7_F4ereCpOk2vAe2GMZXZ7jNLmOQbqALZDtRQgsDqSp1dk2yTdm2aK7Oa0PkkZPvu1yFlpPXbpEcl40T8EYyPk-rMdW_bwWVWZJBif76Do_yhUBLMLvSlP4oatrJ7pOU/s1600/Screen+Shot+2015-01-09+at+22.15.17.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="130" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxmrUQS40DzIa7_F4ereCpOk2vAe2GMZXZ7jNLmOQbqALZDtRQgsDqSp1dk2yTdm2aK7Oa0PkkZPvu1yFlpPXbpEcl40T8EYyPk-rMdW_bwWVWZJBif76Do_yhUBLMLvSlP4oatrJ7pOU/s1600/Screen+Shot+2015-01-09+at+22.15.17.png" width="320" /></a></div>
<div style="text-align: center;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br />
Add at least one language: </span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjoywb8tR5IW7VwLpAqwUlUNKkhrl3UTaLCqp_y96CvGv2AnWYs9fCYvabfHfCfgutICEEKUie5wJjfUGi1EsLm-Vk-q4vuvKR37Gv7uzbR9HfCGFzEoDMHl1Jvyw6qX7zvEcLJ-NO7qI/s1600/Screen+Shot+2015-01-09+at+22.16.01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="149" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjoywb8tR5IW7VwLpAqwUlUNKkhrl3UTaLCqp_y96CvGv2AnWYs9fCYvabfHfCfgutICEEKUie5wJjfUGi1EsLm-Vk-q4vuvKR37Gv7uzbR9HfCGFzEoDMHl1Jvyw6qX7zvEcLJ-NO7qI/s1600/Screen+Shot+2015-01-09+at+22.16.01.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVqeBAoOjhrv1RHr85768X14WyXRZBQ2OtdmWadMs0NcgzNaW_ZzDARcS6Uq8c_kYlseBVO_roecPwfqsFJ_EAqvEMSEXpOFkqAoO4Yc8pB1uVmbSHSPfPrSFuvCjRbKubbAU__6gkX-4/s1600/Screen+Shot+2015-01-09+at+22.16.12.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="95" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVqeBAoOjhrv1RHr85768X14WyXRZBQ2OtdmWadMs0NcgzNaW_ZzDARcS6Uq8c_kYlseBVO_roecPwfqsFJ_EAqvEMSEXpOFkqAoO4Yc8pB1uVmbSHSPfPrSFuvCjRbKubbAU__6gkX-4/s1600/Screen+Shot+2015-01-09+at+22.16.12.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitF_X4gXTAJdbPXEDpHFPePkuqcdSHXEIOhEaaggYpW1WeKcSaHJiyDtCRrDrQmVwcc4mQ6uwDeENLpHRTLjw-muO9Rz46-IjMpOTuIGYxCjOlvSDWvEnfWV2ltyK7mCOX8k8rvlryFR0/s1600/Screen+Shot+2015-01-09+at+22.17.57.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="109" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitF_X4gXTAJdbPXEDpHFPePkuqcdSHXEIOhEaaggYpW1WeKcSaHJiyDtCRrDrQmVwcc4mQ6uwDeENLpHRTLjw-muO9Rz46-IjMpOTuIGYxCjOlvSDWvEnfWV2ltyK7mCOX8k8rvlryFR0/s1600/Screen+Shot+2015-01-09+at+22.17.57.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">
Confirm your changes by selecting Done. Now back to XCode.
<br />
</span></span></span><br />
<h3>
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;">
3. Create a Sandbox Test User in iTunes Connect
</span></span></h3>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">
To test the Game Center integration inside your App you need to create one or more </span><span style="font-family: Courier New, Courier, monospace;">Sandbox Testers</span><span style="font-family: Helvetica Neue Light;"> in iTunes Connect. More details about creating them can be found in the <a href="https://developer.apple.com/library/ios/documentation/LanguagesUtilities/Conceptual/iTunesConnect_Guide/Chapters/SettingUpUserAccounts.html">Apple Documentation</a>. </span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br />

Open the Users and Roles section in </span><span style="font-family: Courier New, Courier, monospace;">iTunes Connect</span><span style="font-family: Helvetica Neue Light;">: </span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivU4pOFIsQFkbBdp__Cfbauajhl_f6lraLfL69SeGBwg4ICErixtJ1Htaa4Qn1pLtHEOrf0aIRzEqXNbWuqM4NjHleFyr8kqOBdyzkhFak0Kryn7uw1ZTygG2HLNpupSu8H1FfjARgdu8/s1600/Screen+Shot+2015-01-09+at+23.10.17.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivU4pOFIsQFkbBdp__Cfbauajhl_f6lraLfL69SeGBwg4ICErixtJ1Htaa4Qn1pLtHEOrf0aIRzEqXNbWuqM4NjHleFyr8kqOBdyzkhFak0Kryn7uw1ZTygG2HLNpupSu8H1FfjARgdu8/s1600/Screen+Shot+2015-01-09+at+23.10.17.png" /></a></div>
<div style="text-align: center;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br />
Choose the Sandbox Testers tab at the right and create at least one: </span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhzMx3-uhzhYaKOGdHLriFZgy9krg_kw11RON4E5VF2bMlIf9StmQE2VxA9Ndbq0vGumfYlP7LTCs2rFag8bWY3f7K0iRUJqeGrkV3yXAEcNvSvkuvbB0g_f2PRyKCo8TQf-_ElzRSODNo/s1600/Screen+Shot+2015-01-09+at+23.10.26.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="58" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhzMx3-uhzhYaKOGdHLriFZgy9krg_kw11RON4E5VF2bMlIf9StmQE2VxA9Ndbq0vGumfYlP7LTCs2rFag8bWY3f7K0iRUJqeGrkV3yXAEcNvSvkuvbB0g_f2PRyKCo8TQf-_ElzRSODNo/s1600/Screen+Shot+2015-01-09+at+23.10.26.png" width="320" /></a></div>
<div style="text-align: center;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br />
Fill out the required fields. You need a valid email account for this: </span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_R-I7uJuJP5f1XDzmLCKBAdBET-xmNySlktCB2ySdNnbaN6kJgILVamGzq5qne6bPFRlNOASY42VmpmPajfn9qcgobOhQH5Sf-8HOu08Q4eyCdBAds6d06Y7oiER4y30lC_MaOwUdiC0/s1600/Screen+Shot+2015-01-09+at+23.10.44.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="190" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_R-I7uJuJP5f1XDzmLCKBAdBET-xmNySlktCB2ySdNnbaN6kJgILVamGzq5qne6bPFRlNOASY42VmpmPajfn9qcgobOhQH5Sf-8HOu08Q4eyCdBAds6d06Y7oiER4y30lC_MaOwUdiC0/s1600/Screen+Shot+2015-01-09+at+23.10.44.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuS2C63XyhY5J_LLSPtn2EnbjaUBJes3x-mcn9N2zKZkavHXFFcJgxaHsczpOVYV2osTZGh7CNZrU5ImZCK17NWBLuxAlY-V-LqyBRguFNmdysyYqUQtplJmmx2LdZX4m95ITJBMyqrbM/s1600/Screen+Shot+2015-01-09+at+23.16.55.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="80" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuS2C63XyhY5J_LLSPtn2EnbjaUBJes3x-mcn9N2zKZkavHXFFcJgxaHsczpOVYV2osTZGh7CNZrU5ImZCK17NWBLuxAlY-V-LqyBRguFNmdysyYqUQtplJmmx2LdZX4m95ITJBMyqrbM/s1600/Screen+Shot+2015-01-09+at+23.16.55.png" width="320" /></a></div>
<div style="text-align: center;">
<span style="color: #323333; font-family: 'Helvetica Neue Light';"><br /></span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">
</span></span></span>
<br />
<h3>
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;">
4. Coding
</span></span></h3>
<span style="color: #323333;"></span><br />
<div style="text-align: justify;">
<span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light';">Comparing to the iTunes Connect stuff the code changes are simple. I'll implement it in </span><span style="font-family: Courier New, Courier, monospace;">GameViewController.swift</span><span style="font-family: 'Helvetica Neue Light';"> and </span><span style="font-family: Courier New, Courier, monospace;">GameScene.swift</span><span style="font-family: 'Helvetica Neue Light';">. As the first code change import the </span><span style="font-family: Courier New, Courier, monospace;">GameKit Framework</span><span style="font-family: 'Helvetica Neue Light';"> in both files: </span></span></div>
<span style="color: #323333;">
</span>
<br />
<div style="text-align: justify;">
<span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light';"><br /></span></span></div>
<span style="color: #323333;">
</span>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">import</span> GameKit</div>
<div style="text-align: justify;">
</div>
<span style="color: #323333;"></span><br />
<div style="text-align: justify;">
<span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light';"><br /></span></span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br />
The next image shows the </span></span></span><span style="color: #323333; font-family: 'Helvetica Neue Light';">control flow of my</span><span style="color: #323333; font-family: 'Helvetica Neue Light';"> game center integration: </span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgw8aTgkEblZjIJKvEhHMXgG0-r5ng4oyMVu_FZl1GMlBmk71eZnKeGa6JRbP54qRuEEq1yn2_mIMifGViBt_w57ZRvNUHk5jFDbedw0OI7O5XbiIsXNf3n8TzElb41H_uJfr1T4_MAA4/s1600/Bildschirmfoto+2015-01-13+um+23.25.44.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgw8aTgkEblZjIJKvEhHMXgG0-r5ng4oyMVu_FZl1GMlBmk71eZnKeGa6JRbP54qRuEEq1yn2_mIMifGViBt_w57ZRvNUHk5jFDbedw0OI7O5XbiIsXNf3n8TzElb41H_uJfr1T4_MAA4/s1600/Bildschirmfoto+2015-01-13+um+23.25.44.png" width="207" /></a></div>
<div style="text-align: center;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<h4>
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;">
Game Center initialization:
</span></span></h4>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">
Check if Game Center is available. Otherwise present the Login Screen:</span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_PGSVKjkJaP61jhtTirnhatDGratMCdE6qnzK_acRC0mqhHULm9Qyd9RcTxNNAxvG6o6wsOOBtXq2UWWpbpK2oY225BjZsKtDkCpnG2EDvjFcG01oai0qVwva7LKBcy7jf1cfLfe95hc/s1600/IMG_8715.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_PGSVKjkJaP61jhtTirnhatDGratMCdE6qnzK_acRC0mqhHULm9Qyd9RcTxNNAxvG6o6wsOOBtXq2UWWpbpK2oY225BjZsKtDkCpnG2EDvjFcG01oai0qVwva7LKBcy7jf1cfLfe95hc/s1600/IMG_8715.PNG" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZvbRooX7Bof_LeDDFCKsEJbFlO17fSMbHs52aCfea2r-UeiF59IpQbYGaCpn0DbvGC7iffDtoum2bPfTNeEacg48gGxxvB2UtNwVEHvBOKfFDZVVC0_IZ-41Lpp4hkjeK5_ZdmqJNUuQ/s1600/IMG_8714.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZvbRooX7Bof_LeDDFCKsEJbFlO17fSMbHs52aCfea2r-UeiF59IpQbYGaCpn0DbvGC7iffDtoum2bPfTNeEacg48gGxxvB2UtNwVEHvBOKfFDZVVC0_IZ-41Lpp4hkjeK5_ZdmqJNUuQ/s1600/IMG_8714.PNG" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="color: #323333; font-family: Helvetica Neue Light;">Move the </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">Scene</span><span style="color: #323333; font-family: Helvetica Neue Light;"> declaration outside </span><span style="color: #323333; font-family: 'Helvetica Neue Light';">the </span><span style="color: #323333;"><span style="font-family: Courier New, Courier, monospace;">viewDidAppear</span></span><span style="color: #323333; font-family: Helvetica Neue Light;"> method to make it a global property. This is necessary because you need to access the scene outside of </span><span style="color: #323333; font-family: 'Courier New', Courier, monospace;">viewDidAppear</span><span style="color: #323333;"><span style="font-family: Helvetica Neue Light;">:</span></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="color: #323333; font-family: 'Helvetica Neue Light';"><br /></span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">class</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> GameViewController: </span>UIViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;">, </span>GKGameCenterControllerDelegate<span style="color: black; font-variant-ligatures: no-common-ligatures;">, </span>GameSceneProtocol<span style="color: black; font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: red;"><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> scene : <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">GameScene</span>?</span> </div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> viewDidAppear(animated: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Bool</span>) {</div>
<div style="font-family: Menlo; font-size: 11px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">super</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">viewDidAppear</span>(animated)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af;">...</span></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Create a fullscreen Scene object</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: red;"><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">scene</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">GameScene</span>(size: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGSizeMake</span>(width, height))</span></div>
<div style="font-family: Menlo; font-size: 11px;">
</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>scene<span style="color: black; font-variant-ligatures: no-common-ligatures;">!.</span>scaleMode<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = .</span>AspectFill</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
...</div>
<div class="separator" style="clear: both; text-align: left;">
<span style="color: #323333; font-family: 'Helvetica Neue Light';"><br /></span></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="color: #323333; font-family: 'Helvetica Neue Light';">To implement this behavior open </span><span style="font-family: Courier New, Courier, monospace;">GameViewController<span style="color: #323333; text-align: justify;">.swift</span></span><span style="color: #323333; font-family: 'Courier New', Courier, monospace; text-align: justify;"> </span><span style="color: #323333; font-family: 'Helvetica Neue Light';">and add a new method </span><span style="color: #323333;"><span style="font-family: Courier New, Courier, monospace;">initGameCenter</span></span><span style="color: #323333; font-family: 'Helvetica Neue Light';">: </span></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// Initialize Game Center</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> initGameCenter() {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Check if user is already authenticated in game center</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>GKLocalPlayer<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>localPlayer<span style="color: black; font-variant-ligatures: no-common-ligatures;">().</span>authenticated<span style="color: black; font-variant-ligatures: no-common-ligatures;"> == </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Show the Login Prompt for Game Center</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">GKLocalPlayer</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">localPlayer</span>().<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">authenticateHandler</span> = {(viewController, error) -> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Void</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> viewController != <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span> {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">scene!</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gamePaused</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">presentViewController</span>(viewController, animated: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span>, completion: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Add an observer which calls 'gameCenterStateChanged' to handle a changed game center state</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> notificationCenter = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">NSNotificationCenter</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">defaultCenter</span>()</div>
<div style="color: #b4261a; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> notificationCenter.</span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addObserver</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, selector:</span>"gameCenterStateChanged"<span style="color: black; font-variant-ligatures: no-common-ligatures;">, name: </span>"GKPlayerAuthenticationDidChangeNotificationName"<span style="color: black; font-variant-ligatures: no-common-ligatures;">, object: </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<span style="color: #323333; font-family: 'Helvetica Neue Light';">Add another method to </span><span style="font-family: Courier New, Courier, monospace;">GameViewController<span style="color: #323333; text-align: justify;">.swift</span></span><span style="color: #323333; font-family: 'Courier New', Courier, monospace; text-align: justify;"> </span><span style="color: #323333; font-family: Helvetica Neue Light;">which will be called by the </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">observer</span><span style="color: #323333; font-family: Helvetica Neue Light;"> created before in case of a changed game center change:</span><br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><br /></span>
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// Continue the Game, if GameCenter Authentication state </div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// has been changed (login dialog is closed)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> gameCenterStateChanged() {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>scene!<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>gamePaused<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span></div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><br /></span>
<span style="color: #323333; font-family: 'Helvetica Neue Light';">Finally call </span><span style="color: #323333;"><span style="font-family: Courier New, Courier, monospace;">initGameCenter</span></span><span style="color: #323333; font-family: 'Helvetica Neue Light';"> in the </span><span style="color: #323333;"><span style="font-family: Courier New, Courier, monospace;">viewDidLoad</span></span><span style="color: #323333; font-family: 'Helvetica Neue Light';"> method:</span><br />
<span style="color: #323333; font-family: 'Helvetica Neue Light';"><br /></span>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> viewDidLoad() {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">super</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">viewDidLoad</span>()</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Initialize game center</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>initGameCenter<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><br /></span>
<br />
<div style="text-align: justify;">
You can use your sandbox test user to test this on a device. Go to the <span style="font-family: Courier New, Courier, monospace;">Settings</span> Dialog, open <span style="font-family: Courier New, Courier, monospace;">Game Center</span> and click on <span style="font-family: Courier New, Courier, monospace;">Apple-ID</span>. The current user must logout. Scroll down to the developer section and activate the <span style="font-family: Courier New, Courier, monospace;">Sandbox</span> switch. Now start the game and login with your test user.</div>
<h4>
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;">Submit a new leaderboard score:</span></span></h4>
<div style="text-align: justify;">
Open <span style="font-family: Courier New, Courier, monospace;">GameScene.swift</span><span style="color: #323333; font-family: 'Helvetica Neue Light';"> </span>and change the type of the global <span style="font-family: Courier New, Courier, monospace;">score</span> property from <span style="font-family: Courier New, Courier, monospace;">Integer</span>:</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> score = 0</div>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
to <span style="font-family: Courier New, Courier, monospace;">Int64</span>, the type chosen for the leaderboard </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> score : <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Int64</span> = 0</div>
</div>
<div>
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></div>
<div>
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;">Add another global property to store a game over state:</span></span></div>
<div>
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></div>
<div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> gameOver = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"><br /></span></div>
<span style="color: #323333; font-family: Helvetica Neue Light;">Modify the </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">update</span><span style="color: #323333; font-family: Helvetica Neue Light;"> method </span><span style="color: #323333; font-family: Helvetica Neue Light;">to handle the new </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">gameOver</span><span style="color: #323333; font-family: Helvetica Neue Light;"> property:</span><br />
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;"><br /></span></span>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff;">override</span> <span style="color: #0433ff;">func</span> update(currentTime: <span style="color: #3495af;">CFTimeInterval</span>) {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff;">if</span> !<span style="color: #0433ff;">self</span>.<span style="color: #3495af;">gamePaused</span> <span style="color: red;">&& !self.gameOver</span> {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff;">...</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"><br /></span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"><br /></span></div>
<div style="font-size: 11px;">
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: small;">Add/Remove the red marked lines in the </span><span style="color: #323333; font-size: small;"><span style="font-family: Courier New, Courier, monospace;">showGameOverAlert</span></span><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: small;"> method of </span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"><br /></span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> showGameOverAlert() {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff;"> </span><span style="color: red;"><strike>self</strike><strike>.gamePaused = true</strike></span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: red;">self.gameOver = true</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> alert = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertController</span>(title: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Game Over"</span>, message: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">""</span>, preferredStyle: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertControllerStyle</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Alert</span>)</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> alert.</span>addAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>UIAlertAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(title: </span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"OK"</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, style: </span>UIAlertActionStyle<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>Default<span style="color: black; font-variant-ligatures: no-common-ligatures;">) { </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">_</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// restore lifes in HUD</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">remainingLifes</span>=3</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">for</span>(<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> i = 0; i<3; i++) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">lifeNodes</span>[i].<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">alpha</span>=1.0</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// reset score</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: red;"> self.addLeaderboardScore(self.score)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">score</span>=0</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">scoreNode</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">text</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">String</span>(0)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<strike style="color: red;">self</strike><strike style="color: red;">.gamePaused = true</strike></div>
<div style="font-family: Menlo; font-size: 11px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
})</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"><br /></span></div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"><br /></span></div>
</div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">Add a new method </span><span style="font-family: Courier New, Courier, monospace;">addLeaderboardScore</span><span style="font-family: Helvetica Neue Light;"> to submit a new score: </span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> addLeaderboardScore(score: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Int64</span>) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> newGCScore = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">GKScore</span>(leaderboardIdentifier: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"MySecondGameLeaderboard"</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
newGCScore.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">value</span> = score</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">GKScore</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">reportScores</span>([newGCScore], withCompletionHandler: {(error) -> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Void</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> error != <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span> {</div>
<div style="color: #b4261a; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">println</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>"Score not submitted"<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #008f00; font-variant-ligatures: no-common-ligatures;">// Continue</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gameOver</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span></div>
<div style="font-family: Menlo; font-size: 11px;">
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Notify the delegate to show the game center leaderboard:</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #008f00;">// Not implemented yet</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
})</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<h4>
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;">Show the Game Center leaderboard after game over:</span></span></h4>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKATjKSC9kyr7LbogcSxF0_-hvlXi9kUxUIUNntUG1cP4vqI5iZ8mBVbn8h-5P_S8YgO8fWAYrKOHT0nND0zI1w_6aba41PvzSkOM5FY8V9Dv5OOjddytbHmPIPc6AGXATRIiXOjfohKo/s1600/IMG_8712.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKATjKSC9kyr7LbogcSxF0_-hvlXi9kUxUIUNntUG1cP4vqI5iZ8mBVbn8h-5P_S8YgO8fWAYrKOHT0nND0zI1w_6aba41PvzSkOM5FY8V9Dv5OOjddytbHmPIPc6AGXATRIiXOjfohKo/s1600/IMG_8712.PNG" width="320" /></a></div>
<div>
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><br /></span></span>
<br />
<div style="text-align: justify;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="color: #323333;"><span style="font-family: Helvetica Neue Light;">The game center leaderboard can only be called/shown from a </span><span style="font-family: Courier New, Courier, monospace;">ViewController</span><span style="font-family: Helvetica Neue Light;">, not inside a </span><span style="font-family: Courier New, Courier, monospace;">SpriteKit</span><span style="font-family: Helvetica Neue Light;"> </span><span style="font-family: Courier New, Courier, monospace;">Scene</span><span style="font-family: Helvetica Neue Light;">. To </span></span><span style="color: #323333; font-family: Helvetica Neue Light;">notify the container </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">ViewController</span><span style="color: #323333; font-family: Helvetica Neue Light;"> of our scene add a protocol to </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">GameScene.swift</span><span style="color: #323333; font-family: Helvetica Neue Light;">:</span></span></span></div>
<br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><br /></span>
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// protocol to inform the delegate (GameViewController) about a game over situation</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">protocol</span> GameSceneDelegate {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> gameOver()</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">Don't forget to create a global Delegate Property:</span></span></span><br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"><br /></span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> gameCenterDelegate : <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">GameSceneDelegate</span>?</div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span><span style="color: #323333; font-family: Helvetica Neue Light;">Modify </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">addLeaderboardScore</span><span style="color: #323333; font-family: Helvetica Neue Light;"> to notify the delegate</span><span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">:</span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> addLeaderboardScore(score: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Int64</span>) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> newGCScore = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">GKScore</span>(leaderboardIdentifier: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"MySecondGameLeaderboard"</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
newGCScore.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">value</span> = score</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">GKScore</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">reportScores</span>([newGCScore], withCompletionHandler: {(error) -> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Void</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> error != <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span> {</div>
<div style="color: #b4261a; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">println</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>"Score not submitted"<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #008f00; font-variant-ligatures: no-common-ligatures;">// Continue</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gameOver</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span></div>
<div style="font-family: Menlo; font-size: 11px;">
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Notify the delegate to show the game center leaderboard</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span><span style="color: red;"> self.gameCenterDelegate!.gameOver()</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
})</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span><span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span><br />
<div style="text-align: justify;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><span style="color: #323333;"><span style="font-family: Helvetica Neue Light;">Open </span><span style="font-family: Courier New, Courier, monospace;">GameViewController.swift</span><span style="font-family: Helvetica Neue Light;"> </span></span><span style="color: #323333;"><span style="font-family: Helvetica Neue Light;">and add these protocols to</span></span> the declaration of GameViewController:</span></span></span></div>
<br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">class</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> GameViewController: </span>UIViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;">, </span>GameSceneDelegate, GKGameCenterControllerDelegate<span style="color: black;">{</span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div style="text-align: justify;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">The first one is used by our game scene class to notify about a game over event. The second one is provided by GameKit to notify when the leaderboard has been closed.</span></span></span></div>
<br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">Now set the Delegate property for the scene class:</span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> viewDidAppear(animated: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Bool</span>) {</div>
<div style="font-family: Menlo; font-size: 11px;">
...</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Create a fullscreen Scene object</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">scene</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">GameScene</span>(size: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGSizeMake</span>(width, height))</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>scene<span style="color: black; font-variant-ligatures: no-common-ligatures;">!.</span>scaleMode<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = .</span>AspectFill</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span><span style="color: red;">scene!.gameCenterDelegate = self</span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">To show the leaderboard implement the </span><span style="font-family: Courier New, Courier, monospace;">gameOver</span><span style="font-family: Helvetica Neue Light;"> protocol method:</span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// Show game center leaderboard</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> gameOver() {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> gcViewController = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">GKGameCenterViewController</span>()</div>
<div style="font-family: Menlo; font-size: 11px;">
gcViewController.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gameCenterDelegate</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> gcViewController.</span>viewState<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>GKGameCenterViewControllerState<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>Leaderboards</div>
<div style="font-family: Menlo; font-size: 11px;">
gcViewController.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">leaderboardIdentifier</span> = <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"MySecondGameLeaderboard"</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Show leaderboard</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">presentViewController</span>(gcViewController, animated: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span>, completion: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<br />
<span style="color: #323333; font-family: Helvetica Neue Light;">To continue the game implement this</span><span style="color: #323333; font-family: Helvetica Neue Light;"> protocol method:</span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// Continue the game after GameCenter is closed</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff;">func</span> gameCenterViewControllerDidFinish(gameCenterViewController: <span style="color: #3495af;">GKGameCenterViewController</span>!) {</div>
<div style="font-family: Menlo; font-size: 11px;">
gameCenterViewController.<span style="color: #3495af;">dismissViewControllerAnimated</span>(<span style="color: #0433ff;">true</span>, completion: <span style="color: #0433ff;">nil</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af;">scene</span>!.<span style="color: #3495af;">gameOver</span> = <span style="color: #0433ff;">false</span></div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">
That's all for today. You can download the code from GitHub: <a href="https://github.com/stfnjstn/MySecondGame/releases/tag/v0.6">Part 6</a> or the <a href="https://github.com/stfnjstn/MySecondGame/tree/master">latest version</a>. In my next tutorial I'll show how to integrate Apples advertising framework iAD.
<br /><br />
</span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">You can also download my prototyping App for this tutorial series: </span></span></span><br />
<div style="text-align: center;">
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></span></span></span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">
</span></span></span>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">Cheers, </span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">Stefan
<br />
</span><br />
</span></span></div>
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>
Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com3tag:blogger.com,1999:blog-7769140974661448994.post-1986841790763757422014-12-17T13:41:00.001-08:002015-08-30T10:03:51.089-07:00How to implement a space shooter with SpriteKit and SWIFT - Part 5: Particles and Sound<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 24px; text-align: justify;">Adding particles and sound </span><br />
<h3 style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: bolder; margin: 0.83em 0px; padding: 0px; position: relative; text-align: justify;">
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<iframe allowfullscreen="1" frameborder="0" height="400" src="//www.youtube.com/embed/8d8MH_gXt84" width="600"></iframe></div>
<div style="font-weight: normal;">
<br /></div>
<div style="text-align: center;">
<a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></div>
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<br /></div>
</h3>
<div style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-weight: normal; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;">Welcome to part 5 of my tutorial series. In the previous parts we've created a sprite, added movement, created enemies which follow our sprite, added bullets, a HUD, collision detection, .... </span></span><br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;">But, for a real game some important elements are missing to beautify the game:</span></span><br />
<ul>
<li><span style="color: #323333; font-family: 'Helvetica Neue Light';">Sound effects: explosions, game over, ...</span></li>
<li><span style="color: #323333; font-family: Helvetica Neue Light;">Particle effects: explosions, background star field </span></li>
</ul>
<br />
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;">I'll show how to implement this today. As a starting point you can download the code from tutorial part 4 </span><a href="https://github.com/stfnjstn/MySecondGame/releases/tag/v0.4" style="font-family: 'Helvetica Neue Light'; font-weight: normal;">here</a><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;">. </span><br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"></span></span><br />
<a name='more'></a><span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<br />
<h3 style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: bolder; margin: 0.83em 0px; padding: 0px; position: relative; text-align: justify;">
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 14px;">Tutorial Overview:</span></h3>
<div style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-weight: normal; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with.html">Part 1</a>: Initial project setup, sprite creation and movement using </span><span style="font-family: 'Courier New', Courier, monospace;">SKAction</span><span style="font-family: 'Helvetica Neue Light';"> and </span><span style="font-family: 'Courier New', Courier, monospace;">SKConstraint</span></span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_11.html">Part 2</a>: Adding enemies, bullets and shooting with </span></span><span style="color: #323333; font-family: 'Courier New', Courier, monospace;">SKAction</span><span style="color: #323333; font-family: 'Helvetica Neue Light';"> and </span><span style="color: #323333; font-family: 'Courier New', Courier, monospace;">SKConstraint</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_19.html">Part 3</a>: Adding a HUD with </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">SKLabelNode</span><span style="color: #323333; font-family: 'Helvetica Neue Light';"> and </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">SKSpriteNode</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_27.html">Part 4</a>: Adding basic game logic and collision detection</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/12/how-to-implement-space-shooter-with.html">Part 5</a>: Adding particles and sound </span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="https://www.blogger.com/I've%20finished%20Tutorial%20Part%206:%20How%20to%20impl.%20a%20space%20shooter%20in%20#SpriteKit and #SWIFT' - Game Center integration: http://stefansdevplayground.blogspot.de/2015/01/how-to-implement-space-shooter-with.html">Part 6</a>: GameCenter integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2015/02/how-to-implement-space-shooter-with.html">Part 7</a>: iAd integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2015/04/how-to-implement-in-app-purchase-for.html">Part 8</a>: In-App Purchases</span></li>
</ul>
</div>
<h3>
<span style="color: #323333; font-family: Helvetica Neue Light;"><br /></span></h3>
<h3>
<span style="color: #323333; font-family: Helvetica Neue Light;">Let's start:</span></h3>
<div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><br /></span></div>
<h4>
<span style="color: #323333; font-family: Helvetica Neue Light;">1. Adding sound effects</span></h4>
<div>
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;">First me need some sound effects in WAV format, which is supported out of the box by iOS. There are several ways to get them. For example:</span><br />
<br />
<ul>
<li><span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;">GarageBand: An excellent </span></span><span style="font-weight: normal;"><a href="http://www.raywenderlich.com/26341/how-to-make-game-music-with-garage-band">tutorial</a></span><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"> is available on raywenderlich.com.</span></li>
<li><span style="font-weight: normal;"><a href="http://thirdcog.eu/apps/cfxr">CFXR</a>: A very simple tool to create 8Bit sound effects</span></li>
</ul>
<br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;">To keep your project structure clean, create a new group and name it 'resources':</span></span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguvvY42xnwx35pOOS_93V5f8HR1slPmlfEVWwBjRZLzyCGAKn4QgbZb7WVDVk50az8bSamE91AA7z5qR95Cpj5_baHQaieg0JZnlslCrdG5o_IEaH_ff4sdi0sU5a01dD3821PvbY_zrs/s1600/Bildschirmfoto+2014-12-15+um+21.25.22.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="123" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguvvY42xnwx35pOOS_93V5f8HR1slPmlfEVWwBjRZLzyCGAKn4QgbZb7WVDVk50az8bSamE91AA7z5qR95Cpj5_baHQaieg0JZnlslCrdG5o_IEaH_ff4sdi0sU5a01dD3821PvbY_zrs/s1600/Bildschirmfoto+2014-12-15+um+21.25.22.png" width="320" /></a></div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;">Once you have a wav file add it to your project:</span></span><br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHON_7OCM4wNoTmnrwmhZ7hdt2OtGd2FhG6zrcXYNYXwkp1yFBGuHKEBow0DnYymRejfXaQ3WxZisS05SK-VKyqlHi8kKhsb7O3aNiY9V8Ij5xTKxi1Os26bq6gEXihc51KYjY_eDqjMc/s1600/Bildschirmfoto+2014-12-15+um+21.25.45.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHON_7OCM4wNoTmnrwmhZ7hdt2OtGd2FhG6zrcXYNYXwkp1yFBGuHKEBow0DnYymRejfXaQ3WxZisS05SK-VKyqlHi8kKhsb7O3aNiY9V8Ij5xTKxi1Os26bq6gEXihc51KYjY_eDqjMc/s1600/Bildschirmfoto+2014-12-15+um+21.25.45.png" /></a></div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">To play the sound add this line of code to your </span><span style="font-family: Courier New, Courier, monospace;">lifeLost</span><span style="font-family: Helvetica Neue Light;"> method:</span></span></span><br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<br />
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
runAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>SKAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>playSoundFileNamed<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Explosion.wav"</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, waitForCompletion: </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">))</span></div>
</div>
<div>
<br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;">That's all!</span></span></div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<br />
<h3>
<span style="color: #323333; font-family: Helvetica Neue Light;">2. Adding particle effects</span></h3>
<span style="font-weight: normal;">The video at the top shows two different kind of particle effects. One for explosions and one for the background star field. I'll use the </span><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;">built in particle editor from XCode to create these effects. </span><span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span><span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span><br />
<h4>
<span style="color: #323333; font-family: Helvetica Neue Light;">2.1 Add Explosions</span></h4>
<h4>
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;">Add a new 'SpriteKit Particle File' to your project:</span></h4>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjdsklc6w_dtf4K4KLv4sTgCHR9yGyvipoWs-yxHLhAPX8Bx1VxDGhemGJZ577i7yNpOMUYZQhfzveZ_I8KiO8KxuznCrnNqtgefUejjlpVL8wraks7DKfR5PtssTasFBmCYhSJg8YyLg/s1600/Bildschirmfoto+2014-12-15+um+22.56.58.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="187" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjdsklc6w_dtf4K4KLv4sTgCHR9yGyvipoWs-yxHLhAPX8Bx1VxDGhemGJZ577i7yNpOMUYZQhfzveZ_I8KiO8KxuznCrnNqtgefUejjlpVL8wraks7DKfR5PtssTasFBmCYhSJg8YyLg/s1600/Bildschirmfoto+2014-12-15+um+22.56.58.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-weight: normal;">Choose template type '<span style="font-family: Courier New, Courier, monospace;">fire</span>' and name it '<span style="font-family: Courier New, Courier, monospace;">ExplosionParticle</span>'</span></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgakdrYVaQRqi9nmZug5FfvUTitEFfP-wZYQozbt03XjnWl7nchEDFTV0ew1pW_BCRK58y7Jjc91zalzmo32EOkmtl1GZtL2My4T8AchTljCqW4JQ8iB_M47GvpyDhxVFuSfh0xQrxjQnw/s1600/Bildschirmfoto+2014-12-15+um+22.57.06.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="188" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgakdrYVaQRqi9nmZug5FfvUTitEFfP-wZYQozbt03XjnWl7nchEDFTV0ew1pW_BCRK58y7Jjc91zalzmo32EOkmtl1GZtL2My4T8AchTljCqW4JQ8iB_M47GvpyDhxVFuSfh0xQrxjQnw/s1600/Bildschirmfoto+2014-12-15+um+22.57.06.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvTtk8Khf4vx2Odpim9nCvM25wZBTyoyUobofkHTMjnAz3YPld4TA3Qc2kxmgyOztffYZKMULPzY62bC0w7KtW0HcycYRRLR-P76J8U6PDqzvp7yczWvRjF-Rjqzt1F8YQcYacCyfPoXU/s1600/Bildschirmfoto+2014-12-15+um+22.57.53.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvTtk8Khf4vx2Odpim9nCvM25wZBTyoyUobofkHTMjnAz3YPld4TA3Qc2kxmgyOztffYZKMULPzY62bC0w7KtW0HcycYRRLR-P76J8U6PDqzvp7yczWvRjF-Rjqzt1F8YQcYacCyfPoXU/s1600/Bildschirmfoto+2014-12-15+um+22.57.53.png" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-weight: normal;">The result will look like this:</span></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfKhMbqznpePo5dguax787rxCNXaJ-G1F7PXDdVm-B4Dw84uNs95sEq3_7oHTh4Gxt0WVVxliuLHsBLIXRjyk6Vv464zNx3JuDe-S8ElNAWDC4jwMa7NOHHPMDIHdIEaop-GXV_756vcE/s1600/Bildschirmfoto+2014-12-15+um+22.59.44.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfKhMbqznpePo5dguax787rxCNXaJ-G1F7PXDdVm-B4Dw84uNs95sEq3_7oHTh4Gxt0WVVxliuLHsBLIXRjyk6Vv464zNx3JuDe-S8ElNAWDC4jwMa7NOHHPMDIHdIEaop-GXV_756vcE/s1600/Bildschirmfoto+2014-12-15+um+22.59.44.png" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: justify;">
<span style="font-weight: normal;">Change 'Position Range' to 50 for 'X' and 'Y' to get an particle object with same height and width. </span><span style="font-weight: normal;">Currently the particles are moving upwards. A circle movement from one center into all directions looks more like an explosion. To achieve this set the 'Start Angle' to 0 and change the 'Range' to 360 degrees. </span><span style="font-weight: normal; text-align: left;">You can also play with the 'Speed' values to adjust the explosion effect.</span></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1LUCUNHOeUzOuWJn9PJetXR4uyMqSxFipMawovH5BGKllz7vWsiSAANB3Z0d9hTeOkB-WPGgMO-_cyFEe813nYdhEBfzqsbqJ-w462sXKf4z646i6a0ndXG4YeC32s_D6-B9xqpdCd7g/s1600/Bildschirmfoto+2014-12-15+um+23.03.24.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1LUCUNHOeUzOuWJn9PJetXR4uyMqSxFipMawovH5BGKllz7vWsiSAANB3Z0d9hTeOkB-WPGgMO-_cyFEe813nYdhEBfzqsbqJ-w462sXKf4z646i6a0ndXG4YeC32s_D6-B9xqpdCd7g/s1600/Bildschirmfoto+2014-12-15+um+23.03.24.png" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-weight: normal;"><br /></span></div>
<div class="separator" style="clear: both; text-align: justify;">
<span style="font-weight: normal;">The timeframe for the explosion depends on 'Birthrate' and 'Maximum'. A smaller maximum together with a high birthrate leads to a short explosion effect. </span><span style="font-weight: normal;">The size can be changed with the 'Lifetime' values. A longer particle lifetime will result in a bigger explosion.</span></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheUIrFLR2-0wftaKY6dLiy7fRuEh601ldAR4fSIs3KFNo_r0kN923TaRlXjQ_5e9HkjO9V9ckEnzcz-CxNd4g4L5NCQYfvCbu6WsynIT3kiT7Sy8ebfVmDtQbtMSGgrMbmCA-gVyFA5bM/s1600/Bildschirmfoto+2014-12-15+um+23.26.18.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheUIrFLR2-0wftaKY6dLiy7fRuEh601ldAR4fSIs3KFNo_r0kN923TaRlXjQ_5e9HkjO9V9ckEnzcz-CxNd4g4L5NCQYfvCbu6WsynIT3kiT7Sy8ebfVmDtQbtMSGgrMbmCA-gVyFA5bM/s1600/Bildschirmfoto+2014-12-15+um+23.26.18.png" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEWVYtZDOk_W5hnS8rn2HflyZyvLGbp-1rzPT2CTpmPSBZ6XfG-DJF4eQfDNc9d3t1RydpvxxYkCqz3wVSR3FdikfifNZ8Zt02SEa_Y9CyopaldkmjLDxNXlJkt1O-fOxb6l73wdKg4wk/s1600/Bildschirmfoto+2014-12-15+um+23.03.31.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEWVYtZDOk_W5hnS8rn2HflyZyvLGbp-1rzPT2CTpmPSBZ6XfG-DJF4eQfDNc9d3t1RydpvxxYkCqz3wVSR3FdikfifNZ8Zt02SEa_Y9CyopaldkmjLDxNXlJkt1O-fOxb6l73wdKg4wk/s1600/Bildschirmfoto+2014-12-15+um+23.03.31.png" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div>
<span style="font-weight: normal;">Now, add a new method <span style="font-family: Courier New, Courier, monospace;">explosion</span> to <span style="font-family: Courier New, Courier, monospace;">GameScene.swift</span>:</span></div>
<div>
<span style="font-weight: normal;"><br /></span></div>
<div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> explosion(pos: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>) {<br />
<span style="color: #0433ff;">var</span> emitterNode = <span style="color: #3495af;">SKEmitterNode</span>(fileNamed: <span style="color: #b4261a;">"ExplosionParticle.sks"</span>)<br />
emitterNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">particlePosition</span> = pos<br />
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addChild</span>(emitterNode)<br />
// Don't forget to remove the emitter node after the explosion<br />
<div style="color: #3495af;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>runAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>SKAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>waitForDuration<span style="color: black; font-variant-ligatures: no-common-ligatures;">(2), completion: { emitterNode.</span>removeFromParent<span style="color: black; font-variant-ligatures: no-common-ligatures;">() })</span></div>
<br />
}</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<br /></div>
</div>
<div>
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><br /></span></div>
<div>
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;">Call the new method from </span><span style="color: #323333; font-weight: normal;"><span style="font-family: Courier New, Courier, monospace;">lifeLost</span></span><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;">:</span></div>
<div>
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><br /></span></div>
<div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff;">func</span> lifeLost() {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>explosion<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> ...</span></div>
</div>
<div>
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><br /></span>
<br />
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<iframe allowfullscreen="1" frameborder="0" height="400" src="//www.youtube.com/embed/4DouwA_t-fE" width="600"></iframe></div>
<h4>
<span style="color: #323333; font-family: Helvetica Neue Light;">2.2 Add a startfield:</span></h4>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">To create a starfield with a parallax effect I'll combine multiple emitter nodes. The node on top will show big, light stars which moves fast. The stars for the middle layer are medium sized and darker with a slower movement. The background layer has the darkest, smallest and slowest stars. To avoid the effort to create 3 different '</span><span style="font-family: Courier New, Courier, monospace;">sks</span><span style="font-family: Helvetica Neue Light;">' files I'll create the particles with code.</span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<span style="font-weight: normal;"><span style="color: #323333;"><span style="font-family: Helvetica Neue Light;">Add an image with a star to the asset catalog</span></span><span style="color: #323333; font-family: Helvetica Neue Light;">: </span></span><br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5xGZtLvVjHk4Yn-W7G58j5QBk_0dhZEZst5tvTxKNzj52kw3gZrLGb1eIf55QdcUey7kStnSzPuz7LiXbXtNX1ihpQfzLHPJ5ObL-d9YYjJKlCIrAxU7pV6xFo4gCDgWaCkyaJtq8k6Q/s1600/Star.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5xGZtLvVjHk4Yn-W7G58j5QBk_0dhZEZst5tvTxKNzj52kw3gZrLGb1eIf55QdcUey7kStnSzPuz7LiXbXtNX1ihpQfzLHPJ5ObL-d9YYjJKlCIrAxU7pV6xFo4gCDgWaCkyaJtq8k6Q/s1600/Star.png" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDW-odaFif_XrqHMf-Hhyphenhyphen8Dzb80reHkLrjr1U6yNaEEDJJCfXvkAMIdRJh_WTJpxAZf7FqwKdUO_rlWZG9QdPvkUv6nZcOP1gdrz3fo4mWI_wRS1eFfROKUX0C9PQQIpHMMsMx-uscSAA/s1600/Bildschirmfoto+2014-12-17+um+22.38.16.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="162" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDW-odaFif_XrqHMf-Hhyphenhyphen8Dzb80reHkLrjr1U6yNaEEDJJCfXvkAMIdRJh_WTJpxAZf7FqwKdUO_rlWZG9QdPvkUv6nZcOP1gdrz3fo4mWI_wRS1eFfROKUX0C9PQQIpHMMsMx-uscSAA/s1600/Bildschirmfoto+2014-12-17+um+22.38.16.png" width="400" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<span style="font-weight: normal;">Add a new method <span style="font-family: Courier New, Courier, monospace;">starfieldEmitter</span> to <span style="font-family: Courier New, Courier, monospace;">GameScene.swift</span>:</span></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func </span>starfieldEmitter(color: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKColor</span>, starSpeedY: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span>, starsPerSecond: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span>, starScaleFactor: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span>) -> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKEmitterNode</span> {</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
<br /></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Determine the time a star is visible on screen</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> lifetime = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">frame</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">height</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">*</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIScreen</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">mainScreen</span>().<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">scale</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">/</span> starSpeedY</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Create the emitter node</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> emitterNode = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKEmitterNode</span>()</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
emitterNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">particleTexture</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKTexture</span>(imageNamed: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"StarParticle"</span>)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
emitterNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">particleBirthRate</span> = starsPerSecond</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> emitterNode.</span>particleColor<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>SKColor<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>lightGrayColor<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
emitterNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">particleSpeed</span> = starSpeedY <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">*</span> -1</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
emitterNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">particleScale</span> = starScaleFactor</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
emitterNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">particleColorBlendFactor</span> = 1</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
emitterNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">particleLifetime</span> = lifetime</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Position in the middle at top of the screen</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> emitterNode.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>CGPoint<span style="color: black; font-variant-ligatures: no-common-ligatures;">(x: </span>frame<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>width/<span style="color: black; font-variant-ligatures: no-common-ligatures;">2, y: </span>frame<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>height<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> emitterNode.</span>particlePositionRange<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>CGVector<span style="color: black; font-variant-ligatures: no-common-ligatures;">(dx: </span>frame<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>width<span style="color: black; font-variant-ligatures: no-common-ligatures;">, dy: 0)</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Fast forward the effect to start with a filled screen</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> emitterNode.</span>advanceSimulationTime<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>NSTimeInterval<span style="color: black; font-variant-ligatures: no-common-ligatures;">(lifetime))</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">return</span> emitterNode</div>
<div class="separator" style="clear: both; text-align: left;">
</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
}</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="" style="clear: both; text-align: left;">
<span style="font-weight: normal;">Create three emitter nodes for the starfield at the end of <span style="font-family: Courier New, Courier, monospace;">didMoveToView</span>:</span></div>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Add Starfield with 3 emitterNodes for a parallax effect</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// - Stars in top layer: light, fast, big</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// - ...</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// - Stars in back layer: dark, slow, small</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> emitterNode = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">starfieldEmitter</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKColor</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">lightGrayColor</span>(), starSpeedY: 50, starsPerSecond: 1, starScaleFactor: 0.2)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
emitterNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">zPosition</span> = -10</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addChild</span>(emitterNode)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
emitterNode = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">starfieldEmitter</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKColor</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">grayColor</span>(), starSpeedY: 30, starsPerSecond: 2, starScaleFactor: 0.1)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
emitterNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">zPosition</span> = -11</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addChild</span>(emitterNode)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
emitterNode = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">starfieldEmitter</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKColor</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">darkGrayColor</span>(), starSpeedY: 15, starsPerSecond: 4, starScaleFactor: 0.05)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
emitterNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">zPosition</span> = -12</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addChild</span>(emitterNode)</div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<br />
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<iframe allowfullscreen="1" frameborder="0" height="400" src="//www.youtube.com/embed/GDazsaYDGjQ" width="600"></iframe></div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<br />
<div style="font-weight: normal; text-align: start;">
<span style="font-weight: normal;">That's all for today. In my next part I'll show how to integrate game center. You can download the code from GitHub: <a href="https://github.com/stfnjstn/MySecondGame/releases/tag/v0.5">Part 5</a> or the latest version <a href="https://github.com/stfnjstn/MySecondGame/tree/master">here</a>.</span><br />
<br />
You can also download my prototyping App for this tutorial series:</div>
<div style="text-align: center;">
<a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></div>
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<br /></div>
<div style="font-weight: normal; text-align: start;">
<span style="font-weight: normal;"><br /></span></div>
<div style="font-weight: normal; text-align: start;">
<span style="font-weight: normal;">Cheers,</span><br />
<span style="font-weight: normal;">Stefan</span></div>
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>
</div>
</div>
Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com0tag:blogger.com,1999:blog-7769140974661448994.post-55078907221301441752014-11-27T14:59:00.001-08:002015-08-30T10:05:16.698-07:00How to implement a space shooter with SpriteKit and SWIFT - Part 4: Collision Detection<h3>
<span style="color: #323333; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><span style="font-size: 20px;">Adding basic game logic and collision detection</span></span></h3>
<h3 style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: bolder; margin: 0.83em 0px; padding: 0px; position: relative; text-align: justify;">
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<iframe allowfullscreen="1" frameborder="0" height="400" src="//www.youtube.com/embed/8d8MH_gXt84" width="600"></iframe></div>
<div style="font-weight: normal;">
<br /></div>
<br />
<div style="text-align: center;">
<a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></div>
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<br /></div>
</h3>
<div style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-weight: normal; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<div>
<br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;">Welcome to part 4 of my tutorial series. In the previous parts we've created a sprite, added movement, created enemies which follow our sprite, added bullets and a HUD. But, for a real game some essential parts are missing:</span></span><br />
<br />
<ul>
<li><span style="color: #323333; font-family: 'Helvetica Neue Light';">Collision detection between the bullets and our sprite</span></li>
<li><span style="color: #323333; font-family: Helvetica Neue Light;">Basic game logic</span></li>
<ul>
<li><span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;">Scoring</span></span></li>
<li><span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;">Pause</span></span></li>
<li><span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;">Life Lost & </span></span><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;">Game Over</span></li>
</ul>
</ul>
<br />
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;">I'll show how to implement this today. As a starting point you can download the code from tutorial part 3 </span><a href="https://github.com/stfnjstn/MySecondGame/releases/tag/v0.3" style="font-family: 'Helvetica Neue Light'; font-weight: normal;">here</a><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;">. </span><br />
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><br /></span>
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"></span><br />
<a name='more'></a><br />
<h3 style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: bolder; margin: 0.83em 0px; padding: 0px; position: relative; text-align: justify;">
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 14px;">Tutorial Overview:</span></h3>
<div style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-weight: normal; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with.html">Part 1</a>: Initial project setup, sprite creation and movement using </span><span style="font-family: 'Courier New', Courier, monospace;">SKAction</span><span style="font-family: 'Helvetica Neue Light';"> and </span><span style="font-family: 'Courier New', Courier, monospace;">SKConstraint</span></span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_11.html">Part 2</a>: Adding enemies, bullets and shooting with </span></span><span style="color: #323333; font-family: 'Courier New', Courier, monospace;">SKAction</span><span style="color: #323333; font-family: 'Helvetica Neue Light';"> and </span><span style="color: #323333; font-family: 'Courier New', Courier, monospace;">SKConstraint</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_19.html">Part 3</a>: Adding a HUD with </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">SKLabelNode</span><span style="color: #323333; font-family: 'Helvetica Neue Light';"> and </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">SKSpriteNode</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_27.html">Part 4</a>: Adding basic game logic and collision detection</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2014/12/how-to-implement-space-shooter-with.html">Part 5</a>: Adding particles and sound </span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="https://www.blogger.com/I've%20finished%20Tutorial%20Part%206:%20How%20to%20impl.%20a%20space%20shooter%20in%20#SpriteKit and #SWIFT' - Game Center integration: http://stefansdevplayground.blogspot.de/2015/01/how-to-implement-space-shooter-with.html">Part 6</a>: GameCenter integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2015/02/how-to-implement-space-shooter-with.html">Part 7</a>: iAd integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light';"><a href="http://stefansdevplayground.blogspot.de/2015/04/how-to-implement-in-app-purchase-for.html">Part 8</a>: In-App Purchases</span></li>
</ul>
<div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><br /></span></div>
<h3>
<span style="color: #323333; font-family: Helvetica Neue Light;">Let's start</span></h3>
</div>
</div>
<div>
</div>
</div>
<h3>
<span style="color: #323333; font-family: Helvetica Neue Light;"><br /></span></h3>
<h3>
<span style="color: #323333; font-family: Helvetica Neue Light;">1. Collision Detection</span></h3>
<span style="color: #323333; font-family: Helvetica Neue Light;">Sprite Kit makes it increadible easy to implement collision detection. </span><br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><br /></span>
<br />
<h4>
<span style="color: #323333; font-family: Helvetica Neue Light;">1.1. Add the </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">SKPhysicsContactDelegate</span><span style="color: #323333; font-family: Helvetica Neue Light;"> Interface and implement the </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">didBeginContact</span><span style="color: #323333; font-family: Helvetica Neue Light;"> delegate method inside </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">GameScene.swift</span><span style="color: #323333; font-family: Helvetica Neue Light;">:</span></h4>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">class</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> GameScene: </span>SKScene<span style="color: black; font-variant-ligatures: no-common-ligatures;">, </span>SKPhysicsContactDelegate<span style="color: black; font-variant-ligatures: no-common-ligatures;"> {</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;">...</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"><br /></span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> didBeginContact(contact: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKPhysicsContact</span>) {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;">...</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;">}</span></div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><br /></span>
<br />
<h4>
<span style="color: #323333; font-family: Helvetica Neue Light;">1.2. Set the delegate at the end of </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">didMoveToView</span><span style="color: #323333; font-family: Helvetica Neue Light;">:</span></h4>
<span style="color: #323333; font-family: Helvetica Neue Light;"><br /></span>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> didMoveToView(view: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKView</span>) {</div>
<div style="font-family: Menlo; font-size: 11px;">
...</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Handle collisions</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>physicsWorld<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>contactDelegate<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<h4>
<span style="color: #323333; font-family: Helvetica Neue Light;">1.3. Create Collision Categories</span><span style="color: #323333; font-family: 'Helvetica Neue Light';">:</span></h4>
<span style="color: #323333; font-family: Helvetica Neue Light;">We have two different sprite types which can collide. The Hero sprite and the bullets. I've created them outside the class as global constants, because we need the categories also in other classes.</span><br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><br /></span>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">import</span> SpriteKit</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> collisionBulletCategory: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UInt32</span> = 0x1 << 0</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> collisionHeroCategory: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UInt32</span> = 0x1 << 1</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<br />
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">class</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> GameScene: </span>SKScene<span style="color: black; font-variant-ligatures: no-common-ligatures;">, </span>SKPhysicsContactDelegate<span style="color: black; font-variant-ligatures: no-common-ligatures;"> {</span></div>
<br />
<h4>
<span style="color: #323333; font-family: Helvetica Neue Light;">1.4. Create a physics bodies for the sprites:</span></h4>
<span style="color: #323333; font-family: Helvetica Neue Light;">We have to add a physics body to our hero and bullet sprites. After that the built in physics engine of SpriteKit will handle the collision detection automatically. SpriteKit provides several possibilities to define the shape of the </span><span style="color: #323333; font-family: Courier New, Courier, monospace;">SKPhysicsBody</span><span style="color: #323333; font-family: Helvetica Neue Light;">. The easiest is a rectangle. This is not accurate enough for bullets, but not for the Hero sprite. A triangle would be better. Perfect in this scenario is to use the ship texture. That means SpriteKit will use all non transparent pixels to detect the shape by itself:</span><br />
<br />
<div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibsvd3Eiep8tb09NB7Dq4aQnNaT1aG4SilOCDnbkwrb0StPR9btSvejaBJUgYKAiS5bfABYqzX5DLNHmLNrGcoy54T9X9UnEAQ-8nyG51ghWosibd1LHB6-KVhyphenhyphenakjnQvKLeUNCWIwZpI/s1600/Bildschirmfoto+2014-11-27+um+21.34.46.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibsvd3Eiep8tb09NB7Dq4aQnNaT1aG4SilOCDnbkwrb0StPR9btSvejaBJUgYKAiS5bfABYqzX5DLNHmLNrGcoy54T9X9UnEAQ-8nyG51ghWosibd1LHB6-KVhyphenhyphenakjnQvKLeUNCWIwZpI/s1600/Bildschirmfoto+2014-11-27+um+21.34.46.png" /></a></div>
Inside of <span style="font-family: Courier New, Courier, monospace;">didMoveToView</span> in <span style="font-family: Courier New, Courier, monospace;">GameScene.swift</span> add this code snippet after the sprite creation:<br />
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<br /></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Add physics body for collision detection</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>physicsBody<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>dynamic<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>physicsBody<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>SKPhysicsBody<span style="color: black; font-variant-ligatures: no-common-ligatures;">(texture: </span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>texture<span style="color: black; font-variant-ligatures: no-common-ligatures;">, size: </span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>physicsBody<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>affectedByGravity<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>physicsBody<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>categoryBitMask<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>collisionHeroCategory</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>physicsBody<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>contactTestBitMask<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>collisionBulletCategory</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>physicsBody<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>collisionBitMask<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = 0x0</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
</div>
<div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><br /></span></div>
<div>
Inside of <span style="font-family: Courier New, Courier, monospace;">shoot</span> in <span style="font-family: Courier New, Courier, monospace;">EnemySpriteController.swift</span> add this code snippet after the sprite creation:<br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><br /></span>
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Add physics body for collision detection</div>
<div style="font-family: Menlo; font-size: 11px;">
bullet.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">physicsBody</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKPhysicsBody</span>(rectangleOfSize: bullet.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">frame</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
bullet.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">physicsBody</span>?.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">dynamic</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> bullet.</span>physicsBody<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>affectedByGravity<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> bullet.</span>physicsBody<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>categoryBitMask<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>collisionBulletCategory</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> bullet.</span>physicsBody<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>contactTestBitMask<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>collisionHeroCategory</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
bullet.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">physicsBody</span>?.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">collisionBitMask</span> = 0x0;</div>
</div>
<div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><br /></span></div>
<div>
<div style="font-weight: normal; text-align: start;">
<span style="font-weight: normal;">The <span style="font-family: Courier New, Courier, monospace;">categoryBitMask</span> defines the sprite category. The <span style="font-family: Courier New, Courier, monospace;">contactTestBitMask</span> define with which sprite categories a collision will be checked. </span><br />
Now you can add a breakpoint at the <span style="font-family: Courier New, Courier, monospace;">didBeginContact</span> method and run the game to check if the collisions are detected.<br />
<br />
<br />
<h3>
2. Basic Game Logic </h3>
<h4>
2.1. Scoring</h4>
<h4>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmnlmcFt6N1xNe19maNGCjrvpMur81ieMwP2xdL5x5m501FZaigWp0lHuYKez9UP0uRPNBK1KSClB0l3w66xHEZH_lBVFZIvtpgC27qqzE7A_EoURc-jWRh7wDPtT9IJ3HielsWrXfnaE/s1600/Bildschirmfoto+2014-11-19+um+21.20.55.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmnlmcFt6N1xNe19maNGCjrvpMur81ieMwP2xdL5x5m501FZaigWp0lHuYKez9UP0uRPNBK1KSClB0l3w66xHEZH_lBVFZIvtpgC27qqzE7A_EoURc-jWRh7wDPtT9IJ3HielsWrXfnaE/s1600/Bildschirmfoto+2014-11-19+um+21.20.55.png" /></a></div>
<div style="text-align: justify;">
<span style="font-family: Times; font-size: small; font-weight: normal;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: Times; font-size: small; font-weight: normal;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: Times; font-size: small; font-weight: normal;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: Times; font-size: small; font-weight: normal;">I'll create a very simple scoring </span><span style="font-weight: normal;">mechanism: Everytime an enemy is shooting, the score will increase. Hence the label and the score property have been implemented in the last post, only two additional lines are needed in the <span style="font-family: Courier New, Courier, monospace;">update</span> method.</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="font-family: Times; font-size: small;"><br /></span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> update(currentTime: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CFTimeInterval</span>) {</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> currentTime - <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">_dLastShootTime</span> >= 1 {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>enemySprites<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>shoot<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">_dLastShootTime</span>=currentTime</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Increase score</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">score</span>++</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>scoreNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>text<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>String<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>score<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
}</div>
<div>
<br /></div>
</h4>
<h4>
</h4>
<h4>
2.2. Pause button</h4>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhaOtstCdwwgA6aZS0Yi3VQY2HrSKCyAeUA8UqrB9mdZdUwGJ57osIKIfcaVgHz-eeJDmbaY6fpngpVl_9m8tCduLQs3lv_O6RGBgXvZRyRjDZWrmQJ-FZyksMyAQfmRSalLYElooD7Yls/s1600/Bildschirmfoto+2014-11-19+um+21.20.48.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhaOtstCdwwgA6aZS0Yi3VQY2HrSKCyAeUA8UqrB9mdZdUwGJ57osIKIfcaVgHz-eeJDmbaY6fpngpVl_9m8tCduLQs3lv_O6RGBgXvZRyRjDZWrmQJ-FZyksMyAQfmRSalLYElooD7Yls/s1600/Bildschirmfoto+2014-11-19+um+21.20.48.png" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<br />
<span style="font-weight: normal;">The pause button was created as part of the HUD in part 3 of my tutorial. To detect if the pause button is touched the button and it's container have an unique name: <span style="font-family: Courier New, Courier, monospace;">PauseButton</span> and <span style="font-family: Courier New, Courier, monospace;">PauseButtonContainer</span>.</span><br />
<span style="font-weight: normal;"><br /></span>
Now extend the <span style="font-family: Courier New, Courier, monospace;">touchesBegan</span> method to check, if pause has been pressed:<br />
<span style="font-weight: normal;"><br /></span>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> touchesBegan(touches: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">NSSet</span>, withEvent event: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIEvent</span>) {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>/* Called when a touch begins */</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">for</span> touch: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">AnyObject</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span> touches {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> location = touch.locationInNode(<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> node = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.nodeAtPoint(location)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> (node.name == <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"PauseButton"</span>) || (node.name == <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"PauseButtonContainer"</span>) {</div>
<div style="font-family: Menlo; font-size: 11px;">
showPauseAlert()</div>
<div style="font-family: Menlo; font-size: 11px;">
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
...</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<span style="font-weight: normal;"><br /></span>
<span style="font-weight: normal;">Add a new global property to store the <span style="font-family: Courier New, Courier, monospace;">gamePaused</span> state, if the </span>pause button is pressed:<br />
<span style="font-weight: normal;"></span><br />
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> gamePaused = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"><br /></span></div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"><br /></span></div>
<span style="font-weight: normal;">Add a new method for the pause handling to show an <span style="font-family: Courier New, Courier, monospace;">UIAlertController</span>:</span><br />
<span style="font-weight: normal;"><br /></span>
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// Show Pause Alert<br />
<span style="color: #0433ff;">func</span> showPauseAlert() {<br />
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gamePaused</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span><br />
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> alert = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertController</span>(title: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Pause"</span>, message: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">""</span>, preferredStyle: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertControllerStyle</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Alert</span>)<br />
<div style="color: #3495af;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> alert.</span>addAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>UIAlertAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(title: </span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Continue"</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, style: </span>UIAlertActionStyle<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>Default<span style="color: black; font-variant-ligatures: no-common-ligatures;">) { </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">_</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span></div>
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gamePaused</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span><br />
})<br />
<div style="color: #3495af;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>view<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>window<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>rootViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>presentViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;">(alert, animated: </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, completion: </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<br />
}</div>
<span style="font-weight: normal;"><br /></span><span style="font-weight: normal;">Inside of <span style="font-family: Courier New, Courier, monospace;">update</span> check for <span style="font-family: Courier New, Courier, monospace;">gamePaused</span> state</span><br />
<span style="font-weight: normal;"><br /></span>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> update(currentTime: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CFTimeInterval</span>) {<br />
<div style="min-height: 13px;">
<br /></div>
<br />
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> !<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gamePaused</span> {<br />
<div>
<br /></div>
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
...</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<span style="font-weight: normal;"><br /></span>
<span style="font-weight: normal;">Inside of <span style="font-family: Courier New, Courier, monospace;">didBeginContact</span> check for <span style="font-family: Courier New, Courier, monospace;">gamePaused</span> state:</span><br />
<span style="font-weight: normal;"><br /></span>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> didBeginContact(contact: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKPhysicsContact</span>) {<br />
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> !<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gamePaused</span> {<br />
<div style="min-height: 13px;">
</div>
}</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<span style="font-weight: normal;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHdmU0pO2AnMIRT8cgDYZZksYyYUSbaXiaI7OSgFVEYr8UuXa6SSspLFcFWizkEGTW04q88W3hqIHA25vsLL_7fEqyFdjKIWurXScUaEZFfEcI8Kh3keVUo4nKDCNi8kvHz8oU88aLyV4/s1600/Bildschirmfoto+2014-11-27+um+22.09.53.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="179" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHdmU0pO2AnMIRT8cgDYZZksYyYUSbaXiaI7OSgFVEYr8UuXa6SSspLFcFWizkEGTW04q88W3hqIHA25vsLL_7fEqyFdjKIWurXScUaEZFfEcI8Kh3keVUo4nKDCNi8kvHz8oU88aLyV4/s1600/Bildschirmfoto+2014-11-27+um+22.09.53.png" width="320" /></a></div>
<span style="font-weight: normal;"><br /></span>
<br />
<h4>
2.3. LifeLost & GameOver</h4>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYFEQ0sWHLpXpAh0pn8cvg7AKF20TETmSQw1yTqINv5rGM6XVIa0gyKdNUfU5WgJ6kXfdOa91AjLkGHEUnE2qaT_IzTPWtnc5XKIn3uEYqW4pSPD99NZuLfTsW4FfK6yUygS8UVysdBTQ/s1600/Bildschirmfoto+2014-11-19+um+21.20.40.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYFEQ0sWHLpXpAh0pn8cvg7AKF20TETmSQw1yTqINv5rGM6XVIa0gyKdNUfU5WgJ6kXfdOa91AjLkGHEUnE2qaT_IzTPWtnc5XKIn3uEYqW4pSPD99NZuLfTsW4FfK6yUygS8UVysdBTQ/s1600/Bildschirmfoto+2014-11-19+um+21.20.40.png" /></a></div>
<div>
<br /></div>
<span style="font-weight: normal;"><br /></span>
<span style="font-weight: normal;"><br /></span>
<span style="font-weight: normal;">Everytime a collision is detected one life is lost and will be removed from the HUD. This is handled in the new <span style="font-family: Courier New, Courier, monospace;">lifeLost</span> new method:</span><br />
<span style="font-weight: normal;"><br /></span>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> lifeLost() {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gamePaused</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// remove one life from hud</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">remainingLifes</span>>0 {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>self<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>lifeNodes<span style="color: black; font-variant-ligatures: no-common-ligatures;">[</span>remainingLifes<span style="color: black; font-variant-ligatures: no-common-ligatures;">-1].</span>alpha<span style="color: black; font-variant-ligatures: no-common-ligatures;">=0.0</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">remainingLifes</span>--;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// check if remaining lifes exists</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> (<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">remainingLifes</span>==0) {</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>showGameOverAlert<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Stop movement, fade out, move to center, fade in</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>removeAllActions<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>runAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>SKAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>fadeOutWithDuration<span style="color: black; font-variant-ligatures: no-common-ligatures;">(1) , completion: {</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>CGPointMake<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>width/<span style="color: black; font-variant-ligatures: no-common-ligatures;">2, </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>height/<span style="color: black; font-variant-ligatures: no-common-ligatures;">2)</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>runAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>SKAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>fadeInWithDuration<span style="color: black; font-variant-ligatures: no-common-ligatures;">(1), completion: {</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gamePaused</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span></div>
<div style="font-family: Menlo; font-size: 11px;">
})</div>
<div style="font-family: Menlo; font-size: 11px;">
})</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<span style="font-weight: normal;"><br /></span>
<span style="font-weight: normal;"><span style="font-family: Courier New, Courier, monospace;">lifeLost</span> is called inside of <span style="font-family: Courier New, Courier, monospace;">didBeginContact</span>:</span><br />
<span style="font-weight: normal;"><br /></span>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> didBeginContact(contact: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKPhysicsContact</span>) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> !<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gamePaused</span> {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">lifeLost</span>()</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<span style="font-weight: normal;"><br /></span>
<span style="font-weight: normal;">If the last life is lost a GameOver Dialog will be shown:</span><br />
<span style="font-weight: normal;"><br /></span>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> showGameOverAlert() {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gamePaused</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> alert = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertController</span>(title: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Game Over"</span>, message: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">""</span>, preferredStyle: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIAlertControllerStyle</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Alert</span>)</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> alert.</span>addAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>UIAlertAction<span style="color: black; font-variant-ligatures: no-common-ligatures;">(title: </span><span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"OK"</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, style: </span>UIAlertActionStyle<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>Default<span style="color: black; font-variant-ligatures: no-common-ligatures;">) { </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">_</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// restore lifes in HUD</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">remainingLifes</span>=3</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">for</span>(<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> i = 0; i<3; i++) {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">lifeNodes</span>[i].<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">alpha</span>=1.0</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// reset score</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">score</span>=0</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">scoreNode</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">text</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">String</span>(0)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">gamePaused</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">false</span></div>
<div style="font-family: Menlo; font-size: 11px;">
})</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// show alert</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>view<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>window<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>rootViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;">?.</span>presentViewController<span style="color: black; font-variant-ligatures: no-common-ligatures;">(alert, animated: </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">, completion: </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIwp7VUdir-WDIf6WH0eTYTHbh-c2vqjtgQdWu-oXe-hs83GOkST3gfAbhot1jDpdL0R1SG_e6j34R76059gK7GiavtjM5MGqXLPtdhVgnJf-Y22bnyLi9AFfeGq6AbI4K_78NrWowIYM/s1600/Bildschirmfoto+2014-11-27+um+22.57.22.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="178" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIwp7VUdir-WDIf6WH0eTYTHbh-c2vqjtgQdWu-oXe-hs83GOkST3gfAbhot1jDpdL0R1SG_e6j34R76059gK7GiavtjM5MGqXLPtdhVgnJf-Y22bnyLi9AFfeGq6AbI4K_78NrWowIYM/s1600/Bildschirmfoto+2014-11-27+um+22.57.22.png" width="320" /></a></div>
<span style="font-weight: normal;"><br /></span>
<span style="font-weight: normal;"><br /></span>
<span style="font-weight: normal;"></span>
<span style="font-weight: normal;">That's all for today. In my next part I'll add particle and sound effects</span><span style="font-weight: normal;">.</span></div>
<div style="font-weight: normal; text-align: start;">
<span style="font-weight: normal;">You can download the code from GitHub: <a href="https://github.com/stfnjstn/MySecondGame/releases/tag/v0.4">Part 4</a> or the latest version <a href="https://github.com/stfnjstn/MySecondGame/tree/master">here</a>.</span></div>
<div style="font-weight: normal; text-align: start;">
<br /></div>
You can also download my prototyping App for this tutorial series:
<br />
<br />
<div style="text-align: center;">
<a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></div>
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<br /></div>
<div style="font-weight: normal; text-align: start;">
Cheers,<br />
Stefan</div>
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>
</div>
Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com4tag:blogger.com,1999:blog-7769140974661448994.post-45018602385324216852014-11-19T12:52:00.002-08:002015-08-30T10:05:59.399-07:00How to implement a space shooter with SpriteKit and SWIFT - Part 3: Create a HUD<h3>
<span style="color: #323333; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><span style="font-size: 20px;">Adding a HUD with SpriteKit and SWIFT</span></span></h3>
<h3 style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: bolder; margin: 0.83em 0px; padding: 0px; position: relative; text-align: justify;">
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<iframe allowfullscreen="1" frameborder="0" height="400" src="//www.youtube.com/embed/8d8MH_gXt84" width="600"></iframe></div>
<div style="font-weight: normal;">
<br /></div>
<br />
<div style="text-align: center;">
<a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></div>
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<br /></div>
</h3>
<h3 style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: bolder; margin: 0.83em 0px; padding: 0px; position: relative; text-align: justify;">
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 14px;">Tutorial Overview:</span></h3>
<div style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-weight: normal; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<br />
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with.html">Part 1</a>: Initial project setup, sprite creation and movement using </span><span style="font-family: 'Courier New', Courier, monospace;">SKAction</span><span style="font-family: 'Helvetica Neue Light';"> and </span><span style="font-family: 'Courier New', Courier, monospace;">SKConstraint</span></span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_11.html">Part 2</a>: Adding enemies, bullets and shooting with </span></span><span style="color: #323333; font-family: 'Courier New', Courier, monospace; font-weight: normal;">SKAction</span><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"> and </span><span style="color: #323333; font-family: 'Courier New', Courier, monospace; font-weight: normal;">SKConstraint</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_19.html">Part 3</a>: Adding a HUD with </span><span style="color: #323333; font-family: Courier New, Courier, monospace; font-weight: normal;">SKLabelNode</span><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"> and </span><span style="color: #323333; font-family: Courier New, Courier, monospace; font-weight: normal;">SKSpriteNode</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_27.html">Part 4</a>: Adding basic game logic and collision detection</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/12/how-to-implement-space-shooter-with.html">Part 5</a>: Adding particles and sound </span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="https://www.blogger.com/I've%20finished%20Tutorial%20Part%206:%20How%20to%20impl.%20a%20space%20shooter%20in%20#SpriteKit and #SWIFT' - Game Center integration: http://stefansdevplayground.blogspot.de/2015/01/how-to-implement-space-shooter-with.html">Part 6</a>: GameCenter integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2015/02/how-to-implement-space-shooter-with.html">Part 7</a>: iAd integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2015/04/how-to-implement-in-app-purchase-for.html">Part 8</a>: In-App Purchases</span></li>
</ul>
<div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span></div>
<div>
<h3>
<span style="color: #323333; font-family: Helvetica Neue Light;">Add the HUD:</span></h3>
</div>
<div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;">I'll add a simple HUD which shows the score, the remaining lifes and a pause button. As a starting point you can download the code from tutorial part 2 </span><a href="https://github.com/stfnjstn/MySecondGame/releases/tag/v0.2" style="font-weight: normal;">here</a><span style="font-weight: normal;">. </span></span></div>
<div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span></div>
<div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;">First of all let's fix a bug in the sample code of my last post. Open </span></span><span style="font-weight: normal;"><span style="font-family: Courier New, Courier, monospace;">GameViewController.swift</span></span><span style="color: #323333;"><span style="font-family: Helvetica Neue Light; font-weight: normal;"> and move the code from </span><span style="font-family: Courier New, Courier, monospace; font-weight: normal;">viewDidLoad </span><span style="font-family: Helvetica Neue Light; font-weight: normal;">to </span><span style="font-family: Courier New, Courier, monospace; font-weight: normal;">viewDidAppear</span><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">. This is necessary because the orientation change to landscape might not be completed. In this case </span><span style="font-family: Courier New, Courier, monospace;">width</span><span style="font-family: Helvetica Neue Light;"> and </span><span style="font-family: Courier New, Courier, monospace;">height</span><span style="font-family: Helvetica Neue Light;"> are inverted, if we determine them in </span><span style="font-family: Courier New, Courier, monospace;">viewDidLoad</span><span style="font-family: Helvetica Neue Light;">:</span></span></span><br />
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"></span></span></span><br />
<a name='more'></a><span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span></div>
<div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span></div>
<div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> viewDidLoad() {</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">super</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">viewDidLoad</span>()</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> viewDidAppear(animated: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">Bool</span>) {</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">super</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">viewDidAppear</span>(animated)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Detect the screensize</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> sizeRect = </span>UIScreen<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>mainScreen<span style="color: black; font-variant-ligatures: no-common-ligatures;">().</span>applicationFrame</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> width = sizeRect.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">width</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">*</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIScreen</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">mainScreen</span>().<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">scale</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> height = sizeRect.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">height</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">*</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIScreen</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">mainScreen</span>().<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">scale</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Scene should be shown in fullscreen mode</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> scene = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">GameScene</span>(size: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGSizeMake</span>(width, height))</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Configure the view.</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> skView = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">view</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">as</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKView</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
skView.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">showsFPS</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
skView.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">showsNodeCount</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>/* Sprite Kit applies additional optimizations to improve rendering performance */</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> skView.</span>ignoresSiblingOrder<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>/* Set the scale mode to scale to fit the window */</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> scene.</span>scaleMode<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = .</span>AspectFill</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
skView.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">presentScene</span>(scene)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
}</div>
</div>
<div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span></div>
<div>
<span style="color: #323333; font-family: Helvetica Neue Light;">Now let's create the HUD: </span></div>
<div>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjn2ujEOKYMmY_9Gu8_i1XObD4VtdCE4aVjnxeP75aIJ4X9uJ3sEdBLuHjSHn-B08VhxMKXwJ1YX-u8PWDqddPXOoIsenKQ0Q-reuc6vYPDO7PI8vtnKjGGJcA4tgnFZDuIDJE47YbgiUk/s1600/Bildschirmfoto+2014-11-19+um+21.21.20.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="11" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjn2ujEOKYMmY_9Gu8_i1XObD4VtdCE4aVjnxeP75aIJ4X9uJ3sEdBLuHjSHn-B08VhxMKXwJ1YX-u8PWDqddPXOoIsenKQ0Q-reuc6vYPDO7PI8vtnKjGGJcA4tgnFZDuIDJE47YbgiUk/s1600/Bildschirmfoto+2014-11-19+um+21.21.20.png" width="400" /></a><span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;">
<br />
</span></span><br />
<div>
<br /></div>
<span style="color: #323333; font-family: Helvetica Neue Light;">
It contains 3 different elements </span><span style="color: #323333; font-family: Helvetica Neue Light;">:</span></div>
<div>
<ul>
<li><span style="color: #323333;"><span style="font-family: Helvetica Neue Light;">Remaining lifes:<span style="font-weight: normal;"> Must be accessible outside of the </span></span><span style="font-family: Courier New, Courier, monospace; font-weight: normal;">createHUD</span><span style="font-family: Helvetica Neue Light; font-weight: normal;"> method to enable extra life or life lost events. Stored in a global array of </span><span style="font-family: Courier New, Courier, monospace; font-weight: normal;">SKSpriteNode</span><span style="font-family: Helvetica Neue Light; font-weight: normal;">s. </span></span></li>
</ul>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIkO4q1ZKm5yO0zl4UVPuOpnfsMLp0tmDUMqqwZkkJUARuRbEi5yvXvQPXbzMdjiYV-uFkKthclzI9_819LwRD4VzTTzyEpq4tzWflb91squGCtHU0mmw6qhAS9MUCMzjImaP0K75nNoU/s1600/Bildschirmfoto+2014-11-19+um+21.20.40.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="71" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIkO4q1ZKm5yO0zl4UVPuOpnfsMLp0tmDUMqqwZkkJUARuRbEi5yvXvQPXbzMdjiYV-uFkKthclzI9_819LwRD4VzTTzyEpq4tzWflb91squGCtHU0mmw6qhAS9MUCMzjImaP0K75nNoU/s1600/Bildschirmfoto+2014-11-19+um+21.20.40.png" width="200" /></a></div>
<ul>
<li><span style="color: #323333;"><span style="font-family: Helvetica Neue Light;">Pause button:</span><span style="font-family: Helvetica Neue Light; font-weight: normal;"> A <span style="font-family: 'Courier New', Courier, monospace;">SKLabelNode</span> which is placed in a transparent <span style="font-family: 'Courier New', Courier, monospace;">SKSpriteNode</span> to increase the touchable area.</span></span></li>
</ul>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfunTfWZvPc6NXAFGZ6ytHpR9H26U_LtggaZ76R3pQ7irRwh0CeDpTbm7U5c-b4gxQ_S5Rm6ibIgTPGb4prTlYbql1M5U32aDDNfZILwlOQJKFPAICg5pQN8w-itfBC-S_tzOvCaAuOLo/s1600/Bildschirmfoto+2014-11-19+um+21.20.48.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="73" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfunTfWZvPc6NXAFGZ6ytHpR9H26U_LtggaZ76R3pQ7irRwh0CeDpTbm7U5c-b4gxQ_S5Rm6ibIgTPGb4prTlYbql1M5U32aDDNfZILwlOQJKFPAICg5pQN8w-itfBC-S_tzOvCaAuOLo/s1600/Bildschirmfoto+2014-11-19+um+21.20.48.png" width="200" /></a></div>
<div>
<ul>
<li><span style="color: #323333; font-family: Helvetica Neue Light;">Score:<span style="font-weight: normal;"> </span></span><span style="color: #323333; font-weight: normal;"><span style="font-family: Helvetica Neue Light; font-weight: bold;"><span style="font-weight: normal;">Must be accessible outside of the </span></span><span style="font-family: Courier New, Courier, monospace;">createHUD</span><span style="font-family: Helvetica Neue Light;"> method.</span><span style="font-family: Courier New, Courier, monospace;"> </span></span><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;">Stored in a global <span style="font-family: 'Courier New', Courier, monospace;">SKLabelNode </span>property.</span></li>
</ul>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiB57zwceM41sgqcE9CHgrepKd4fHx9xkEo_gze2lcOVToZOTVkHghK6leX1T1HqdYOxLUhd3iflfuEiRo1fEaK8VvxsgtcXdRkji1ofI4rL1OFA1fRxQIrvGCpBQZNkVrdRXtR0tXHOvE/s1600/Bildschirmfoto+2014-11-19+um+21.20.55.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="81" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiB57zwceM41sgqcE9CHgrepKd4fHx9xkEo_gze2lcOVToZOTVkHghK6leX1T1HqdYOxLUhd3iflfuEiRo1fEaK8VvxsgtcXdRkji1ofI4rL1OFA1fRxQIrvGCpBQZNkVrdRXtR0tXHOvE/s1600/Bildschirmfoto+2014-11-19+um+21.20.55.png" width="200" /></a></div>
<div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span></div>
</div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<span style="color: #323333;"><span style="font-weight: normal;"><span style="font-family: Helvetica Neue Light;">To access the HUD elements add these global properties to </span><span style="font-family: Courier New, Courier, monospace;">GameScene.swift</span><span style="font-family: Helvetica Neue Light;">:</span></span></span><br />
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-weight: normal;"><br /></span></span>
<br />
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>/// HUD Global properties</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> lifeNodes : [<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>] = []</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> remainingLifes = 3</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> scoreNode = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKLabelNode</span>()</div>
<br />
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> score = 0</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<br /></div>
<div style="font-size: 11px; font-weight: normal;">
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 24px;">To create a HUD add this method to </span><span style="color: #323333; font-size: 24px;"><span style="font-family: Courier New, Courier, monospace;">GameScene.swift</span></span><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 24px;">:</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 24px;"><br /></span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> createHUD() {</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Create a root node with black background to position and group the HUD elemets</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// HUD size is relative to the screen resolution to handle iPad and iPhone screens</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> hud = </span>SKSpriteNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">(color: </span>UIColor<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>blackColor<span style="color: black; font-variant-ligatures: no-common-ligatures;">(), size: </span>CGSizeMake<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>width<span style="color: black; font-variant-ligatures: no-common-ligatures;">, </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>height*<span style="color: black; font-variant-ligatures: no-common-ligatures;">0.05))</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> hud.</span>anchorPoint<span style="color: black; font-variant-ligatures: no-common-ligatures;">=</span>CGPointMake<span style="color: black; font-variant-ligatures: no-common-ligatures;">(0, 0)</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> hud.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>CGPointMake<span style="color: black; font-variant-ligatures: no-common-ligatures;">(0, </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>height-<span style="color: black; font-variant-ligatures: no-common-ligatures;">hud.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>height<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addChild</span>(hud)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Display the remaining lifes</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Add icons to display the remaining lifes</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Reuse the Spaceship image: Scale and position releative to the HUD size</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> lifeSize = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGSizeMake</span>(hud.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">height-</span>10, hud.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">height-</span>10)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">for</span>(<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> i = 0; i<<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">remainingLifes</span>; i++) {</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> tmpNode = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>(imageNamed: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Spaceship"</span>)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">lifeNodes</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">append</span>(tmpNode)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
tmpNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span> = lifeSize</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
tmpNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span>=<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPointMake</span>(tmpNode.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">width</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">*</span> 1.3 <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">*</span> (1.0 <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">+</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span>(i)), (hud.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">height-</span>5)<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">/</span>2)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
hud.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addChild</span>(tmpNode)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
}</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Pause button container and label</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Needed to increase the touchable area</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Names will be used to identify these elements in the touch handler</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> pauseContainer = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>()</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
pauseContainer.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPointMake</span>(hud.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">width/</span>1.5, 1)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
pauseContainer.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGSizeMake</span>(hud.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">height*</span>3, hud.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">height*</span>2)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
pauseContainer.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">name</span> = <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"PauseButtonContainer"</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
hud.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addChild</span>(pauseContainer)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> pauseButton = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKLabelNode</span>()</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
pauseButton.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPointMake</span>(hud.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">width/</span>1.5, 1)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
pauseButton.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">text</span>=<span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"I I"</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
pauseButton.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">fontSize</span>=hud.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">height</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> pauseButton.</span>horizontalAlignmentMode<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>SKLabelHorizontalAlignmentMode<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>Center</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
pauseButton.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">name</span>=<span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"PauseButton"</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
hud.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addChild</span>(pauseButton)</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Display the current score</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">score</span> = 0</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>scoreNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>CGPointMake<span style="color: black; font-variant-ligatures: no-common-ligatures;">(hud.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>width-<span style="color: black; font-variant-ligatures: no-common-ligatures;">hud.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>width<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>*<span style="color: black; font-variant-ligatures: no-common-ligatures;"> 0.1, 1)</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">scoreNode</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">text</span> = <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"0"</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>scoreNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>fontSize<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = hud.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>height</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> hud.</span>addChild<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>scoreNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
}</div>
<div>
<br /></div>
<div>
<span style="font-weight: normal;">To add the HUD to the screen add this lines at the end of the <span style="font-family: Courier New, Courier, monospace;">didMoveToView</span> method of <span style="font-family: Courier New, Courier, monospace;">GameScene.swift</span>: </span></div>
<div>
<span style="font-weight: normal;"><br /></span></div>
<div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Add HUD</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">createHUD</span>()</div>
</div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 24px;"><br /></span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="font-family: Times; font-size: 24px;">Here we are:</span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 24px;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQjiVt0fHT7Rp94QSnQ8Tk4haqPCQiCQPRvZs77B0zpU4KHWkiu_ALYjSBgS_3EKRT1VbXDyDAeBUdN0skDsUtwYZSYq3bJqL80NkGkj17YnDUZN8tdFLZpSa5f0EkFlI09ZdGHEWZaUQ/s1600/Bildschirmfoto+2014-11-19+um+21.11.26.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="181" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQjiVt0fHT7Rp94QSnQ8Tk4haqPCQiCQPRvZs77B0zpU4KHWkiu_ALYjSBgS_3EKRT1VbXDyDAeBUdN0skDsUtwYZSYq3bJqL80NkGkj17YnDUZN8tdFLZpSa5f0EkFlI09ZdGHEWZaUQ/s1600/Bildschirmfoto+2014-11-19+um+21.11.26.png" width="320" /></a></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 24px;"><br /></span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 24px;"><br /></span></div>
<div style="font-family: Menlo; font-size: 11px; font-weight: normal;">
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 24px;"><br /></span></div>
<br /></div>
<div>
<div style="font-weight: normal; text-align: start;">
<span style="font-weight: normal;">That's all for today. In my next part I'll add </span><span style="font-weight: normal;">collision detection and </span><span style="font-weight: normal;">a basic game logic for life lost, game over, pause, score behavior.</span></div>
<div style="font-weight: normal; text-align: start;">
<span style="font-weight: normal;">You can download the code from GitHub: <a href="https://github.com/stfnjstn/MySecondGame/releases/tag/v0.3">Part 3</a> or the latest version <a href="https://github.com/stfnjstn/MySecondGame/tree/master">here</a>.</span></div>
<div style="font-weight: normal; text-align: start;">
<br />
You can also download my prototyping App for this tutorial series: </div>
</div>
</div>
<div style="text-align: center;">
<a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></div>
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<br /></div>
<div style="font-weight: normal; text-align: start;">
Cheers,<br />
Stefan</div>
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>
Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com0tag:blogger.com,1999:blog-7769140974661448994.post-32878865853978853272014-11-11T13:55:00.000-08:002015-08-30T10:09:03.428-07:00How to implement a space shooter with SpriteKit and SWIFT - Part 2<h3>
<span style="color: #323333; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; font-size: 20px;">Adding enemies, bullets and shooting with </span><span style="color: #323333; font-size: 20px;"><span style="font-family: Courier New, Courier, monospace;">SKAction</span></span><span style="color: #323333; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; font-size: 20px;"> and </span><span style="color: #323333; font-size: 20px;"><span style="font-family: Courier New, Courier, monospace;">SKConstraint</span></span></h3>
<h3 style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: bolder; margin: 0.83em 0px; padding: 0px; position: relative; text-align: justify;">
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<iframe allowfullscreen="1" frameborder="0" height="400" src="//www.youtube.com/embed/8d8MH_gXt84" width="600"></iframe></div>
<div style="font-weight: normal;">
<br /></div>
<br />
<div style="text-align: center;">
<a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></div>
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<br /></div>
</h3>
<h3 style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: bolder; margin: 0.83em 0px; padding: 0px; position: relative; text-align: justify;">
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 14px;">Tutorial Overview:</span></h3>
<div style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-weight: normal; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with.html">Part 1</a>: Initial project setup, sprite creation and movement using </span><span style="font-family: 'Courier New', Courier, monospace;">SKAction</span><span style="font-family: 'Helvetica Neue Light';"> and </span><span style="font-family: 'Courier New', Courier, monospace;">SKConstraint</span></span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_11.html">Part 2</a>: Adding enemies, bullets and shooting with </span></span><span style="color: #323333; font-family: 'Courier New', Courier, monospace; font-weight: normal;">SKAction</span><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"> and </span><span style="color: #323333; font-family: 'Courier New', Courier, monospace; font-weight: normal;">SKConstraint</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_19.html">Part 3</a>: </span><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;">Adding a HUD with </span><span style="color: #323333; font-family: Courier New, Courier, monospace; font-weight: normal;">SKLabelNode</span><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"> and </span><span style="color: #323333; font-family: Courier New, Courier, monospace; font-weight: normal;">SKSpriteNode</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_27.html">Part 4</a>: </span><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;">Adding basic game logic and collision detection</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_27.html">Part 5</a>: Adding particles and sound </span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="https://www.blogger.com/I've%20finished%20Tutorial%20Part%206:%20How%20to%20impl.%20a%20space%20shooter%20in%20#SpriteKit and #SWIFT' - Game Center integration: http://stefansdevplayground.blogspot.de/2015/01/how-to-implement-space-shooter-with.html">Part 6</a>: GameCenter integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2015/02/how-to-implement-space-shooter-with.html">Part 7</a>: iAd integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2015/04/how-to-implement-in-app-purchase-for.html">Part 8</a>: In-App Purchases</span></li>
</ul>
</div>
<h2 style="text-align: justify;">
<div style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-weight: normal; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
</ul>
</div>
</h2>
<h3>
<span style="color: #323333;"><span style="font-size: 18px;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span></h3>
<h3>
<span style="color: #323333;"><span style="font-size: 18px;"><span style="font-family: Helvetica Neue Light;">Add the enemies:</span></span></span></h3>
<div>
<div style="text-align: justify;">
I'll add several enemy sprites. These will automatically follow and orient to the hero sprite. You can download the code from Part 1 <a href="https://github.com/stfnjstn/MySecondGame/releases/tag/v0.1">here</a>.
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9nSrITJG-NbocJk494WCNVdw_b6rFn6G2DTXKkWJtqD8jTw2iqPQWuMLINbvg41OUYie1V9STyPSV4rm3n0Rk8k6BKqnd0N7gPY4BK47EmMiHpSkiF6-IPM0laCqE-VGJhzYAjM1pjS0/s1600/Bildschirmfoto+2014-10-23+um+23.22.48.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" height="193" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9nSrITJG-NbocJk494WCNVdw_b6rFn6G2DTXKkWJtqD8jTw2iqPQWuMLINbvg41OUYie1V9STyPSV4rm3n0Rk8k6BKqnd0N7gPY4BK47EmMiHpSkiF6-IPM0laCqE-VGJhzYAjM1pjS0/s1600/Bildschirmfoto+2014-10-23+um+23.22.48.png" width="320" /></a></div>
<br />
<br />
<a name='more'></a><br />
<br />
<h4>
1. Add a new class EnemySpriteController:</h4>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm45QH3AIaj663tXL80sC3eRXDyHLqTNjptHjdhSNGU6RWFwff5aZNWPwSTlT4ywxDB26eKhJ0lv7CMoNL1bJo7JBFA8iUV35UfDRKpGuGC3DqWU1yF4keJnb9dh_91h95ewMAC2PVoX8/s1600/Bildschirmfoto+2014-11-09+um+23.47.25.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="187" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm45QH3AIaj663tXL80sC3eRXDyHLqTNjptHjdhSNGU6RWFwff5aZNWPwSTlT4ywxDB26eKhJ0lv7CMoNL1bJo7JBFA8iUV35UfDRKpGuGC3DqWU1yF4keJnb9dh_91h95ewMAC2PVoX8/s1600/Bildschirmfoto+2014-11-09+um+23.47.25.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghcQ-z8qXFdOYJyW-hKLo8jOupOP5173rmxSi3RAmnmnOEhEbchBjuViaDWwoi56gQIzUXauivF459ujwSUbF5DMsXiyq6Vpp7wl8jQW2VZVkryLEKnmFDU7VBlZi-OxO_g4qzzIuSHAM/s1600/Bildschirmfoto+2014-11-09+um+23.48.08.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghcQ-z8qXFdOYJyW-hKLo8jOupOP5173rmxSi3RAmnmnOEhEbchBjuViaDWwoi56gQIzUXauivF459ujwSUbF5DMsXiyq6Vpp7wl8jQW2VZVkryLEKnmFDU7VBlZi-OxO_g4qzzIuSHAM/s1600/Bildschirmfoto+2014-11-09+um+23.48.08.png" /></a></div>
<br />
<h4>
2. Import SpriteKit, define the class and add an array which stores all enemies:</h4>
<br />
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">import</span> SpriteKit</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// Controller class for:</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// - creating/destroying enemies, </div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// - shooting</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// - animitaions</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">class</span> EnemySpriteController {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> enemySprites: [<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>] = []</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<h4>
3. Add a new method <span style="font-family: Courier New, Courier, monospace;">spawnEnemy</span> to <span style="font-family: Courier New, Courier, monospace;">EnemySpriteController</span>:</h4>
<div style="text-align: justify;">
Nothing magic here. Just create a <span style="font-family: Courier New, Courier, monospace;">SKSpriteNode</span> and add it to the enemy collection. Targeting and orientation behavior is implemented with <span style="font-family: Courier New, Courier, monospace;">SKConstraints</span>. For details check my post: <a href="http://stefansdevplayground.blogspot.de/2014/09/howto-implement-targeting-or-follow.html">HowTo: Implement targeting or follow behavior for sprites with SpriteKit and SKConstraint</a>.</div>
<div>
<br /></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Return a new enemy sprite which follows the targetSprite node</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> spawnEnemy(targetSprite: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKNode</span>) -> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span> {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// create a new enemy sprite</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> newEnemy = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>(imageNamed:<span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"Spaceship"</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">enemySprites</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">append</span>(newEnemy)</div>
<div style="font-family: Menlo; font-size: 11px;">
newEnemy.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">xScale</span> = 0.08</div>
<div style="font-family: Menlo; font-size: 11px;">
newEnemy.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">yScale</span> = 0.08</div>
<div style="font-family: Menlo; font-size: 11px;">
newEnemy.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">color</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIColor</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">redColor</span>()</div>
<div style="font-family: Menlo; font-size: 11px;">
newEnemy.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">colorBlendFactor</span>=0.4</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// position new sprite at a random position on the screen</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> sizeRect = </span>UIScreen<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>mainScreen<span style="color: black; font-variant-ligatures: no-common-ligatures;">().</span>applicationFrame<span style="color: black; font-variant-ligatures: no-common-ligatures;">;</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> posX = </span>arc4random_uniform<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>UInt32<span style="color: black; font-variant-ligatures: no-common-ligatures;">(sizeRect.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>width<span style="color: black; font-variant-ligatures: no-common-ligatures;">))</span></div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> posY = </span>arc4random_uniform<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>UInt32<span style="color: black; font-variant-ligatures: no-common-ligatures;">(sizeRect.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>height<span style="color: black; font-variant-ligatures: no-common-ligatures;">))</span></div>
<div style="font-family: Menlo; font-size: 11px;">
newEnemy.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPoint</span>(x: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span>(posX), y: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span>(posY))</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Define Constraints for orientation/targeting behavior</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> i = </span>enemySprites<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>count<span style="color: black; font-variant-ligatures: no-common-ligatures;">-1</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> rangeForOrientation = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKRange</span>(constantValue:<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">M_2_PI</span>*7))</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> orientConstraint = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKConstraint</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">orientToNode</span>(targetSprite, offset: rangeForOrientation)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> rangeToSprite = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKRange</span>(lowerLimit: 80, upperLimit: 90)</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> distanceConstraint: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKConstraint</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// First enemy has to follow spriteToFollow, second enemy has to follow first enemy, ...</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">enemySprites</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">count</span>-1 == 0 {</div>
<div style="font-family: Menlo; font-size: 11px;">
distanceConstraint = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKConstraint</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">distance</span>(rangeToSprite, toNode: targetSprite)</div>
<div style="font-family: Menlo; font-size: 11px;">
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> {</div>
<div style="font-family: Menlo; font-size: 11px;">
distanceConstraint = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKConstraint</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">distance</span>(rangeToSprite, toNode: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">enemySprites</span>[i-1])</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
newEnemy.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">constraints</span> = [orientConstraint, distanceConstraint]</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">return</span> newEnemy</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
<h4>
4. Create a property for the <span style="font-family: Courier New, Courier, monospace;">EnemySpriteController</span> object inside <span style="font-family: Courier New, Courier, monospace;">GameScene.swift</span>:</h4>
<div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> enemySprites = </span>EnemySpriteController<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span></div>
</div>
<br />
<h4>
5. Create some enemies at the end of <span style="font-family: Courier New, Courier, monospace;">didMoveToView</span> method inside <span style="font-family: Courier New, Courier, monospace;">GameScene.swift</span>:</h4>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// Add enemy sprites</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">for</span>(<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> i=0; i<3;i++){</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff;">self</span>.<span style="color: #3495af;">addChild</span>(<span style="color: #3495af;">enemySprites</span>.<span style="color: #3495af;">spawnEnemy</span>(<span style="color: #3495af;">heroSprite</span>))</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<br />
Result are three red enemy sprites which will follow the white spaceship. Next steps are adding bullets and shooting.<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjK2-d6iLRLVJBQH88lRCNGFf_jympA6Vif-EQDjqSCw6zh0KD6udNqNWCy57yeKo1iyyEZ_1CqAq_iEXlJKq9qZEv5k3fIcX-CEXgjqyy4ocH_n5tKCa2y_0K7t0MoEdQ6itgPWtEIkDY/s1600/IMG_8379-1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjK2-d6iLRLVJBQH88lRCNGFf_jympA6Vif-EQDjqSCw6zh0KD6udNqNWCy57yeKo1iyyEZ_1CqAq_iEXlJKq9qZEv5k3fIcX-CEXgjqyy4ocH_n5tKCa2y_0K7t0MoEdQ6itgPWtEIkDY/s320/IMG_8379-1.jpg" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<h4>
6. Add a <span style="font-family: Courier New, Courier, monospace;">shoot</span> method inside <span style="font-family: Courier New, Courier, monospace;">EnemySpriteController.swift</span>: </h4>
<div>
<span style="text-align: justify;">The shoot method iterates over each enemy sprite, creates a bullet, determines a vector to the target object and starts a SKAction which moves the bullet.</span></div>
<div>
<span style="text-align: justify;"><br /></span></div>
<div>
<span style="text-align: justify;"><br /></span></div>
<div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
// Shoot in direction of spriteToShoot</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> shoot(targetSprite: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKNode</span>) {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">for</span> enemy <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">enemySprites</span> {</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<span style="color: #008f00;">// Create the bullet sprite</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> bullet = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>()</div>
<div style="font-family: Menlo; font-size: 11px;">
bullet.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">color</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIColor</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">greenColor</span>()</div>
<div style="font-family: Menlo; font-size: 11px;">
bullet.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGSize</span>(width: 5,height: 5)</div>
<div style="font-family: Menlo; font-size: 11px;">
bullet.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span> = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPointMake</span>(enemy.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">x</span>, enemy.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">y</span>)</div>
<div style="font-family: Menlo; font-size: 11px;">
targetSprite.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">parent</span>?.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">addChild</span>(bullet)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #008f00;">// Determine vector to targetSprite</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> vector = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGVectorMake</span>((targetSprite.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">x-</span>enemy.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">x</span>), targetSprite.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">y-</span>enemy.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">y</span>)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #008f00;">// Create the action to move the bullet. Don't forget to remove the bullet!</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff;">let</span><span style="color: black;"> bulletAction = </span><span style="color: #3495af;">SKAction</span><span style="color: black;">.</span><span style="color: #3495af;">sequence</span><span style="color: black;">([</span><span style="color: #3495af;">SKAction</span><span style="color: black;">.</span><span style="color: #3495af;">repeatAction</span><span style="color: black;">(</span><span style="color: #3495af;">SKAction</span><span style="color: black;">.</span><span style="color: #3495af;">moveBy</span><span style="color: black;">(vector, duration: 1), count: 10) , </span><span style="color: #3495af;">SKAction</span><span style="color: black;">.</span><span style="color: #3495af;">waitForDuration</span><span style="color: black;">(30.0/60.0), </span><span style="color: #3495af;">SKAction</span><span style="color: black;">.</span><span style="color: #3495af;">removeFromParent</span><span style="color: black;">()])</span></div>
<div style="font-family: Menlo; font-size: 11px;">
bullet.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">runAction</span>(bulletAction)</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<h4>
7. Call <span style="font-family: Courier New, Courier, monospace;">shoot</span> inside the <span style="font-family: Courier New, Courier, monospace;">update</span> method of <span style="font-family: Courier New, Courier, monospace;">GameScene.swift</span>: </h4>
</div>
<div>
<span style="text-align: justify;"><span style="font-family: Courier New, Courier, monospace;">SpriteKit</span> cannot guarantee in which time intervals the <span style="font-family: Courier New, Courier, monospace;">update</span> method is called. To ensure that the enemies shoot every second, I'll store the time interval when <span style="font-family: Courier New, Courier, monospace;">shoot</span> was called in a global property. </span></div>
<div>
<span style="text-align: justify;"><br /></span></div>
<div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> _dLastShootTime: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CFTimeInterval</span> = 1</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<span style="color: #0433ff;"><br /></span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<span style="color: #0433ff;">override</span> <span style="color: #0433ff;">func</span> update(currentTime: <span style="color: #3495af;">CFTimeInterval</span>) {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>/* Called before each frame is rendered */</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<span style="color: #0433ff;">if</span> currentTime - <span style="color: #3495af;">_dLastShootTime</span> >= 1 {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af;">enemySprites<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>shoot<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">_dLastShootTime</span>=currentTime</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRAPKvcn3AGmhzhKL_Y3_7CIahu-UIJGP5h-mLF6SvStmc6QEzhPfPjDQAaeRtF5Gj-UbiM1Ql4ATcaeL7thACAvy_ARoCISCSfSOcTgHHOLOYW1891cYedmoa8iDeArua8rMWDZnFdLw/s1600/IMG_8379.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRAPKvcn3AGmhzhKL_Y3_7CIahu-UIJGP5h-mLF6SvStmc6QEzhPfPjDQAaeRtF5Gj-UbiM1Ql4ATcaeL7thACAvy_ARoCISCSfSOcTgHHOLOYW1891cYedmoa8iDeArua8rMWDZnFdLw/s320/IMG_8379.PNG" width="320" /></a>
</div>
<br />
That's all for today. In my next part I'll add a HUD, implement a basic game loging and add collision detection.<br />
You can download the code from GitHub: <a href="https://github.com/stfnjstn/MySecondGame/releases/tag/v0.2">Part 2</a> or the latest version <a href="https://github.com/stfnjstn/MySecondGame/tree/master">here</a>.<br />
<br />
You can also download my prototyping App for this tutorial series:</div>
<br />
<div style="text-align: center;">
<a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></div>
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<br /></div>
<br />
Cheers,
Stefan
<br />
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>
Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com8tag:blogger.com,1999:blog-7769140974661448994.post-90574126393025794672014-11-05T15:11:00.002-08:002015-08-30T10:08:14.637-07:00How to implement a space shooter with SpriteKit and SWIFT - Part 1<h2>
Initial project setup, sprite creation and movement using <span style="font-family: Courier New, Courier, monospace;">SKAction</span> and <span style="font-family: Courier New, Courier, monospace;">SKConstraint</span></h2>
<br />
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen="1" frameborder="0" height="400" src="//www.youtube.com/embed/8d8MH_gXt84" width="600"></iframe>
</div>
<br />
<br />
<div style="text-align: center;">
<a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></div>
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<h3 style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: bolder; margin: 0.83em 0px; padding: 0px; position: relative; text-align: justify;">
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 14px;">Tutorial Overview:</span></h3>
<div style="color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-weight: normal; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with.html">Part 1</a>: Initial project setup, sprite creation and movement using </span><span style="font-family: 'Courier New', Courier, monospace;">SKAction</span><span style="font-family: 'Helvetica Neue Light';"> and </span><span style="font-family: 'Courier New', Courier, monospace;">SKConstraint</span></span></li>
</ul>
<div>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_11.html">Part 2</a>: Adding enemies, bullets and shooting with </span></span><span style="color: #323333; font-family: 'Courier New', Courier, monospace; font-weight: normal;">SKAction</span><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"> and </span><span style="color: #323333; font-family: 'Courier New', Courier, monospace; font-weight: normal;">SKConstraint</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_19.html">Part 3</a>: Adding a HUD with </span><span style="color: #323333; font-family: Courier New, Courier, monospace; font-weight: normal;">SKLabelNode</span><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"> and </span><span style="color: #323333; font-family: Courier New, Courier, monospace; font-weight: normal;">SKSpriteNode</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_27.html">Part 4</a>: Adding basic game logic and collision detection</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/12/how-to-implement-space-shooter-with.html">Part 5</a>: Adding particles and sound </span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="https://www.blogger.com/I've%20finished%20Tutorial%20Part%206:%20How%20to%20impl.%20a%20space%20shooter%20in%20#SpriteKit and #SWIFT' - Game Center integration: http://stefansdevplayground.blogspot.de/2015/01/how-to-implement-space-shooter-with.html">Part 6</a>: GameCenter integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2015/02/how-to-implement-space-shooter-with.html">Part 7</a>: iAd integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2015/04/how-to-implement-in-app-purchase-for.html">Part 8</a>: In-App Purchases</span></li>
</ul>
</div>
</div>
<h2 style="text-align: justify;">
<span style="color: #323333;"><span style="font-size: 18px;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span></h2>
<h2 style="text-align: justify;">
<span style="color: #323333;"><span style="font-size: 18px;"><span style="font-family: Helvetica Neue Light;">1. Create a new universal project (template: game; language: Swift)</span></span></span></h2>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtW1Yo-cF0KmdetCPG92RToJpBLREzrTHk9U1X640M2pnd5zIhPI77rsCfJ66DbnmgKsKCMmpe-h5P6aPXjQHOUDTKlZcJDSyeKZ_dH-1c9sXej_PbzctZXJER3sELk7sQkvRXJolN0jU/s1600/Bildschirmfoto+2014-10-23+um+23.35.10.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="188" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtW1Yo-cF0KmdetCPG92RToJpBLREzrTHk9U1X640M2pnd5zIhPI77rsCfJ66DbnmgKsKCMmpe-h5P6aPXjQHOUDTKlZcJDSyeKZ_dH-1c9sXej_PbzctZXJER3sELk7sQkvRXJolN0jU/s1600/Bildschirmfoto+2014-10-23+um+23.35.10.png" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhaORFlkzE0nkxYJtDdae7XuKyHyRn0IwHtZukzyz0CU3RY7-VqxiXLs0CXuFFDk48TGxVc1qQba3vXi1A1wIZzlurcsa1w4bA8IL8FIqOqnhSPGDe0O3sgzeQzeOGCTOtbyqcUUtsl3K8/s1600/Bildschirmfoto+2014-10-23+um+23.35.33.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="188" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhaORFlkzE0nkxYJtDdae7XuKyHyRn0IwHtZukzyz0CU3RY7-VqxiXLs0CXuFFDk48TGxVc1qQba3vXi1A1wIZzlurcsa1w4bA8IL8FIqOqnhSPGDe0O3sgzeQzeOGCTOtbyqcUUtsl3K8/s1600/Bildschirmfoto+2014-10-23+um+23.35.33.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<a name='more'></a><br />
<br />
<div>
<h3>
<span style="color: #323333;"><span style="font-size: 18px;"><span style="font-family: Helvetica Neue Light;">2. Remove the GameScene.sks file</span></span></span></h3>
</div>
<div>
<h4>
<span style="color: #323333;"><span style="font-size: 14px;"><span style="font-family: Helvetica Neue Light;">I'll not use the internal level editor, so this file is obsolete and can be deleted:</span></span></span></h4>
</div>
<div>
<span style="color: #323333;"><span style="font-size: 14px;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbZFLRyT5n3W-Y0rWiwgyuwWiheKAFRfXgiTTa2v2kJRS_7iqZFlCv1OBWiQy9WTDdqtCM4UyQ0hh3ty1J5sdtPkX5DeXpBHQeYKfm9p3cnePmz_rWueJ56myn_CYdS7deFC_q5-wAKDc/s1600/Bildschirmfoto+2014-10-23+um+23.40.45.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbZFLRyT5n3W-Y0rWiwgyuwWiheKAFRfXgiTTa2v2kJRS_7iqZFlCv1OBWiQy9WTDdqtCM4UyQ0hh3ty1J5sdtPkX5DeXpBHQeYKfm9p3cnePmz_rWueJ56myn_CYdS7deFC_q5-wAKDc/s1600/Bildschirmfoto+2014-10-23+um+23.40.45.png" /></a></div>
<div>
<span style="color: #323333;"><span style="font-size: 14px;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span></div>
<div>
<h4>
<span style="color: #323333;"><span style="font-size: 14px;"><span style="font-family: Helvetica Neue Light;">Inside </span><span style="font-family: Courier New, Courier, monospace;">GameController.swift</span><span style="font-family: Helvetica Neue Light;"> remove this code block:</span></span></span></h4>
</div>
<div>
<div style="font-family: Menlo; font-size: 11px;">
<div style="color: #0433ff;">
extension<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKNode</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> {</span></div>
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">class</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> unarchiveFromFile(file : <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">NSString</span>) -> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKNode</span>? {<br />
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> path = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">NSBundle</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">mainBundle</span>().<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">pathForResource</span>(file, ofType: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"sks"</span>) {<br />
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> sceneData = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">NSData</span>(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span>)!<br />
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> archiver = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">NSKeyedUnarchiver</span>(forReadingWithData: sceneData)<br />
<div style="min-height: 13px;">
</div>
archiver.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">setClass</span>(<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">classForKeyedUnarchiver</span>(), forClassName: <span style="color: #b4261a; font-variant-ligatures: no-common-ligatures;">"SKScene"</span>)<br />
<div style="color: #3495af;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> scene = archiver.</span>decodeObjectForKey<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>NSKeyedArchiveRootObjectKey<span style="color: black; font-variant-ligatures: no-common-ligatures;">) </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">as</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>GameScene</div>
archiver.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">finishDecoding</span>()<br />
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">return</span> scene<br />
} <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">else</span> {<br />
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">return</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">nil</span><br />
}<br />
}<br />
<br />
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<h4>
<span style="color: #323333;"><span style="font-size: 14px;"><span style="font-family: Helvetica Neue Light;">Replace the </span><span style="font-family: Courier New, Courier, monospace;">ViewDidLoad </span></span></span><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 14px;">method with this code to create a scene in fullscreen mode for iPad and iPhone:</span></h4>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> viewDidLoad() {</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">super</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">viewDidLoad</span>()</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Detect the screensize</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> sizeRect = </span>UIScreen<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>mainScreen<span style="color: black; font-variant-ligatures: no-common-ligatures;">().</span>applicationFrame</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> width = sizeRect.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">width</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">*</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIScreen</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">mainScreen</span>().<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">scale</span></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> height = sizeRect.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">size</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">height</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">*</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIScreen</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">mainScreen</span>().<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">scale</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Scene should be shown in fullscreen mode</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> scene = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">GameScene</span>(size: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGSizeMake</span>(width, height))</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Configure the view.</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> skView = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">view</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">as</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKView</span></div>
<div style="font-family: Menlo; font-size: 11px;">
skView.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">showsFPS</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span></div>
<div style="font-family: Menlo; font-size: 11px;">
skView.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">showsNodeCount</span> = <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>/* Sprite Kit applies additional optimizations to improve rendering performance */</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> skView.</span>ignoresSiblingOrder<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">true</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>/* Set the scale mode to scale to fit the window */</div>
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> scene.</span>scaleMode<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = .</span>AspectFill</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
skView.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">presentScene</span>(scene)</div>
<br />
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<span style="color: #323333; font-family: Helvetica Neue Light;"><span style="font-size: 14px;"><br /></span></span>
<br />
<div style="color: #3495af; font-size: 11px;">
<h4 style="color: black;">
<span style="color: #323333;"><span style="font-size: 14px;"><span style="font-family: Helvetica Neue Light;">Limit the supported screen orientations to </span><span style="font-family: Courier New, Courier, monospace;">LandscapeLeft</span><span style="font-family: Helvetica Neue Light;">:</span></span></span></h4>
<div style="font-family: Menlo;">
<br /></div>
<div style="font-family: Menlo;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> supportedInterfaceOrientations() -> Int {</div>
<div style="font-family: Menlo;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">return</span><span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>Int<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>UIInterfaceOrientationMask<span style="color: black; font-variant-ligatures: no-common-ligatures;">.LandscapeLeft.</span>rawValue<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="font-family: Menlo;">
</div>
<div style="font-family: Menlo;">
}</div>
</div>
</div>
<div>
<br />
<h3>
<span style="color: #323333;"><span style="font-size: 18px;"><span style="font-family: Helvetica Neue Light;">3. Add the space ship:</span></span></span></h3>
<h4>
</h4>
<div>
I'll add a spaceship sprite. The sprite will automatically orient and move to a touch location on the screen:<br />
<br /></div>
<div>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpTCI0pTUt5SzdvqTkpIKiif1qT1cA1trDFeMxp01AUBKXdxr57cSXs8ukcdZonPa-xHCneLrUTX5nifyi91JPQq7vANjt2Wwd5uUj5M_y-KTLpZXwEMKQEmohN3wblxuI9QMo7yAcVhM/s1600/Bildschirmfoto+2014-10-23+um+23.23.13.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" height="197" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpTCI0pTUt5SzdvqTkpIKiif1qT1cA1trDFeMxp01AUBKXdxr57cSXs8ukcdZonPa-xHCneLrUTX5nifyi91JPQq7vANjt2Wwd5uUj5M_y-KTLpZXwEMKQEmohN3wblxuI9QMo7yAcVhM/s1600/Bildschirmfoto+2014-10-23+um+23.23.13.png" width="320" /></a><br />
<br />
<br /></div>
<div style="text-align: justify;">
<div style="text-align: start;">
<div style="text-align: justify;">
The automated orienting of the spaceship is implemented with a little trick. I'll add a <span style="font-family: Courier New, Courier, monospace;">SKConstraint</span> to an invisible sprite. I case of a touch event, the invisible sprite is moved to the touch location and the spaceship is automatically oriented to this invisible sprite/touch location.</div>
<br />
<br />
<h3 style="text-align: justify;">
<span style="color: #323333;"><span style="font-size: 14px;"><span style="font-family: Helvetica Neue Light;">3.1. Open the </span><span style="font-family: Courier New, Courier, monospace;">GameScene.swift</span><span style="font-family: Helvetica Neue Light;"> file and r</span></span></span><span style="color: #323333;"><span style="font-size: 14px;"><span style="font-family: Helvetica Neue Light;">emove the code for creating a label in </span><span style="font-family: Courier New, Courier, monospace;">didMoveToView</span><span style="font-family: Helvetica Neue Light;">:</span></span></span></h3>
<div style="text-align: justify;">
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff;">override</span> <span style="color: #0433ff;">func</span> didMoveToView(view: <span style="color: #3495af;">SKView</span>) {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>/* Setup your scene here */</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<h3>
<span style="color: #323333;"><span style="font-size: 14px;"><span style="font-family: Helvetica Neue Light;">3.2. Create two global Sprite properties:</span></span></span></h3>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff;">var</span> heroSprite = <span style="color: #3495af;">SKSpriteNode</span>(imageNamed:<span style="color: #b4261a;">"Spaceship"</span>)<br />
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;"> var</span> invisibleControllerSprite = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKSpriteNode</span>()</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #0433ff;">override</span> <span style="color: #0433ff;">func</span> didMoveToView(view: <span style="color: #3495af;">SKView</span>) {</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>/* Setup your scene here */</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<h3>
<span style="color: #323333;"><span style="font-size: 14px;"><span style="font-family: Helvetica Neue Light;">3.3. Create a sprite inside </span></span></span><span style="color: #323333; font-family: Courier New, Courier, monospace; font-size: 14px;">didMoveToView</span><span style="color: #323333; font-family: Helvetica Neue Light; font-size: 14px;">:</span></h3>
</div>
<div style="text-align: justify;">
<div style="color: #3495af; font-family: Menlo; font-size: 11px;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>backgroundColor<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>UIColor<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>blackColor<span style="color: black; font-variant-ligatures: no-common-ligatures;">()</span><br />
<div style="min-height: 13px;">
</div>
<div style="color: #008f00;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Create the hero sprite and place it in the middle of the screen</div>
<div style="color: #008f00;">
<span style="color: #3495af;"> </span><span style="color: #3495af;">heroSprite</span><span style="color: #3495af;">.</span><span style="color: #3495af;">xScale</span><span style="color: #3495af;"> = 0.15</span></div>
<div style="color: #008f00;">
<span style="color: #3495af;"> </span><span style="color: #3495af;">heroSprite</span><span style="color: #3495af;">.</span><span style="color: #3495af;">yScale</span><span style="color: #3495af;"> = 0.15</span></div>
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>position<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>CGPointMake<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>frame<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>width/<span style="color: black; font-variant-ligatures: no-common-ligatures;">2, </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>frame<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>height/<span style="color: black; font-variant-ligatures: no-common-ligatures;">2)</span><br />
<span style="color: black;"> </span><span style="color: #0433ff;">self</span><span style="color: black;">.</span>addChild<span style="color: black;">(</span>heroSprite<span style="color: black;">)</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<br /></div>
<h3>
<span style="color: #323333;"><span style="font-family: Helvetica Neue Light;"><span style="font-size: 14px;">3.4. Create an invisible sprite to implement the orientation behavior inside </span></span></span><span style="color: #323333; font-family: Courier New, Courier, monospace; font-size: 14px;">didMoveToView</span><span style="color: #323333; font-family: Helvetica Neue Light; font-size: 14px;">:</span></h3>
</div>
<div style="color: #008f00; font-family: Menlo; font-size: 11px; text-align: justify;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Define invisible sprite for rotating and steering behavior without trigonometry<br />
<div style="color: #3495af;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>invisibleControllerSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>size<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = </span>CGSizeMake<span style="color: black; font-variant-ligatures: no-common-ligatures;">(0, 0)</span></div>
<div style="color: #3495af;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span><span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span><span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>addChild<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>invisibleControllerSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">)</span></div>
<div style="min-height: 13px;">
</div>
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Define a constraint for the orientation behavior<br />
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> rangeForOrientation = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKRange</span>(constantValue: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">M_2_PI</span>*7))<br />
<br />
<div style="color: #3495af;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>heroSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>constraints<span style="color: black; font-variant-ligatures: no-common-ligatures;"> = [</span>SKConstraint<span style="color: black; font-variant-ligatures: no-common-ligatures;">.</span>orientToNode<span style="color: black; font-variant-ligatures: no-common-ligatures;">(</span>invisibleControllerSprite<span style="color: black; font-variant-ligatures: no-common-ligatures;">, offset: rangeForOrientation)]</span></div>
</div>
<div style="font-family: Menlo; font-size: 11px; text-align: justify;">
<br /></div>
<div style="font-family: Menlo; font-size: 11px; text-align: justify;">
<br /></div>
<h3 style="text-align: justify;">
<span style="color: #323333;"><span style="font-size: 14px;"><span style="font-family: Helvetica Neue Light;">3.5. Replace the code inside the </span></span></span><span style="color: #323333; font-family: Courier New, Courier, monospace; font-size: 14px;">touchesBegan</span><span style="color: #323333; font-family: Helvetica Neue Light; font-size: 14px;"> method to implement the sprite movement:</span></h3>
<div style="font-family: Menlo; font-size: 11px; text-align: justify;">
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">override</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">func</span> touchesBegan(touches: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">NSSet</span>, withEvent event: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">UIEvent</span>) {<br />
<div style="color: #008f00;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>/* Called when a touch begins */</div>
<div style="min-height: 13px;">
</div>
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">for</span> touch: <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">AnyObject</span> <span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">in</span> touches {<br />
<div style="min-height: 13px;">
</div>
<div style="min-height: 13px;">
<br /></div>
<div style="color: #008f00;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Determine the new position for the invisible sprite:</div>
<div style="color: #008f00;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// The calculation is needed to ensure the positions of both sprites </div>
<div style="color: #008f00;">
// are nearly the same, but different. Otherwise the hero sprite rotates</div>
<div style="color: #008f00;">
// back to it's original orientation after reaching the location of </div>
<div style="color: #008f00;">
// the invisible sprite</div>
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> xOffset:<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span> = 1.0<br />
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> yOffset:<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGFloat</span> = 1.0<br />
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">var</span> location = touch.locationInNode(<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">self</span>)<br />
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> location.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">x</span>><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">heroSprite</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">x</span> {<br />
xOffset = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">-</span>1.0<br />
}<br />
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">if</span> location.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">y</span>><span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">heroSprite</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">position</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">y</span> {<br />
yOffset = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">-</span>1.0<br />
}<br />
<div style="min-height: 13px;">
</div>
<div style="color: #008f00;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Create an action to move the invisibleControllerSprite. </div>
<div style="color: #008f00;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// This will cause automatic orientation changes for the hero sprite</div>
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> actionMoveInvisibleNode = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKAction</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">moveTo</span>(<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">CGPointMake</span>(location.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">x</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">-</span> xOffset, location.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">y</span> <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">-</span> yOffset), duration: 0.2)<br />
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">invisibleControllerSprite</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">runAction</span>(actionMoveInvisibleNode)<br />
<div style="min-height: 13px;">
</div>
<div style="color: #008f00;">
<span style="color: black; font-variant-ligatures: no-common-ligatures;"> </span>// Create an action to move the hero sprite to the touch location</div>
<span style="color: #0433ff; font-variant-ligatures: no-common-ligatures;">let</span> actionMove = <span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">SKAction</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">moveTo</span>(location, duration: 1)<br />
<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">heroSprite</span>.<span style="color: #3495af; font-variant-ligatures: no-common-ligatures;">runAction</span>(actionMove)<br />
<div style="min-height: 13px;">
</div>
}<br />
<br />
}<br />
<div>
<br /></div>
</div>
</div>
<div style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 14px;">
<br />
That's all for today. In my next part I'll add some enemies and bullets.<br />
You can download the code from GitHub: <a href="https://github.com/stfnjstn/MySecondGame/releases/tag/v0.1">Part 1</a> or the latest version <a href="https://github.com/stfnjstn/MySecondGame/tree/master">here</a>.<br />
<br />
You can also download my prototyping App for this tutorial series: </div>
</div>
</div>
<br />
<div style="text-align: center;">
<a href="https://itunes.apple.com/us/app/yet-another-spaceshooter/id949662362?mt=8"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlGa4tzTokNQ0HE7Lwk_FazHoZPqfLjiqQiM9z_IqZgO-I8ZTcD_w_FER1sUl3xkNLaLE9TX71FQJ5MsWe_3Fds2mkM6GsHainuyIShQLVY36wtbZ5OpF7aRpWZKbtmxHEvqgAzgXN5Nk/s1600/AppStore.png" /></a></div>
<div class="separator" style="clear: both; font-weight: normal; text-align: center;">
<br /></div>
<br />
Cheers,<br />
Stefan
<br />
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>
Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com6tag:blogger.com,1999:blog-7769140974661448994.post-67814516822271814612014-10-21T10:56:00.002-07:002014-10-21T10:56:21.842-07:00Writing games is more fun than writing a blog post: New teaser video for my upcoming tutorialSorry, I love writing blog posts, but this time I have written a small game instead of completing my tutorial. Here's another short teaser video:<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen="1" frameborder="0" height="400" src="//www.youtube.com/embed/8d8MH_gXt84" width="600"></iframe>
</div>
<br />
<br />
That's all for today. The complete code will be available at GitHub soon.<br />
<br />
Cheers,<br />
Stefan
<br />
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>
Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com0tag:blogger.com,1999:blog-7769140974661448994.post-17735917341313374182014-10-08T14:25:00.000-07:002015-06-06T14:53:05.165-07:00Kick off for my new Tutorial: How to implement a space shooter with SpriteKit and SWIFT<h2>
I'm preparing a new tutorial: </h2>
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 14px;">My goal is to implement a complete space shooter with SpriteKit and SWIFT.</span><br />
<h3>
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 14px; text-align: justify;">Planned content is:</span></h3>
<div>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with.html">Part 1</a>: Initial project setup, sprite creation and movement using </span><span style="font-family: 'Courier New', Courier, monospace;">SKAction</span><span style="font-family: 'Helvetica Neue Light';"> and </span><span style="font-family: 'Courier New', Courier, monospace;">SKConstraint</span></span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333;"><span style="font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_11.html">Part 2</a>: Adding enemies, bullets and shooting with </span></span><span style="color: #323333; font-family: 'Courier New', Courier, monospace; font-weight: normal;">SKAction</span><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"> and </span><span style="color: #323333; font-family: 'Courier New', Courier, monospace; font-weight: normal;">SKConstraint</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_19.html">Part 3</a>: Adding a HUD with </span><span style="color: #323333; font-family: Courier New, Courier, monospace; font-weight: normal;">SKLabelNode</span><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"> and </span><span style="color: #323333; font-family: Courier New, Courier, monospace; font-weight: normal;">SKSpriteNode</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/11/how-to-implement-space-shooter-with_27.html">Part 4</a>: Adding basic game logic and collision detection</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2014/12/how-to-implement-space-shooter-with.html">Part 5</a>: Adding particles and sound </span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="https://www.blogger.com/I've%20finished%20Tutorial%20Part%206:%20How%20to%20impl.%20a%20space%20shooter%20in%20#SpriteKit and #SWIFT' - Game Center integration: http://stefansdevplayground.blogspot.de/2015/01/how-to-implement-space-shooter-with.html">Part 6</a>: GameCenter integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2015/02/how-to-implement-space-shooter-with.html">Part 7</a>: iAd integration</span></li>
</ul>
<ul style="list-style-image: initial; list-style-position: initial; margin: 0.5em 0px; outline: none; padding: 0px 0px 0px 2em;">
<li style="margin: 0px; outline: none; padding: 0px;"><span style="color: #323333; font-family: 'Helvetica Neue Light'; font-weight: normal;"><a href="http://stefansdevplayground.blogspot.de/2015/04/how-to-implement-in-app-purchase-for.html">Part 8</a>: In-App Purchases</span></li>
</ul>
</div>
<h3>
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 14px; text-align: justify;">Here's a short teaser video:</span></h3>
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen="1" frameborder="0" height="400" src="//www.youtube.com/embed/yTAAR9c9pYU" width="600"></iframe>
</div>
<br />
<span style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 14px; text-align: justify;">The sample code will be available on GitHub</span>
<br />
<span style="color: #323333;"><span style="font-size: 14px;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<span style="color: #323333;"><span style="font-size: 14px;"><span style="font-family: Helvetica Neue Light;"><br /></span></span></span>
<br />
<div style="color: #323333; font-family: 'Helvetica Neue Light'; font-size: 14px; text-align: justify;">
That's all for today.<br />
<br />
Cheers,<br />
Stefan</div>
<table border="0"><colgroup><col width="30%"></col><col width="30%"></col><col width=""></col></colgroup><tbody>
<tr><td><div style="text-align: center;">
<a class="twitter-follow-button" data-show-count="false" href="https://twitter.com/StefanJosten">Follow @StefanJosten</a></div>
</td><td><div align="center">
<a href="https://github.com/stfnjstn/MyFirstGame.git"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg9p2C5_42OisxH5vwOQqcciA8TLsG6jFlYhVwQ2JpJX1lsJYXgw1_zRPhKft6N_qs8AwixMMsIZut3NwAj09MTZuNPwfyGPlZm7RQ7GFwC5bLoj4EmFrRPAlwddO6p1jaEgVS2aGHJ0aM/s1600/GitHub-Mark-32px.png" /></a></div>
</td><td><div align="right">
<a href="http://stackoverflow.com/users/3366741/stefan"><img alt="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" src="http://stackoverflow.com/users/flair/3366741.png" height="58" title="profile for Stefan at Stack Overflow, Q&A for professional and enthusiast programmers" width="208" /> </a></div>
</td></tr>
</tbody></table>Stefanhttp://www.blogger.com/profile/01659671244400264312noreply@blogger.com0