{"id":2570,"date":"2011-12-18T22:39:50","date_gmt":"2011-12-19T03:39:50","guid":{"rendered":"http:\/\/www.lonhosford.com\/lonblog\/?p=2570"},"modified":"2015-07-31T18:25:49","modified_gmt":"2015-07-31T23:25:49","slug":"kobold2d-xcode-4-introduction-tutorial-lesson-1-basic-accelerometer","status":"publish","type":"post","link":"https:\/\/www.lonhosford.com\/lonblog\/2011\/12\/18\/kobold2d-xcode-4-introduction-tutorial-lesson-1-basic-accelerometer\/","title":{"rendered":"Kobold2D XCode 4 Introduction Tutorial Lesson 1 &#8211; Basic Accelerometer"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p><script src=\"https:\/\/connect.facebook.net\/en_US\/all.js#appId=105467682877384&amp;xfbml=1\"><\/script><fb:like href=\"https:\/\/www.lonhosford.com\/lonblog\/2011\/12\/18\/kobold2d-xcode-4-introduction-tutorial-lesson-1-basic-accelerometer\/\" send=\"true\" width=\"450\" show_faces=\"true\" font=\"\"><\/fb:like><!-- Place this tag where you want the +1 button to render --><br \/>\n<g:plusone annotation=\"inline\"><\/g:plusone><\/p>\n<p><a href=\"https:\/\/www.lonhosford.com\/lonblog\/2011\/12\/19\/kobold2d-xcode-4-introduction-tutorial-lesson-2-set-landscape-orientation\/\">Lesson 2 ==&gt; <\/a>  <\/p>\n<p>This first lesson goes through the steps to set up a Kobold2D project in XCode and use the accelerometer in the IPhone or IPad.  <img loading=\"lazy\" decoding=\"async\" class=\"alignleft\" src=\"https:\/\/www.lonhosford.com\/images\/kobold2d\/common\/blog_article.png\" alt=\"\" width=\"160\" height=\"218\" \/><br \/>\n<figure style=\"width: 121px\" class=\"wp-caption alignright\"><a href=\"http:\/\/www.amazon.com\/gp\/product\/1430238135\/ref=as_li_ss_tl?ie=UTF8&#038;tag=hosfordusa&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=1430238135\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\"  src=\"https:\/\/images-na.ssl-images-amazon.com\/images\/I\/415NHKs30yL._SL160_.jpg\" alt=\"Learn cocos2D Game Development\" width=\"121\" height=\"160\" class=\"  \" title=\"Learn cocos2D Game Development\"\/><\/a><figcaption class=\"wp-caption-text\">Learn cocos2D Game Development<\/figcaption><\/figure> <\/p>\n<p>I am going to focus on the IPad, but the example should work fine in an IPhone as well. The example is very simple and is not yet a game. As the lessons continue a basic game will emerge. <\/p>\n<p><a href=\"http:\/\/www.kobold2d.com\" target=\"_blank\">Kobold2D<\/a> is based on <a href=\"http:\/\/www.cocos2d-iphone.org\/\" target=\"_blank\">Cocos2D for IPhone project<\/a>. Kobold2D helps make developing games in Cocos2D easier.<\/p>\n<p>The example works in portrait orientation. Later lessons you change the orientation. Their is a green circle graphic that sits at the bottom of the screen and you can move it left and right by tilting the device. The accelerometer data is read and using some basic velocity adjustments the image moves until it reaches an edge. Here is a screen shot of the completed project. <\/p>\n<figure style=\"width: 568px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\" \" style=\"border: 0pt none;\" src=\"https:\/\/www.lonhosford.com\/images\/kobold2d\/lesson_01\/ipad_running.png\" alt=\"\" width=\"568\" height=\"741\" \/><figcaption class=\"wp-caption-text\">Finished Lesson Running on IPad2 Simulator<\/figcaption><\/figure>\n<p>The frames per second indicator is an option you will see later in the config.lua file.<\/p>\n<p>You need to download and install <a href=\"http:\/\/www.kobold2d.com\" target = \"_blank\">Kobold2D<\/a> and <a href=\"http:\/\/developer.apple.com\/xcode\/\" target = \"_blank\">XCode<\/a>. <\/p>\n<p><strong>Lesson Downloads<\/strong><\/p>\n<ol>\n<li>\n<a onclick=\"javascript: pageTracker._trackPageview('\/downloads\/kobold2d\/intro_tutorial\/lesson01\/introduction_tutorial_lesson01_images.zip'); \" href=\"https:\/\/www.lonhosford.com\/content\/kobold2d\/intro_tutorial\/lesson01\/introduction_tutorial_lesson01_images.zip\">Images for project<\/a><\/li>\n<li>\n<a onclick=\"javascript: pageTracker._trackPageview('\/downloads\/kobold2d\/intro_tutorial\/lesson01\/Intro_Tutorial_Lesson_01.zip'); \" href=\"https:\/\/www.lonhosford.com\/content\/kobold2d\/intro_tutorial\/lesson01\/Intro_Tutorial_Lesson_01.zip\">Completed Project<\/a>. This is built in Kobold2d 1.0.1.<\/li>\n<\/ol>\n<p>[ad name=&#8221;Google Adsense&#8221;]<\/p>\n<p><strong>Step 1: Start a New Kobold2D Project<\/strong><\/p>\n<p>Kobold2D handles the creating of XCode projects through its own utility. Currently the recommendation is to close XCode before creating a new Kobold2D project. <\/p>\n<p>Find the folder you installed Kobold2D and then launch the Kobold2D Project Starter application.<\/p>\n<figure style=\"width: 243px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\" \" style=\"border: 0pt none;\" src=\"https:\/\/www.lonhosford.com\/images\/kobold2d\/common\/project_starter.png\" alt=\"\" width=\"243\" height=\"72\" \/><figcaption class=\"wp-caption-text\">Kobold2D Project Starter application<\/figcaption><\/figure>\n<p><strong>Step 2: Select the Empty-Project Template<\/strong><\/p>\n<p>You are going to start with a bare bones project. The project will actually run but only displays a default splash screen and title screen.<\/p>\n<p>Select an XCode workspace. The default KBold2d.xcworkspace is fine. All Kobold2d projects must be in an XCode workspace. This will appear to be an overkill once you get into XCode such as the Run shows all workspace projects and each with their own targets. You will find launching the wrong project a common problem. <\/p>\n<p>You can create a new workspace here by typing the name. For now we are staying with the default KBold2d.xcworkspace workspace.<\/p>\n<p>Type a project name and click the Create Project from Template button.<\/p>\n<figure style=\"width: 482px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\" \" style=\"border: 0pt none;\" src=\"https:\/\/www.lonhosford.com\/images\/kobold2d\/lesson_01\/empty_project.png\" alt=\"\" width=\"482\" height=\"665\" \/><figcaption class=\"wp-caption-text\">Selecting the Empty-Project Template<\/figcaption><\/figure>\n<p><strong>Step 3: Add IOS Icons<\/strong><\/p>\n<p>This step is optional as there will be default Kobold2d icons ready in your project. However if you have your own set you need to copy over them outside of XCode since they are already referenced in the XCode project. I provided a download of the various IOS icons in case you want to practice this step. Copy them as per this image.<\/p>\n<figure style=\"width: 481px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\" \" style=\"border: 0pt none;\" src=\"https:\/\/www.lonhosford.com\/images\/kobold2d\/lesson_01\/device_icons_drag.png\" alt=\"\" width=\"481\" height=\"302\" \/><figcaption class=\"wp-caption-text\">Replacing Default IOS Icons<\/figcaption><\/figure>\n<p><strong>Step 4: Add Game Player Images<\/strong><\/p>\n<p>The game pieces are provided in the download. There is a low and high resolution version named according to the XCode IOS conventions. They are going to be in the Project Files->Resources group. <strong>You must copy them from the Finder into the XCode project navigator<\/strong> shown as follows: <\/p>\n<figure style=\"width: 395px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\" \" style=\"border: 0pt none;\" src=\"https:\/\/www.lonhosford.com\/images\/kobold2d\/lesson_01\/copy_game_pieces.png\" alt=\"\" width=\"395\" height=\"215\" \/><figcaption class=\"wp-caption-text\">Adding Player Images<\/figcaption><\/figure>\n<p>Here is the &#8220;Choose options  for adding these files&#8221; dialog. Check &#8220;Copy items into destination group&#8217;s folder (if needed)&#8221;. &#8220;Create folder references for any added folders&#8221; option is selected although it does not apply as we are not copying folders. <\/p>\n<p>Finally Kobold2D is set up to build your game for a Mac delivery if you choose.  We are building for IOS and so you can choose -IOS choice which is proceeded by the name you gave to the project. In our case the &#8220;Intro_Tutorial_Lesson_01-IOS&#8221;<br \/>\n<figure style=\"width: 543px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\" \" style=\"border: 0pt none;\" src=\"https:\/\/www.lonhosford.com\/images\/kobold2d\/lesson_01\/copy_game_pieces_options.png\" alt=\"\" width=\"543\" height=\"455\" \/><figcaption class=\"wp-caption-text\">Choose options  for adding these files Dialog<\/figcaption><\/figure><\/p>\n<p>[ad name=&#8221;Google Adsense&#8221;]<br \/>\n<strong>Step 5: Naming the Game Layer Class Files<\/strong><\/p>\n<p>The default name for the Game Layer class is HelloWorldLayer. We can leave that and proceed without any issues. But most developers want more control over the name of classes. <\/p>\n<p>Plus you get to see the first glimpse to the internal workings of Kobold2D with the  <a href=\"http:\/\/www.lua.org\/about.html\" target = \"_blank\">Lua<\/a> file used for game configuration choices. One configuration is the main game layer class name.<\/p>\n<p>In the project explorer window rename the HelloWorldLayer.h and HelloWorldLayer.m files to GameLayer.h and GameLayer.m files respectively.<\/p>\n<figure style=\"width: 400px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\" \" style=\"border: 0pt none;\" src=\"https:\/\/www.lonhosford.com\/images\/kobold2d\/lesson_01\/rename_hello_world_layer_files.png\" alt=\"\" width=\"400\" height=\"122\" \/><figcaption class=\"wp-caption-text\">Renaming HelloWorldLayer Class Files<\/figcaption><\/figure>\n<p>Now open the config.lua file. You find this in the ProjectFiles->Resources folder. It is a text file. Change line 18 as you see below. You can look over some of the other potential configuration choices we can use as our work progress such as lines 42- 47 for controlling device orientation.<\/p>\n<p>Line 25 for example contains the DisplayFPS to toggle the viewing of the frames per second. You might use this for testing when your game has performance issues. We will leave it on and you should see the 60 frames per second that is defined on line 24 most of the time for this lesson project.<\/p>\n<p>As well you can add our own configuration variables to this file. A future lesson will provide an example.<\/p>\n<pre class=\"brush: jscript; highlight: [18]; title: ; notranslate\" title=\"\">\r\n--&#x5B;&#x5B;\r\n* Kobold2D\u2122 --- http:\/\/www.kobold2d.org\r\n*\r\n* Copyright (c) 2010-2011 Steffen Itterheim. \r\n* Released under MIT License in Germany (LICENSE-Kobold2D.txt).\r\n--]]\r\n\r\n\r\n--&#x5B;&#x5B;\r\n* Need help with the KKStartupConfig settings?\r\n* ------ http:\/\/www.kobold2d.com\/x\/ygMO ------\r\n--]]\r\n\r\n\r\nlocal config =\r\n{\r\n    KKStartupConfig = \r\n    {\r\n        -- load first scene from a class with this name, or from a Lua script with this name with .lua appended\r\n        FirstSceneClassName = &quot;GameLayer&quot;,\r\n\r\n        -- set the director type, and the fallback in case the first isn't available\r\n        DirectorType = DirectorType.DisplayLink,\r\n        DirectorTypeFallback = DirectorType.NSTimer,\r\n\r\n        MaxFrameRate = 60,\r\n        DisplayFPS = YES,\r\n\r\n        EnableUserInteraction = YES,\r\n        EnableMultiTouch = NO,\r\n\r\n        -- Render settings\r\n        DefaultTexturePixelFormat = TexturePixelFormat.RGBA8888,\r\n        GLViewColorFormat = GLViewColorFormat.RGB565,\r\n        GLViewDepthFormat = GLViewDepthFormat.DepthNone,\r\n        GLViewMultiSampling = NO,\r\n        GLViewNumberOfSamples = 0,\r\n\r\n        Enable2DProjection = NO,\r\n        EnableRetinaDisplaySupport = YES,\r\n        EnableGLViewNodeHitTesting = NO,\r\n        EnableStatusBar = NO,\r\n\r\n        -- Orientation &amp; Autorotation\r\n        DeviceOrientation = DeviceOrientation.Portrait,\r\n        AutorotationType = Autorotation.CCDirector,\r\n        ShouldAutorotateToLandscapeOrientations = NO,\r\n        ShouldAutorotateToPortraitOrientations = YES,\r\n        AllowAutorotateOnFirstAndSecondGenerationDevices = YES,\r\n\r\n        -- Ad setup\r\n        EnableAdBanner = NO,\r\n        PlaceBannerOnBottom = YES,\r\n        LoadOnlyPortraitBanners = NO,\r\n        LoadOnlyLandscapeBanners = NO,\r\n        AdProviders = &quot;iAd, AdMob&quot;,\t-- comma seperated list -&gt; &quot;iAd, AdMob&quot; means: use iAd if available, otherwise AdMob\r\n        AdMobRefreshRate = 15,\r\n        AdMobFirstAdDelay = 5,\r\n        AdMobPublisherID = &quot;YOUR_ADMOB_PUBLISHER_ID&quot;, -- how to get an AdMob Publisher ID: http:\/\/developer.admob.com\/wiki\/PublisherSetup\r\n        AdMobTestMode = YES,\r\n\r\n        -- Mac OS specific settings\r\n        AutoScale = NO,\r\n        AcceptsMouseMovedEvents = NO,\r\n        WindowFrame = RectMake(1024-640, 768-480, 640, 480),\r\n        EnableFullScreen = NO,\r\n    },\r\n}\r\n\r\nreturn config\r\n\r\n<\/pre>\n<p>Next each GameLayer.h and GameLayer.m file needs to be edited to replace HelloWorldLayer with GameLayer. I will point out those changes as their code is explored for this lesson.<\/p>\n<p><strong>Step 6: The GameLayer.h File<\/strong><\/p>\n<p>Our header file is rather simple. <\/p>\n<p>First be sure you change HelloWorldLayer to GameLayer on line 10.<\/p>\n<p>On line 12 we define a <a href=\"http:\/\/www.cocos2d-iphone.org\/api-ref\/latest-stable\/interface_c_c_sprite.html\" target = \"_blank\">CCSprite<\/a>. This defines a 2d image game sprite for our game.<\/p>\n<p>Line 13 defines the velocity for the player. This is a <a href=\"http:\/\/developer.apple.com\/library\/mac\/#DOCUMENTATION\/GraphicsImaging\/Reference\/CGGeometry\/Reference\/reference.html\" target = \"_blank\">CGPoint<\/a> object defined as &#8220;a structure that contains a point in a two-dimensional coordinate system&#8221;. In other words an x and y value. <\/p>\n<pre class=\"brush: objc; highlight: [10,12,13]; title: ; notranslate\" title=\"\">\r\n\/*\r\n * Kobold2D\u2122 --- http:\/\/www.kobold2d.org\r\n *\r\n * Copyright (c) 2010-2011 Steffen Itterheim. \r\n * Released under MIT License in Germany (LICENSE-Kobold2D.txt).\r\n *\/\r\n\r\n#import &quot;kobold2d.h&quot;\r\n\r\n@interface GameLayer : CCLayer\r\n{\r\n    CCSprite* player;\r\n    CGPoint playerVelocity;\r\n}\r\n@end\r\n<\/pre>\n<p>[ad name=&#8221;Google Adsense&#8221;]<br \/>\n<strong>Step 7: The GameLayer.m File<\/strong><\/p>\n<p>First you need to rename HelloWorldLayer to GameLayer on lines  8, 10 , 20 highlighted so you do not miss them.<\/p>\n<p>Constants defined here include the deceleration of the x velocity of our player at 40%. You can play with the responsiveness of of the accelerometer. The value of 6 seems to work well. Last is the fastest movement for the x velocity of our player.<\/p>\n<pre class=\"brush: objc; highlight: [8,10,20]; title: ; notranslate\" title=\"\">\r\n\/*\r\n * Kobold2D\u2122 --- http:\/\/www.kobold2d.org\r\n *\r\n * Copyright (c) 2010-2011 Steffen Itterheim. \r\n * Released under MIT License in Germany (LICENSE-Kobold2D.txt).\r\n *\/\r\n\r\n#import &quot;GameLayer.h&quot;\r\n\r\n@interface GameLayer (PrivateMethods)\r\n@end\r\n\r\n\/\/ Velocity deceleration\r\nconst float deceleration = 0.4f;\r\n\/\/ Accelerometer sensitivity (higher = more sensitive)\r\nconst float sensitivity = 6.0f;\r\n\/\/ Maximum velocity\r\nconst float maxVelocity = 100.0f;\r\n\r\n@implementation GameLayer\r\n<\/pre>\n<p>The init method allows us to set up basic elements in the game.<\/p>\n<p>Line 27 gets the <a href=\"http:\/\/www.kobold2d.com\/display\/KKDOC\/KKInput+Accelerometer\" target = \"_blank\">accelerometer<\/a> involved. Line 28 sets the filtering in a range of 0 to 1 to respond to movements of the device. Here you are looking for impact of spikes.<\/p>\n<p>Line 30 and 31 adds our player sprite. The z argument is for layering. The tag argument is for developers to use to identify sprites in other Kobol2D methods without having to reference or create a CCSprite object. We do not have a use of the tag property at this point.<\/p>\n<p>Lines 33-35 are for horizontally centering our player. The <a href=\"http:\/\/www.cocos2d-iphone.org\/api-ref\/latest-stable\/interface_c_c_director.html\" target = \"_blank\">CCDirector<\/a> class gives us access to the viewing size with the winSize property. The <a href=\"http:\/\/www.cocos2d-iphone.org\/api-ref\/latest-stable\/interface_c_c_director.html\" target = \"_blank\">CCDirector<\/a> is a Cocos2D class creates and handle the main Window and manages how and when to execute the Scenes.<\/p>\n<p>Line 36 gives the bluish background color. The glClearColor function is clearing the graphics layer.<\/p>\n<p>Lines 38 &#8211; 50 demonstrate how to add labels to the game. There are two lines of labels for this example.<\/p>\n<p>The positioning takes advantage of the  <a href=\"http:\/\/www.cocos2d-iphone.org\/api-ref\/latest-stable\/interface_c_c_director.html\" target = \"_blank\">CCDirector<\/a> screenCenter property for the first label. The second label uses screenCenter for the x property and offsets against the first label&#8217;s y position.<\/p>\n<p>The <a href=\"http:\/\/www.learn-cocos2d.com\/api-ref\/1.0\/Kobold2D\/html\/cc_more_types_8h.html\" target = \"_blank\">ccCyan<\/a> is the color for the text.<\/p>\n<p>The <a href=\"http:\/\/www.cocos2d-iphone.org\/wiki\/doku.php\/prog_guide:draw_update?s[]=scheduleupdate#scheduleupdate\" target = \"_blank\">scheduleUpdate<\/a> method on line  52 calls the update method as often as possible up to the MaxFrameRate set in the config.lua file.<\/p>\n<pre class=\"brush: objc; first-line: 22; title: ; notranslate\" title=\"\">\r\n-(id) init\r\n{\r\n\tif ((self = &#x5B;super init]))\r\n\t{\r\n        \/\/ Enable accelerometer input events.\r\n\t\t&#x5B;KKInput sharedInput].accelerometerActive = YES;\r\n\t\t&#x5B;KKInput sharedInput].acceleration.filteringFactor = 0.2f;\r\n        \/\/ Graphic for player\r\n        player = &#x5B;CCSprite spriteWithFile:@&quot;green_ball.png&quot;];\r\n\t\t&#x5B;self addChild:player z:0 tag:1];\r\n        \/\/ Position player        \r\n        CGSize screenSize = &#x5B;&#x5B;CCDirector sharedDirector] winSize];\r\n\t\tfloat imageHeight = &#x5B;player texture].contentSize.height;\r\n\t\tplayer.position = CGPointMake(screenSize.width \/ 2, imageHeight \/ 2);\r\n\t\tglClearColor(0.1f, 0.1f, 0.3f, 1.0f);\r\n\t\t\/\/ First line of title\r\n\t\tCCLabelTTF* label = &#x5B;CCLabelTTF labelWithString:@&quot;Kobold2d Intro Tutorial&quot; \r\n                                               fontName:@&quot;Arial&quot;  \r\n                                               fontSize:30];\r\n\t\tlabel.position = &#x5B;CCDirector sharedDirector].screenCenter;\r\n\t\tlabel.color = ccCYAN;\r\n        &#x5B;self addChild:label];\r\n        \/\/ Second line of title\r\n \t\tCCLabelTTF* label2 = &#x5B;CCLabelTTF labelWithString:@&quot;Lesson 1&quot;\r\n                                                fontName:@&quot;Arial&quot;\r\n                                                fontSize:24];\r\n\t\tlabel2.color = ccCYAN;\r\n        label2.position = CGPointMake(&#x5B;CCDirector sharedDirector].screenCenter.x ,label.position.y - label.boundingBox.size.height);\r\n        &#x5B;self addChild:label2];\r\n        \/\/ Start animation -  the update method is called.\r\n        &#x5B;self scheduleUpdate];;\r\n\t}\r\n\treturn self;\r\n}\r\n<\/pre>\n<p>These next lines handle the case of using automatic reference counting added with the XCode 4.2 compiler. Kobold2D has these constants to help with the alternative compiling choices.<\/p>\n<pre class=\"brush: objc; first-line: 56; title: ; notranslate\" title=\"\">\r\n-(void) dealloc\r\n{\r\n#ifndef KK_ARC_ENABLED\r\n\t&#x5B;super dealloc];\r\n#endif \/\/ KK_ARC_ENABLED\r\n}\r\n\r\n<\/pre>\n<p>The acceleratePlayerWithX method adjust the player x velocity keeping it within the maximum limits.<\/p>\n<p>Line 67 is degrading the velocity with the deceleration while increasing it based on the provided input coming from the accelerometer we will see in the update method.<\/p>\n<pre class=\"brush: objc; first-line: 63; title: ; notranslate\" title=\"\">\r\n#pragma mark Player Movement\r\n-(void) acceleratePlayerWithX:(double)xAcceleration\r\n{\r\n    \/\/ Adjust velocity based on current accelerometer acceleration\r\n    playerVelocity.x = (playerVelocity.x * deceleration) + (xAcceleration * sensitivity);\r\n    \/\/ Limit the maximum velocity of the player sprite, in both directions (positive &amp; negative values)\r\n    if (playerVelocity.x &gt; maxVelocity)\r\n    {\r\n        playerVelocity.x = maxVelocity;\r\n    }\r\n    else if (playerVelocity.x &lt; -maxVelocity)\r\n    {\r\n        playerVelocity.x = -maxVelocity;\r\n    }\r\n}\r\n\r\n<\/pre>\n<p>The update method on lines 82 and 83 uses <a href=\"http:\/\/www.kobold2d.com\/display\/KKDOC\/KKInput+Accelerometer\" target = \"_blank\">KKInput acceleration<\/a> smoothedX value to call the acceleratePlayerWithX method. The smoothing helps take out spikes in device data input.<\/p>\n<p>The remaining lines 84 to 108 are positioning the player but keeping it withing the left and right boundaries. <\/p>\n<pre class=\"brush: objc; first-line: 78; title: ; notranslate\" title=\"\">\r\n#pragma mark update\r\n-(void) update:(ccTime)delta\r\n{\r\n    \/\/ Gain access to the user input devices \/ states\r\n    KKInput* input = &#x5B;KKInput sharedInput];\r\n    &#x5B;self acceleratePlayerWithX:input.acceleration.smoothedX];\r\n    \/\/ Accumulate up the playerVelocity to the player's position\r\n    CGPoint pos = player.position;\r\n    pos.x += playerVelocity.x;\r\n    \/\/ The player constrainted to inside the screen\r\n    CGSize screenSize = &#x5B;&#x5B;CCDirector sharedDirector] winSize];\r\n    \/\/ Half the player image size player sprite position is the center of the image\r\n    float imageWidthHalved = &#x5B;player texture].contentSize.width * 0.5f;\r\n    float leftBorderLimit = imageWidthHalved;\r\n    float rightBorderLimit = screenSize.width - imageWidthHalved;\r\n    \/\/ Hit left boundary\r\n    if (pos.x &lt; leftBorderLimit)\r\n    {\r\n        pos.x = leftBorderLimit;\r\n        \/\/ Set velocity to zero\r\n        playerVelocity = CGPointZero;\r\n    }\r\n    \/\/ Hit right boundary\r\n    else if (pos.x &gt; rightBorderLimit)\r\n    {\r\n        pos.x = rightBorderLimit;\r\n        \/\/ Set velocity to zero\r\n        playerVelocity = CGPointZero;\r\n    }\r\n    \/\/ Move the player\r\n    player.position = pos; \r\n}  \r\n@end\r\n<\/pre>\n<p><a href=\"https:\/\/www.lonhosford.com\/lonblog\/2011\/12\/19\/kobold2d-xcode-4-introduction-tutorial-lesson-2-set-landscape-orientation\/\">Lesson 2 ==&gt; <\/a>  <\/p>\n<div id=\"fb-root\"><\/div>\n<p><script src=\"https:\/\/connect.facebook.net\/en_US\/all.js#appId=105467682877384&amp;xfbml=1\"><\/script><fb:like href=\"https:\/\/www.lonhosford.com\/lonblog\/2011\/12\/18\/kobold2d-xcode-4-introduction-tutorial-lesson-1-basic-accelerometer\/\" send=\"true\" width=\"450\" show_faces=\"true\" font=\"\"><\/fb:like><\/p>\n<div id=\"fb-root\"><\/div>\n<p><script src=\"https:\/\/connect.facebook.net\/en_US\/all.js#xfbml=1\"><\/script><fb:comments href=\"https:\/\/www.lonhosford.com\/lonblog\/2011\/12\/18\/kobold2d-xcode-4-introduction-tutorial-lesson-1-basic-accelerometer\/\" num_posts=\"3\" width=\"500\"><\/fb:comments><\/p>\n<p><!-- Place this render call where appropriate --><br \/>\n<script type=\"text\/javascript\">\n  (function() {\n    var po = document.createElement('script'); po.type = 'text\/javascript'; po.async = true;\n    po.src = 'https:\/\/apis.google.com\/js\/plusone.js';\n    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);\n  })();\n<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lesson 2 ==&gt; This first lesson goes through the steps to set up a Kobold2D project in XCode and use the accelerometer in the IPhone or IPad. I am going to focus on the IPad, but the example should work fine in an IPhone as well. The example is very simple and is not yet [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[78,79,31,77,68],"class_list":["post-2570","post","type-post","status-publish","format-standard","hentry","category-general","tag-accelerometer","tag-cocos2d","tag-gaming","tag-kobold2d","tag-xcode"],"_links":{"self":[{"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/posts\/2570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/comments?post=2570"}],"version-history":[{"count":90,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/posts\/2570\/revisions"}],"predecessor-version":[{"id":3666,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/posts\/2570\/revisions\/3666"}],"wp:attachment":[{"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/media?parent=2570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/categories?post=2570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lonhosford.com\/lonblog\/wp-json\/wp\/v2\/tags?post=2570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}