{"id":3140,"date":"2014-05-28T01:47:54","date_gmt":"2014-05-28T08:47:54","guid":{"rendered":"http:\/\/www.nathalielawhead.com\/candybox\/?p=3140"},"modified":"2014-05-28T01:47:54","modified_gmt":"2014-05-28T08:47:54","slug":"html5-game-tutorial-making-a-platformer-part-1","status":"publish","type":"post","link":"https:\/\/www.nathalielawhead.com\/candybox\/html5-game-tutorial-making-a-platformer-part-1","title":{"rendered":"HTML5 Game Tutorial: Making A Platformer (PART 1)"},"content":{"rendered":"<p><a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Tutorial_HTML5_Platformer_Part1\/FaceTheMusic_Tutorial1.zip\" target=\"_blank\">DOWNLOAD SOURCE HERE<\/a><\/p>\n<p>I&#8217;ve been developing a totally new game for <a href=\"http:\/\/tetrageddon.com\" target=\"_blank\">Tetrageddon<\/a>. It&#8217;s going to be available for mobile (iOS, and Android), as well as the desktop. <\/p>\n<p>This is an HTML5 game (for a change), with CreateJS, using FlashCC&#8217;s HTML5 Canvas document. Doing so will make things easier for me animation wise, as well as managing complex animated &#8220;cut scenes&#8221;, and other graphical aspects&#8230;<\/p>\n<p>The game is codenamed &#8220;Face The Music&#8221; because it will essentially be about death and &#8220;giving up&#8221; &#8212; in a playful yet poetic pixel art style. :)<br \/>\nBest of all, &#8220;Face The Music&#8221; is a platformer game!<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/sourcefiles\/Tutorial_HTML5_Platformer_Part1\/Tutorial\/MrFrombie_final.png\" alt=\"\" \/><\/p>\n<p>I will be documenting the development process, and releasing it as a series of tutorials that will walk you through creating this game. I will be doing this for all of Tetrageddon&#8217;s projects from now on, because I want to place even more emphasis on the &#8220;learning resource&#8221; aspect of <a href=\"http:\/\/tetrageddon.com\" target=\"_blank\">Tetrageddon Games<\/a>. This will provide excellent content for that.<\/p>\n<p>Let&#8217;s begin!<\/p>\n<p>*************<\/p>\n<p><strong>The Basic Platformer<\/strong><\/p>\n<p><a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Tutorial_HTML5_Platformer_Part1\/FaceTheMusic_Tutorial1.zip\" target=\"_blank\">DOWNLOAD SOURCE HERE<\/a><\/p>\n<p>The finished product will behave like this. Sorry, it&#8217;s in the &#8220;programmer art&#8221; stage. :)<\/p>\n<p>[iframe src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Tutorial_HTML5_Platformer_Part1\/FaceTheMusic_Tutorial1.html&#8221; width=&#8221;800&#8243; height=&#8221;480&#8243;]<br \/>\n<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Tutorial_HTML5_Platformer_Part1\/FaceTheMusic_Tutorial1.html\" target=\"_blank\">http:\/\/nathalielawhead.com\/sourcefiles\/Tutorial_HTML5_Platformer_Part1\/FaceTheMusic_Tutorial1.html<\/a><\/p>\n<p>LEFT\/RIGHT = Move<br \/>\nDOWN = Duck<br \/>\nUP = Jump (hold up to jump higher)<br \/>\nFalling off = Death<br \/>\nGetting to right of screen = Win (it&#8217;s a short game at the moment)<\/p>\n<p>*************<\/p>\n<p><strong>The Setup<\/strong><\/p>\n<p>For starters head down to <a href=\"https:\/\/github.com\/madrobby\/keymaster\" target=\"_blank\">https:\/\/github.com\/madrobby\/keymaster<\/a> and grab a copy of <code>keymaster.js<\/code>. I&#8217;m using this because I figure I&#8217;ll want more complex keyboard functionality (maybe I&#8217;ll change this later, but for now I&#8217;m using this).<\/p>\n<p>The code to import this will be as follows (first thing, first frame):<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">\/\/import .js<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> js_keys <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">document<\/span>.createElement(<span style=\"background-color: #fff0f0\">&quot;script&quot;<\/span>);\r\njs_keys.src <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;libs\/keymaster.js&quot;<\/span>;\r\njs_keys.type <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;text\/javascript&quot;<\/span>;\r\n<span style=\"color: #007020\">document<\/span>.getElementsByTagName(<span style=\"background-color: #fff0f0\">&quot;head&quot;<\/span>)[<span style=\"color: #0000DD; font-weight: bold\">0<\/span>].appendChild(js_keys);\r\n<\/pre>\n<\/div>\n<p>There are some miscellaneous functions that will handle returning various calculations.<br \/>\n<code>num_randrange<\/code>, which returns a random number between a minimum and maximum specified value&#8230;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">\/\/Math<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> num_randrange(num_max, num_min)\r\n{\r\n     <span style=\"color: #008800; font-weight: bold\">return<\/span> <span style=\"color: #007020\">Math<\/span>.ceil(<span style=\"color: #007020\">Math<\/span>.random()<span style=\"color: #333333\">*<\/span>(num_max<span style=\"color: #333333\">-<\/span>num_min)<span style=\"color: #333333\">+<\/span>num_min);\r\n}\r\n<\/pre>\n<\/div>\n<p>and <code>gotoAndStopRandom<\/code>, which sets a sprite to a random frame (I&#8217;ll be using this for the player sprite in later versions of the game).<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">\/\/Timelines<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> gotoAndStopRandom(clip){\r\n\tclip.gotoAndStop(<span style=\"color: #007020\">Math<\/span>.ceil(<span style=\"color: #007020\">Math<\/span>.random()<span style=\"color: #333333\">*<\/span>clip.timeline.duration));\r\n};\r\n<\/pre>\n<\/div>\n<p>Moving on to the actual meat of the game, we&#8217;ll set up the &#8220;world properties&#8221; (like gravity, friction, and stage width\/height, and other necessary Arrays, variables&#8230;)<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">this<\/span>.stop();\r\n\r\n<span style=\"color: #888888\">\/\/Stage<\/span>\r\n_stage <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">this<\/span>;\r\nnum_stageWidth <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">800<\/span>;\r\nnum_stageHeight <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">480<\/span>;\r\n\r\n<span style=\"color: #888888\">\/\/&quot;World&quot; properties<\/span>\r\nfriction <span style=\"color: #333333\">=<\/span> <span style=\"color: #6600EE; font-weight: bold\">0.6<\/span>;\r\ngravity <span style=\"color: #333333\">=<\/span> <span style=\"color: #6600EE; font-weight: bold\">0.8<\/span>;\r\n\r\n<span style=\"color: #888888\">\/\/Booleans<\/span>\r\nbool_keyDown <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;<span style=\"color: #888888\">\/\/key is held down<\/span>\r\nbool_jumpDown <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;<span style=\"color: #888888\">\/\/jump key is down<\/span>\r\n\r\nbool_lastpanel <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">true<\/span>;<span style=\"color: #888888\">\/\/false;\/\/is this the last panel, if so the game will win after you leave the screen<\/span>\r\n\r\n<span style=\"color: #888888\">\/\/Sprites<\/span>\r\n<span style=\"color: #888888\">\/\/Every sprite set is stored in that set&#39;s own array - <\/span>\r\n<span style=\"color: #888888\">\/\/for collision and sprite specific properties\/functionality<\/span>\r\n<span style=\"color: #888888\">\/\/the player<\/span>\r\nmc_frombie <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">null<\/span>;\r\narr_players <span style=\"color: #333333\">=<\/span> [];<span style=\"color: #888888\">\/\/array of any hero sprites [0] = mc_frombie<\/span>\r\n<span style=\"color: #888888\">\/\/ground<\/span>\r\narr_colidable <span style=\"color: #333333\">=<\/span> [];<span style=\"color: #888888\">\/\/array of any colidable platform<\/span>\r\n<\/pre>\n<\/div>\n<p>Most of this is fairly self explanatory.<\/p>\n<p><code>bool_lastpanel<\/code> is the current screen you&#8217;re on. I&#8217;m dividing the game up like it&#8217;s comic book panels that you are playing through. This being set to true indicates that once you leave the right of the screen, you &#8220;win&#8221;.<\/p>\n<p>The last two arrays: <code>arr_players<\/code> and <code>arr_colidable<\/code>, are sprites that will check against each-others collision&#8230; I mean, I put everything in their respective arrays to handle the &#8220;object type&#8221;, if it&#8217;s in <code>arr_colidable<\/code> then it&#8217;s a platform and the player (<code>arr_players<\/code>) will stand on it.<\/p>\n<p>*************<\/p>\n<p><strong>Managing Sprites<\/strong><\/p>\n<p>I&#8217;m wrapping up managing sprites into a set of functions. The first one (<code>addSprite<\/code>) is going to be responsible for adding sprites. It takes five properties. They are as follows.<br \/>\n<code>libItem<\/code> = name of library item. As you can see, I have any sprites that need to be manipulated through code added on the &#8220;first frame&#8221;. This way it registers &#8212; workaround for not having library linkages.<br \/>\n<code>name<\/code> = new name of the added sprite.<br \/>\n<code>arr<\/code> = the array it should be added to. Such as <code>arr_players<\/code> (for a player), <code>arr_colidable<\/code> (if it&#8217;s a collideable platform).<br \/>\n<code>x<\/code> = x position.<br \/>\n<code>y<\/code> = y position.<br \/>\n<code>addSprite<\/code> creates a clip via <code>var clip = new libItem();<\/code><br \/>\nAdds it to <code>_stage<\/code> (our &#8220;root timeline&#8221; as saved in the variable from the above setup), names it, and adds it to the specified array, then places it at the desired x\/y coordinates.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">\/\/create the sprite<\/span>\r\n<span style=\"color: #888888\">\/\/usage: addSprite(lib.Player, &quot;mc_frombie&quot;, arr_players, 150, 150);<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> addSprite(libItem, name, arr, x, y){\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> clip <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> libItem();<span style=\"color: #888888\">\/\/lib.LINKAGE<\/span>\r\n\t_stage.addChild(clip);\r\n\tclip.name <span style=\"color: #333333\">=<\/span> name;\r\n\tarr.push(clip);\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\tclip.x <span style=\"color: #333333\">=<\/span> x;\r\n\tclip.y <span style=\"color: #333333\">=<\/span> y;\r\n};\r\n<\/pre>\n<\/div>\n<p>Then there&#8217;s <code>removeAllSprites()<\/code>. This one gets called for game win, or game over (dying). To clear off everything on the stage. It simply loops through all the arrays, and removes their contents from <code>_stage<\/code>.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">\/\/remove anything created by game<\/span>\r\n<span style=\"color: #888888\">\/\/could use removeAllChildren, but I don&#39;t know what I might want to keep...<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> removeAllSprites(){\r\n\t<span style=\"color: #888888\">\/\/get rid of the player<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">for<\/span>(<span style=\"color: #008800; font-weight: bold\">var<\/span> plr <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>; plr<span style=\"color: #333333\">&lt;<\/span>arr_players.length; <span style=\"color: #333333\">++<\/span>plr){\r\n\t\t_stage.removeChild(arr_players[plr]);\r\n\t};\r\n\t<span style=\"color: #888888\">\/\/get rid of anything colidable<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">for<\/span>(<span style=\"color: #008800; font-weight: bold\">var<\/span> col <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>; col<span style=\"color: #333333\">&lt;<\/span>arr_colidable.length; <span style=\"color: #333333\">++<\/span>col){\r\n\t\t_stage.removeChild(arr_colidable[col]);\r\n\t};\r\n};\r\n<\/pre>\n<\/div>\n<p>And finally we have a set of functions specific to managing the player&#8217;s states; <code>player_die<\/code>, <code>player_win<\/code>, and <code>player_duck<\/code>.<br \/>\n<code>player_die<\/code> and <code>player_win<\/code> are basically identical, but they set the <code>_stage<\/code> to a different &#8220;frame&#8221;.<br \/>\n<code>player_die<\/code> first calls <code>evnt_removeAll()<\/code>, which removes all event listeners (will explain later). Then <code>removeAllSprites<\/code>, then sends <code>_stage<\/code> to &#8220;gameover&#8221;. <code>player_win<\/code> does the same thing.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">\/\/Player<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> player_die(){\r\n\t<span style=\"color: #888888\">\/\/remove all listeners<\/span>\r\n\tevnt_removeAll();\r\n\t<span style=\"color: #888888\">\/\/remove all created children<\/span>\r\n\tremoveAllSprites();\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\tconsole.log(<span style=\"background-color: #fff0f0\">&quot;gameover&quot;<\/span>);\r\n\t<span style=\"color: #888888\">\/\/go to gameover<\/span>\r\n\t_stage.gotoAndStop(<span style=\"background-color: #fff0f0\">&quot;gameover&quot;<\/span>);\r\n};\r\n\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> player_win(){\r\n\t<span style=\"color: #888888\">\/\/remove all listeners<\/span>\r\n\tevnt_removeAll();\r\n\t<span style=\"color: #888888\">\/\/remove all created children<\/span>\r\n\tremoveAllSprites();\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\tconsole.log(<span style=\"background-color: #fff0f0\">&quot;gamewin&quot;<\/span>);\r\n\t<span style=\"color: #888888\">\/\/go to gameover<\/span>\r\n\t_stage.gotoAndStop(<span style=\"background-color: #fff0f0\">&quot;win&quot;<\/span>);\r\n};\r\n<\/pre>\n<\/div>\n<p><code>player_duck<\/code> handles the player&#8217;s &#8220;crouched&#8221; state. The player ducks when you press the <code>DOWN<\/code> key, or he will be ducked down (in an &#8220;ouch my head!&#8221; animated state) if a collision takes place where the player hits his head on the bottom of a platform&#8230; like, jumps up against something&#8230; as a cute detail.<br \/>\nTo duck, the players ducking state is set to <code>true<\/code>, and then the player&#8217;s height reduced (I will explain player properties later). If a certain type <code>\"duck\"<\/code>, or <code>\"ouch\"<\/code>, then set the player to that state&#8230;<br \/>\nThe finished function is as follows:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">\/\/ducking acts as an &quot;ouch my head&quot;, and &quot;be careful my head&quot; animation<\/span>\r\n<span style=\"color: #888888\">\/\/like he&#39;s sensitive about his head... aside from reducing his height so he can crawl through small areas<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> player_duck(type){\r\n\tmc_frombie.ducking <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">true<\/span>;\r\n\tmc_frombie.height <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">10<\/span>;\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(type<span style=\"color: #333333\">==<\/span><span style=\"background-color: #fff0f0\">&quot;duck&quot;<\/span>){\r\n\t\tmc_frombie.gotoAndStop(<span style=\"background-color: #fff0f0\">&quot;duck&quot;<\/span>);\r\n\t}\r\n\t<span style=\"color: #888888\">\/\/hit his head on something, now sees stars or some type of injury<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(type<span style=\"color: #333333\">==<\/span><span style=\"background-color: #fff0f0\">&quot;ouch&quot;<\/span>){\r\n\t\tmc_frombie.gotoAndStop(<span style=\"background-color: #fff0f0\">&quot;ouch&quot;<\/span>);\r\n\t};\r\n};\r\n<\/pre>\n<\/div>\n<p>Finally, we&#8217;ll add the sprites to the stage.<br \/>\nThis is a prototype of a game &#8220;level&#8221;&#8230; a very, very, small level. So we&#8217;ll create a few platforms (of different types), and the player.<br \/>\nStarting with the player.<br \/>\nThe player is first added, and the currently controllable player saved to a variable (<code>mc_frombie<\/code>), so that any collision detection, and controlling will only apply to him &#8212; in the event that I want to add more controllable characters, I can just pull from the array and re-assign <code>mc_frombie<\/code> to the desired controllable sprite.<br \/>\nThen we have the player properties (values), which are set manually.<br \/>\nWe set the <code>.width<\/code> and <code>.height<\/code>, this is important for collision detection (which uses width height to check). When the player crouches\/ducks the <code>.height<\/code> is set to the crouch value. These values will change throughout the game, depending on the player&#8217;s state.<br \/>\nThen there are the last remaining properties that have to do with the physics, and states like jumping, if the player is on the ground, and ducking&#8230;<br \/>\n<code>mc_frombie.speed<\/code> = the speed of the player.<br \/>\n<code>mc_frombie.jumpHeight<\/code> = how high the player&#8217;s initial jump is. The player jumps higher if the up key is held down longer. If the up key is tapped then the player jumps at this default value.<br \/>\n<code>mc_frombie.velX<\/code> = the x velocity.<br \/>\n<code>mc_frombie.velY<\/code> = the y velocity.<br \/>\n<code>mc_frombie.jumping<\/code> = is the player jumping?<br \/>\n<code>mc_frombie.grounded<\/code> = is the player on the ground (on a platform)?<br \/>\n<code>mc_frombie.ducking<\/code> = is the player ducking (crouching)?<\/p>\n<p>In conclusion, player setup looks like this:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">\/\/PLAYER<\/span>\r\naddSprite(lib.Player, <span style=\"background-color: #fff0f0\">&quot;mc_frombie&quot;<\/span>, arr_players, <span style=\"color: #0000DD; font-weight: bold\">150<\/span>, <span style=\"color: #0000DD; font-weight: bold\">150<\/span>);\r\nmc_frombie <span style=\"color: #333333\">=<\/span> arr_players[<span style=\"color: #0000DD; font-weight: bold\">0<\/span>];\r\n<span style=\"color: #888888\">\/\/player properties<\/span>\r\n<span style=\"color: #888888\">\/\/width and height is set manually<\/span>\r\n<span style=\"color: #888888\">\/\/this is the standing position<\/span>\r\n<span style=\"color: #888888\">\/\/crouching is adjusted in the down arrow event (reduce height, and go to another frame)<\/span>\r\n<span style=\"color: #888888\">\/\/this way I don&#39;t need fancy collision detection because I always know the w\/h values...<\/span>\r\nmc_frombie.width <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">40<\/span>;<span style=\"color: #888888\">\/\/66<\/span>\r\nmc_frombie.height <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">110<\/span>;\r\n<span style=\"color: #888888\">\/\/<\/span>\r\nmc_frombie.speed <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">6<\/span>;<span style=\"color: #888888\">\/\/9;<\/span>\r\nmc_frombie.jumpHeight <span style=\"color: #333333\">=<\/span> <span style=\"color: #6600EE; font-weight: bold\">1.5<\/span>;<span style=\"color: #888888\">\/\/2<\/span>\r\nmc_frombie.velX <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>;\r\nmc_frombie.velY <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>;\r\nmc_frombie.jumping <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\nmc_frombie.grounded <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\nmc_frombie.ducking <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n<span style=\"color: #888888\">\/\/<\/span>\r\nmc_frombie.stop();\r\ngotoAndStopRandom(mc_frombie.mc_frombie);\r\n<\/pre>\n<\/div>\n<p>Now we set up our platforms\/floors. There are two types of floors. One is the classic version where if you hit the sides, and bottom, it will register as a collision and the player may not move there. The second is the one where you can jump through it, and land on it&#8230; Hope that makes sense. Envision the first being a box, and the second is like a cloud&#8230;<br \/>\nEach platform is added to <code>arr_colidable<\/code>, and variable assigned to it.<br \/>\nEach platform needs to have its own <code>width\/height<\/code> set, and then its <code>isPlatform<\/code> boolean set. <code>False<\/code> means you can&#8217;t jump through it (collision registers on all sides), <code>true<\/code> means you can jump through it and land on it (collision only registers for top).<br \/>\nWe have four platforms to start with.<br \/>\nThe end result looks like this:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">\/\/GROUND<\/span>\r\naddSprite(lib.Colidable_Floor, <span style=\"background-color: #fff0f0\">&quot;mc_floor1&quot;<\/span>, arr_colidable, <span style=\"color: #0000DD; font-weight: bold\">50<\/span>, <span style=\"color: #0000DD; font-weight: bold\">300<\/span>);\r\nmc_floor1 <span style=\"color: #333333\">=<\/span> arr_colidable[<span style=\"color: #0000DD; font-weight: bold\">0<\/span>];\r\n<span style=\"color: #888888\">\/\/properties<\/span>\r\nmc_floor1.width <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">800<\/span>;\r\nmc_floor1.height <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">50<\/span>;\r\nmc_floor1.isPlatform <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n\r\n<span style=\"color: #888888\">\/\/GROUND 2<\/span>\r\naddSprite(lib.Colidable_Floor, <span style=\"background-color: #fff0f0\">&quot;mc_floor2&quot;<\/span>, arr_colidable, <span style=\"color: #0000DD; font-weight: bold\">1000<\/span>, <span style=\"color: #0000DD; font-weight: bold\">130<\/span>);\r\nmc_floor2 <span style=\"color: #333333\">=<\/span> arr_colidable[<span style=\"color: #0000DD; font-weight: bold\">1<\/span>];\r\n<span style=\"color: #888888\">\/\/properties<\/span>\r\nmc_floor2.width <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">800<\/span>;\r\nmc_floor2.height <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">50<\/span>;\r\nmc_floor2.isPlatform <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n\r\n<span style=\"color: #888888\">\/\/GROUND 3<\/span>\r\naddSprite(lib.Colidable_Platform, <span style=\"background-color: #fff0f0\">&quot;mc_floor3&quot;<\/span>, arr_colidable, <span style=\"color: #0000DD; font-weight: bold\">300<\/span>, <span style=\"color: #0000DD; font-weight: bold\">50<\/span>);\r\nmc_floor3 <span style=\"color: #333333\">=<\/span> arr_colidable[<span style=\"color: #0000DD; font-weight: bold\">2<\/span>];\r\n<span style=\"color: #888888\">\/\/properties<\/span>\r\nmc_floor3.width <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">100<\/span>;\r\nmc_floor3.height <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">50<\/span>;\r\nmc_floor3.isPlatform <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n\r\n<span style=\"color: #888888\">\/\/Platform, you can walk through it, and jump up through it, but you can stand on it<\/span>\r\naddSprite(lib.Colidable_Platform, <span style=\"background-color: #fff0f0\">&quot;mc_platform1&quot;<\/span>, arr_colidable, <span style=\"color: #0000DD; font-weight: bold\">500<\/span>, <span style=\"color: #0000DD; font-weight: bold\">200<\/span>);\r\nmc_platform1 <span style=\"color: #333333\">=<\/span> arr_colidable[<span style=\"color: #0000DD; font-weight: bold\">3<\/span>];\r\n<span style=\"color: #888888\">\/\/properties<\/span>\r\nmc_platform1.width <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">100<\/span>;\r\nmc_platform1.height <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">50<\/span>;\r\nmc_platform1.isPlatform <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">true<\/span>;<span style=\"color: #888888\">\/\/if it is a platform you can stand on it, but hitting the bottom and sides does nothing<\/span>\r\n<\/pre>\n<\/div>\n<p>*************<\/p>\n<p><strong>Collision Detection!<\/strong><\/p>\n<p>I wanted the simplest, most basic, and corner cutting approach possible, because I want to move on to making the game. I never really thought about the finer aspects of collision detection before, since there are so many libraries available. You can just use what&#8217;s already provided. It was interesting to wrap my head around it.<br \/>\nI found <a href=\"http:\/\/www.somethinghitme.com\/2013\/04\/16\/creating-a-canvas-platformer-tutorial-part-tw\/\" target=\"_blank\">Somethinghitme&#8217;s explanation of collision detection<\/a> to be an excellent example for getting my head thinking in those terms. I recommend checking it out, it&#8217;s basically what I&#8217;m doing here&#8230;<\/p>\n<p>To begin we will calculate the amount of overlap between the two objects (object1 and object2), on both sides, top and bottom:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\">leftDist    <span style=\"color: #333333\">=<\/span> (object2.x <span style=\"color: #333333\">-<\/span> object2.width<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>) <span style=\"color: #333333\">-<\/span> (object1.x <span style=\"color: #333333\">+<\/span> object1.width<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>);\r\nrightDist   <span style=\"color: #333333\">=<\/span> (object1.x <span style=\"color: #333333\">-<\/span> object1.width<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>) <span style=\"color: #333333\">-<\/span> (object2.x <span style=\"color: #333333\">+<\/span> object2.width<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>);\r\ntopDist     <span style=\"color: #333333\">=<\/span> (object2.y <span style=\"color: #333333\">-<\/span> object2.height<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>) <span style=\"color: #333333\">-<\/span> (object1.y <span style=\"color: #333333\">+<\/span> object1.height<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>);\r\nbottomDist  <span style=\"color: #333333\">=<\/span> (object1.y <span style=\"color: #333333\">-<\/span> object1.height<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>) <span style=\"color: #333333\">-<\/span> (object2.y <span style=\"color: #333333\">+<\/span> object2.height<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>);\r\n<\/pre>\n<\/div>\n<p>And then the half width and height of both objects. This will be used later for determining which side the collision occurred on.<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">var<\/span> hWidths <span style=\"color: #333333\">=<\/span> (object1.width <span style=\"color: #333333\">\/<\/span> <span style=\"color: #0000DD; font-weight: bold\">2<\/span>) <span style=\"color: #333333\">+<\/span> (object2.width <span style=\"color: #333333\">\/<\/span> <span style=\"color: #0000DD; font-weight: bold\">2<\/span>);\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> hHeights <span style=\"color: #333333\">=<\/span> (object1.height <span style=\"color: #333333\">\/<\/span> <span style=\"color: #0000DD; font-weight: bold\">2<\/span>) <span style=\"color: #333333\">+<\/span> (object2.height <span style=\"color: #333333\">\/<\/span> <span style=\"color: #0000DD; font-weight: bold\">2<\/span>);\r\n<\/pre>\n<\/div>\n<p>Then we&#8217;ll move on to our <code>if<\/code> block. First to just get a collision (regardless of what side):<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">if<\/span>(leftDist <span style=\"color: #333333\">&lt;<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span> <span style=\"color: #333333\">&amp;&amp;<\/span> rightDist <span style=\"color: #333333\">&lt;<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span> <span style=\"color: #333333\">&amp;&amp;<\/span> topDist <span style=\"color: #333333\">&lt;<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span> <span style=\"color: #333333\">&amp;&amp;<\/span> bottomDist <span style=\"color: #333333\">&lt;<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>){\r\n<\/pre>\n<\/div>\n<p>In this block we&#8217;ll fine tune our check, and return what side the collision took place on.<br \/>\nFirst we&#8217;ll set up the string <code>direction_string<\/code>. This will return <code>\"left\"<\/code>, <code>\"right\"<\/code>, <code>\"top\"<\/code>, or <code>\"bottom\"<\/code>.<\/p>\n<p>Then we have our distance variables (as the comment suggests) between the two objects:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">var<\/span> xDist <span style=\"color: #333333\">=<\/span> object1.x <span style=\"color: #333333\">-<\/span> object2.x;\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> yDist <span style=\"color: #333333\">=<\/span> object1.y <span style=\"color: #333333\">-<\/span> object2.y;\r\n<\/pre>\n<\/div>\n<p>The last set of variables is where the magic happens! This <code>oX<\/code> and <code>oY<\/code> determine what side the collision occurred on. Simply put:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">var<\/span> oX <span style=\"color: #333333\">=<\/span> hWidths <span style=\"color: #333333\">-<\/span> <span style=\"color: #007020\">Math<\/span>.abs(xDist);\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> oY <span style=\"color: #333333\">=<\/span> hHeights <span style=\"color: #333333\">-<\/span> <span style=\"color: #007020\">Math<\/span>.abs(yDist);\r\n<\/pre>\n<\/div>\n<p>Moving on to our final set of <code>if<\/code>&#8216;s&#8230; These check on what side\/top\/bottom the collision took place, and set <code>direction_string<\/code> to that side. Then <code>object1<\/code>&#8216;s (the player) <code>.y<\/code> or <code>.x<\/code> is set to <code>oY<\/code> or <code>oX<\/code> (landed on it, or can&#8217;t go beyond it)&#8230; just force it to that value.<br \/>\nAll that looks like this:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">if<\/span> (oX <span style=\"color: #333333\">&gt;=<\/span> oY) {\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (yDist <span style=\"color: #333333\">&gt;<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span> <span style=\"color: #333333\">&amp;&amp;<\/span> <span style=\"color: #333333\">!<\/span>object2.isPlatform) {<span style=\"color: #888888\">\/\/hit the top<\/span>\r\n\t\tdirection_string <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;top&quot;<\/span>;\r\n\t\tobject1.y <span style=\"color: #333333\">+=<\/span> oY;\r\n\t}\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (yDist <span style=\"color: #333333\">&lt;<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>){<span style=\"color: #888888\">\/\/standing on it<\/span>\r\n\t\tdirection_string <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;bottom&quot;<\/span>;\r\n\t\tobject1.y <span style=\"color: #333333\">-=<\/span> oY;\r\n\t}\r\n} <span style=\"color: #008800; font-weight: bold\">else<\/span> {\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (xDist <span style=\"color: #333333\">&gt;<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span> <span style=\"color: #333333\">&amp;&amp;<\/span> <span style=\"color: #333333\">!<\/span>object2.isPlatform) {<span style=\"color: #888888\">\/\/left of<\/span>\r\n\t\tdirection_string <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;left&quot;<\/span>;\r\n\t\tobject1.x <span style=\"color: #333333\">+=<\/span> oX;\r\n\t} \r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (xDist <span style=\"color: #333333\">&lt;<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span> <span style=\"color: #333333\">&amp;&amp;<\/span> <span style=\"color: #333333\">!<\/span>object2.isPlatform) {<span style=\"color: #888888\">\/\/right of<\/span>\r\n\t\tdirection_string <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;right&quot;<\/span>;\r\n\t\tobject1.x <span style=\"color: #333333\">-=<\/span> oX;\r\n\t}\r\n}\r\n<\/pre>\n<\/div>\n<p>Finally return the value of <code>direction_string<\/code>. We worked hard to get this!<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">return<\/span> direction_string;\r\n<\/pre>\n<\/div>\n<p>The entire <code>colisionCheck<\/code> is as follows:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">\/\/colision detection for player and platform (ground)<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> colisionCheck(object1, object2) {\r\n\t<span style=\"color: #888888\">\/\/amount of overlap<\/span>\r\n\tleftDist    <span style=\"color: #333333\">=<\/span> (object2.x <span style=\"color: #333333\">-<\/span> object2.width<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>) <span style=\"color: #333333\">-<\/span> (object1.x <span style=\"color: #333333\">+<\/span> object1.width<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>);\r\n\trightDist   <span style=\"color: #333333\">=<\/span> (object1.x <span style=\"color: #333333\">-<\/span> object1.width<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>) <span style=\"color: #333333\">-<\/span> (object2.x <span style=\"color: #333333\">+<\/span> object2.width<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>);\r\n\ttopDist     <span style=\"color: #333333\">=<\/span> (object2.y <span style=\"color: #333333\">-<\/span> object2.height<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>) <span style=\"color: #333333\">-<\/span> (object1.y <span style=\"color: #333333\">+<\/span> object1.height<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>);\r\n\tbottomDist  <span style=\"color: #333333\">=<\/span> (object1.y <span style=\"color: #333333\">-<\/span> object1.height<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>) <span style=\"color: #333333\">-<\/span> (object2.y <span style=\"color: #333333\">+<\/span> object2.height<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>);\r\n    <span style=\"color: #888888\">\/\/half widths and half heights of the objects<\/span>\r\n    <span style=\"color: #008800; font-weight: bold\">var<\/span> hWidths <span style=\"color: #333333\">=<\/span> (object1.width <span style=\"color: #333333\">\/<\/span> <span style=\"color: #0000DD; font-weight: bold\">2<\/span>) <span style=\"color: #333333\">+<\/span> (object2.width <span style=\"color: #333333\">\/<\/span> <span style=\"color: #0000DD; font-weight: bold\">2<\/span>);\r\n    <span style=\"color: #008800; font-weight: bold\">var<\/span> hHeights <span style=\"color: #333333\">=<\/span> (object1.height <span style=\"color: #333333\">\/<\/span> <span style=\"color: #0000DD; font-weight: bold\">2<\/span>) <span style=\"color: #333333\">+<\/span> (object2.height <span style=\"color: #333333\">\/<\/span> <span style=\"color: #0000DD; font-weight: bold\">2<\/span>);\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(leftDist <span style=\"color: #333333\">&lt;<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span> <span style=\"color: #333333\">&amp;&amp;<\/span> rightDist <span style=\"color: #333333\">&lt;<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span> <span style=\"color: #333333\">&amp;&amp;<\/span> topDist <span style=\"color: #333333\">&lt;<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span> <span style=\"color: #333333\">&amp;&amp;<\/span> bottomDist <span style=\"color: #333333\">&lt;<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>){\r\n\t\t<span style=\"color: #888888\">\/\/left, right, top, bottom<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> direction_string;\r\n\t\t<span style=\"color: #888888\">\/\/distance variables<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> xDist <span style=\"color: #333333\">=<\/span> object1.x <span style=\"color: #333333\">-<\/span> object2.x;\r\n\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> yDist <span style=\"color: #333333\">=<\/span> object1.y <span style=\"color: #333333\">-<\/span> object2.y;\r\n\t\t<span style=\"color: #888888\">\/\/which side does the colision occur (get the side it occurs on)<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> oX <span style=\"color: #333333\">=<\/span> hWidths <span style=\"color: #333333\">-<\/span> <span style=\"color: #007020\">Math<\/span>.abs(xDist);\r\n\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> oY <span style=\"color: #333333\">=<\/span> hHeights <span style=\"color: #333333\">-<\/span> <span style=\"color: #007020\">Math<\/span>.abs(yDist);\r\n\t\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (oX <span style=\"color: #333333\">&gt;=<\/span> oY) {\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (yDist <span style=\"color: #333333\">&gt;<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span> <span style=\"color: #333333\">&amp;&amp;<\/span> <span style=\"color: #333333\">!<\/span>object2.isPlatform) {<span style=\"color: #888888\">\/\/hit the top<\/span>\r\n\t\t\t\tdirection_string <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;top&quot;<\/span>;\r\n\t\t\t\tobject1.y <span style=\"color: #333333\">+=<\/span> oY;\r\n\t\t\t}\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (yDist <span style=\"color: #333333\">&lt;<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>){<span style=\"color: #888888\">\/\/standing on it<\/span>\r\n\t\t\t\tdirection_string <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;bottom&quot;<\/span>;\r\n\t\t\t\tobject1.y <span style=\"color: #333333\">-=<\/span> oY;\r\n\t\t\t}\r\n\t\t} <span style=\"color: #008800; font-weight: bold\">else<\/span> {\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (xDist <span style=\"color: #333333\">&gt;<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span> <span style=\"color: #333333\">&amp;&amp;<\/span> <span style=\"color: #333333\">!<\/span>object2.isPlatform) {<span style=\"color: #888888\">\/\/left of<\/span>\r\n\t\t\t\tdirection_string <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;left&quot;<\/span>;\r\n\t\t\t\tobject1.x <span style=\"color: #333333\">+=<\/span> oX;\r\n\t\t\t} \r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (xDist <span style=\"color: #333333\">&lt;<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span> <span style=\"color: #333333\">&amp;&amp;<\/span> <span style=\"color: #333333\">!<\/span>object2.isPlatform) {<span style=\"color: #888888\">\/\/right of<\/span>\r\n\t\t\t\tdirection_string <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;right&quot;<\/span>;\r\n\t\t\t\tobject1.x <span style=\"color: #333333\">-=<\/span> oX;\r\n\t\t\t}\r\n\t\t}\r\n\t\t\t<span style=\"color: #888888\">\/\/console.log(&quot;direction_string: &quot;+direction_string);<\/span>\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">return<\/span> direction_string;\r\n\t}\r\n}\r\n<\/pre>\n<\/div>\n<p>To use the above, we just need to loop through our arrays, and check if <code>mc_frombie<\/code> collided with any <code>arr_colidable<\/code>.<br \/>\nBasically it will look like this:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">for<\/span> (<span style=\"color: #008800; font-weight: bold\">var<\/span> i <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>; i <span style=\"color: #333333\">&lt;<\/span> arr_colidable.length; i<span style=\"color: #333333\">++<\/span>) {\r\n     dir <span style=\"color: #333333\">=<\/span> colisionCheck(mc_frombie, arr_colidable[i]);\r\n\tconsole.log(dir);\r\n};\r\n<\/pre>\n<\/div>\n<p>We will handle this in a <a href=\"http:\/\/createjs.com\/Docs\/EaselJS\/classes\/Ticker.html\" target=\"_blank\">Ticker<\/a>, later.<\/p>\n<p>*************<\/p>\n<p><strong>Physics &#038; Player Movement<\/strong><\/p>\n<p>I created a couple of physics prototypes for this. They are both good examples of basic &#8220;physics&#8221;, and are great for getting your head into it.<\/p>\n<p><strong>Click<\/strong> to generate particles on mouse position.<br \/>\n[iframe src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Super_Simple_Snippets\/gravity_gibsJS.html&#8221; width=&#8221;480&#8243; height=&#8221;500&#8243;]<br \/>\n<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Super_Simple_Snippets\/gravity_gibsJS.html\" target=\"_blank\">http:\/\/nathalielawhead.com\/sourcefiles\/Super_Simple_Snippets\/gravity_gibsJS.html<\/a><br \/>\n<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Super_Simple_Snippets\/gravity_gibsJS.zip\" target=\"_blank\">DOWNLOAD SOURCE HERE<\/a><\/p>\n<p><strong>Click<\/strong> FIRE! to generate particles.<br \/>\n[iframe src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Projectiles\/gravity_projectileJS.html&#8221; width=&#8221;480&#8243; height=&#8221;500&#8243;]<br \/>\n<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Projectiles\/gravity_projectileJS.html\" target=\"_blank\">http:\/\/nathalielawhead.com\/sourcefiles\/Projectiles\/gravity_projectileJS.html<\/a><br \/>\n<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Projectiles\/gravity_projectileJS.zip\" target=\"_blank\">DOWNLOAD SOURCE HERE<\/a><\/p>\n<p>In this game&#8217;s case, the approach is similar, except we are also checking for collision, and then we handle that appropriately (depending on platform type). Since we already have collision detection done, we just need things to fall on things now&#8230;<\/p>\n<p>We&#8217;ll start with setting up the <a href=\"http:\/\/createjs.com\/Docs\/EaselJS\/classes\/Ticker.html\" target=\"_blank\">Ticker<\/a> (to update positions), and the keyboard (to move the player).<br \/>\nLike I said, I&#8217;m using <code>keymaster.js<\/code> for controlling&#8230; as things later turned out, I needed to use <code>KEY UP<\/code> events (releasing <code>UP<\/code> after holding it down &#8211; to jump really high, and &#8220;un-crouching&#8221;), so I had to resort to old-school keyboard functionality. This is messy, and not efficient (I know!). I will be phasing out <code>keymaster.js<\/code> later (or fixing it so I use only <code>keymaster.js<\/code>), but (for now) we have two approaches&#8230;<\/p>\n<p>We&#8217;ll create the events:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">\/\/Events<\/span>\r\ncreatejs.Ticker.addEventListener(<span style=\"background-color: #fff0f0\">&quot;tick&quot;<\/span>, event_movement_ticker);\r\n<span style=\"color: #007020\">document<\/span>.onkeydown <span style=\"color: #333333\">=<\/span> evnt_KEYDOWN;\r\n<span style=\"color: #007020\">document<\/span>.onkeyup <span style=\"color: #333333\">=<\/span> event_KEYUP;\r\n<\/pre>\n<\/div>\n<p>And also make sure to create <code>evnt_removeAll();<\/code> (usage explained above). I realize that you could also use <a href=\"http:\/\/createjs.com\/Docs\/EaselJS\/files\/createjs_events_EventDispatcher.js.html#l258\" target=\"_blank\"><code>removeAllEventListeners([type])<\/code><\/a> to remove absolutely everything, but I&#8217;m not sure what events I will want to keep in the future, so I&#8217;m doing this for now&#8230;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">\/\/remove events<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> evnt_removeAll(){\r\n\tcreatejs.Ticker.removeEventListener(<span style=\"background-color: #fff0f0\">&quot;tick&quot;<\/span>, event_movement_ticker);\r\n\t<span style=\"color: #007020\">document<\/span>.onkeydown <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">null<\/span>;\r\n\t<span style=\"color: #007020\">document<\/span>.onkeyup <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">null<\/span>;\r\n};\r\n<\/pre>\n<\/div>\n<p>Now we&#8217;ll set up the <code>keyCode<\/code>&#8216;s necessary for <code>KEY UP<\/code>:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\">KEY_UP <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">38<\/span>;\r\nKEY_DOWN <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">40<\/span>;\r\nKEY_S <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">83<\/span>;\r\n<\/pre>\n<\/div>\n<p>There are two classic key down and key up functions. These take care of ducking, and jumping.<br \/>\nKey down manages jumping mostly (at the moment). This increases <code>mc_frombie.jumpHeight<\/code>. The longer up is pressed the higher the player will jump. This is going to be an important mechanic later on in the game. It also sets <code>bool_keyDown<\/code> to <code>true<\/code> to indicate that a key is being pressed.<br \/>\nKey down looks like this:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">function<\/span> evnt_KEYDOWN(event){\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(<span style=\"color: #333333\">!<\/span>event){ <span style=\"color: #008800; font-weight: bold\">var<\/span> event <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">window<\/span>.event; }\r\n\t<span style=\"color: #888888\">\/\/get ready for jump height - the longer it&#39;s pressed the heigher he will jump<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (key.isPressed(<span style=\"background-color: #fff0f0\">&quot;up&quot;<\/span>) <span style=\"color: #333333\">||<\/span> key.isPressed(<span style=\"background-color: #fff0f0\">&quot;w&quot;<\/span>)) {\r\n\t\tmc_frombie.jumpHeight <span style=\"color: #333333\">+=<\/span> .<span style=\"color: #0000DD; font-weight: bold\">5<\/span>;\r\n\t\tbool_keyDown <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">true<\/span>;\r\n\t};\r\n};\r\n<\/pre>\n<\/div>\n<p>Then there&#8217;s the key up event. It sets <code>bool_keyDown<\/code> to <code>false<\/code>, indicating that the key has been released.<br \/>\nIn the case of the up key having been released (if the player isn&#8217;t already jumping), it sets <code>bool_jumpDown<\/code> to <code>true<\/code>. The jump will now be handled in the <code>Ticker<\/code> event.<br \/>\nDucking is a little simpler. If the <code>DOWN<\/code> or <code>S<\/code> key are released, and <code>mc_frombie<\/code> is ducking, set ducking to <code>false<\/code>. The actual &#8220;un-ducking&#8221; will also be handled in the <code>Ticker<\/code>.<br \/>\nThe finished <code>KEY UP<\/code> event is as follows:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">function<\/span> event_KEYUP(event){\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(<span style=\"color: #333333\">!<\/span>event){ <span style=\"color: #008800; font-weight: bold\">var<\/span> event <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">window<\/span>.event; }\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\tbool_keyDown <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n\t<span style=\"color: #888888\">\/\/now trigger the jump (only if not already jumping and the key is not down)<\/span>\r\n\t<span style=\"color: #888888\">\/\/greater than default is how it tells if you triggered a jump)<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> ((<span style=\"color: #333333\">!<\/span>bool_keyDown <span style=\"color: #333333\">&amp;&amp;<\/span> <span style=\"color: #333333\">!<\/span>mc_frombie.jumping) <span style=\"color: #333333\">&amp;&amp;<\/span> \r\n\t\t(event.keyCode <span style=\"color: #333333\">==<\/span> KEY_UP)) {\r\n\t\tbool_jumpDown <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">true<\/span>;\r\n\t};\r\n\t<span style=\"color: #888888\">\/\/unduck - ducking is in ticker because it needs to happen imediately - there is a keydown delay in multiple key<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> ((event.keyCode <span style=\"color: #333333\">==<\/span> KEY_DOWN <span style=\"color: #333333\">||<\/span> event.keyCode <span style=\"color: #333333\">==<\/span> KEY_S) \r\n\t\t<span style=\"color: #333333\">&amp;&amp;<\/span> (mc_frombie.ducking)) {\r\n\t\tmc_frombie.ducking <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n\t};\r\n};\r\n<\/pre>\n<\/div>\n<p>Moving on to the actual player physics, I&#8217;ll break down the basics of how it works&#8230;<\/p>\n<p>Jumping is the first thing you&#8217;ll notice. As the <code>if<\/code> condition suggests, you may only jump if <code>bool_jumpDown<\/code> is <code>true<\/code> (this is set in KEY UP event), <code>mc_frombie<\/code> isn&#8217;t already jumping, and <code>mc_frombie<\/code> is on the ground.<br \/>\nIf this is the case then jumping is set to <code>true<\/code>, grounded is <code>false<\/code> (he&#8217;s jumping now), and ducking is <code>false<\/code> (resets ducking if he was ducking), and then set the <code>velY<\/code> to move:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\">mc_frombie.velY <span style=\"color: #333333\">=<\/span> <span style=\"color: #333333\">-<\/span>mc_frombie.speed <span style=\"color: #333333\">*<\/span> mc_frombie.jumpHeight;\r\n<\/pre>\n<\/div>\n<p>That&#8217;s the important part.<br \/>\nThe last two <code>bool_jumpDown<\/code> and <code>bool_keyDown<\/code> being set to <code>false<\/code> basically resets things. Once player is back on the ground you can jump again.<br \/>\nThe above looks like this:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">if<\/span> (bool_jumpDown <span style=\"color: #333333\">&amp;&amp;<\/span> <span style=\"color: #333333\">!<\/span>mc_frombie.jumping <span style=\"color: #333333\">&amp;&amp;<\/span> mc_frombie.grounded) {\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\tmc_frombie.jumping <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">true<\/span>;\r\n\tmc_frombie.grounded <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n\tmc_frombie.ducking <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;<span style=\"color: #888888\">\/\/reset ducking also if up is pressed<\/span>\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\tmc_frombie.velY <span style=\"color: #333333\">=<\/span> <span style=\"color: #333333\">-<\/span>mc_frombie.speed <span style=\"color: #333333\">*<\/span> mc_frombie.jumpHeight;\r\n\t<span style=\"color: #888888\">\/\/reset<\/span>\r\n\tbool_jumpDown <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n\tbool_keyDown <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n}\r\n<\/pre>\n<\/div>\n<p>There&#8217;s another condition that follows which resets <code>mc_frombie.jumpHeight<\/code> to the default value.<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">if<\/span>(<span style=\"color: #333333\">!<\/span>bool_jumpDown <span style=\"color: #333333\">&amp;&amp;<\/span> mc_frombie.jumping <span style=\"color: #333333\">&amp;&amp;<\/span> <span style=\"color: #333333\">!<\/span>mc_frombie.grounded){\r\n\tmc_frombie.jumpHeight <span style=\"color: #333333\">=<\/span> <span style=\"color: #6600EE; font-weight: bold\">1.5<\/span>;\r\n};\r\n<\/pre>\n<\/div>\n<p>After jumping you&#8217;ll notice ducking. Ducking is simple. If <code>DOWN<\/code> or <code>S<\/code>, and you&#8217;re not jumping, and on the ground, <code>player_duck(\"duck\");<\/code> is called.<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">if<\/span> ((key.isPressed(<span style=\"background-color: #fff0f0\">&quot;down&quot;<\/span>) <span style=\"color: #333333\">||<\/span> key.isPressed(<span style=\"background-color: #fff0f0\">&quot;s&quot;<\/span>)) <span style=\"color: #333333\">&amp;&amp;<\/span> \r\n\t(<span style=\"color: #333333\">!<\/span>mc_frombie.jumping <span style=\"color: #333333\">&amp;&amp;<\/span> mc_frombie.grounded)) {\r\n\tplayer_duck(<span style=\"background-color: #fff0f0\">&quot;duck&quot;<\/span>);\r\n}\r\n<\/pre>\n<\/div>\n<p>You may then unduck only if <code>height<\/code> is less than the default (so no conflict with other <code>gotoAndStop<\/code> labels takes place), and if <code>mc_frombie.ducking<\/code> is <code>false<\/code>. If that&#8217;s the case then the <code>height<\/code> is set back to normal, and player is set back to <code>\"walk\"<\/code>.<br \/>\nUnduck looks like this:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">if<\/span>(<span style=\"color: #333333\">!<\/span>mc_frombie.ducking <span style=\"color: #333333\">&amp;&amp;<\/span> mc_frombie.height<span style=\"color: #333333\">&lt;<\/span><span style=\"color: #0000DD; font-weight: bold\">110<\/span>){\r\n\tmc_frombie.height <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">110<\/span>;\r\n\tmc_frombie.gotoAndStop(<span style=\"background-color: #fff0f0\">&quot;walk&quot;<\/span>);\r\n};\r\n<\/pre>\n<\/div>\n<p>Finally we have our left and right movements.<br \/>\nLeft\/right looks like this, and is fairly self explanatory:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">\/\/left\/right movement<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">if<\/span> ((key.isPressed(<span style=\"background-color: #fff0f0\">&quot;left&quot;<\/span>) <span style=\"color: #333333\">||<\/span> key.isPressed(<span style=\"background-color: #fff0f0\">&quot;a&quot;<\/span>))) {\r\n    <span style=\"color: #008800; font-weight: bold\">if<\/span> (mc_frombie.velX <span style=\"color: #333333\">&gt;<\/span> <span style=\"color: #333333\">-<\/span>mc_frombie.speed) {\r\n        mc_frombie.velX<span style=\"color: #333333\">-=<\/span>mc_frombie.speed;\r\n    }\r\n}\r\n<span style=\"color: #008800; font-weight: bold\">if<\/span> ((key.isPressed(<span style=\"background-color: #fff0f0\">&quot;right&quot;<\/span>) <span style=\"color: #333333\">||<\/span> key.isPressed(<span style=\"background-color: #fff0f0\">&quot;d&quot;<\/span>))) {\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (mc_frombie.velX <span style=\"color: #333333\">&lt;<\/span> mc_frombie.speed) {\r\n        mc_frombie.velX<span style=\"color: #333333\">+=<\/span>mc_frombie.speed;\r\n    }\r\n}\r\n<\/pre>\n<\/div>\n<p>After we&#8217;re done with all the above, we need to set mc_frombie&#8217;s <code>velX<\/code> and <code>velY<\/code> (friction, and gravity), and set <code>mc_frombie.grounded<\/code> to <code>false<\/code>. Basically calculate all the above to apply later&#8230; which looks like this:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">\/\/friction to velX and and gravity to velY (horizontal and vertical values)<\/span>\r\nmc_frombie.velX <span style=\"color: #333333\">*=<\/span> friction;\r\nmc_frombie.velY <span style=\"color: #333333\">+=<\/span> gravity;\r\n<span style=\"color: #888888\">\/\/<\/span>\r\nmc_frombie.grounded <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n<\/pre>\n<\/div>\n<p>Then we have the fun part! Checking collision, and acting appropriately.<br \/>\nWe loop through <code>arr_colidable<\/code>, and set variable <code>dir<\/code> to the returned value of <code>colisionCheck<\/code>.<br \/>\nThen there&#8217;s our condition for each direction (left, right, top, or bottom).<br \/>\nLeft and right are basically the same thing. They stop <code>velX<\/code>, and set <code>mc_frombie.jumping<\/code> to <code>false<\/code>.<br \/>\nBottom means you&#8217;re standing on the ground. So <code>mc_frombie.grounded<\/code> is set to <code>true<\/code> and <code>mc_frombie.jumping<\/code> is set to <code>false<\/code> (you&#8217;re not in the air anymore).<br \/>\nTop means that he hit his head on the bottom of a platform. <code>velY<\/code> is set back (push him down), and then the <code>\"ouch\"<\/code> state is set &#8212; he&#8217;s ducking, and I&#8217;ll have a cute animation there.<br \/>\nThe entire block looks like this:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">for<\/span> (<span style=\"color: #008800; font-weight: bold\">var<\/span> i <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>; i <span style=\"color: #333333\">&lt;<\/span> arr_colidable.length; i<span style=\"color: #333333\">++<\/span>) {\r\n    <span style=\"color: #888888\">\/\/<\/span>\r\n    dir <span style=\"color: #333333\">=<\/span> colisionCheck(mc_frombie, arr_colidable[i]);\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n    <span style=\"color: #008800; font-weight: bold\">if<\/span> (dir <span style=\"color: #333333\">===<\/span> <span style=\"background-color: #fff0f0\">&quot;left&quot;<\/span> <span style=\"color: #333333\">||<\/span> dir <span style=\"color: #333333\">===<\/span> <span style=\"background-color: #fff0f0\">&quot;right&quot;<\/span>) {\r\n        mc_frombie.velX <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>;\r\n        mc_frombie.jumping <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n    } <span style=\"color: #008800; font-weight: bold\">else<\/span> <span style=\"color: #008800; font-weight: bold\">if<\/span> (dir <span style=\"color: #333333\">===<\/span> <span style=\"background-color: #fff0f0\">&quot;bottom&quot;<\/span>) {\r\n        mc_frombie.grounded <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">true<\/span>;\r\n        mc_frombie.jumping <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n    } <span style=\"color: #008800; font-weight: bold\">else<\/span> <span style=\"color: #008800; font-weight: bold\">if<\/span> (dir <span style=\"color: #333333\">===<\/span> <span style=\"background-color: #fff0f0\">&quot;top&quot;<\/span>) {\r\n        mc_frombie.velY <span style=\"color: #333333\">*=<\/span> <span style=\"color: #333333\">-<\/span><span style=\"color: #0000DD; font-weight: bold\">1<\/span>;\r\n\t\t<span style=\"color: #888888\">\/\/if you hit the top then he protects his head (you scared him)<\/span>\r\n\t\t<span style=\"color: #888888\">\/\/jumping resets this<\/span>\r\n\t\tplayer_duck(<span style=\"background-color: #fff0f0\">&quot;ouch&quot;<\/span>);\r\n\t\t<span style=\"color: #888888\">\/\/<\/span>\r\n    }\r\n}\r\n<\/pre>\n<\/div>\n<p>After that we manage <code>velY<\/code> if he&#8217;s on the ground (not jumping anymore, above collision with ground took place).<br \/>\nJust reset\/set <code>velY<\/code> to 0 (default state):<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">if<\/span>(mc_frombie.grounded){\r\n     mc_frombie.velY <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>;\r\n}\r\n<\/pre>\n<\/div>\n<p>FINALLY, we need to set the player&#8217;s <code>x<\/code> and <code>y<\/code> to the calculated values.<br \/>\nSimply put:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\">mc_frombie.x <span style=\"color: #333333\">+=<\/span> mc_frombie.velX;\r\nmc_frombie.y <span style=\"color: #333333\">+=<\/span> mc_frombie.velY;\r\n<\/pre>\n<\/div>\n<p>The last bit manages the player falling off the screen and dying, or walking off the right and winning:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #888888\">\/\/Player death<\/span>\r\n<span style=\"color: #888888\">\/\/fell off stage<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">if<\/span>(mc_frombie.y <span style=\"color: #333333\">&gt;<\/span> (num_stageHeight <span style=\"color: #333333\">+<\/span> mc_frombie.height)){\r\n\tplayer_die();\r\n};\r\n<span style=\"color: #888888\">\/\/Player went to next screen<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">if<\/span>(mc_frombie.x<span style=\"color: #333333\">&gt;<\/span> (num_stageWidth <span style=\"color: #333333\">+<\/span> mc_frombie.width)){\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(bool_lastpanel){\r\n\t\tplayer_win();\r\n\t}<span style=\"color: #008800; font-weight: bold\">else<\/span>{\r\n\t\tconsole.log(<span style=\"background-color: #fff0f0\">&quot;next panel&quot;<\/span>);\r\n\t};\r\n};\r\n<\/pre>\n<\/div>\n<p>That&#8217;s it! The entire thing looks like this:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">function<\/span> event_movement_ticker(event) {\r\n\t<span style=\"color: #888888\">\/\/key presses<\/span>\r\n\t<span style=\"color: #888888\">\/\/jump<\/span>\r\n\t<span style=\"color: #888888\">\/\/make the jump - set to values - reset after<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (bool_jumpDown <span style=\"color: #333333\">&amp;&amp;<\/span> <span style=\"color: #333333\">!<\/span>mc_frombie.jumping <span style=\"color: #333333\">&amp;&amp;<\/span> mc_frombie.grounded) {\r\n\t\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t\tmc_frombie.jumping <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">true<\/span>;\r\n\t\tmc_frombie.grounded <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n\t\tmc_frombie.ducking <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;<span style=\"color: #888888\">\/\/reset ducking also if up is pressed<\/span>\r\n\t\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t\tmc_frombie.velY <span style=\"color: #333333\">=<\/span> <span style=\"color: #333333\">-<\/span>mc_frombie.speed <span style=\"color: #333333\">*<\/span> mc_frombie.jumpHeight;\r\n\t\t<span style=\"color: #888888\">\/\/reset<\/span>\r\n\t\tbool_jumpDown <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n\t\tbool_keyDown <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n\t}\r\n\t<span style=\"color: #888888\">\/\/reset the jump height while in the air<\/span>\r\n\t<span style=\"color: #888888\">\/\/if done above sometimes it doesn&#39;t reset<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(<span style=\"color: #333333\">!<\/span>bool_jumpDown <span style=\"color: #333333\">&amp;&amp;<\/span> mc_frombie.jumping <span style=\"color: #333333\">&amp;&amp;<\/span> <span style=\"color: #333333\">!<\/span>mc_frombie.grounded){\r\n\t\tmc_frombie.jumpHeight <span style=\"color: #333333\">=<\/span> <span style=\"color: #6600EE; font-weight: bold\">1.5<\/span>;\r\n\t};\r\n\t<span style=\"color: #888888\">\/\/start ducking - ducking is in the ticker because it needs to trigger RIGHT AWAY<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> ((key.isPressed(<span style=\"background-color: #fff0f0\">&quot;down&quot;<\/span>) <span style=\"color: #333333\">||<\/span> key.isPressed(<span style=\"background-color: #fff0f0\">&quot;s&quot;<\/span>)) <span style=\"color: #333333\">&amp;&amp;<\/span> \r\n\t\t(<span style=\"color: #333333\">!<\/span>mc_frombie.jumping <span style=\"color: #333333\">&amp;&amp;<\/span> mc_frombie.grounded)) {\r\n\t\t<span style=\"color: #888888\">\/\/duck<\/span>\r\n\t\tplayer_duck(<span style=\"background-color: #fff0f0\">&quot;duck&quot;<\/span>);\r\n\t}\r\n\t<span style=\"color: #888888\">\/\/unduck - only if also height is less than default so that no conflict with other gotoAndStop labels...<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(<span style=\"color: #333333\">!<\/span>mc_frombie.ducking <span style=\"color: #333333\">&amp;&amp;<\/span> mc_frombie.height<span style=\"color: #333333\">&lt;<\/span><span style=\"color: #0000DD; font-weight: bold\">110<\/span>){\r\n\t\tmc_frombie.height <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">110<\/span>;\r\n\t\tmc_frombie.gotoAndStop(<span style=\"background-color: #fff0f0\">&quot;walk&quot;<\/span>);\r\n\t};\r\n\t<span style=\"color: #888888\">\/\/left\/right movement<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> ((key.isPressed(<span style=\"background-color: #fff0f0\">&quot;left&quot;<\/span>) <span style=\"color: #333333\">||<\/span> key.isPressed(<span style=\"background-color: #fff0f0\">&quot;a&quot;<\/span>))) {\r\n        <span style=\"color: #008800; font-weight: bold\">if<\/span> (mc_frombie.velX <span style=\"color: #333333\">&gt;<\/span> <span style=\"color: #333333\">-<\/span>mc_frombie.speed) {\r\n            mc_frombie.velX<span style=\"color: #333333\">-=<\/span>mc_frombie.speed;\r\n        }\r\n\t}\r\n\t<span style=\"color: #888888\">\/\/stop player movement<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> ((key.isPressed(<span style=\"background-color: #fff0f0\">&quot;right&quot;<\/span>) <span style=\"color: #333333\">||<\/span> key.isPressed(<span style=\"background-color: #fff0f0\">&quot;d&quot;<\/span>))) {\r\n\t\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (mc_frombie.velX <span style=\"color: #333333\">&lt;<\/span> mc_frombie.speed) {\r\n            mc_frombie.velX<span style=\"color: #333333\">+=<\/span>mc_frombie.speed;\r\n        }\r\n\t\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t}\r\n\t<span style=\"color: #888888\">\/\/friction to velX and and gravity to velY (horizontal and vertical values)<\/span>\r\n\tmc_frombie.velX <span style=\"color: #333333\">*=<\/span> friction;\r\n    mc_frombie.velY <span style=\"color: #333333\">+=<\/span> gravity;\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\tmc_frombie.grounded <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #888888\">\/\/HIT-----<\/span>\r\n    <span style=\"color: #008800; font-weight: bold\">for<\/span> (<span style=\"color: #008800; font-weight: bold\">var<\/span> i <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>; i <span style=\"color: #333333\">&lt;<\/span> arr_colidable.length; i<span style=\"color: #333333\">++<\/span>) {\r\n        <span style=\"color: #888888\">\/\/<\/span>\r\n        dir <span style=\"color: #333333\">=<\/span> colisionCheck(mc_frombie, arr_colidable[i]);\r\n\t\t<span style=\"color: #888888\">\/\/<\/span>\r\n        <span style=\"color: #008800; font-weight: bold\">if<\/span> (dir <span style=\"color: #333333\">===<\/span> <span style=\"background-color: #fff0f0\">&quot;left&quot;<\/span> <span style=\"color: #333333\">||<\/span> dir <span style=\"color: #333333\">===<\/span> <span style=\"background-color: #fff0f0\">&quot;right&quot;<\/span>) {\r\n            mc_frombie.velX <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>;\r\n            mc_frombie.jumping <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n        } <span style=\"color: #008800; font-weight: bold\">else<\/span> <span style=\"color: #008800; font-weight: bold\">if<\/span> (dir <span style=\"color: #333333\">===<\/span> <span style=\"background-color: #fff0f0\">&quot;bottom&quot;<\/span>) {\r\n            mc_frombie.grounded <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">true<\/span>;\r\n            mc_frombie.jumping <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n        } <span style=\"color: #008800; font-weight: bold\">else<\/span> <span style=\"color: #008800; font-weight: bold\">if<\/span> (dir <span style=\"color: #333333\">===<\/span> <span style=\"background-color: #fff0f0\">&quot;top&quot;<\/span>) {\r\n            mc_frombie.velY <span style=\"color: #333333\">*=<\/span> <span style=\"color: #333333\">-<\/span><span style=\"color: #0000DD; font-weight: bold\">1<\/span>;\r\n\t\t\t<span style=\"color: #888888\">\/\/if you hit the top then he protects his head (you scared him)<\/span>\r\n\t\t\t<span style=\"color: #888888\">\/\/jumping resets this<\/span>\r\n\t\t\tplayer_duck(<span style=\"background-color: #fff0f0\">&quot;ouch&quot;<\/span>);\r\n\t\t\t<span style=\"color: #888888\">\/\/<\/span>\r\n        }\r\n    }\r\n\t<span style=\"color: #888888\">\/\/END HIT-----<\/span>\r\n\t<span style=\"color: #888888\">\/\/if on the ground then set velY to default state<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(mc_frombie.grounded){\r\n         mc_frombie.velY <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>;\r\n    }\r\n\t<span style=\"color: #888888\">\/\/now set the actual x and y to calculated values<\/span>\r\n\tmc_frombie.x <span style=\"color: #333333\">+=<\/span> mc_frombie.velX;\r\n    mc_frombie.y <span style=\"color: #333333\">+=<\/span> mc_frombie.velY;\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #888888\">\/\/Player death<\/span>\r\n\t<span style=\"color: #888888\">\/\/fell off stage<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(mc_frombie.y <span style=\"color: #333333\">&gt;<\/span> (num_stageHeight <span style=\"color: #333333\">+<\/span> mc_frombie.height)){\r\n\t\tplayer_die();\r\n\t};\r\n\t<span style=\"color: #888888\">\/\/Player went to next screen<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(mc_frombie.x<span style=\"color: #333333\">&gt;<\/span> (num_stageWidth <span style=\"color: #333333\">+<\/span> mc_frombie.width)){\r\n\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(bool_lastpanel){\r\n\t\t\tplayer_win();\r\n\t\t}<span style=\"color: #008800; font-weight: bold\">else<\/span>{\r\n\t\t\tconsole.log(<span style=\"background-color: #fff0f0\">&quot;next panel&quot;<\/span>);\r\n\t\t};\r\n\t};\r\n}\r\n<\/pre>\n<\/div>\n<p>Finished! Now we have the groundwork for a simple platformer game.<br \/>\nThis is stage one. Obviously it&#8217;s a work in progress, and I still need to optimize parts, but&#8230; small steps! :)<br \/>\nI&#8217;m now moving on to incorporating a few levels, and some of the features I want in the game&#8230; as well as WAY better artwork. :)<\/p>\n<p><a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Tutorial_HTML5_Platformer_Part1\/FaceTheMusic_Tutorial1.zip\" target=\"_blank\">DOWNLOAD SOURCE HERE<\/a><\/p>\n<p><a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Tutorial_HTML5_Platformer_Part1\/FaceTheMusic_Tutorial1.html\" target=\"_blank\">PLAY IT HERE<\/a><\/p>\n<p>Or check out my other tutorials:<br \/>\n<a href=\"http:\/\/www.nathalielawhead.com\/candybox\/tutorial-building-a-game-in-edge-animate-offender\" target=\"_self\">* Tutorial: Building A Game In Edge Animate (Offender)<\/a><br \/>\n<a href=\"http:\/\/www.nathalielawhead.com\/candybox\/tutorial-building-a-game-with-flash-html5-canvas-createjs-haxatron-2000\" target=\"_self\">* Tutorial: Building A Game With Flash HTML5 Canvas &#038; CreateJS (Haxatron 2000)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>DOWNLOAD SOURCE HERE I&#8217;ve been developing a totally new game for Tetrageddon. It&#8217;s going to be available for mobile (iOS, and Android), as well as the desktop. This is an HTML5 game (for a change), with CreateJS, using FlashCC&#8217;s HTML5 Canvas document. Doing so will make things easier for me animation wise, as well as managing complex animated &#8220;cut scenes&#8221;, and other graphical aspects&#8230; The game is codenamed &#8220;Face The Music&#8221; because it will essentially be about death and &#8220;giving up&#8221; &#8212; in a playful yet poetic pixel art style. :) Best of all, &#8220;Face The Music&#8221; is a platformer&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":4522,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","footnotes":"","_links_to":"","_links_to_target":""},"categories":[30,12],"tags":[],"class_list":["post-3140","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-games","category-resources"],"_links":{"self":[{"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/3140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/comments?post=3140"}],"version-history":[{"count":11,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/3140\/revisions"}],"predecessor-version":[{"id":3151,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/3140\/revisions\/3151"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/media\/4522"}],"wp:attachment":[{"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/media?parent=3140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/categories?post=3140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/tags?post=3140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}