{"id":474,"date":"2011-07-12T16:09:00","date_gmt":"2011-07-12T16:09:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/2012\/10\/android-game-development-the-game-loop.html"},"modified":"2012-10-21T20:00:40","modified_gmt":"2012-10-21T20:00:40","slug":"android-game-development-game-loop","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-loop.html","title":{"rendered":"Android Game Development &#8211; The Game Loop"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left\">The game loop is the heartbeat of every game. We used a very rudimentary one so far (you can find it <a href=\"http:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-basic-game_05.html\">here<\/a>) without any control over how fast or slow we update our game state and which frames to render.<\/p>\n<p>To recapitulate, the most rudimentary game loop is a while loop that keeps executing some instructions until we signal it to finish, usually by setting a variable called <i>running<\/i> to <i>false<\/i><\/p>\n<pre class=\"brush:java\">boolean running = true;\r\nwhile (!running)\r\n{\r\n   updateGameState();\r\n   displayGameState();\r\n}\r\n<\/pre>\n<p>The above code runs blindly without a care for timing and resources. If you have a fast device then it will run very fast and if you have a slow one it will run slower.<\/p>\n<p>The <strong><i>updateGameState()<\/i><\/strong> updates the state of every object in the game and the <strong><i>displayGameState()<\/i><\/strong> renders the objects into an image which is displayed onto the screen.<\/p>\n<p>There are two things we should consider here: FPS and UPS.<\/p>\n<p><strong>FPS<\/strong> \u2013 <strong>Frames per Second<\/strong> \u2013 the number of times <strong><i>displayGameState()<\/i><\/strong> is being called per second.<br \/>\n<strong>UPS<\/strong> \u2013 <strong>Update per Second<\/strong> \u2013 the number of times <strong><i>updateGameState()<\/i><\/strong> is being called per second.<\/p>\n<p>Ideally the <i>update<\/i> and <i>render<\/i> methods will be called the same number of times per second (preferably not less than 20-25 times per second). 25 FPS is usually enough on a phone so us humans won\u2019t notice the animation being sluggish.<\/p>\n<p>For example if we target 25 FPS then it means we have to call the <strong><i>displayGameState()<\/i><\/strong> method every 40ms (1000 \/ 25 = 40ms, 1000ms = 1s). We need to bear in mind that <strong><i>updateGameState()<\/i><\/strong> is also called before the display method and for us to reach 25 FPS, we have to make sure that the update \u2013 <i>display<\/i> sequence executes in exactly 40ms. If it takes less than 40ms, then we have a higher FPS. If it takes more than that, then we have a slower running game.<\/p>\n<p>Let\u2019s see some examples to understand the FPS better.<\/p>\n<p>The following diagram shows exactly 1 FPS. It takes the <i>update<\/i> \u2013 <i>render<\/i> cycle exactly one second to execute. This means that you will see the image on the screen change once every second.<\/p>\n<table align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"tr-caption-container\" style=\"margin-left: auto;margin-right: auto;text-align: center\">\n<tbody>\n<tr>\n<td style=\"text-align: center\"><a href=\"http:\/\/obviam.net\/wp-content\/uploads\/2010\/08\/GameLoop2-e1282668890168.png\"><img decoding=\"async\" border=\"0\" height=\"203\" src=\"http:\/\/obviam.net\/wp-content\/uploads\/2010\/08\/GameLoop2-e1282668890168.png\" width=\"320\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td class=\"tr-caption\" style=\"text-align: center\"><span class=\"Apple-style-span\" style=\"color: #444444;font-family: verdana;line-height: 17px\">1 Frame per Second<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The following diagram shows 10FPS. An <i>update<\/i> \u2013 <i>render<\/i> cycle takes 100ms. This means every tenth of a second the image changes.<\/p>\n<table align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"tr-caption-container\" style=\"margin-left: auto;margin-right: auto;text-align: center\">\n<tbody>\n<tr>\n<td style=\"text-align: center\"><a href=\"http:\/\/obviam.net\/wp-content\/uploads\/2010\/08\/GameLoop21.png\"><img decoding=\"async\" border=\"0\" height=\"320\" src=\"http:\/\/obviam.net\/wp-content\/uploads\/2010\/08\/GameLoop21.png\" width=\"217\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td class=\"tr-caption\" style=\"text-align: center\">10 FPS<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>But the above scenario means that the <i>update<\/i>&#8211;<i>render<\/i> cycle executes in 1\/10 of a second EVERY time. That is an assumption and we can\u2019t control the actual times on cycle executes, or can we? What happens if we have 200 enemies and every enemy is shooting at us? We need to update the state of each enemy and the states of their bullets and check for collisions in one single update. It\u2019s different when we have just 2 enemies. The times will clearly differ. The same applies to the render method. Rendering 200 firing droids will clearly take more time than rendering only 2.<\/p>\n<p>So what are the scenarios? We could have an update-render cycle that finishes in less than 100ms (1\/10 of a second), finishes in exactly 100ms or finishes in more than that. On a powerful hardware it will be faster than on a weaker one. Let\u2019s see the diagrams.<\/p>\n<p>The cycle finishes before the desired timeframe so we have a small amount of free time before running the next cycle.<\/p>\n<table align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"tr-caption-container\" style=\"margin-left: auto;margin-right: auto;text-align: center\">\n<tbody>\n<tr>\n<td style=\"text-align: center\"><a href=\"http:\/\/obviam.net\/wp-content\/uploads\/2010\/08\/GameLoop32-e1282740010976.png\"><img decoding=\"async\" border=\"0\" height=\"266\" src=\"http:\/\/obviam.net\/wp-content\/uploads\/2010\/08\/GameLoop32-e1282740010976.png\" width=\"320\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td class=\"tr-caption\" style=\"text-align: center\"><span class=\"Apple-style-span\" style=\"color: #444444;font-family: verdana;line-height: 17px\">Frame with time to spare<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The following diagram shows a cycle which falls behind. That means that the time it takes for a update-render cycle to finish is greater than the desired one. If it takes 12ms that means we are 2ms behind (still considering the 10FPS). This can mount up and every cycle we loose time and the game will run slowly.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<table align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"tr-caption-container\" style=\"margin-left: auto;margin-right: auto;text-align: center\">\n<tbody>\n<tr>\n<td style=\"text-align: center\"><a href=\"http:\/\/obviam.net\/wp-content\/uploads\/2010\/08\/GameLoop31-e1282739949997.png\"><img decoding=\"async\" border=\"0\" height=\"320\" src=\"http:\/\/obviam.net\/wp-content\/uploads\/2010\/08\/GameLoop31-e1282739949997.png\" width=\"265\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td class=\"tr-caption\" style=\"text-align: center\"><span class=\"Apple-style-span\" style=\"color: #444444;font-family: verdana;line-height: 17px\">Overdue Frame<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The first situation is the desired one. This gives us some free time to do something before we kick off the next cycle. We don\u2019t need to do anything so we just tell the game loop to go to sleep for the remaining time period and wake up when the next cycle is due. If we won\u2019t do this the game will run quicker than intended. By introducing the sleep time we achieved <strong>constant frame rate<\/strong>.<\/p>\n<p>The second situation (I skipped the ideal one as it almost never happens) when the loop is behind, requires a different approach.<\/p>\n<p>To achieve constant speed in a game we need to update the state of our objects when required. Imagine a droid approaching you at a constant speed. You know if it travelled half the screen in one second, so it will take another second to reach the other side of the screen. To calculate the position accurately we need to know either the time delta since the last postion, and the current speed of the droid, or we update the position (status) of the droid at constant intervals. I will choose the second one as playing with deltas in the game update can be tricky. To achieve a <strong>constant game speed<\/strong> we will have to <strong>skip<\/strong> displaying frames. Game speed is NOT FPS!<\/p>\n<p>Examine the following diagram. It is a scenario in which the <i>update<\/i>&#8211;<i>render<\/i> cycle takes longer than the desired time so we have to catch up. To do that we will skip the rendering of this frame and will do another update so the game speed won\u2019t be affected. We\u2019ll do a normal cycle in the next frame with even some time to give to the CPU to rest.<\/p>\n<table align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"tr-caption-container\" style=\"margin-left: auto;margin-right: auto;text-align: center\">\n<tbody>\n<tr>\n<td style=\"text-align: center\"><a href=\"http:\/\/obviam.net\/wp-content\/uploads\/2010\/08\/GameLoop33-e1282740145321.png\"><img decoding=\"async\" border=\"0\" height=\"236\" src=\"http:\/\/obviam.net\/wp-content\/uploads\/2010\/08\/GameLoop33-e1282740145321.png\" width=\"320\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td class=\"tr-caption\" style=\"text-align: center\"><span class=\"Apple-style-span\" style=\"color: #444444;font-family: verdana;line-height: 17px\">Constant Game Speed with Variable FPS<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The above scenario has many variations. You can imagine the game update taking more than one full frame. In this case we can do nothing to keep the game speed constant and the game will run slower. We might have to skip multiple renderings to keep the speed constant but we have to make sure that we set the maximum number of frames allowed to be skipped because it can take quite a few updates to catch up and in case we skipped 15 frames that means we lost a lot from the game and it will just be unplayable.<\/p>\n<p>The <strong>MainThread.java<\/strong>\u2018s <strong><i>run()<\/i><\/strong> looks like this:<\/p>\n<pre class=\"brush:java\">\/\/ desired fps\r\nprivate final static int \tMAX_FPS = 50;\r\n\/\/ maximum number of frames to be skipped\r\nprivate final static int\tMAX_FRAME_SKIPS = 5;\r\n\/\/ the frame period\r\nprivate final static int\tFRAME_PERIOD = 1000 \/ MAX_FPS;\t\r\n\r\n@Override\r\npublic void run() {\r\n\tCanvas canvas;\r\n\tLog.d(TAG, \"Starting game loop\");\r\n\r\n\tlong beginTime;\t\t\/\/ the time when the cycle begun\r\n\tlong timeDiff;\t\t\/\/ the time it took for the cycle to execute\r\n\tint sleepTime;\t\t\/\/ ms to sleep (&lt;0 if we're behind)\r\n\tint framesSkipped;\t\/\/ number of frames being skipped \r\n\r\n\tsleepTime = 0;\r\n\r\n\twhile (running) {\r\n\t\tcanvas = null;\r\n\t\t\/\/ try locking the canvas for exclusive pixel editing\r\n\t\t\/\/ in the surface\r\n\t\ttry {\r\n\t\t\tcanvas = this.surfaceHolder.lockCanvas();\r\n\t\t\tsynchronized (surfaceHolder) {\r\n\t\t\t\tbeginTime = System.currentTimeMillis();\r\n\t\t\t\tframesSkipped = 0;\t\/\/ resetting the frames skipped\r\n\t\t\t\t\/\/ update game state\r\n\t\t\t\tthis.gamePanel.update();\r\n\t\t\t\t\/\/ render state to the screen\r\n\t\t\t\t\/\/ draws the canvas on the panel\r\n\t\t\t\tthis.gamePanel.render(canvas);\r\n\t\t\t\t\/\/ calculate how long did the cycle take\r\n\t\t\t\ttimeDiff = System.currentTimeMillis() - beginTime;\r\n\t\t\t\t\/\/ calculate sleep time\r\n\t\t\t\tsleepTime = (int)(FRAME_PERIOD - timeDiff);\r\n\r\n\t\t\t\tif (sleepTime &gt; 0) {\r\n\t\t\t\t\t\/\/ if sleepTime &gt; 0 we're OK\r\n\t\t\t\t\ttry {\r\n\t\t\t\t\t\t\/\/ send the thread to sleep for a short period\r\n\t\t\t\t\t\t\/\/ very useful for battery saving\r\n\t\t\t\t\t\tThread.sleep(sleepTime);\r\n\t\t\t\t\t} catch (InterruptedException e) {}\r\n\t\t\t\t}\r\n\r\n\t\t\t\twhile (sleepTime &lt; 0 &amp;&amp; framesSkipped &lt; MAX_FRAME_SKIPS) {\r\n\t\t\t\t\t\/\/ we need to catch up\r\n\t\t\t\t\t\/\/ update without rendering\r\n\t\t\t\t\tthis.gamePanel.update();\r\n\t\t\t\t\t\/\/ add frame period to check if in next frame\r\n\t\t\t\t\tsleepTime += FRAME_PERIOD;\r\n\t\t\t\t\tframesSkipped++;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t} finally {\r\n\t\t\t\/\/ in case of an exception the surface is not left in\r\n\t\t\t\/\/ an inconsistent state\r\n\t\t\tif (canvas != null) {\r\n\t\t\t\tsurfaceHolder.unlockCanvasAndPost(canvas);\r\n\t\t\t}\r\n\t\t}\t\/\/ end finally\r\n\t}\r\n}\r\n<\/pre>\n<p>Examine the above code very carefully as it implements the logic behind the diagram.&nbsp;You can find the full code in the downloadable project.<\/p>\n<p>There is another approach which I like. It is constant game speed with maximum number of frames per second. It uses interpolation to draw the state and it occurs on fast hardwares when there is time left for another rendering before the next game update. This can enhance the visuals of a game as it enables smoother animation but because we use mobile devices, giving the CPU some rest will save the battery quite a lot.<\/p>\n<p>There is an awesome article on game loops <a href=\"http:\/\/www.koonsolo.com\/news\/dewitters-gameloop\/\">here<\/a>. I personally understood the game loops reading this article so I highly recommend it. The best I could find.<\/p>\n<p>Note that I also modified the default values in the <strong>Speed.java<\/strong> class. The speed is measured in units\/second.&nbsp;Because we are setting our desired FPS to 50 that means that the speed will increase by 50*speed.value every update. To have the speed of let\u2019s say 40 pixels\/second you will need to set the speed delta for every tick to 2 (40 \/ (1000 \/ 50) = 2). In other words you need the droid to advance 2 pixels every game update (when you have 50 game updates per second) to cover 40 pixels per second.<\/p>\n<p>Download the code <a href=\"http:\/\/www.obviam.net\/source_code\/droidz.android.constant-game-speed.tar.gz\">here<\/a> and play with it.<\/p>\n<p>Examine it and you have a constant game speed with variable frame rate.<\/p>\n<p><strong>Reference:<\/strong>&nbsp;<a href=\"http:\/\/obviam.net\/index.php\/the-android-game-loop\/\">The Game Loop<\/a>&nbsp;from our&nbsp;<a href=\"http:\/\/www.javacodegeeks.com\/p\/jcg.html\">JCG<\/a>&nbsp;partner Tamas Jano&nbsp;from &#8220;<a href=\"http:\/\/obviam.net\/\">Against The Grain<\/a>&#8221; blog.<\/p>\n<div style=\"margin: 0px\">Do not forget to check out our new <strong><i>Android Game<\/i><\/strong> <strong><i><a href=\"http:\/\/www.javacodegeeks.com\/2011\/06\/jcg-studios-arkdroid-official-launch.html\">ArkDroid<\/a>&nbsp;<span class=\"Apple-style-span\" style=\"font-weight: normal\"><span class=\"Apple-style-span\" style=\"font-style: normal\">(screenshots below)<\/span><\/span><\/i><\/strong>. You feedback will be more than helpful!<\/div>\n<div class=\"separator\" style=\"clear: both;text-align: center\"><a href=\"http:\/\/1.bp.blogspot.com\/-0dr410FpxAA\/TgdJc44bfvI\/AAAAAAAAADg\/GyMLvayT_rc\/s1600\/arkdroid_ingame.jpg\"><img decoding=\"async\" border=\"0\" height=\"200\" src=\"http:\/\/1.bp.blogspot.com\/-0dr410FpxAA\/TgdJc44bfvI\/AAAAAAAAADg\/GyMLvayT_rc\/s200\/arkdroid_ingame.jpg\" width=\"133\" \/><\/a><a href=\"http:\/\/1.bp.blogspot.com\/-sEqMxR7YZnY\/ThBGdHcIYnI\/AAAAAAAAABs\/S6iKs2lycyk\/s1600\/multiple_sticky.jpg\"><img decoding=\"async\" border=\"0\" height=\"200\" src=\"http:\/\/1.bp.blogspot.com\/-sEqMxR7YZnY\/ThBGdHcIYnI\/AAAAAAAAABs\/S6iKs2lycyk\/s200\/multiple_sticky.jpg\" style=\"cursor: move\" width=\"133\" \/><\/a><a href=\"http:\/\/3.bp.blogspot.com\/-n99pqdxWzqM\/ThBFy-5CGmI\/AAAAAAAAABg\/LfSrtmW-RaQ\/s1600\/multiple_laser.jpg\"><img decoding=\"async\" border=\"0\" height=\"200\" src=\"http:\/\/3.bp.blogspot.com\/-n99pqdxWzqM\/ThBFy-5CGmI\/AAAAAAAAABg\/LfSrtmW-RaQ\/s200\/multiple_laser.jpg\" width=\"133\" \/><\/a><\/div>\n<div style=\"margin: 0px\"><strong>Related Articles:<\/strong><\/div>\n<ul style=\"text-align: left\">\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/06\/android-game-development-tutorials.html\">Android Game Development Tutorials Introduction<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-idea.html\">Android Game Development &#8211; The Game Idea<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-create-project.html\">Android Game Development &#8211; Create The Project<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-basic-game.html\">Android Game Development &#8211; A Basic Game Architecture<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-basic-game_05.html\">Android Game Development &#8211; A Basic Game Loop<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-displaying.html\">Android Game Development &#8211; Displaying Images with Android<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-moving-images.html\">Android Game Development &#8211; Moving Images on Screen<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-measuring-fps.html\">Android Game Development &#8211; Measuring FPS<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-sprite.html\">Android Game Development &#8211; Sprite Animation<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/08\/android-game-development-particle.html\">Android Game Development &#8211; Particle Explosion<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/08\/android-game-development-design-in-game.html\">Android Game Development &#8211; Design In-game Entities &#8211; The Strategy Pattern<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/09\/android-game-development-using-bitmap.html\">Android Game Development &#8211; Using Bitmap Fonts<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/09\/android-game-development-switching-from.html\">Android Game Development &#8211; Switching from Canvas to OpenGL ES<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/android-game-development-displaying.html\">Android Game Development \u2013 Displaying Graphical Elements (Primitives) with OpenGL ES<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/android-game-development-opengl-texture.html\">Android Game Development \u2013 OpenGL Texture Mapping<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/android-game-development-design-in-game.html\">Android Game Development \u2013 Design In-game Entities \u2013 The State Pattern<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/?tag=android-games\">Android Games Article Series<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The game loop is the heartbeat of every game. We used a very rudimentary one so far (you can find it here) without any control over how fast or slow we update our game state and which frames to render. To recapitulate, the most rudimentary game loop is a while loop that keeps executing some &hellip;<\/p>\n","protected":false},"author":27,"featured_media":46,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-474","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-games"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Android Game Development - The Game Loop - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"The game loop is the heartbeat of every game. We used a very rudimentary one so far (you can find it here) without any control over how fast or slow we\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-loop.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android Game Development - The Game Loop - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"The game loop is the heartbeat of every game. We used a very rudimentary one so far (you can find it here) without any control over how fast or slow we\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-loop.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2011-07-12T16:09:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-10-21T20:00:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/android-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Impaler\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Impaler\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/07\\\/android-game-development-game-loop.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/07\\\/android-game-development-game-loop.html\"},\"author\":{\"name\":\"Impaler\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/6bad1a2db4fb0129703629617c049f8c\"},\"headline\":\"Android Game Development &#8211; The Game Loop\",\"datePublished\":\"2011-07-12T16:09:00+00:00\",\"dateModified\":\"2012-10-21T20:00:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/07\\\/android-game-development-game-loop.html\"},\"wordCount\":1415,\"commentCount\":11,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/07\\\/android-game-development-game-loop.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/android-logo.jpg\",\"articleSection\":[\"Android Games\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/07\\\/android-game-development-game-loop.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/07\\\/android-game-development-game-loop.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/07\\\/android-game-development-game-loop.html\",\"name\":\"Android Game Development - The Game Loop - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/07\\\/android-game-development-game-loop.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/07\\\/android-game-development-game-loop.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/android-logo.jpg\",\"datePublished\":\"2011-07-12T16:09:00+00:00\",\"dateModified\":\"2012-10-21T20:00:40+00:00\",\"description\":\"The game loop is the heartbeat of every game. We used a very rudimentary one so far (you can find it here) without any control over how fast or slow we\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/07\\\/android-game-development-game-loop.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/07\\\/android-game-development-game-loop.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/07\\\/android-game-development-game-loop.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/android-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/android-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/07\\\/android-game-development-game-loop.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/android\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Android Games\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/android\\\/android-games\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Android Game Development &#8211; The Game Loop\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/6bad1a2db4fb0129703629617c049f8c\",\"name\":\"Impaler\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/def7cf34ac1eee0f6c2de98be951379d6bf14fd498acf7d3864e2e570ece357c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/def7cf34ac1eee0f6c2de98be951379d6bf14fd498acf7d3864e2e570ece357c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/def7cf34ac1eee0f6c2de98be951379d6bf14fd498acf7d3864e2e570ece357c?s=96&d=mm&r=g\",\"caption\":\"Impaler\"},\"sameAs\":[\"http:\\\/\\\/obviam.net\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/Impaler\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android Game Development - The Game Loop - Java Code Geeks","description":"The game loop is the heartbeat of every game. We used a very rudimentary one so far (you can find it here) without any control over how fast or slow we","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-loop.html","og_locale":"en_US","og_type":"article","og_title":"Android Game Development - The Game Loop - Java Code Geeks","og_description":"The game loop is the heartbeat of every game. We used a very rudimentary one so far (you can find it here) without any control over how fast or slow we","og_url":"https:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-loop.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2011-07-12T16:09:00+00:00","article_modified_time":"2012-10-21T20:00:40+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/android-logo.jpg","type":"image\/jpeg"}],"author":"Impaler","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Impaler","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-loop.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-loop.html"},"author":{"name":"Impaler","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/6bad1a2db4fb0129703629617c049f8c"},"headline":"Android Game Development &#8211; The Game Loop","datePublished":"2011-07-12T16:09:00+00:00","dateModified":"2012-10-21T20:00:40+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-loop.html"},"wordCount":1415,"commentCount":11,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-loop.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/android-logo.jpg","articleSection":["Android Games"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-loop.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-loop.html","url":"https:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-loop.html","name":"Android Game Development - The Game Loop - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-loop.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-loop.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/android-logo.jpg","datePublished":"2011-07-12T16:09:00+00:00","dateModified":"2012-10-21T20:00:40+00:00","description":"The game loop is the heartbeat of every game. We used a very rudimentary one so far (you can find it here) without any control over how fast or slow we","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-loop.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-loop.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-loop.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/android-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/android-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2011\/07\/android-game-development-game-loop.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Android","item":"https:\/\/www.javacodegeeks.com\/category\/android"},{"@type":"ListItem","position":3,"name":"Android Games","item":"https:\/\/www.javacodegeeks.com\/category\/android\/android-games"},{"@type":"ListItem","position":4,"name":"Android Game Development &#8211; The Game Loop"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/6bad1a2db4fb0129703629617c049f8c","name":"Impaler","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/def7cf34ac1eee0f6c2de98be951379d6bf14fd498acf7d3864e2e570ece357c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/def7cf34ac1eee0f6c2de98be951379d6bf14fd498acf7d3864e2e570ece357c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/def7cf34ac1eee0f6c2de98be951379d6bf14fd498acf7d3864e2e570ece357c?s=96&d=mm&r=g","caption":"Impaler"},"sameAs":["http:\/\/obviam.net\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/Impaler"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/474","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/27"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=474"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/474\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/46"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}