{"id":3095,"date":"2014-04-24T21:59:51","date_gmt":"2014-04-25T04:59:51","guid":{"rendered":"http:\/\/www.nathalielawhead.com\/candybox\/?p=3095"},"modified":"2014-04-27T12:49:39","modified_gmt":"2014-04-27T19:49:39","slug":"tutorial-building-a-game-with-flash-html5-canvas-createjs-haxatron-2000","status":"publish","type":"post","link":"https:\/\/www.nathalielawhead.com\/candybox\/tutorial-building-a-game-with-flash-html5-canvas-createjs-haxatron-2000","title":{"rendered":"Tutorial: Building A Game With Flash HTML5 Canvas &#038; CreateJS (Haxatron 2000)"},"content":{"rendered":"<p><strong>Introduction:<\/strong><\/p>\n<p>Flash Professional CC introduced a document type in which you can create an HTML5 Canvas document. It publishes HTML5 content using the <a href=\"http:\/\/createjs.com\/\" target=\"_blank\">CreateJS<\/a> libraries (but you could easily use other libraries if you wanted to).<br \/>\nAlthough this feature has been out for a while, there is little to no information (at least I couldn&#8217;t find much), about how to make something more complex in it (like a game).<\/p>\n<p>I love this development because (from Adobe documentation) &#8220;Flash publishes to HTML5 by leveraging the Canvas API. It translates objects created on stage in to their Canvas counterparts.&#8221; Meaning, I don&#8217;t have to worry about re-animating everything to accommodate a new development environment, and I can continue to use the assets already created&#8230; as well as the tools.<br \/>\nThis is still so much easier than having to do everything through code (XML or other). <\/p>\n<p>In this tutorial I hope to provide a concise resource on building an HTML5 document in Flash.<br \/>\nI will be updating this post as things surface.<\/p>\n<p>* DOWNLOAD the source files here: <a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxatron2000\/HTML5\/Haxatron2000_HTML5_Tutorial.zip\" target=\"_blank\">Haxatron2000_HTML5_Tutorial.zip<\/a><\/p>\n<p>* Play the finished game here: <a href=\"http:\/\/haxatron.alienmelon.com\/browserhax\/\" target=\"_blank\">http:\/\/haxatron.alienmelon.com\/browserhax\/<\/a><\/p>\n<p>* Visit the Haxatron 2000 website here: <a href=\"http:\/\/haxatron.alienmelon.com\/\" target=\"_blank\">http:\/\/haxatron.alienmelon.com\/<\/a><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxatron2000\/HTML5\/tutorial\/tutorial_font_1.png\" alt=\"\" \/><\/p>\n<p><strong>Links about Flash and the HTML5 Canvas document type:<\/strong><\/p>\n<p>* <a href=\"http:\/\/www.adobe.com\/inspire\/2014\/03\/flash-pro-jigsaw.html\" target=\"_blank\">Build HTML5 Canvas interactive games with Flash Professional CC<\/a><br \/>\n* <a href=\"http:\/\/hemanthkumarr.wordpress.com\/2013\/12\/03\/what-is-this-html-canvas-document-in-flash-pro-cc-13-1\/\" target=\"_blank\">What is this &#8220;HTML Canvas&#8221; document in Flash Pro CC 13.1?<\/a><br \/>\n* <a href=\"http:\/\/helpx.adobe.com\/flash\/using\/creating-publishing-html5-canvas-document.html?PID=3662453\" target=\"_blank\">Flash Professional Help \/ Creating and publishing an HTML5 Canvas document<\/a><\/p>\n<p><strong>Other Resources:<\/strong><\/p>\n<p>From what I&#8217;ve seen Adobe&#8217;s tools are still the best when it comes to creating vector art\/content.<br \/>\nYes, I am biased, because it fits into my workflow so well. :)<br \/>\nNevertheless, some other resources regarding vector\/SVG tools are:<\/p>\n<p>* <a href=\"http:\/\/wiki.inkscape.org\/wiki\/index.php\/Animation-%28Timeline%29\" target=\"_blank\">A great list of SVG animation\/asset programs.<\/a><\/p>\n<p><strong>Some libraries to help get started with SVG:<\/strong><\/p>\n<p>* <a href=\"http:\/\/snapsvg.io\/\" target=\"_blank\">Snap.svg<\/a><br \/>\n&#8220;the Snap.svg JavaScript library makes working with your SVG assets as easy as jQuery makes working with the DOM.&#8221;<br \/>\n* <a href=\"http:\/\/raphaeljs.com\/\" target=\"_blank\">Rapha\u00ebl\u2014JavaScript Library<\/a><br \/>\n&#8220;Rapha\u00ebl is a small JavaScript library that should simplify your work with vector graphics on the web.&#8221;<br \/>\n* <a href=\"http:\/\/www.svgjs.com\/\" target=\"_blank\">SVG.jg<\/a><br \/>\n&#8220;A lightweight library for manipulating and animating SVG.&#8221;<\/p>\n<p>*************<\/p>\n<p><strong>Issues Encountered &#038; Resolved (Things To Look Out For):<\/strong><\/p>\n<p>Sounds loaded externally (via createjs.LoadQueue&#8230;) will fail in Safari, unless the sounds are also physically included in the library. Nevertheless, they DO work in Firefox, and Chrome without doing so.<\/p>\n<p>Sounds will also not work in Safari unless the quicktime plugin is installed (as per SoundJS documentation: <a href=\"http:\/\/createjs.com\/Docs\/SoundJS\/classes\/Sound.html\" target=\"_blank\">http:\/\/createjs.com\/Docs\/SoundJS\/classes\/Sound.html<\/a>).<\/p>\n<p>Frame numbers in EaselJS start at 0 instead of 1. For example, this affects gotoAndStop and gotoAndPlay calls.<\/p>\n<p>You cannot modify the generated .html page, it will be overwritten next time you publish in Flash (not safe, like in Edge Animate), and your changes will be lost. My solution was to do everything in javascript, and not touch the .html page.<\/p>\n<p>*************<\/p>\n<p><strong>SETTING UP THE DOCUMENT<\/strong><\/p>\n<p>All setting up and styling (css, scripts, style, etc&#8230;) is done in the document. I&#8217;ve kept it out of the generated .html page on purpose (reasons mentioned above). What I hope to do here is provide <em>as many examples as possible<\/em> in order to help point you (or a googler) in the right direction.<br \/>\nCreating a better structure is up to you.<\/p>\n<p>So, walking through the setup&#8230;<\/p>\n<p><code>@fontface<\/code> rule. Described in further detail in the <strong>Fonts<\/strong> section bellow.<\/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\">\/*JAVASCRIPT*\/<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> font_fixedsys <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">document<\/span>.createElement(<span style=\"background-color: #fff0f0\">&quot;style&quot;<\/span>);\r\nfont_fixedsys.appendChild(<span style=\"color: #007020\">document<\/span>.createTextNode(<span style=\"background-color: #fff0f0\">&quot;\\<\/span>\r\n<span style=\"background-color: #fff0f0\">@font-face {\\<\/span>\r\n<span style=\"background-color: #fff0f0\">    font-family: &#39;&quot;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&#39;fixedsys_excelsior_3&#39;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;&#39;;\\<\/span>\r\n<span style=\"background-color: #fff0f0\">\tsrc: url(&#39;&quot;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&#39;fontquirrels\/fsex300-webfont.eot?#iefix&#39;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;&#39;) format(&#39;embedded-opentype&#39;),\\<\/span>\r\n<span style=\"background-color: #fff0f0\">\t\t url(&#39;&quot;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&#39;fontquirrels\/fsex300-webfont.woff&#39;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;&#39;) format(&#39;woff&#39;),\\<\/span>\r\n<span style=\"background-color: #fff0f0\">\t     url(&#39;&quot;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&#39;fontquirrels\/fsex300-webfont.ttf&#39;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;&#39;) format(&#39;truetype&#39;),\\<\/span>\r\n<span style=\"background-color: #fff0f0\">\t\t url(&#39;&quot;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&#39;fontquirrels\/fsex300-webfont.svg#fixedsys_excelsior_3.01Rg&#39;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;&#39;) format(&#39;svg&#39;);\\<\/span>\r\n<span style=\"background-color: #fff0f0\">\tfont-weight: normal;\\<\/span>\r\n<span style=\"background-color: #fff0f0\">\tfont-style: normal;\\<\/span>\r\n<span style=\"background-color: #fff0f0\">}\\<\/span>\r\n<span style=\"background-color: #fff0f0\">&quot;<\/span>));\r\n\r\n<span style=\"color: #007020\">document<\/span>.head.appendChild(font_fixedsys);\r\n<\/pre>\n<\/div>\n<p>Including other javascript libraries is just as easy. You would use <code>document.getElementsByTagName(\"head\")[0].appendChild(script);<\/code> to do so.<br \/>\nFurther links on the topic are, <a href=\"http:\/\/stackoverflow.com\/questions\/1140402\/how-to-add-jquery-in-js-file\" target=\"_blank\">How to add jQuery in JS file<\/a>, <a href=\"http:\/\/stackoverflow.com\/questions\/3991492\/include-a-javascript-file-in-a-javascript-file\" target=\"_blank\">Include a JavaScript file in a JavaScript file<\/a>, <a href=\"http:\/\/stackoverflow.com\/questions\/950087\/how-to-include-a-javascript-file-in-another-javascript-file\" target=\"_blank\">How to include a JavaScript file in another JavaScript file?<\/a>, and <a href=\"http:\/\/stackoverflow.com\/questions\/16368973\/include-jquery-from-another-javascript-file\" target=\"_blank\">Include jQuery from another JavaScript file<\/a>.<\/p>\n<p>In this case, I am importing <code>keymaster.js<\/code> (<a href=\"https:\/\/github.com\/madrobby\/keymaster\" target=\"_blank\">https:\/\/github.com\/madrobby\/keymaster<\/a>) &#8212; a simple micro-library for defining and dispatching keyboard shortcuts in web applications. In case of a game it&#8217;s good because it will reduce cross browser issues with keyboard keys (to a certain extent). Example of usage is in the <strong>Keyboard<\/strong> section.<\/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\">\/\/Example of usage bellow in \/*KEYBOARD*\/ section<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> js_misc <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">document<\/span>.createElement(<span style=\"background-color: #fff0f0\">&quot;script&quot;<\/span>);\r\njs_misc.src <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;libs\/keymaster.js&quot;<\/span>;\r\njs_misc.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_misc);\r\n<\/pre>\n<\/div>\n<p>I am also controlling page elements, properties, style, etc&#8230;<br \/>\nThings like setting the background color of the page (and other styles).<br \/>\nFor more information refer to <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Traversing_an_HTML_table_with_JavaScript_and_DOM_Interfaces\" target=\"_blank\">Traversing an HTML table with JavaScript and DOM Interfaces<\/a><br \/>\nOr <a href=\"http:\/\/www.w3schools.com\/jsref\/dom_obj_style.asp\" target=\"_blank\">HTML DOM Style Object<\/a><br \/>\nAs a good explanation on what I&#8217;m doing.<\/p>\n<p>In this case, I am saving references to page body, and the canvas &#8212; see <code>var page_body<\/code> and <code>var page_canvas<\/code>.<br \/>\nThen I&#8217;m applying styles to them.<br \/>\nI&#8217;m referencing <code>page_canvas<\/code> in order to get the current height and width (useful for responsive designs, etc).<\/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\">var<\/span> page_body <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">document<\/span>.getElementsByTagName(<span style=\"background-color: #fff0f0\">&quot;body&quot;<\/span>)[<span style=\"color: #0000DD; font-weight: bold\">0<\/span>];\r\npage_body.style.backgroundColor <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;black&quot;<\/span>;\r\n<span style=\"color: #888888\">\/\/note, alternative would be: document.body.style.backgroundColor = &quot;black&quot;;<\/span>\r\npage_body.style.overflow <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;hidden&quot;<\/span>;<span style=\"color: #888888\">\/\/hide scrollbars<\/span>\r\npage_body.style.position <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;fixed&quot;<\/span>;<span style=\"color: #888888\">\/\/no scroll. ever.<\/span>\r\n\r\n<span style=\"color: #888888\">\/\/the canvas (this is the game)<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> page_canvas <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">document<\/span>.getElementsByTagName(<span style=\"background-color: #fff0f0\">&quot;canvas&quot;<\/span>)[<span style=\"color: #0000DD; font-weight: bold\">0<\/span>];\r\nstageWidth <span style=\"color: #333333\">=<\/span> page_canvas.width;\r\nstageHeight <span style=\"color: #333333\">=<\/span> page_canvas.height;\r\n<\/pre>\n<\/div>\n<p>Then I&#8217;m also displaying an &#8220;overlay&#8221; image above the page (scanlines).<br \/>\nAppending a div (or something else) above everything, and loading content into it (like an image), would look like,<\/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\">\/\/Scanlines (I like scanlines)<\/span>\r\n<span style=\"color: #888888\">\/\/alternatively, you should consider: http:\/\/createjs.com\/Docs\/EaselJS\/classes\/DOMElement.html<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> overlay <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">document<\/span>.createElement(<span style=\"background-color: #fff0f0\">&quot;div_overlay&quot;<\/span>);\r\n<span style=\"color: #888888\">\/\/make sure it&#39;s before the canvas - so inserting before<\/span>\r\n<span style=\"color: #007020\">document<\/span>.body.insertBefore(overlay, page_canvas);\r\n<span style=\"color: #888888\">\/\/create image and append overlay<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> img <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">document<\/span>.createElement(<span style=\"background-color: #fff0f0\">&quot;img&quot;<\/span>);\r\nimg.src <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;images\/IMG_Scalines_DARK.png&quot;<\/span>;\r\noverlay.appendChild(img);\r\n<span style=\"color: #888888\">\/\/place it above everything (zIndex)<\/span>\r\noverlay.style.position <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;absolute&quot;<\/span>;\r\noverlay.style.zIndex<span style=\"color: #333333\">=<\/span><span style=\"background-color: #fff0f0\">&quot;999&quot;<\/span>;\r\n<\/pre>\n<\/div>\n<p>ALTERNATIVELY, aside from all that I just mentioned, EaselJS comes with a <code>DOMElement Class<\/code>.<br \/>\nIt&#8217;s recommended you use that. See: <a href=\"http:\/\/createjs.com\/Docs\/EaselJS\/classes\/DOMElement.html\" target=\"_blank\">http:\/\/createjs.com\/Docs\/EaselJS\/classes\/DOMElement.html<\/a><\/p>\n<p>As a final note, we will want the page to be full screen, and preferably centered.<br \/>\nI picked two approaches to this which I thought would match common problems people might have\/wish to solve.<\/p>\n<p>Option #1 is for scaling the canvas proportionately (no centering). Thanks goes to <a href=\"http:\/\/community.createjs.com\/discussions\/createjs\/547-resizing-canvas-and-its-content-proportionally-cross-platform\" target=\"_blank\">CreateJS community support<\/a>, because no one likes reinventing the wheel. :) <\/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> onResize()\r\n{\r\n\t<span style=\"color: #888888\">\/\/OPTION 1:<\/span>\r\n\t<span style=\"color: #888888\">\/\/Scales proportionately<\/span>\r\n\t<span style=\"color: #888888\">\/\/ browser viewport size<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> w <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">window<\/span>.innerWidth;\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> h <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">window<\/span>.innerHeight;\r\n\t<span style=\"color: #888888\">\/\/ stage dimensions<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> ow <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">960<\/span>; <span style=\"color: #888888\">\/\/ your stage width<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> oh <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">640<\/span>; <span style=\"color: #888888\">\/\/ your stage height<\/span>\r\n\t<span style=\"color: #888888\">\/\/ keep aspect ratio<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> scale <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">Math<\/span>.min(w <span style=\"color: #333333\">\/<\/span> ow, h <span style=\"color: #333333\">\/<\/span> oh);\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> newHeight <span style=\"color: #333333\">=<\/span> ow <span style=\"color: #333333\">*<\/span> scale;\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> newWidth <span style=\"color: #333333\">=<\/span> oh <span style=\"color: #333333\">*<\/span> scale;\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\tstage.scaleX <span style=\"color: #333333\">=<\/span> scale;\r\n\tstage.scaleY <span style=\"color: #333333\">=<\/span> scale;\r\n\t<span style=\"color: #888888\">\/\/ adjust canvas size<\/span>\r\n\tstage.canvas.width <span style=\"color: #333333\">=<\/span> newHeight;\r\n\tstage.canvas.height <span style=\"color: #333333\">=<\/span> newWidth;\r\n\t<span style=\"color: #888888\">\/\/set width and height variables again<\/span>\r\n\t<span style=\"color: #888888\">\/\/var page_canvas = document.getElementsByTagName(&quot;canvas&quot;)[0];<\/span>\r\n\t<span style=\"color: #888888\">\/\/stageWidth = page_canvas.width;<\/span>\r\n\t<span style=\"color: #888888\">\/\/stageHeight = page_canvas.height;<\/span>\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #888888\">\/\/ update the stage<\/span>\r\n\tstage.update()\r\n}\r\n<span style=\"color: #888888\">\/\/<\/span>\r\n<span style=\"color: #007020\">window<\/span>.onresize <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">function<\/span>()\r\n{\r\n     onResize();\r\n}\r\n<span style=\"color: #888888\">\/\/call it on first run<\/span>\r\nonResize();\r\n<\/pre>\n<\/div>\n<p>Then there&#8217;s Option 2. This resizes and centers everything.<br \/>\nFor more information on doing this refer to <a href=\"http:\/\/stackoverflow.com\/questions\/13368724\/how-to-dynamically-resize-and-center-a-flash-createjs-canvas-element-animation\" target=\"_blank\">How to dynamically resize and center a Flash CreateJS canvas element animation<\/a> &#8211;Which is a great discussion with more tips.<\/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> onResize()\r\n{\r\n\t<span style=\"color: #888888\">\/\/OPTION 2: scale and center<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> widthToHeight <span style=\"color: #333333\">=<\/span> stageWidth <span style=\"color: #333333\">\/<\/span> stageHeight;\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> newWidth <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">window<\/span>.innerWidth;\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> newHeight <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">window<\/span>.innerHeight;\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> newWidthToHeight <span style=\"color: #333333\">=<\/span> newWidth <span style=\"color: #333333\">\/<\/span> newHeight;\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (newWidthToHeight <span style=\"color: #333333\">&gt;<\/span> widthToHeight)\r\n\t{\r\n\t\tnewWidth <span style=\"color: #333333\">=<\/span> newHeight <span style=\"color: #333333\">*<\/span> widthToHeight;\r\n\t\tpage_canvas.style.height <span style=\"color: #333333\">=<\/span> newHeight <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;px&quot;<\/span>;\r\n\t\tpage_canvas.style.width <span style=\"color: #333333\">=<\/span> newWidth <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;px&quot;<\/span>;\r\n\t} <span style=\"color: #008800; font-weight: bold\">else<\/span> {\r\n\t\tnewHeight <span style=\"color: #333333\">=<\/span> newWidth <span style=\"color: #333333\">\/<\/span> widthToHeight;\r\n\t\tpage_canvas.style.height <span style=\"color: #333333\">=<\/span> newHeight <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;px&quot;<\/span>;\r\n\t\tpage_canvas.style.width <span style=\"color: #333333\">=<\/span> newWidth <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;px&quot;<\/span>;\r\n\t}\r\n\t\tscale <span style=\"color: #333333\">=<\/span> newWidthToHeight <span style=\"color: #333333\">\/<\/span> widthToHeight;\r\n\t\tstage.width <span style=\"color: #333333\">=<\/span> newWidth;\r\n\t\tstage.height <span style=\"color: #333333\">=<\/span> newHeight;\r\n\t\tpage_canvas.style.marginTop <span style=\"color: #333333\">=<\/span> ((<span style=\"color: #007020\">window<\/span>.innerHeight <span style=\"color: #333333\">-<\/span> newHeight) <span style=\"color: #333333\">\/<\/span> <span style=\"color: #0000DD; font-weight: bold\">2<\/span>) <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;px&quot;<\/span>;\r\n\t\tpage_canvas.style.marginLeft <span style=\"color: #333333\">=<\/span> ((<span style=\"color: #007020\">window<\/span>.innerWidth <span style=\"color: #333333\">-<\/span> newWidth) <span style=\"color: #333333\">\/<\/span> <span style=\"color: #0000DD; font-weight: bold\">2<\/span>) <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;px&quot;<\/span>;\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n}\r\n<span style=\"color: #888888\">\/\/<\/span>\r\n<span style=\"color: #007020\">window<\/span>.onresize <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">function<\/span>()\r\n{\r\n     onResize();\r\n}\r\n<span style=\"color: #888888\">\/\/call on first run<\/span>\r\nonResize();\r\n<\/pre>\n<\/div>\n<p>*************<\/p>\n<p><strong>FONTS, TEXT FIELDS &#038; EFFECTS<\/strong><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxatron2000\/HTML5\/tutorial\/haxatron2000.png\" alt=\"\" \/><\/p>\n<p>For starters, the link to the <code>Text Class<\/code>:<br \/>\n<a href=\"http:\/\/createjs.com\/Docs\/EaselJS\/classes\/Text.html\" target=\"_blank\">http:\/\/createjs.com\/Docs\/EaselJS\/classes\/Text.html<\/a><\/p>\n<p>There are a few ways of getting fonts to work in your project. It isn&#8217;t as simple as &#8220;publishing&#8221; your project. The font will not show for visitors\/players unless that font is installed on their machine.<br \/>\nSo you can use a couple of methods to get that to work.<\/p>\n<p>You can use <a href=\"http:\/\/www.w3schools.com\/cssref\/css3_pr_font-face_rule.asp\" target=\"_blank\"><code>@fontface<\/code><\/a>.<br \/>\nIn this tutorial that is what I&#8217;m doing. The font I&#8217;m using is <a href=\"http:\/\/en.wikipedia.org\/wiki\/Fixedsys_Excelsior\" target=\"_blank\">Fixedsys Excelsior<\/a>, as generated from <a href=\"http:\/\/www.fontsquirrel.com\/\" target=\"_blank\">fontsquirrel<\/a> with some fall back fonts (<a href=\"http:\/\/www.w3schools.com\/cssref\/css_websafe_fonts.asp\" target=\"_blank\">monaco, lucidia console<\/a>, etc&#8230;).<br \/>\nNote that <code>@fontface<\/code> may fail if the font is not loaded when it&#8217;s drawn.<\/p>\n<p>You can also use Bitmap Text (more advisable for games, but unsupported). A tutorial for implementing this is:<br \/>\n<a href=\"http:\/\/www.dehats.com\/drupal\/node\/116\" target=\"_blank\">Bitmap Font Support for CreateJS<\/a><\/p>\n<p>Or web fonts:<br \/>\n<a href=\"http:\/\/www.createjs.com\/tutorials\/Fonts\/\" target=\"_blank\">http:\/\/www.createjs.com\/tutorials\/Fonts\/<\/a><\/p>\n<p>Like I mentioned earlier, modifying the generated .html page will result in your changes being lost next time you publish. To get around this I challenged myself to not touching the .html page, and relying on javascript for everything.<br \/>\nThere are a number of ways of doing this (for example <a href=\"http:\/\/jonraasch.com\/blog\/javascript-style-node\" target=\"_blank\">Appending Style Nodes with Javascript<\/a>, or like a <a href=\"https:\/\/www.google.com\/search?q=javascript+append+to+document+head\" target=\"_blank\">simple google search<\/a> would show).<br \/>\nI&#8217;m certain there are better ways, but I&#8217;m doing it this way.<\/p>\n<p>Following is the <code>@fontface<\/code> rule:<\/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\">var<\/span> font_fixedsys <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">document<\/span>.createElement(<span style=\"background-color: #fff0f0\">&quot;style&quot;<\/span>);\r\nfont_fixedsys.appendChild(<span style=\"color: #007020\">document<\/span>.createTextNode(<span style=\"background-color: #fff0f0\">&quot;\\<\/span>\r\n<span style=\"background-color: #fff0f0\">@font-face {\\<\/span>\r\n<span style=\"background-color: #fff0f0\">    font-family: &#39;&quot;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&#39;fixedsys_excelsior_3&#39;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;&#39;;\\<\/span>\r\n<span style=\"background-color: #fff0f0\">\tsrc: url(&#39;&quot;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&#39;fontquirrels\/fsex300-webfont.eot?#iefix&#39;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;&#39;) format(&#39;embedded-opentype&#39;),\\<\/span>\r\n<span style=\"background-color: #fff0f0\">\t\t url(&#39;&quot;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&#39;fontquirrels\/fsex300-webfont.woff&#39;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;&#39;) format(&#39;woff&#39;),\\<\/span>\r\n<span style=\"background-color: #fff0f0\">\t     url(&#39;&quot;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&#39;fontquirrels\/fsex300-webfont.ttf&#39;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;&#39;) format(&#39;truetype&#39;),\\<\/span>\r\n<span style=\"background-color: #fff0f0\">\t\t url(&#39;&quot;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&#39;fontquirrels\/fsex300-webfont.svg#fixedsys_excelsior_3.01Rg&#39;<\/span> <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;&#39;) format(&#39;svg&#39;);\\<\/span>\r\n<span style=\"background-color: #fff0f0\">\tfont-weight: normal;\\<\/span>\r\n<span style=\"background-color: #fff0f0\">\tfont-style: normal;\\<\/span>\r\n<span style=\"background-color: #fff0f0\">}\\<\/span>\r\n<span style=\"background-color: #fff0f0\">&quot;<\/span>));\r\n\r\n<span style=\"color: #007020\">document<\/span>.head.appendChild(font_fixedsys);\r\n<\/pre>\n<\/div>\n<p>This gets the font ready to use. You could use just one source, but most recommended formats are included in this case.<\/p>\n<p>There are going to be many text fields, so I&#8217;m saving the properties as variables.<\/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\">\/\/text field properties<\/span>\r\ntxtCol <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;#00FF00&quot;<\/span>;<span style=\"color: #888888\">\/\/the text color<\/span>\r\ntxtFont <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;26px &#39;fixedsys_excelsior_3&#39;, &#39;Lucida Console&#39;, Monaco, monospace&quot;<\/span>; <span style=\"color: #888888\">\/\/large text field<\/span>\r\ntxtFont_sml <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;15px &#39;fixedsys_excelsior_3&#39;, &#39;Lucida Console&#39;, Monaco, monospace&quot;<\/span>; <span style=\"color: #888888\">\/\/small subtext<\/span>\r\n<\/pre>\n<\/div>\n<p>And now create the text field, with the font.<\/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 text fields<\/span>\r\ntxt_title <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> createjs.Text(<span style=\"background-color: #fff0f0\">&quot;Haxatron 2000&quot;<\/span>, txtFont, txtCol);\r\ntxt_title.x <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">384<\/span>;\r\ntxt_title.y <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">197<\/span>;\r\n<span style=\"color: #008800; font-weight: bold\">this<\/span>.addChild(txt_title); <span style=\"color: #888888\">\/\/add to stage<\/span>\r\n<span style=\"color: #888888\">\/\/shadow is much like glow, so I&#39;ll use shadow<\/span>\r\n<span style=\"color: #888888\">\/\/color String = The color of the shadow.<\/span>\r\n<span style=\"color: #888888\">\/\/offsetX Number = The x offset of the shadow in pixels.<\/span>\r\n<span style=\"color: #888888\">\/\/offsetY Number = The y offset of the shadow in pixels.<\/span>\r\n<span style=\"color: #888888\">\/\/blur Number = The size of the blurring effect.<\/span>\r\n<span style=\"color: #888888\">\/\/See: http:\/\/www.createjs.com\/Docs\/EaselJS\/classes\/Shadow.html for more<\/span>\r\ntxt_title.shadow <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> createjs.Shadow(<span style=\"background-color: #fff0f0\">&quot;#00FF00&quot;<\/span>, <span style=\"color: #0000DD; font-weight: bold\">3<\/span>, <span style=\"color: #0000DD; font-weight: bold\">0<\/span>, <span style=\"color: #0000DD; font-weight: bold\">10<\/span>);\r\n<\/pre>\n<\/div>\n<p>Here the field is <code>txt_title<\/code>.<br \/>\nThe game has a glow filter applied to all text fields. CreateJS does not have a glow filter, so I&#8217;m using shadow (which is similar).<br \/>\nSee: <a href=\"http:\/\/www.createjs.com\/Docs\/EaselJS\/classes\/Shadow.html\" target=\"_blank\">http:\/\/www.createjs.com\/Docs\/EaselJS\/classes\/Shadow.html<\/a> for more information.<\/p>\n<p><em>Text Effects (Write Out):<\/em><\/p>\n<p>The game writes out text to the created text fields. It&#8217;s a cute, and classic effect.<br \/>\nThe function is a simple one,<\/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\">\/\/Write out text<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> writeInt;\r\nwriteText <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">function<\/span>(myText, myTextField) {\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> i <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>;\r\n\t<span style=\"color: #008800; font-weight: bold\">function<\/span> write() {\r\n\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (i<span style=\"color: #333333\">&lt;=<\/span>myText.length) {\r\n\t\t\tmyTextField.text <span style=\"color: #333333\">=<\/span> myText.substr(<span style=\"color: #0000DD; font-weight: bold\">0<\/span>, i)<span style=\"color: #333333\">+<\/span><span style=\"background-color: #fff0f0\">&quot;_&quot;<\/span>;\r\n\t\t\trandTypeSnd();\r\n\t\t\ti <span style=\"color: #333333\">=<\/span> i<span style=\"color: #333333\">+<\/span><span style=\"color: #0000DD; font-weight: bold\">1<\/span>;\r\n\t\t} <span style=\"color: #008800; font-weight: bold\">else<\/span> {\r\n\t\t\tmyTextField.text <span style=\"color: #333333\">=<\/span> myText.substr(<span style=\"color: #0000DD; font-weight: bold\">0<\/span>, i);\r\n\t\t\tclearInterval(writeInt);\r\n\t\t}\r\n\t}\r\n\tclearInterval(writeInt);\r\n\twriteInt <span style=\"color: #333333\">=<\/span> setInterval(write, <span style=\"color: #0000DD; font-weight: bold\">40<\/span>);\r\n}\r\n<\/pre>\n<\/div>\n<p>To use it you call, <code>writeText(\"My text as string.\", txt_myTextField);<\/code><br \/>\nThis function, and most of the game, uses <a href=\"http:\/\/createjs.com\/Docs\/EaselJS\/classes\/Ticker.html#method_setInterval\" target=\"_blank\"><code>setInterval<\/code><\/a> a lot, so as to not deviate from the original code too much. For other options see the Ticker Class: <a href=\"http:\/\/createjs.com\/Docs\/EaselJS\/classes\/Ticker.html\" target=\"_blank\">http:\/\/createjs.com\/Docs\/EaselJS\/classes\/Ticker.html<\/a><br \/>\nIn the above example, as long as <code>i<\/code> is less than the <code>length<\/code> of your string continue writing. Increment <code>i<\/code> with every letter written.<br \/>\nWith each character added to the text field, a random typing sound plays. That&#8217;s what <code>randTypeSnd();<\/code> is.<\/p>\n<p><em>Clearing Text Field (Scene Change):<\/em><\/p>\n<p>The game is keyboard only. Once you&#8217;ve made your selection from any of the menus, the text fields need to be cleared before proceeding to the next screen. There is one function called <code>clearText<\/code> responsible for this&#8230; Which is as simple as,<\/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\">\/\/clearing all text fields<\/span>\r\n<span style=\"color: #888888\">\/\/for going between scenes<\/span>\r\nclearText <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">function<\/span>(){\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> arr_fields <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> <span style=\"color: #007020\">Array<\/span>(txt_title, txt_start, txt_faq, txt_msg, txt_msg_win, txt_stats_win, txt_seconds_win, txt_tries_win, txt_options_win, txt_option1_win, txt_option2_win);\r\n\t<span style=\"color: #008800; font-weight: bold\">for<\/span>(i<span style=\"color: #333333\">=<\/span><span style=\"color: #0000DD; font-weight: bold\">0<\/span>; i<span style=\"color: #333333\">&lt;<\/span>arr_fields.length; <span style=\"color: #333333\">++<\/span>i){\r\n\t\tarr_fields[i].text <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;&quot;<\/span>;\r\n\t};\r\n}\r\n<\/pre>\n<\/div>\n<p>And that&#8217;s it for text&#8230;<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxatron2000\/HTML5\/tutorial\/tutorial_font.png\" alt=\"\" \/><\/p>\n<p>*************<\/p>\n<p><strong>SOUND<\/strong><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxatron2000\/HTML5\/tutorial\/tutorial_sounds.png\" alt=\"\" \/><\/p>\n<p>Two things that are important. They are the <a href=\"http:\/\/www.createjs.com\/Docs\/SoundJS\/modules\/SoundJS.html\" target=\"_blank\">SoundJS<\/a> and <a href=\"http:\/\/www.createjs.com\/Docs\/PreloadJS\/modules\/PreloadJS.html\" target=\"_blank\">PreloadJS<\/a> libraries. If you&#8217;re familiar with HTML5 development, then you&#8217;re familiar with the messiness that is cross-browser audio support. SoundJS solves most of that.<br \/>\nTwo great articles are:<br \/>\n<a href=\"http:\/\/createjs.com\/tutorials\/Mobile%20Safe%20Approach\/\" target=\"_blank\">SoundJS: Mobile Safe Approach<\/a><br \/>\nand <a href=\"http:\/\/createjs.com\/tutorials\/SoundJS%20and%20PreloadJS\/\" target=\"_blank\">SoundJS: Preloading Audio<\/a><br \/>\nThey cover this pretty well.<br \/>\nThe documentation for SoundJS is here <a href=\"http:\/\/www.createjs.com\/Docs\/SoundJS\/modules\/SoundJS.html\" target=\"_blank\">http:\/\/www.createjs.com\/Docs\/SoundJS\/modules\/SoundJS.html<\/a><br \/>\nCheck out the examples here (sourcecode): <a href=\"https:\/\/github.com\/CreateJS\/SoundJS\/tree\/master\/examples\" target=\"_blank\">https:\/\/github.com\/CreateJS\/SoundJS\/tree\/master\/examples<\/a><br \/>\nAnd here is a great example of use in a game: <a href=\"https:\/\/github.com\/CreateJS\/SoundJS\/blob\/master\/examples\/Game.html\" target=\"_blank\">https:\/\/github.com\/CreateJS\/SoundJS\/blob\/master\/examples\/Game.html<\/a><\/p>\n<p>Note, sounds will not play in Safari unless they are also included in the library (if you&#8217;re working with Flash CC). This does not happen in Chrome or Firefox. Sounds can be loaded from an external source without that problem in both those browsers.<br \/>\nSounds in Safari also require the quicktime plugin to be installed in order to play.<\/p>\n<p>Following is a breakdown of the VERY basics of playing a sound:<\/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\">\/\/to play a sound that&#39;s in the library<\/span>\r\ncreatejs.Sound.play(<span style=\"background-color: #fff0f0\">&quot;Die&quot;<\/span>);\r\n\r\n<span style=\"color: #888888\">\/\/to play a sound that&#39;s in a library, and use again<\/span>\r\n<span style=\"color: #888888\">\/\/AUD_Die is the reference.<\/span>\r\nAUD_Die <span style=\"color: #333333\">=<\/span> createjs.Sound.play(<span style=\"background-color: #fff0f0\">&quot;sounds\/Die.mp3&quot;<\/span>);<span style=\"color: #888888\">\/\/call once<\/span>\r\n<span style=\"color: #888888\">\/\/play anytime after that<\/span>\r\nAUD_Die.play();\r\n\r\n<span style=\"color: #888888\">\/\/to loop a sound<\/span>\r\n<span style=\"color: #888888\">\/\/http:\/\/createjs.com\/Docs\/SoundJS\/classes\/SoundInstance.html<\/span>\r\n<span style=\"color: #888888\">\/*Once a SoundInstance is created, <\/span>\r\n<span style=\"color: #888888\">a reference can be stored that can be used <\/span>\r\n<span style=\"color: #888888\">to control the audio directly through the SoundInstance. <\/span>\r\n<span style=\"color: #888888\">If the reference is not stored, <\/span>\r\n<span style=\"color: #888888\">the SoundInstance will play out its audio (and any loops), <\/span>\r\n<span style=\"color: #888888\">and is then de-referenced from the Sound class*\/<\/span>\r\n<span style=\"color: #888888\">\/\/<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> myInstance <span style=\"color: #333333\">=<\/span> createjs.Sound.play(<span style=\"background-color: #fff0f0\">&quot;sounds\/Die.mp3&quot;<\/span>, {loop<span style=\"color: #333333\">:<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>});<span style=\"color: #888888\">\/\/loop:NUM = number of times to loop<\/span>\r\nmyInstance.addEventListener(<span style=\"background-color: #fff0f0\">&quot;loop&quot;<\/span>, handleLoop);\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> handleLoop(event) {\r\n\tmyInstance.volume <span style=\"color: #333333\">=<\/span> myInstance.volume <span style=\"color: #333333\">*<\/span> <span style=\"color: #6600EE; font-weight: bold\">0.5<\/span>;<span style=\"color: #888888\">\/\/fades out...<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>For more detail, see the Sound docs: <a href=\"http:\/\/www.createjs.com\/Docs\/SoundJS\/classes\/Sound.html\" target=\"_blank\">http:\/\/www.createjs.com\/Docs\/SoundJS\/classes\/Sound.html<\/a><\/p>\n<p>Sound can be used as a plugin with PreloadJS to help preload audio properly. When it&#8217;s preloaded with PreloadJS it is automatically registered. If sound is not preloaded from the outset it will do an internal load, as a result audio may not play immediately the first time you call it. It is recommended that you preload all audio.<br \/>\nThis is what I&#8217;m doing. All audio is loaded externally from the &#8220;<code>sounds<\/code>&#8221; folder, and preloaded. <\/p>\n<p>First I save the <code>assetPath<\/code> (directory), and then make a <code>manifest<\/code> &#8212; register them for loading and playback.<br \/>\n<code>id<\/code> = the name you want to give them.<br \/>\n<code>src<\/code> = the filename.<br \/>\nThe last line, <code>var queue<\/code>, creates a new <code>LoadQueue<\/code> instance.<br \/>\n<a href=\"http:\/\/createjs.com\/Docs\/PreloadJS\/classes\/LoadQueue.html\" target=\"_blank\"><code>LoadQueue<\/code><\/a> can preload either a single file, or queue of files. Here it&#8217;s many files.<\/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\">\/\/based on: http:\/\/createjs.com\/tutorials\/SoundJS%20and%20PreloadJS\/<\/span>\r\n<span style=\"color: #888888\">\/\/assetsPath = A path that will be prepended on to the <\/span>\r\n<span style=\"color: #888888\">\/\/source parameter of all items in the queue before they are loaded.<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> assetsPath <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;sounds\/&quot;<\/span>;\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> snd_manifest <span style=\"color: #333333\">=<\/span> [\r\n\t{id<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;Die&quot;<\/span>, src<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;Die.ogg&quot;<\/span>},\r\n\t{id<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;Intro&quot;<\/span>, src<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;Intro.ogg&quot;<\/span>},\r\n\t{id<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;Move1&quot;<\/span>, src<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;Move1.ogg&quot;<\/span>},\r\n\t{id<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;Move2&quot;<\/span>, src<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;Move2.ogg&quot;<\/span>},\r\n\t{id<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;Spawn&quot;<\/span>, src<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;Spawn.ogg&quot;<\/span>},\r\n\t{id<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;TransitionNewScene&quot;<\/span>, src<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;TransitionNewScene.ogg&quot;<\/span>},\r\n\t{id<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;Aud_Type1&quot;<\/span>, src<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;Typing1.ogg&quot;<\/span>},\r\n\t{id<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;Aud_Type2&quot;<\/span>, src<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;Typing2.ogg&quot;<\/span>},\r\n\t{id<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;Aud_Type3&quot;<\/span>, src<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;TypingNewline.ogg&quot;<\/span>},\r\n\t{id<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;TypingSelect&quot;<\/span>, src<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;TypingSelect.ogg&quot;<\/span>},\r\n\t{id<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;Win&quot;<\/span>, src<span style=\"color: #333333\">:<\/span><span style=\"background-color: #fff0f0\">&quot;Win.ogg&quot;<\/span>}\r\n];\r\n\r\n<span style=\"color: #888888\">\/\/[useXHR=true], [basePath=&quot;&quot;]<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> queue <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> createjs.LoadQueue(<span style=\"color: #008800; font-weight: bold\">true<\/span>, assetsPath);\r\n<\/pre>\n<\/div>\n<p>As you&#8217;ve noticed I&#8217;m specifying <code>.ogg<\/code> as the default sound format.<br \/>\nSoundJS will only preload audio that is supported (by the users browser). If multiple formats are provided, only the one that the browser can play will be preloaded.<br \/>\nYou can specify other alternate extensions via <code>alternateExtensions<\/code>. See: <a href=\"http:\/\/www.createjs.com\/Docs\/SoundJS\/classes\/Sound.html#property_alternateExtensions\" target=\"_blank\">http:\/\/www.createjs.com\/Docs\/SoundJS\/classes\/Sound.html#property_alternateExtensions<\/a><br \/>\nIt&#8217;s an array of extensions to attempt to use when loading sound, if the default is unsupported by the active plugin.<br \/>\nI&#8217;m giving <code>mp3<\/code>, and <code>wav<\/code> as alternatives.<\/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\">\/\/alternateExtensions = array of extensions to attempt to use when loading sound<\/span>\r\ncreatejs.Sound.alternateExtensions <span style=\"color: #333333\">=<\/span> [<span style=\"background-color: #fff0f0\">&quot;mp3&quot;<\/span>, <span style=\"background-color: #fff0f0\">&quot;wav&quot;<\/span>];\r\n\r\n<span style=\"color: #888888\">\/\/Install the SoundJS sound class -- to load the HTML audio. <\/span>\r\n<span style=\"color: #888888\">\/\/Should be installed before loading any audio files.<\/span>\r\nqueue.installPlugin(createjs.Sound);\r\n<\/pre>\n<\/div>\n<p>PreloadJS supports the following listeners:<br \/>\n<code>complete<\/code>: fired when a queue completes loading all files<br \/>\n<code>error<\/code>: fired when the queue encounters an error with any file.<br \/>\n<code>progress<\/code>: Progress for the entire queue has changed.<br \/>\n<code>fileload<\/code>: A single file has completed loading.<br \/>\n<code>fileprogress<\/code>: Progress for a single file has changes. Note that only files loaded with <code>XHR<\/code> (or possibly by plugins) will fire progress events other than 0 or 100%.<\/p>\n<p>I have two. One for when the load is <code>complete<\/code> (which plays &#8212; continues), and the other to keep track of the loading <code>progress<\/code> (in this case it just sends to <code>console.log<\/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\">\/\/two event listeners - one when load is complete, and the other to display the load progress<\/span>\r\nqueue.addEventListener(<span style=\"background-color: #fff0f0\">&quot;complete&quot;<\/span>, handleComplete);\r\nqueue.addEventListener(<span style=\"background-color: #fff0f0\">&quot;progress&quot;<\/span>, handleProgress);\r\n<\/pre>\n<\/div>\n<p><code>Load manifest<\/code> is to load multiple sounds\/a set of them. See: <a href=\"http:\/\/createjs.com\/Docs\/PreloadJS\/classes\/LoadQueue.html#method_loadManifest\" target=\"_blank\">http:\/\/createjs.com\/Docs\/PreloadJS\/classes\/LoadQueue.html#method_loadManifest<\/a><\/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%\">queue.loadManifest(snd_manifest);\r\n<span style=\"color: #888888\">\/\/to load an individual file:<\/span>\r\n<span style=\"color: #888888\">\/\/queue.loadFile({id:&quot;Die&quot;, src:&quot;sounds\/Die.ogg&quot;});<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> handleComplete(evt) {\r\n\tconsole.log(<span style=\"background-color: #fff0f0\">&quot;Loading sound complete.&quot;<\/span>);\r\n\tplaySound(<span style=\"background-color: #fff0f0\">&quot;Spawn&quot;<\/span>);<span style=\"color: #888888\">\/\/&quot;LOADED&quot; sound indication<\/span>\r\n\ttxt_title.text <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;&quot;<\/span>;\r\n\t<span style=\"color: #888888\">\/\/play the timeline after loading (continue)<\/span>\r\n\t_stage.play();\r\n}\r\n\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> handleProgress(evt) {\r\n\t<span style=\"color: #888888\">\/\/console.log(&quot;Event Loading: &quot; + (queue.progress.toFixed(2) * 100) + &quot;%&quot;);<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>Finally, I have two functions. One for playing a sound. The other for playing a sound loop.<br \/>\nIf you wanted to play a sound, now all you would have to do is call <code>playSound(\"SOUND_NAME\");<\/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: #008800; font-weight: bold\">function<\/span> playSound(name) {\r\n\t<span style=\"color: #008800; font-weight: bold\">return<\/span> createjs.Sound.play(name);\r\n}\r\n\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> playSoundLoop(name, loops){\r\n\t<span style=\"color: #008800; font-weight: bold\">return<\/span> createjs.Sound.play(name, {loop<span style=\"color: #333333\">:<\/span>loops});\r\n};\r\n<\/pre>\n<\/div>\n<p>The game has this cute little typing effect. Whenever a word gets typed, a random type sound plays.<br \/>\nThe function for that 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\">\/\/set up an array of typing sounds<\/span>\r\nArr_TypingSnds <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> <span style=\"color: #007020\">Array<\/span>(<span style=\"background-color: #fff0f0\">&quot;Aud_Type1&quot;<\/span>, <span style=\"background-color: #fff0f0\">&quot;Aud_Type2&quot;<\/span>);\r\n<span style=\"color: #888888\">\/\/chose a random typing sound and play it<\/span>\r\n<span style=\"color: #888888\">\/\/for text writing<\/span>\r\nrandTypeSnd <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">function<\/span>() {\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> randNum <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">Math<\/span>.ceil(<span style=\"color: #007020\">Math<\/span>.random()<span style=\"color: #333333\">*<\/span>(Arr_TypingSnds.length))<span style=\"color: #333333\">-<\/span><span style=\"color: #0000DD; font-weight: bold\">1<\/span>;\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> randomSound <span style=\"color: #333333\">=<\/span> Arr_TypingSnds[randNum];\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(randomSound<span style=\"color: #333333\">==<\/span><span style=\"color: #008800; font-weight: bold\">undefined<\/span>){\r\n\t\trandomSound <span style=\"color: #333333\">=<\/span> Arr_TypingSnds[<span style=\"color: #0000DD; font-weight: bold\">0<\/span>];\r\n\t};\r\n\tplaySound(randomSound);\r\n}\r\n<\/pre>\n<\/div>\n<p>*************<\/p>\n<p><strong>KEYBOARD CONTROLS<\/strong><\/p>\n<p>I have two examples here.<br \/>\nIf problems arise, for example, numbers may differ cross browsers, I would recommend using <code>keymaster.js<\/code> (<a href=\"https:\/\/github.com\/madrobby\/keymaster\" target=\"_blank\">https:\/\/github.com\/madrobby\/keymaster<\/a>) over the &#8220;classic&#8221; version.<br \/>\n<code>keymaster.js<\/code> is the javascript library we appended earlier.<\/p>\n<p>Following is how you would implement <code>keymaster.js<\/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\">\/\/keymaster.js keyboard:<\/span>\r\n<span style=\"color: #888888\">\/\/create ticker: http:\/\/createjs.com\/Docs\/EaselJS\/classes\/Ticker.html<\/span>\r\n<span style=\"color: #888888\">\/\/to handle keyboard input<\/span>\r\n\r\ncreatejs.Ticker.addEventListener(<span style=\"background-color: #fff0f0\">&quot;tick&quot;<\/span>, keyboard_ticker);\r\n\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> keyboard_ticker(event) {\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\tconsole.log(<span style=\"background-color: #fff0f0\">&quot;UP pressed&quot;<\/span>);\r\n\t}\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>)) {\r\n\t\tconsole.log(<span style=\"background-color: #fff0f0\">&quot;DOWN pressed&quot;<\/span>);\r\n\t}\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\t\tconsole.log(<span style=\"background-color: #fff0f0\">&quot;LEFT pressed&quot;<\/span>);\r\n\t}\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\tconsole.log(<span style=\"background-color: #fff0f0\">&quot;RIGHT pressed&quot;<\/span>);\r\n\t}\r\n}\r\n<\/pre>\n<\/div>\n<p>You can see, it&#8217;s a lot simpler.<br \/>\nI am using the &#8220;normal&#8221; keyboard functionality (<code>document.onkeydown<\/code> and <code>document.onkeyup<\/code>).<br \/>\nOther good examples of (similar) keyboard use in createjs games are:<br \/>\n<a href=\"https:\/\/github.com\/CreateJS\/EaselJS\/blob\/master\/examples\/Game.html\" target=\"_blank\">https:\/\/github.com\/CreateJS\/EaselJS\/blob\/master\/examples\/Game.html<\/a><br \/>\nand <a href=\"https:\/\/github.com\/javierj\/EaselJS\/blob\/master\/KeyboardDemo\/DemoKeyboard.html\" target=\"_blank\">https:\/\/github.com\/javierj\/EaselJS\/blob\/master\/KeyboardDemo\/DemoKeyboard.html<\/a><\/p>\n<p>So, first set up the key codes:<\/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_ENTER <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">13<\/span>;\r\nKEY_SPACE <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">32<\/span>;\r\nKEY_ESC <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">27<\/span>;\r\nKEY_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_LEFT <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">37<\/span>;\r\nKEY_RIGHT <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">39<\/span>;\r\nKEY_W <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">87<\/span>;\r\nKEY_A <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">65<\/span>;\r\nKEY_S <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">83<\/span>;\r\nKEY_D <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">68<\/span>;\r\n<\/pre>\n<\/div>\n<p>And then create the key down event:<\/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%\">evnt_KEYDOWN <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">function<\/span>(e){\r\n\t<span style=\"color: #888888\">\/\/cross browser issues exist<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(<span style=\"color: #333333\">!<\/span>e){ <span style=\"color: #008800; font-weight: bold\">var<\/span> e <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">window<\/span>.event; }\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\tconsole.log(<span style=\"background-color: #fff0f0\">&quot;press &quot;<\/span> <span style=\"color: #333333\">+<\/span> e.keyCode);\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(_scene <span style=\"color: #333333\">==<\/span> <span style=\"background-color: #fff0f0\">&quot;faq&quot;<\/span>){\r\n\t\t<span style=\"color: #888888\">\/\/escape - back to menu<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(e.keyCode <span style=\"color: #333333\">==<\/span> KEY_ESC <span style=\"color: #333333\">||<\/span> e.keyCode <span style=\"color: #333333\">==<\/span> KEY_SPACE <span style=\"color: #333333\">||<\/span> e.keyCode <span style=\"color: #333333\">==<\/span> KEY_ENTER){\r\n\t\t\tplaySound(<span style=\"background-color: #fff0f0\">&quot;TypingSelect&quot;<\/span>);\r\n\t\t\tselectedMenuItem <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;menu&quot;<\/span>;\r\n\t\t\ttransition();\r\n\t\t};\r\n\t};\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(_scene <span style=\"color: #333333\">==<\/span> <span style=\"background-color: #fff0f0\">&quot;menu&quot;<\/span>){\r\n\t\t<span style=\"color: #888888\">\/\/make menu sellection<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(e.keyCode <span style=\"color: #333333\">==<\/span> KEY_SPACE <span style=\"color: #333333\">||<\/span> e.keyCode <span style=\"color: #333333\">==<\/span> KEY_ENTER){\r\n\t\t\tplaySound(<span style=\"background-color: #fff0f0\">&quot;TypingSelect&quot;<\/span>);\r\n\t\t\ttransition();\r\n\t\t};\r\n\t\t<span style=\"color: #888888\">\/\/up\/down on menu items<\/span>\r\n\t\t<span style=\"color: #888888\">\/\/only two items, so, simply put...<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(e.keyCode <span style=\"color: #333333\">==<\/span> KEY_UP){\r\n\t\t\twriteText(<span style=\"background-color: #fff0f0\">&quot;START&quot;<\/span>,txt_start);\r\n\t\t\tselectItem(<span style=\"background-color: #fff0f0\">&quot;game&quot;<\/span>,txt_start,txt_faq);\r\n\t\t};\r\n\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(e.keyCode <span style=\"color: #333333\">==<\/span> KEY_DOWN){\r\n\t\t\twriteText(<span style=\"background-color: #fff0f0\">&quot;WIKI&quot;<\/span>,txt_faq);\r\n\t\t\tselectItem(<span style=\"background-color: #fff0f0\">&quot;faq&quot;<\/span>,txt_faq,txt_start);\r\n\t\t};\r\n\t};\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(_scene <span style=\"color: #333333\">==<\/span> <span style=\"background-color: #fff0f0\">&quot;menu_win&quot;<\/span>){\r\n\t\t<span style=\"color: #888888\">\/\/select - if &quot;submit&quot; submit to twitter - if &quot;game&quot; then hack again<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(e.keyCode <span style=\"color: #333333\">==<\/span> KEY_SPACE <span style=\"color: #333333\">||<\/span> e.keyCode <span style=\"color: #333333\">==<\/span> KEY_ENTER){\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (selectedMenuItem <span style=\"color: #333333\">==<\/span> <span style=\"background-color: #fff0f0\">&quot;submit&quot;<\/span>) {\r\n\t\t\t\ttinyURL();\r\n\t\t\t}\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(selectedMenuItem <span style=\"color: #333333\">==<\/span> <span style=\"background-color: #fff0f0\">&quot;game&quot;<\/span>){\r\n\t\t\t\tplaySound(<span style=\"background-color: #fff0f0\">&quot;TypingSelect&quot;<\/span>);\r\n\t\t\t\ttransition();\r\n\t\t\t}\r\n\t\t};\r\n\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(e.keyCode <span style=\"color: #333333\">==<\/span> KEY_UP){\r\n\t\t\twriteText(<span style=\"background-color: #fff0f0\">&quot;Submit This Hack&quot;<\/span>,txt_option1_win);\r\n\t\t\tselectItem(<span style=\"background-color: #fff0f0\">&quot;submit&quot;<\/span>,txt_option1_win,txt_option2_win);\r\n\t\t};\r\n\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(e.keyCode <span style=\"color: #333333\">==<\/span> KEY_DOWN){\r\n\t\t\twriteText(<span style=\"background-color: #fff0f0\">&quot;Hack Again&quot;<\/span>,txt_option2_win);\r\n\t\t\tselectItem(<span style=\"background-color: #fff0f0\">&quot;game&quot;<\/span>,txt_option2_win,txt_option1_win);\r\n\t\t};\r\n\t};\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n};\r\n<\/pre>\n<\/div>\n<p>There are several &#8220;scenes&#8221;\/areas in the game (excluding the actual game), all of which do something different.<br \/>\nI&#8217;m using one keyboard event for all of them (excluding the game). If <code>_scene<\/code> variable is equal to that area, then keyboard functionality for that unlocks.<br \/>\n<code>writeText(\"\",txt_field)<\/code> is the write text function. When you select a menu item it should write out the text (indicating that you select it).<br \/>\nThe other function <code>selectItem(\"scene\",txt_field1,txt_field2)<\/code> is setting up the variables for your next selection, and toggling the alpha between the two text fields (to make it obvious which one you&#8217;re selecting).<br \/>\nIt 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\">\/\/For menu item selection<\/span>\r\n<span style=\"color: #888888\">\/\/toggles the alpha between items, <\/span>\r\n<span style=\"color: #888888\">\/\/and sets the selectedMenuItem to current selection<\/span>\r\nselectItem <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">function<\/span>(selectedItm, currItem, prevItem) {\r\n\tplaySound(<span style=\"background-color: #fff0f0\">&quot;Aud_Type3&quot;<\/span>);\r\n\tselectedMenuItem <span style=\"color: #333333\">=<\/span> selectedItm;\r\n\tprevItem.alpha <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">1<\/span>;\r\n\tcurrItem.alpha <span style=\"color: #333333\">=<\/span> <span style=\"color: #6600EE; font-weight: bold\">0.5<\/span>;\r\n}\r\n<\/pre>\n<\/div>\n<p>Once you want to select that (<code>SPACE<\/code>, or <code>ENTER<\/code>). Another function, <code>transition();<\/code>, is called to go to that selected scene\/area.<\/p>\n<p><code>transition();<\/code> looks like:<\/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\">\/\/transition<\/span>\r\n<span style=\"color: #888888\">\/\/plays a sound when you change screens<\/span>\r\n<span style=\"color: #888888\">\/\/also handles miscelaneous clearing<\/span>\r\ntransition <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">function<\/span>() {\r\n\tplaySound(<span style=\"background-color: #fff0f0\">&quot;TransitionNewScene&quot;<\/span>);\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #888888\">\/\/clear all fields<\/span>\r\n\tclearText();\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t_stage.gotoAndPlay(selectedMenuItem);\r\n}\r\n<\/pre>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxatron2000\/HTML5\/tutorial\/tutorial_win.png\" alt=\"\" \/><\/p>\n<p>&#8220;<code>menu_win<\/code>&#8221; keyboard event is just a little different in that it also offers you the choice to submit your stats to twitter. <code>tinyURL();<\/code><br \/>\nWhich is:<\/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\">\/\/Twitter (submit everything to twitter) -- see menu_win<\/span>\r\ntinyURL <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">function<\/span>(){\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> url <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;http:\/\/twitter.com\/home?status=I haxed teh system with Haxatron 2000: &quot;<\/span><span style=\"color: #333333\">+<\/span>pageURL<span style=\"color: #333333\">+<\/span><span style=\"background-color: #fff0f0\">&quot; in &quot;<\/span><span style=\"color: #333333\">+<\/span>seconds<span style=\"color: #333333\">+<\/span><span style=\"background-color: #fff0f0\">&quot; seconds, and &quot;<\/span><span style=\"color: #333333\">+<\/span>tries<span style=\"color: #333333\">+<\/span><span style=\"background-color: #fff0f0\">&quot; tries. Haxing is tricky business.&quot;<\/span>;\r\n\t<span style=\"color: #007020\">window<\/span>.open(url,<span style=\"background-color: #fff0f0\">&quot;_blank&quot;<\/span>);\r\n}\r\n<\/pre>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxatron2000\/HTML5\/tutorial\/tutorial_twitter.png\" alt=\"\" \/><\/p>\n<p>*************<\/p>\n<p><strong>THE GAME ENGINE<\/strong><\/p>\n<p>Starting with the higher level game specific logic, this first section handles the map, map tiles, and drawing\/redrawing it.<br \/>\nThere are three types of tiles:<br \/>\n<strong><code>Exit<\/code><\/strong> = the exit\/goal to navigate the player to.<br \/>\n<img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxatron2000\/HTML5\/tutorial\/tutorial_tile_exit.png\" alt=\"\" \/><br \/>\n<strong><code>Player<\/code><\/strong> = the little green guy &#8211; controllable character.<br \/>\n<img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxatron2000\/HTML5\/tutorial\/tutorial_tile_player.png\" alt=\"\" \/><br \/>\n<strong><code>Memory<\/code><\/strong> = the main tile of the game. It has 3 states &#8211; up, down, error (red).<br \/>\n<img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxatron2000\/HTML5\/tutorial\/tutorial_tile_leftright.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxatron2000\/HTML5\/tutorial\/tutorial_tile_error.png\" alt=\"\" \/><\/p>\n<p>First of all I&#8217;ll walk you through the 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: #888888\">\/\/\/\/\/\/\/\/\/\/\/<\/span>\r\n<span style=\"color: #888888\">\/\/ Higher-level, global game-specific logic and resources.<\/span>\r\n<span style=\"color: #888888\">\/\/\/\/\/\/\/\/\/\/<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> gameTileParent; <span style=\"color: #888888\">\/\/Sprite<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> gameLoops <span style=\"color: #333333\">=<\/span> {mapRedraw<span style=\"color: #333333\">:<\/span> <span style=\"color: #333333\">-<\/span><span style=\"color: #0000DD; font-weight: bold\">1<\/span>}; <span style=\"color: #888888\">\/\/Object, see: http:\/\/www.w3schools.com\/js\/js_objects.asp for more info<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> gameTileSize <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">100<\/span>;<span style=\"color: #888888\">\/\/the size of the tile in both width and height<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> gameCurrentMap; <span style=\"color: #888888\">\/\/Object - see gameLoadMap(...) for what goes in this<\/span>\r\n<span style=\"color: #888888\">\/\/intervals for changing and redrawing, see gameStartMapRedraw()<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> changeInt <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">30<\/span>;\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> redrawInt <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">50<\/span>;\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> shouldCenterMap <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">true<\/span>;<span style=\"color: #888888\">\/\/true = centered. false = not... In the event this is a map that should not be centered<\/span>\r\n<\/pre>\n<\/div>\n<p><code>var gameTileParent<\/code> will be the container that all the tiles, and player are placed in.<br \/>\nFrom the docs <a href=\"http:\/\/createjs.com\/Docs\/EaselJS\/classes\/Container.html\" target=\"_blank\">http:\/\/createjs.com\/Docs\/EaselJS\/classes\/Container.html<\/a> : &#8220;A Container is a nestable display list that allows you to work with compound display elements. For example you could group arm, leg, torso and head Bitmap instances together into a Person Container, and transform them as a group, while still being able to move the individual parts relative to each other. Children of containers have their transform and alpha properties concatenated with their parent Container.&#8221;<br \/>\nSo it&#8217;s nice they though of that. :)<\/p>\n<p><code>var gameLoops = {mapRedraw: -1};<\/code> is one of the many object literals (<a href=\"http:\/\/www.w3schools.com\/js\/js_objects.asp\" target=\"_blank\">http:\/\/www.w3schools.com\/js\/js_objects.asp<\/a>) of this game. <code>gameLoops<\/code> is in charge of the two intervals that redraw, and shuffle the map. They will look like this:<br \/>\n<code>gameLoops.mapRedraw = setInterval(gameRedrawMap, redrawInt);<br \/>\ngameLoops.mapShuffle = setInterval(gameShuffleMap, changeInt);<\/code><br \/>\nThis will take place in a function called <code>gameStartMapRedraw()<\/code>, which I&#8217;ll walk you through in a moment&#8230;<\/p>\n<p><code>var gameTileSize<\/code> = the size of the tile in both <code>width<\/code> and <code>height<\/code>, for easy changing.<\/p>\n<p><code>var gameCurrentMap;<\/code> Is another object that will keep all the maps properties (set in <code>gameLoadMap(...)<\/code>). Tiles, width, and height.<br \/>\nFor example: <code>gameCurrentMap = {map: tiles, width: mapWidth, height: mapHeight};<\/code><\/p>\n<p>The interval values &#8211; at what rate the tiles change, and the map redraws:<br \/>\n<code>var changeInt = 30;<br \/>\nvar redrawInt = 50;<\/code><\/p>\n<p><code>var shouldCenterMap<\/code> = boolean. Should the map be centered or not. <code>True<\/code> is centered, <code>false<\/code> not.<\/p>\n<p>Moving on to the functions, the first one is <code>gameLoadMap(tiles, mapWidth, mapHeight)<\/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\">\/\/this is what gets called to start everything<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> gameLoadMap(tiles, mapWidth, mapHeight) {\r\n\t<span style=\"color: #888888\">\/\/Using a Container for this: http:\/\/createjs.com\/Docs\/EaselJS\/classes\/Container.html<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> gamemc <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> createjs.Container();<span style=\"color: #888888\">\/\/new createjs.MovieClip();<\/span>\r\n\tgameCurrentMap <span style=\"color: #333333\">=<\/span> {map<span style=\"color: #333333\">:<\/span> tiles, width<span style=\"color: #333333\">:<\/span> mapWidth, height<span style=\"color: #333333\">:<\/span> mapHeight};\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t_stage.addChild(gamemc);\r\n\tgamemc.name <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;gamemc&quot;<\/span>;\r\n\tgameTileParent <span style=\"color: #333333\">=<\/span> gamemc;\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\tgameStartMapRedraw();\r\n}\r\n<\/pre>\n<\/div>\n<p>This is what gets called (in <code>init()<\/code>) to create the map. In creates the container <code>gameTileParent<\/code> (<code>var gamemc<\/code>) on <code>_stage<\/code>. As well as setting the current maps properties (according to what you pass to it later, in <code>init()<\/code>).<br \/>\nThen it starts the redrawing <code>gameStartMapRedraw();<\/code><br \/>\n<code>gameStartMapRedraw<\/code> is a simple function that starts our intervals (as part of the <code>gameLoops<\/code> object).<\/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> gameStartMapRedraw() {\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (gameLoops.mapRedraw <span style=\"color: #333333\">==<\/span> <span style=\"color: #333333\">-<\/span><span style=\"color: #0000DD; font-weight: bold\">1<\/span>) {\r\n\t\tgameLoops.mapRedraw <span style=\"color: #333333\">=<\/span> setInterval(gameRedrawMap, redrawInt);\r\n\t\tgameLoops.mapShuffle <span style=\"color: #333333\">=<\/span> setInterval(gameShuffleMap, changeInt);\r\n\t}\r\n}\r\n<\/pre>\n<\/div>\n<p>Called in the <code>gameRedrawMap()<\/code> interval (which I&#8217;ll get to), <code>gameRenderTile<\/code> creates a new tile if none exist (<code>if (tile.mc == null)<\/code>).<br \/>\nEverything else, after that, manages updates (if relocated since last redraw).<\/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> gameRenderTile(tile) {\r\n\t<span style=\"color: #888888\">\/\/ Create totally new tile<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (tile.mc <span style=\"color: #333333\">==<\/span> <span style=\"color: #008800; font-weight: bold\">null<\/span>) {\r\n\t\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> mc <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> tile.tile();\r\n\t\tgameTileParent.addChild(mc);\r\n\t\tmc.name <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;tile_&quot;<\/span> <span style=\"color: #333333\">+<\/span> tile.x <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;_&quot;<\/span> <span style=\"color: #333333\">+<\/span> tile.y;\r\n\t\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t\tmc.x <span style=\"color: #333333\">=<\/span> tile.x <span style=\"color: #333333\">*<\/span> gameTileSize;\r\n\t\tmc.y <span style=\"color: #333333\">=<\/span> tile.y <span style=\"color: #333333\">*<\/span> gameTileSize;\r\n\t\tmc.stop();\r\n\t\ttile.mc <span style=\"color: #333333\">=<\/span> mc;\r\n\t}\r\n\t<span style=\"color: #888888\">\/\/ Check if tile relocated since last redraw<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">else<\/span>\r\n\t{\r\n\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (tile.mc.x <span style=\"color: #333333\">!=<\/span> (tile.x <span style=\"color: #333333\">*<\/span> gameTileSize) <span style=\"color: #333333\">||<\/span> (tile.mc.y <span style=\"color: #333333\">!=<\/span> tile.y <span style=\"color: #333333\">*<\/span> gameTileSize))\r\n\t\t{\r\n\t\t\ttile.mc.x <span style=\"color: #333333\">=<\/span> tile.x <span style=\"color: #333333\">*<\/span> gameTileSize;\r\n\t\t\ttile.mc.y <span style=\"color: #333333\">=<\/span> tile.y <span style=\"color: #333333\">*<\/span> gameTileSize;\r\n\t\t\ttile.x <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">Math<\/span>.floor(tile.mc.x <span style=\"color: #333333\">\/<\/span> gameTileSize);\r\n\t\t\ttile.y <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">Math<\/span>.floor(tile.mc.y <span style=\"color: #333333\">\/<\/span> gameTileSize);\r\n\t\t}\r\n\t}\r\n\ttile.mc.stop();\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (<span style=\"color: #333333\">!<\/span>tile.isPlayer) tile.mc.gotoAndStop(tile.opening);<span style=\"color: #888888\">\/\/if it&#39;s not the player, then...<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>Now, <code>gameRedrawMap()<\/code>. This is called in the interval (<code>gameLoops.mapRedraw<\/code>).<br \/>\n<code>gameRedrawMap()<\/code> updates the entire map, saves it (see the loop).<br \/>\nThe loop gathers all the <code>gameCurrentMap.map<\/code> tiles and then calls <code>gameRenderTile(tile)<\/code> on them.<br \/>\nThus the map is redrawn.<br \/>\nThe map is centered, and also <code>playerCheckTile();<\/code> is called, incase the player is on a deadly tile.<br \/>\nThis keeps the player from idling on a deadly tile.<\/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> gameRedrawMap() {\r\n\t<span style=\"color: #888888\">\/\/Update the entire map - save it<\/span>\r\n\t<span style=\"color: #888888\">\/\/this called in an interval<\/span>\r\n\t<span style=\"color: #888888\">\/\/loop through the map, gather all the tiles<\/span>\r\n\t<span style=\"color: #888888\">\/\/update them, which takes place in gameRenderTile again<\/span>\r\n\t<span style=\"color: #888888\">\/\/also centers map and checks if player is on deadly tile (so he doesn&#39;t idle on one)<\/span>\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> tile <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">null<\/span>;\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> player <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">null<\/span>;\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">for<\/span> (<span style=\"color: #008800; font-weight: bold\">var<\/span> a <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>; a <span style=\"color: #333333\">!=<\/span> gameCurrentMap.map.length; <span style=\"color: #333333\">++<\/span>a) {\r\n\t\ttile <span style=\"color: #333333\">=<\/span> gameCurrentMap.map[a];\r\n\t\tgameRenderTile(tile);\r\n\t}\r\n\tcenterMap();\r\n\t<span style=\"color: #888888\">\/\/checked here and in keyboard so he doesn&#39;t idle on a deadly tile<\/span>\r\n\tplayerCheckTile();\r\n}\r\n<\/pre>\n<\/div>\n<p><code>centerMap()<\/code> is fairly self explanatory. It just takes the entire <code>gameTileParent<\/code> container and sets the x, y to center of stage.<\/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\">\/\/center the map - called in the gameRedrawMap interval<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> centerMap(){\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(shouldCenterMap <span style=\"color: #333333\">==<\/span> <span style=\"color: #008800; font-weight: bold\">true<\/span>){\r\n\t\tgameTileParent.x <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">Math<\/span>.floor((stageWidth <span style=\"color: #333333\">\/<\/span> <span style=\"color: #0000DD; font-weight: bold\">2<\/span>) <span style=\"color: #333333\">-<\/span> (containerWidth <span style=\"color: #333333\">\/<\/span> <span style=\"color: #0000DD; font-weight: bold\">2<\/span>)) <span style=\"color: #333333\">-<\/span> <span style=\"color: #0000DD; font-weight: bold\">100<\/span>;\r\n\t\tgameTileParent.y <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">Math<\/span>.floor((stageHeight <span style=\"color: #333333\">\/<\/span> <span style=\"color: #0000DD; font-weight: bold\">2<\/span>) <span style=\"color: #333333\">-<\/span> (containerHeight <span style=\"color: #333333\">\/<\/span> <span style=\"color: #0000DD; font-weight: bold\">2<\/span>)) <span style=\"color: #333333\">-<\/span><span style=\"color: #0000DD; font-weight: bold\">50<\/span>;\t<span style=\"color: #888888\">\/\/-50 added to pull it up a bit<\/span>\r\n\t};\r\n};\r\n<\/pre>\n<\/div>\n<p>One very important function here is <code>gameMakeTile(symbol, tx, ty)<\/code>.<br \/>\nThis is called twice in this game (<code>init()<\/code> and in <code>playerMake()<\/code>).<br \/>\nBasically whenever you create a new tile, this will set the tiles properties.<br \/>\n<code>tile:symbol<\/code> = the type<br \/>\n<code>x:tx, y:tx<\/code> = placement<br \/>\n<code>opening<\/code> = frame to play<\/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\">\/\/responsible for setting the tile&#39;s properties...<\/span>\r\n<span style=\"color: #888888\">\/\/called in any init function - twice in init() &amp; in playerMake()<\/span>\r\n<span style=\"color: #888888\">\/\/tile:symbol = the type, x:tx, y:tx = placement, opening = frame to play<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> gameMakeTile(symbol, tx, ty) {\r\n\t<span style=\"color: #008800; font-weight: bold\">return<\/span> {tile<span style=\"color: #333333\">:<\/span>symbol, x<span style=\"color: #333333\">:<\/span>tx, y<span style=\"color: #333333\">:<\/span>ty, opening<span style=\"color: #333333\">:<\/span>(<span style=\"color: #007020\">Math<\/span>.round(<span style=\"color: #007020\">Math<\/span>.random()))};\r\n}\r\n<\/pre>\n<\/div>\n<p>Note that when you add a movieclip \/ <code>addChild<\/code> to stage (with code), we all know that we need a linkage identifier. That&#8217;s when you go to <code>properties>actionscript>export for actionscript<\/code>. In this case (HTML5 document), that is disabled.<br \/>\nThe only workaround I was able to find was to manually add a copy of all desired <code>addChild<\/code> to the stage (second frame &#8211; there&#8217;s a note).<br \/>\nAfter that you can reference them by the name they have in the library.<br \/>\nExample:<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> myMC <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> lib.LINKAGE();\r\n<span style=\"color: #008800; font-weight: bold\">this<\/span>.addChild(myMC);<span style=\"color: #888888\">\/\/myMC is added to stage!<\/span>\r\n<\/pre>\n<\/div>\n<p>So, back to the engine, if I wanted to add a copy of Exit I would specify <code>lib.Exit<\/code> as the symbol:<br \/>\n<code>gameMakeTile(lib.Exit, ExitX, ExitY);<\/code><br \/>\nOr <code>lib.Player<\/code>, <code>lib.Memory<\/code>&#8230;<\/p>\n<p>And finally we have our very last function <code>gameShuffleMap()<\/code> which is also called in the interval (<code>gameLoops.mapRedraw<\/code>).<br \/>\nThis changes the graphical state of a tile (Memory tile), to either &#8220;deadly&#8221;, or &#8220;walkable&#8221;.<br \/>\nIt should <em>never<\/em> change under the player. That&#8217;s what while (<code>tile1.isPlayer || (tile1.x == player.x && tile1.y == player.y)<\/code>) is for. If it&#8217;s under the player, then try again.<br \/>\nThe <code>tile1.opening<\/code> at the end sets it to one of the 3 states.<\/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\">\/\/Change the graphical state of a tile (if it should be &quot;deadly&quot; or &quot;walkable&quot;), but not under player<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> gameShuffleMap() {\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> tile1 <span style=\"color: #333333\">=<\/span> gameCurrentMap.map[(<span style=\"color: #007020\">Math<\/span>.floor(<span style=\"color: #007020\">Math<\/span>.random() <span style=\"color: #333333\">*<\/span> gameCurrentMap.map.length <span style=\"color: #333333\">+<\/span> <span style=\"color: #0000DD; font-weight: bold\">1<\/span>)<span style=\"color: #333333\">-<\/span><span style=\"color: #0000DD; font-weight: bold\">1<\/span>)];\r\n\t<span style=\"color: #888888\">\/\/No changing under player! Do again...<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">while<\/span> (tile1.isPlayer <span style=\"color: #333333\">||<\/span> (tile1.x <span style=\"color: #333333\">==<\/span> player.x <span style=\"color: #333333\">&amp;&amp;<\/span> tile1.y <span style=\"color: #333333\">==<\/span> player.y) ) { <span style=\"color: #888888\">\/\/now the setInterval for this function is accurate<\/span>\r\n\t\ttile1 <span style=\"color: #333333\">=<\/span> gameCurrentMap.map[(<span style=\"color: #007020\">Math<\/span>.floor(<span style=\"color: #007020\">Math<\/span>.random() <span style=\"color: #333333\">*<\/span> gameCurrentMap.map.length <span style=\"color: #333333\">+<\/span> <span style=\"color: #0000DD; font-weight: bold\">1<\/span>)<span style=\"color: #333333\">-<\/span><span style=\"color: #0000DD; font-weight: bold\">1<\/span>)];\r\n\t}\r\n\ttile1.opening <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">Math<\/span>.floor(<span style=\"color: #007020\">Math<\/span>.random() <span style=\"color: #333333\">*<\/span> <span style=\"color: #0000DD; font-weight: bold\">3<\/span>);\r\n\t<span style=\"color: #888888\">\/\/to test win (no deadly tiles)<\/span>\r\n\t<span style=\"color: #888888\">\/\/tile1.opening = Math.floor(Math.random() * 2);<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>Next we have our Player&#8217;s control and logic.<br \/>\nAgain, starting with the 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: #888888\">\/\/\/\/\/\/\/\/\/\/\/<\/span>\r\n<span style=\"color: #888888\">\/\/ Player control and render logic<\/span>\r\n<span style=\"color: #888888\">\/\/\/\/\/\/\/\/\/\/<\/span>\r\n\r\n<span style=\"color: #888888\">\/\/ dr = Direction as string: up, down, left, right<\/span>\r\n<span style=\"color: #888888\">\/\/ I left this in from the touchscreen controls version<\/span>\r\n<span style=\"color: #888888\">\/\/ This is here incase touchscreen controls are added back in<\/span>\r\n<span style=\"color: #888888\">\/\/ Each touchscreen button would set dir...<\/span>\r\n<span style=\"color: #888888\">\/\/ Keyboard now uses it as well.<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> dr <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;&quot;<\/span>; \r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> player; <span style=\"color: #888888\">\/\/Object<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> playerCurrentTile <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">null<\/span>; <span style=\"color: #888888\">\/\/Object -- so keyboard knows if player can go there, or if it will kill player<\/span>\r\n<\/pre>\n<\/div>\n<p><code>var dir = \"\";<\/code> Is a direction string. Up, down, left, and right. This was left in from the touchscreen controls version of the game. It&#8217;s here incase touchscreen support is added back (I have it as an <a href=\"https:\/\/itunes.apple.com\/us\/app\/haxatron-2000\/id811892861?ls=1&#038;mt=8\" target=\"_blank\">App in the App Store<\/a> incase you&#8217;re interested :) ).<br \/>\nEach touchscreen button would set a <code>dir<\/code>. Keyboard now uses it as well. So essentially this online version could support both.<\/p>\n<p><code>var player;<\/code> Is the player! As an object, because properties are saved\/set when player is created in <code>playerMake(symbol, px, py)<\/code>. Explained bellow.<\/p>\n<p><code>var playerCurrentTile;<\/code> Is the current tile the player is on. Used in keyboard movement (see if player can move to the next tile). Set in the keyup event.<\/p>\n<p>The first thing will be the function for making the player, <code>playerMake(symbol, px, py);<\/code> This sets the player object to what <code>gameMakeTile(...)<\/code> returns (now you have the basic properties of player). And specify that this tile <code>isPlayer<\/code> (true). Return the player because later we will assign a variable <code>var currPlayer;<\/code> to whatever this returns.<\/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> playerMake(symbol, px, py) {\r\n\tplayer <span style=\"color: #333333\">=<\/span> gameMakeTile(symbol, px, py);\r\n\tplayer.isPlayer <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">true<\/span>;\r\n\t<span style=\"color: #008800; font-weight: bold\">return<\/span> player;\r\n}\r\n<\/pre>\n<\/div>\n<p>We will also have two functions for setting and clearing the keyboard. Easier for killing\/restarting 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: #008800; font-weight: bold\">function<\/span> setKeyboard(){\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #007020\">document<\/span>.onkeydown <span style=\"color: #333333\">=<\/span> playerKeyboardMove;\r\n\t<span style=\"color: #007020\">document<\/span>.onkeyup <span style=\"color: #333333\">=<\/span> playerKeyboardUp;\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n};\r\n\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> clearKeyboard(){\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>So now we&#8217;ll start with the key up event. There are two functions associated with it.<br \/>\nThe first is <code>playerKeyboardUpdate()<\/code>.<br \/>\n<code>playerKeyboardUpdate()<\/code> is called in two areas. The key up event, and in <code>init()<\/code> (to set the starting value of <code>playerCurrentTile<\/code>).<br \/>\nThis sets <code>playerCurrentTile<\/code> to the tile the player is on by looping through the game map, and checking if the tile x\/y is equal to player. Once it finds it then set it.<br \/>\n<code>playerKeyboardUpdate()<\/code> isn&#8217;t the actual keyboard event, <code>playerKeyboardUp(event)<\/code> (following block) because, if I wanted to add touchscreen controls, I could now reference <code>playerKeyboardUpdate()<\/code> for the touch up event.<\/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\">\/\/KEY_UP<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> playerKeyboardUpdate() {\r\n\t<span style=\"color: #888888\">\/\/ Tile player is on right now<\/span>\r\n\t<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\">!=<\/span> gameCurrentMap.map.length; <span style=\"color: #333333\">++<\/span>i) {\r\n\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (gameCurrentMap.map[i].x <span style=\"color: #333333\">==<\/span> player.x <span style=\"color: #333333\">&amp;&amp;<\/span> gameCurrentMap.map[i].y <span style=\"color: #333333\">==<\/span> player.y <span style=\"color: #333333\">&amp;&amp;<\/span> gameCurrentMap.map[i] <span style=\"color: #333333\">!=<\/span> player) {\r\n\t\t\tplayerCurrentTile <span style=\"color: #333333\">=<\/span> gameCurrentMap.map[i];\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">break<\/span>;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<\/div>\n<p>Now to set the actual key up event:<\/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> playerKeyboardUp(event){<span style=\"color: #888888\">\/\/KeyboardEvent<\/span>\r\n\tplayerKeyboardUpdate();\r\n}\r\n<\/pre>\n<\/div>\n<p>As usual key down is a lot more elaborate&#8230;<br \/>\nThe lines that look like: <code>&& playerCurrentTile.opening == 1<\/code><br \/>\nKeep the player from walking in a direction that the tile does not allow.<br \/>\nNotice the <code>gameTileParent.getChildByName(\"tile_\"+(player.x-1)+\"_\"+player.y) != null<\/code> before that? That will keep the player from walking off the actual game grid, away from the game&#8230;<br \/>\nSo we have that to keep them on the board.<br \/>\nThe last <code>if<\/code> statement (<code>if (dr != \"\")<\/code>) will set the player sprite to face the direction you just pressed.<br \/>\nThen we check the tile with <code>playerCheckTile();<\/code> (explained bellow).<\/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\">\/\/KEY_DOWN<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> playerKeyboardMove(e) {<span style=\"color: #888888\">\/\/KeyboardEvent<\/span>\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>e){ <span style=\"color: #008800; font-weight: bold\">var<\/span> e <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">window<\/span>.event; }\r\n\t<span style=\"color: #888888\">\/\/var dr = &quot;&quot;; \/\/ Direction: left | up | down | right <\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">switch<\/span> (e.keyCode){\r\n\t\t<span style=\"color: #008800; font-weight: bold\">case<\/span> KEY_LEFT<span style=\"color: #333333\">:<\/span>\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (gameTileParent.getChildByName(<span style=\"background-color: #fff0f0\">&quot;tile_&quot;<\/span><span style=\"color: #333333\">+<\/span>(player.x<span style=\"color: #333333\">-<\/span><span style=\"color: #0000DD; font-weight: bold\">1<\/span>)<span style=\"color: #333333\">+<\/span><span style=\"background-color: #fff0f0\">&quot;_&quot;<\/span><span style=\"color: #333333\">+<\/span>player.y) <span style=\"color: #333333\">!=<\/span> <span style=\"color: #008800; font-weight: bold\">null<\/span>  <span style=\"color: #333333\">&amp;&amp;<\/span> playerCurrentTile.opening <span style=\"color: #333333\">==<\/span> <span style=\"color: #0000DD; font-weight: bold\">1<\/span>) {\r\n\t\t\t\tplayer.x<span style=\"color: #333333\">--<\/span>;\r\n\t\t\t\tdr <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;left&quot;<\/span>;\r\n\t\t\t\tplaySound(<span style=\"background-color: #fff0f0\">&quot;Move1&quot;<\/span>);\r\n\t\t\t}\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">break<\/span>;\r\n\t\t<span style=\"color: #008800; font-weight: bold\">case<\/span> KEY_UP<span style=\"color: #333333\">:<\/span>\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (gameTileParent.getChildByName(<span style=\"background-color: #fff0f0\">&quot;tile_&quot;<\/span><span style=\"color: #333333\">+<\/span>player.x<span style=\"color: #333333\">+<\/span><span style=\"background-color: #fff0f0\">&quot;_&quot;<\/span><span style=\"color: #333333\">+<\/span>(player.y<span style=\"color: #333333\">-<\/span><span style=\"color: #0000DD; font-weight: bold\">1<\/span>)) <span style=\"color: #333333\">!=<\/span> <span style=\"color: #008800; font-weight: bold\">null<\/span> <span style=\"color: #333333\">&amp;&amp;<\/span> playerCurrentTile.opening <span style=\"color: #333333\">==<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>) { \r\n\t\t\t\tplayer.y<span style=\"color: #333333\">--<\/span>;\r\n\t\t\t\tdr <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;up&quot;<\/span>;\r\n\t\t\t\tplaySound(<span style=\"background-color: #fff0f0\">&quot;Move2&quot;<\/span>);\r\n\t\t\t}\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">break<\/span>;\r\n\t\t<span style=\"color: #008800; font-weight: bold\">case<\/span> KEY_RIGHT<span style=\"color: #333333\">:<\/span>\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (gameTileParent.getChildByName(<span style=\"background-color: #fff0f0\">&quot;tile_&quot;<\/span><span style=\"color: #333333\">+<\/span>(player.x<span style=\"color: #333333\">+<\/span><span style=\"color: #0000DD; font-weight: bold\">1<\/span>)<span style=\"color: #333333\">+<\/span><span style=\"background-color: #fff0f0\">&quot;_&quot;<\/span><span style=\"color: #333333\">+<\/span>player.y) <span style=\"color: #333333\">!=<\/span> <span style=\"color: #008800; font-weight: bold\">null<\/span> <span style=\"color: #333333\">&amp;&amp;<\/span> playerCurrentTile.opening <span style=\"color: #333333\">==<\/span> <span style=\"color: #0000DD; font-weight: bold\">1<\/span>) {\r\n\t\t\t\tplayer.x<span style=\"color: #333333\">++<\/span>;\r\n\t\t\t\tdr <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;right&quot;<\/span>;\r\n\t\t\t\tplaySound(<span style=\"background-color: #fff0f0\">&quot;Move2&quot;<\/span>);\r\n\t\t\t}\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">break<\/span>;\r\n\t\t<span style=\"color: #008800; font-weight: bold\">case<\/span> KEY_DOWN<span style=\"color: #333333\">:<\/span>\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (gameTileParent.getChildByName(<span style=\"background-color: #fff0f0\">&quot;tile_&quot;<\/span><span style=\"color: #333333\">+<\/span>player.x<span style=\"color: #333333\">+<\/span><span style=\"background-color: #fff0f0\">&quot;_&quot;<\/span><span style=\"color: #333333\">+<\/span>(player.y<span style=\"color: #333333\">+<\/span><span style=\"color: #0000DD; font-weight: bold\">1<\/span>)) <span style=\"color: #333333\">!=<\/span> <span style=\"color: #008800; font-weight: bold\">null<\/span> <span style=\"color: #333333\">&amp;&amp;<\/span> playerCurrentTile.opening <span style=\"color: #333333\">==<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>) {\r\n\t\t\t\tplayer.y<span style=\"color: #333333\">++<\/span>;\r\n\t\t\t\tdr <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;down&quot;<\/span>;\r\n\t\t\t\tplaySound(<span style=\"background-color: #fff0f0\">&quot;Move1&quot;<\/span>);\r\n\t\t\t}\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">break<\/span>;\r\n\t}\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (dr <span style=\"color: #333333\">!=<\/span> <span style=\"background-color: #fff0f0\">&quot;&quot;<\/span>)  {\r\n\t\tplayer.mc.gotoAndStop(dr);\r\n\t}\r\n\tplayerCheckTile();\r\n}\r\n<\/pre>\n<\/div>\n<p><code>playerCheckTile()<\/code> checks the tile the player just walked on and determines if the player should explode (die), or bloat with joy (win).<br \/>\nThe first condition (dying) is fairly self explanatory. Checks against the timeline position (what frame), and if it&#8217;s the &#8220;error&#8221; tile then die.<br \/>\nStarting the game again (calling <code>demoLose();<\/code>) is called at the end of the player&#8217;s die animation. There are better ways of doing that, but this was the simplest.<\/p>\n<p>The win condition checks to see if the player actually made it to exit. Notice the <code>ExitX<\/code>, and <code>ExitY<\/code> variables. We save the x\/y of Exit incase we want to change it, or make it random with every new game. It adds a level of flexibility&#8230;<br \/>\nAn interesting problem I ran into while porting this was that the player would sometimes freeze on win (the animation does not play, thus the win function does not get called, and you don&#8217;t proceed past this point). The issue is random and seems to occur more in Chrome than it does in Firefox (sorry, I know, blaming browsers). I &#8220;fixed&#8221; this bug by just forcing win with an <code>interval<\/code> (if it goes beyond a certain time) then just call it. I didn&#8217;t figure out exactly why this happens and why the win animation does not trigger, but you could probably credit it to the fact that it would be better to just remove the player when you win, and place win animation as a separate added child, and handle the win\/die events purely through code, breaking up the player movieclip. Better restructuring would be necessary&#8230; but in this case, good enough!<\/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> playerCheckTile(){\r\n\t<span style=\"color: #008800; font-weight: bold\">var<\/span> currLabel <span style=\"color: #333333\">=<\/span> player.mc.getCurrentLabel();\r\n\t<span style=\"color: #888888\">\/\/Die condition<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span> (gameTileParent.getChildByName(<span style=\"background-color: #fff0f0\">&quot;tile_&quot;<\/span> <span style=\"color: #333333\">+<\/span> player.x <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;_&quot;<\/span> <span style=\"color: #333333\">+<\/span> player.y).timeline.position <span style=\"color: #333333\">==<\/span> <span style=\"color: #0000DD; font-weight: bold\">2<\/span>) {\r\n\t\tplayer.mc.gotoAndStop(<span style=\"background-color: #fff0f0\">&quot;die&quot;<\/span>);\r\n\t}\r\n\t<span style=\"color: #888888\">\/\/Win condition<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(gameTileParent.getChildByName(<span style=\"background-color: #fff0f0\">&quot;tile_&quot;<\/span> <span style=\"color: #333333\">+<\/span> player.x <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;_&quot;<\/span> <span style=\"color: #333333\">+<\/span> player.y)<span style=\"color: #333333\">==<\/span>gameTileParent.getChildByName(<span style=\"background-color: #fff0f0\">&quot;tile_&quot;<\/span> <span style=\"color: #333333\">+<\/span> ExitX <span style=\"color: #333333\">+<\/span> <span style=\"background-color: #fff0f0\">&quot;_&quot;<\/span> <span style=\"color: #333333\">+<\/span> ExitY)){<span style=\"color: #888888\">\/\/if it&#39;s Exit tile: [object Exit]<\/span>\r\n\t\t<span style=\"color: #888888\">\/\/Start win failsafe<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(currLabel<span style=\"color: #333333\">!=<\/span><span style=\"background-color: #fff0f0\">&quot;win&quot;<\/span>){\r\n\t\t\tclearKeyboard();\r\n\t\t\twinInt <span style=\"color: #333333\">=<\/span> setInterval(winInterval,<span style=\"color: #0000DD; font-weight: bold\">1000<\/span>);\r\n\t\t};\r\n\t\t<span style=\"color: #888888\">\/\/default<\/span>\r\n\t\tplayer.mc.gotoAndStop(<span style=\"background-color: #fff0f0\">&quot;win&quot;<\/span>);\r\n\t}\r\n}\r\n<\/pre>\n<\/div>\n<p>Here&#8217;s that &#8220;win failsafe&#8221; just mentioned:<\/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\">\/\/WIN FAILSAFE:<\/span>\r\n<span style=\"color: #888888\">\/\/If the player does not trigger the &quot;win&quot; animation for whatever reason <\/span>\r\n<span style=\"color: #888888\">\/\/(problem seems to sometimes occur, and I&#39;m not sure if what it is, or how to recreate it, or if it still exists...)<\/span>\r\n<span style=\"color: #888888\">\/\/Countdown wait, and then force demoWin(); if the animation doesn&#39;t trigger &amp; call it<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> winInterval(){\r\n\twinCnt<span style=\"color: #333333\">+=<\/span><span style=\"color: #0000DD; font-weight: bold\">1<\/span>;\r\n\t<span style=\"color: #008800; font-weight: bold\">if<\/span>(winCnt<span style=\"color: #333333\">&gt;<\/span><span style=\"color: #0000DD; font-weight: bold\">3<\/span>){\r\n\t\tclearInterval(winInt);\r\n\t\tdemoWin();\r\n\t};\r\n};\r\n<\/pre>\n<\/div>\n<p>Moving on to starting the game!<br \/>\nWe have our 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: #888888\">\/\/\/\/\/\/\/\/\/\/\/<\/span>\r\n<span style=\"color: #888888\">\/\/ Start<\/span>\r\n<span style=\"color: #888888\">\/\/\/\/\/\/\/\/\/\/\/<\/span>\r\n\r\n<span style=\"color: #888888\">\/\/The current map&#39;s width and height<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> currMapWidth; <span style=\"color: #888888\">\/\/Number<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> currMapHeight; <span style=\"color: #888888\">\/\/Number<\/span>\r\n<span style=\"color: #888888\">\/\/To calculate the width and height of the map container (used in centering)<\/span>\r\n<span style=\"color: #888888\">\/\/take gameTileSize and multiply by currMapWidth, and currMapHeight<\/span>\r\n<span style=\"color: #888888\">\/\/CreateJS does not support .width yet, so this is how I handle it...<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> containerWidth;\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> containerHeight;\r\n<span style=\"color: #888888\">\/\/<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> currMapTiles; <span style=\"color: #888888\">\/\/Array<\/span>\r\n<span style=\"color: #888888\">\/\/The current &quot;Player&quot; sprite - in this case, the little green guy<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> currPlayer; <span style=\"color: #888888\">\/\/Object<\/span>\r\n<span style=\"color: #888888\">\/\/Placement of the Exit<\/span>\r\n<span style=\"color: #888888\">\/\/I&#39;m saving this here because location of exit may vary<\/span>\r\n<span style=\"color: #888888\">\/\/--Depending on how much you decide to customize this<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> ExitX;\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> ExitY;\r\n\r\n<span style=\"color: #888888\">\/\/seconds and tries is your ranking<\/span>\r\n<span style=\"color: #888888\">\/\/the player will be congratulated on this at the end<\/span>\r\n<span style=\"color: #888888\">\/\/so both variables are set to global scope<\/span>\r\nseconds <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>;\r\ntries <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>;\r\n\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> timeInt; <span style=\"color: #888888\">\/\/uint<\/span>\r\n<span style=\"color: #888888\">\/\/Win failsafe (if, for whatever reason, win might not trigger)<\/span>\r\nwinInt <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>;\r\n<span style=\"color: #008800; font-weight: bold\">var<\/span> winCnt <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>;\r\n<\/pre>\n<\/div>\n<p><code>var currMapWidth<\/code>, and <code>var currMapHeight;<\/code> are the maps width and height. They&#8217;re set in <code>init<\/code>. Values go by a tile basis, so larger numbers mean more tiles.<\/p>\n<p><code>var containerWidth<\/code> and <code>var containerHeight;<\/code> are new to this. I calculate the width and height of the map container (used for centering) by taking the <code>gameTileSize<\/code> and multiplying that by <code>currMapWidth<\/code>, or <code>currMapHeight<\/code>. This is necessary because CreateJS does not support <code>.width<\/code> or <code>.height<\/code> yet (a workaround is available here <a href=\"https:\/\/github.com\/ibnYusrat\/Get-Shape-Width-Height-in-EaselJS\" target=\"_blank\">https:\/\/github.com\/ibnYusrat\/Get-Shape-Width-Height-in-EaselJS<\/a> ).<\/p>\n<p><code>var currMapTiles;<\/code> Are all the maps tiles. Pushed to with <code>gameMakeTile (currMapTiles.push(gameMakeTile(lib.Exit, ExitX, ExitY));<\/code> for example).<\/p>\n<p><code>var currPlayer;<\/code> Is the player object. Updated when the player is created. Example, <code>currPlayer = playerMake(lib.Player, 0, 2);<\/code><\/p>\n<p><code>var ExitX;<\/code> and <code>var ExitY;<\/code> are the placement of the exit. So it&#8217;s easier to check against and determine if the tile the player is on is the exit. Exit placement may vary (if that behavior was desired).<\/p>\n<p>For our ranking system we have,<br \/>\n<code>seconds = 0;<br \/>\ntries = 0;<\/code><br \/>\nWhich will be like the player&#8217;s &#8220;score&#8221; and they are congratulated on it when they win&#8230; as well as it being sent to twitter. :)<\/p>\n<p>And finally,<br \/>\n<code>var timeInt; \/\/uint<br \/>\n\/\/Win failsafe (if, for whatever reason, win might not trigger)<br \/>\nwinInt = 0;<br \/>\nvar winCnt = 0;<\/code><br \/>\nAre the intervals. Time interval is what increments the seconds variable (how long you&#8217;ve played before winning).<\/p>\n<p>The <code>time<\/code> function is a simple one, just incrementing seconds.<\/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\">\/\/the time interval<\/span>\r\n<span style=\"color: #008800; font-weight: bold\">function<\/span> time(){\r\n\tseconds<span style=\"color: #333333\">+=<\/span><span style=\"color: #0000DD; font-weight: bold\">1<\/span>;\r\n};\r\n<\/pre>\n<\/div>\n<p>Following that is another miscellaneous function meant for &#8220;sloppily&#8221; destroying all intervals. This is a safety precaution just incase the interval didn&#8217;t get cleared for some reason.<\/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> killAllIntervals() {\r\n\t<span style=\"color: #888888\">\/\/console.log(&quot;killAllIntervals() - murder called&quot;);<\/span>\r\n\t<span style=\"color: #888888\">\/\/murder<\/span>\r\n\t<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><span style=\"color: #0000DD; font-weight: bold\">100<\/span>; i<span style=\"color: #333333\">++<\/span>) {\r\n\t\tclearInterval(i);\r\n\t}\r\n}\r\n<\/pre>\n<\/div>\n<p>Finally, we have <code>init()<\/code>, which starts the game!<br \/>\nIt clears any variables (resets them), sets up any necessary values (like the container width\/height), and creates all tiles (player, memory, exit).<br \/>\nBoth arrays are first cleared <code>currMapTiles = []<\/code>, before repopulated. For example, all tiles are pushed to the <code>currMapTiles<\/code> array.<br \/>\nAfter <code>currMapTiles<\/code> is done being populated with necessary tile information, g<code>ameLoadMap(currMapTiles, currMapWidth, currMapHeight);<\/code> is called to start it up.<br \/>\n<code>setKeyboard();<\/code> enables the keyboard controls.<br \/>\nAfter the &#8220;Spawn&#8221; sound is played, the <code>playerKeyboardUpdate()<\/code> is called just to capture the current player position for first run.<\/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> init(){\r\n\twinCnt <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>;\r\n\tdr <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;&quot;<\/span>;\r\n\tcurrPlayer <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">null<\/span>;\r\n\tgameCurrentMap <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">null<\/span>;\r\n\tcurrMapWidth <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">7<\/span>;\r\n\tcurrMapHeight <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">4<\/span>;\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\tcontainerWidth <span style=\"color: #333333\">=<\/span> gameTileSize<span style=\"color: #333333\">*<\/span>currMapWidth;\r\n\tcontainerHeight <span style=\"color: #333333\">=<\/span> gameTileSize<span style=\"color: #333333\">*<\/span>currMapHeight;\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\tcurrMapTiles <span style=\"color: #333333\">=<\/span> [];\r\n\tdemoLibrary <span style=\"color: #333333\">=<\/span> [];\r\n\t<span style=\"color: #888888\">\/\/*Make the Player<\/span>\r\n\tcurrPlayer <span style=\"color: #333333\">=<\/span> playerMake(lib.Player, <span style=\"color: #0000DD; font-weight: bold\">0<\/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\">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> currMapWidth; <span style=\"color: #333333\">++<\/span>i) {\r\n\t\t<span style=\"color: #008800; font-weight: bold\">for<\/span> (<span style=\"color: #008800; font-weight: bold\">var<\/span> j <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>; j <span style=\"color: #333333\">&lt;=<\/span> currMapHeight; <span style=\"color: #333333\">++<\/span>j) {\r\n\t\t\t<span style=\"color: #008800; font-weight: bold\">var<\/span> chosen <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">Math<\/span>.floor(<span style=\"color: #007020\">Math<\/span>.random() <span style=\"color: #333333\">*<\/span> <span style=\"color: #0000DD; font-weight: bold\">1<\/span>);\r\n\t\t\t<span style=\"color: #333333\">--<\/span>chosen;\r\n\t\t\t<span style=\"color: #888888\">\/\/*Make the Memory tiles<\/span>\r\n\t\t\tcurrMapTiles.push(gameMakeTile(lib.Memory, i, j));\r\n\t\t}\r\n\t}\r\n\t<span style=\"color: #888888\">\/\/Place Exit tile -- in this case at the end of the map<\/span>\r\n\t<span style=\"color: #888888\">\/\/First the variables<\/span>\r\n\tExitX <span style=\"color: #333333\">=<\/span> currMapWidth<span style=\"color: #333333\">+<\/span><span style=\"color: #0000DD; font-weight: bold\">1<\/span>;\r\n\tExitY <span style=\"color: #333333\">=<\/span> <span style=\"color: #007020\">Math<\/span>.floor(currMapHeight<span style=\"color: #333333\">\/<\/span><span style=\"color: #0000DD; font-weight: bold\">2<\/span>);\r\n\t<span style=\"color: #888888\">\/\/*Make the Exit tile<\/span>\r\n\tcurrMapTiles.push(gameMakeTile(lib.Exit, ExitX, ExitY));\r\n\tcurrMapTiles.push(currPlayer);\r\n\tgameLoadMap(currMapTiles, currMapWidth, currMapHeight);\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\tshouldCenterMap <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">true<\/span>;\r\n\tsetKeyboard();\r\n\tplaySound(<span style=\"background-color: #fff0f0\">&quot;Spawn&quot;<\/span>);\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\tplayerKeyboardUpdate();\r\n};\r\n<\/pre>\n<\/div>\n<p>After <code>init()<\/code> we have <code>uninit()<\/code> which is a bit simpler. It clears the game (removes all children). This is used in <code>demoLose()<\/code> (called in the player&#8217;s death animation), and <code>demoWin()<\/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: #008800; font-weight: bold\">function<\/span> uninit(){\r\n\tshouldCenterMap <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">false<\/span>;\r\n\tclearKeyboard();\r\n\t<span style=\"color: #888888\">\/\/<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold\">for<\/span> (<span style=\"color: #008800; font-weight: bold\">var<\/span> j <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">0<\/span>; j <span style=\"color: #333333\">&lt;<\/span> gameTileParent.numChildren; j<span style=\"color: #333333\">++<\/span>){\r\n\t\t  gameTileParent.removeChild(gameTileParent.getChildAt(j));\r\n\t}\r\n\t_stage.removeChild(gameTileParent);\r\n};\r\n<\/pre>\n<\/div>\n<p>Then we have <code>demoWin<\/code>, and <code>demoLose<\/code><br \/>\n<code>demoWin<\/code> is kills the entire game. All intervals are cleared, <code>uninit()<\/code> also called, and the default keyboard (other menu keyboard) restarted.<br \/>\nThis sends the timeline to the &#8220;win&#8221; scene.<\/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\">\/\/Scope of win, and lose has been changed to be global<\/span>\r\n<span style=\"color: #888888\">\/\/They are called within the demo player - at the end of its respective animation<\/span>\r\ndemoWin <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">function<\/span>() {\r\n\tclearInterval(gameLoops.mapRedraw);\r\n\tclearInterval(gameLoops.mapShuffle);\r\n\tclearInterval(timeInt);\r\n\tclearInterval(winInt);\r\n\tkillAllIntervals();\r\n\tuninit();\r\n\taddDefaultKeys();<span style=\"color: #888888\">\/\/start the default keyboard again<\/span>\r\n\t<span style=\"color: #888888\">\/\/go to win!<\/span>\r\n\tselectedMenuItem <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;win&quot;<\/span>;\r\n\ttransition();\r\n}\r\n<\/pre>\n<\/div>\n<p><code>demoLose()<\/code> simply increments the player&#8217;s tries, <code>uninit();<\/code>, and starts the game again, <code>init();<\/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%\">demoLose <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">function<\/span>() {\r\n\ttries<span style=\"color: #333333\">+=<\/span><span style=\"color: #0000DD; font-weight: bold\">1<\/span>;\r\n\tuninit();\r\n\tinit();\r\n}\r\n<\/pre>\n<\/div>\n<p>Then finally we start the game with the following.<\/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%\">removeDefaultKeys();<span style=\"color: #888888\">\/\/remove default keyboard<\/span>\r\ninit();\r\ntimeInt <span style=\"color: #333333\">=<\/span> setInterval(time,<span style=\"color: #0000DD; font-weight: bold\">1000<\/span>);\r\n<\/pre>\n<\/div>\n<p>That&#8217;s it! The game is running.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxatron2000\/HTML5\/tutorial\/tutorial_gamedone.png\" alt=\"\" \/><\/p>\n<p>*************<\/p>\n<p><strong>CONCLUSION<\/strong><\/p>\n<p>After having worked with this feature of Flash CC I&#8217;m impressed. It&#8217;s a lot more flexible\/powerful than it&#8217;s given credit, and I&#8217;m excited to see where Adobe will be taking it in the future.<br \/>\nI hope it&#8217;s going to grow in future versions\/releases, as I think it&#8217;s silly to separate Flash from HTML5 when the two could (potentially) work so well together (Flash is still superior when it comes to <a href=\"http:\/\/bluesuburbia.com\" target=\"_blank\">handling vector graphics<\/a>).<\/p>\n<p>Download all sourcefiles here, and take a look:<br \/>\n<a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Haxatron2000\/HTML5\/\" target=\"_blank\">http:\/\/nathalielawhead.com\/sourcefiles\/Haxatron2000\/HTML5\/<\/a><\/p>\n<p>You can play the end result here:<br \/>\n<a href=\"http:\/\/haxatron.alienmelon.com\/browserhax\" target=\"_blank\">http:\/\/haxatron.alienmelon.com\/browserhax<\/a><\/p>\n<p>Visit the PREVIOUS tutorial: <a href=\"http:\/\/www.nathalielawhead.com\/candybox\/tutorial-building-a-game-in-edge-animate-offender\" target=\"_self\">Building A Game In Edge Animate (Offender)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: Flash Professional CC introduced a document type in which you can create an HTML5 Canvas document. It publishes HTML5 content using the CreateJS libraries (but you could easily use other libraries if you wanted to). Although this feature has been out for a while, there is little to no information (at least I couldn&#8217;t find much), about how to make something more complex in it (like a game). I love this development because (from Adobe documentation) &#8220;Flash publishes to HTML5 by leveraging the Canvas API. It translates objects created on stage in to their Canvas counterparts.&#8221; Meaning, I don&#8217;t&#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-3095","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\/3095","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=3095"}],"version-history":[{"count":20,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/3095\/revisions"}],"predecessor-version":[{"id":3119,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/3095\/revisions\/3119"}],"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=3095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/categories?post=3095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/tags?post=3095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}