{"id":90,"date":"2015-07-25T17:52:00","date_gmt":"2015-07-25T17:52:00","guid":{"rendered":"https:\/\/therenaissanceraven.com\/?p=90"},"modified":"2015-07-25T17:52:00","modified_gmt":"2015-07-25T17:52:00","slug":"libgdx-creating-the-map-or-platform-tiled","status":"publish","type":"post","link":"https:\/\/therenaissanceraven.com\/?p=90","title":{"rendered":"*libGDX* Creating the Map or Platform &#8211; Tiled"},"content":{"rendered":"<p><a href=\"https:\/\/www.blogger.com\/blogger.g?blogID=3868422445992620997\" style=\"clear: right; float: right; margin-bottom: 1em; margin-left: 1em;\"><\/a>To create the map that will be used in the game, we use an amazing free open source program called Tiled. You are able to download it from here:<\/p>\n<p><a href=\"http:\/\/www.mapeditor.org\/\">http:\/\/www.mapeditor.org\/<\/a><\/p>\n<p>Open opening this browser, you are able to create a new map. For mine i kept the tile size at 32, and made the map 25&#215;15 as to keep the total pixel size as 800&#215;480 so no screen resizing would be needed in our 800&#215;480 size game. For the tile layer format, Base64 zlib is recommended as the best for tmx maps to use withing libGDX.<\/p>\n<p>Open creating the new map you are exposed to the following:<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"http:\/\/2.bp.blogspot.com\/-VeglSa4GwCw\/VbP1EnpxWUI\/AAAAAAAAAYY\/bAmIgYgI00k\/s1600\/Pic%2B4.png\" style=\"margin-left: 1em; margin-right: 1em;\"><img loading=\"lazy\" decoding=\"async\" border=\"0\" height=\"359\" src=\"https:\/\/2.bp.blogspot.com\/-VeglSa4GwCw\/VbP1EnpxWUI\/AAAAAAAAAYY\/bAmIgYgI00k\/s640\/Pic%2B4.png\" width=\"640\" \/><\/a><a href=\"https:\/\/www.blogger.com\/blogger.g?blogID=3868422445992620997\" style=\"margin-left: 1em; margin-right: 1em;\"><\/a><\/div>\n<p>Tiles &#8211; Simply a graphic you can use to build your map. These can be easily made in paint as I do, or very nice royalty free ones are available online. <br \/>Layers &#8211; These are you layers in to which you can add different tiles, such as a pathways\/walls\/traps, any kind of tile type that you would wish to distinguish from others.<\/p>\n<p><a href=\"https:\/\/www.blogger.com\/blogger.g?blogID=3868422445992620997\" style=\"clear: right; float: right; margin-bottom: 1em; margin-left: 1em;\"><\/a><a href=\"https:\/\/www.blogger.com\/blogger.g?blogID=3868422445992620997\" style=\"clear: left; float: left; margin-bottom: 1em; margin-right: 1em;\"><\/a>Now simply just click the tile from tile set and add it to the map as your heart desires, to achieve a result such as the following with two layers of &#8220;pathway&#8221; (mine is called Tile Layer 1) and &#8220;barricade&#8221;:<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/1.bp.blogspot.com\/-Xm5-dOxumWs\/WH558OT2-QI\/AAAAAAAABeg\/NbaG044TPKYv0J17lalJ5u890QX4Wkx7ACLcB\/s1600\/Capture.PNG\" style=\"margin-left: 1em; margin-right: 1em;\"><img loading=\"lazy\" decoding=\"async\" border=\"0\" height=\"397\" src=\"https:\/\/1.bp.blogspot.com\/-Xm5-dOxumWs\/WH558OT2-QI\/AAAAAAAABeg\/NbaG044TPKYv0J17lalJ5u890QX4Wkx7ACLcB\/s640\/Capture.PNG\" width=\"640\" \/><\/a><\/div>\n<p>And then it is as simple as that, your map is created.<\/p>\n<p><a href=\"https:\/\/www.blogger.com\/blogger.g?blogID=3868422445992620997\" style=\"clear: left; float: left; margin-bottom: 1em; margin-right: 1em;\"><\/a>Now to add it to the game the following must be done in addition to adding the tiled map into your assets folder. In your main class (e.g. GameScreen) which renders the game, you must define two new variables:<br \/><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\">1<br \/>2<br \/>3<br \/>4<br \/>5<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\">    <span style=\"color: #888888;\">\/\/Creates a reference for our tiled map<\/span><br \/>    <span style=\"color: #008800; font-weight: bold;\">private<\/span> TiledMap tiledMap <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> TmxMapLoader<span style=\"color: #333333;\">().<\/span><span style=\"color: #0000cc;\">load<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"testmap.tmx\"<\/span><span style=\"color: #333333;\">);<\/span><br \/><br \/>    <span style=\"color: #888888;\">\/\/creates a reference for our tiledmaprenderer whichs loads are tiled map<\/span><br \/>    <span style=\"color: #008800; font-weight: bold;\">private<\/span> OrthogonalTiledMapRenderer mapRenderer <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> OrthogonalTiledMapRenderer<span style=\"color: #333333;\">(<\/span>tiledMap<span style=\"color: #333333;\">);<\/span><br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Then in the render section, this code will cause the map to be rendered, along with an optional code to center the camera around your character&#8217;s location (you will have to create the character class in order to copy this):<br \/><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> 7<br \/> 8<br \/> 9<br \/>10<br \/>11<br \/>12<br \/>13<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #888888;\">\/\/makes the camera fit onto the available screen<\/span><br \/>        game<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">gameBatch<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">setProjectionMatrix<\/span><span style=\"color: #333333;\">(<\/span>camera<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">combined<\/span><span style=\"color: #333333;\">);<\/span><br \/>        <span style=\"color: #888888;\">\/\/sets the map renderer to display in the camera<\/span><br \/>        mapRenderer<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">setView<\/span><span style=\"color: #333333;\">(<\/span>camera<span style=\"color: #333333;\">);<\/span><br \/>        <span style=\"color: #888888;\">\/\/renders the map<\/span><br \/>        mapRenderer<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">render<\/span><span style=\"color: #333333;\">();<\/span><br \/><br \/>        <span style=\"color: #888888;\">\/\/If you wish for the camera to follow to character<\/span><br \/>        camera<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">position<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">x<\/span> <span style=\"color: #333333;\">=<\/span> character<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">charPosition<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">x<\/span><span style=\"color: #333333;\">;<\/span><br \/>        camera<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">position<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">y<\/span> <span style=\"color: #333333;\">=<\/span> character<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">charPosition<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">y<\/span><span style=\"color: #333333;\">;<\/span><br \/><br \/>        <span style=\"color: #888888;\">\/\/updates camera view<\/span><br \/>        camera<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">update<\/span><span style=\"color: #333333;\">();<\/span><br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>To create the map that will be used in the game, we use an amazing free open source program called Tiled. You are able to download it from here: http:\/\/www.mapeditor.org\/ Open opening this browser, you are able to create a new map. For mine i kept the tile size at 32, and made the map&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-90","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/posts\/90","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=90"}],"version-history":[{"count":0,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/posts\/90\/revisions"}],"wp:attachment":[{"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}