{"id":378,"date":"2025-07-07T16:36:07","date_gmt":"2025-07-07T06:36:07","guid":{"rendered":"https:\/\/nonlinearexperience.com\/?p=378"},"modified":"2025-07-07T16:36:08","modified_gmt":"2025-07-07T06:36:08","slug":"game-states","status":"publish","type":"post","link":"https:\/\/nonlinearexperience.com\/index.php\/2025\/07\/07\/game-states\/","title":{"rendered":"Game states"},"content":{"rendered":"<h1 class=\"wp-block-post-title\">Game states<\/h1>\n\n\n<p class=\"wp-block-paragraph\">See also: <a href=\"https:\/\/nonlinearexperience.com\/index.php\/2025\/07\/07\/game-loop\/\" data-type=\"post\" data-id=\"307\">Game loop<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Games states help you arrange the game into logical groupings<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>LOADING<\/li>\n\n\n\n<li>BUILD MAP &#8211; make <strong>map<\/strong>, then <strong>gameObjects<\/strong><\/li>\n\n\n\n<li>PLAYING<\/li>\n\n\n\n<li>OVER<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Only swap between states when necesary, and only the bits that are necessary &#8211; e.g.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Set the game's current state to `play`:\nlet state = play;\nfunction gameLoop(){\n\u00a0\u00a0\/\/Loop this function at 60 frames per second\n\u00a0 requestAnimationFrame(gameLoop);\n\u00a0 \/\/Update the current game state:\n\u00a0 state();\n\u00a0 \/\/Render the stage to see the animation\n\u00a0 renderer.render(stage);\n}\nfunction play(){\n\u00a0 \/\/Move the sprite 1 pixel to the right each frame\n\u00a0 anySprite.x += 1;\u00a0\n}\n```<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2026structuring your game loop like this will make it much, much easier to do things such as switching game scenes and levels. It means that you can completely change what your game or application is doing by simply pointing the state variable to any other function that contains the next bit of code you want to run.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">HTML<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">sets up a base structure to then fill and modify with JS code<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">e.g. place holders for various aspects of the project<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Game structure<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use multiple 2d arrays to store various elements of the game<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>a game map array<\/li>\n\n\n\n<li>a game object array<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Map code<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And, have a set of map codes for items<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each one of these aligns to a sprite number<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>let EMPTY = 0;<br>let FLOOR = 1;<br>let BOX = 2;<br>let WALL = 3;<br>let ALIEN = 4;<br>let BOMB = 5;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXeMkt9MFq4VZtdeqjUZgZP39HArDjT4VCwOLTZcN0GzxjIJnfGwxBNARFRDovSlIrNkPYk5OkMBseBPfHN_PYUXd7xyOzF4-iNbgUvg1-p6a8U3VQT--meG-zyEyv-OVzrhVgL6prtkyulAyJtIUt8owNKG?key=qInBJpW04kC1Sm63MHgkfg\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">An example structure<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">canvas and drawing surface<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">game map array<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">game object array<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">map code<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">tile cell size<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Render<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Managing levels for maps<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Have an array to hold the map and map objects arrays<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So long as the &#8216;exit&#8217; on one map, matches the &#8216;entry&#8217; (or, start point) on the next map, it should be all good.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But if you have multiple entry\/exit points?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Tile map editors<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Tiled<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Or, create your own JS one<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Swapping between maps<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">this is ONE way to do it\u2026<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Phaser for example &#8211; just swap the map data, not the whole &#8216;level&#8217; scene<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">this is a good tutorial: https:\/\/www.youtube.com\/watch?v=bJ166s6qFVU<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">no need to make a scene for each map<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">all you need to do is reset the game scene<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">detect when entering certain areas<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">restart game scene<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">update map and position data<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">in tiled &#8211; make a layer called &#8216;transportation&#8217; with a game object<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The basic logic is:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>create a &#8216;transportation layer&#8217; object which will hold a collection of all the identified transportation objects<\/li>\n\n\n\n<li>get the transportationLayer data from the map data\n<ul class=\"wp-block-list\">\n<li>map to load<\/li>\n\n\n\n<li>tileSet image to use<\/li>\n\n\n\n<li>spawnX location<\/li>\n\n\n\n<li>spawnY location<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>create custom collisions for each of the transportation objects<\/li>\n\n\n\n<li>add in a call back function which is called when there is an overlap between the objects and the player\n<ul class=\"wp-block-list\">\n<li>save the game state in a &#8216;new game state&#8217;\n<ul class=\"wp-block-list\">\n<li>position X to teleport to<\/li>\n\n\n\n<li>position Y to teleport to<\/li>\n\n\n\n<li>previous map position<\/li>\n\n\n\n<li>health<\/li>\n\n\n\n<li>coins<\/li>\n\n\n\n<li>previous position<\/li>\n\n\n\n<li>current frame of char animation<\/li>\n\n\n\n<li>current facingDirection of char<\/li>\n\n\n\n<li>map to load<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>restart scene (with new map data)<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Resources<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>See also: Game loop Games states help you arrange the game into logical groupings Only swap between states when necesary, and only the bits that are necessary &#8211; e.g.&nbsp; \u2026structuring your game loop like this will make it much, much easier to do things such as switching game scenes and levels. It means that you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"wp-custom-template-nlx-the-knowledge-page","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-378","post","type-post","status-publish","format-standard","hentry","category-the-knowledge"],"_links":{"self":[{"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/posts\/378","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/comments?post=378"}],"version-history":[{"count":1,"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/posts\/378\/revisions"}],"predecessor-version":[{"id":379,"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/posts\/378\/revisions\/379"}],"wp:attachment":[{"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/media?parent=378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/categories?post=378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/tags?post=378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}