LibGDX开发 - Tiled地图 之 图层
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LibGDX开发 - Tiled地图 之 图层相关的知识,希望对你有一定的参考价值。
参考技术A 原文链接 http://doc.mapeditor.org/en/latest/manual/layers/术语对照:
Tiled地图支持多种内容(content),这些内容(content)放在在许多不同的图层中。最常见的图层是Tiled图层和对象图层。还有一个Image图层,用来将单一的前景/背景图片放入到地图中。图层的顺序决定了内容的渲染顺序。
图层可以设置为“不可见”,“部分可见”。图层也可以被“锁定”。图层也有“偏移”。(这句话不是很懂。。。 “位移”可以用来将不同图层之间进行定位,比如模拟深度。)
眼睛图标和锁图标显示了各图层的可见性和锁定状态。
可以用Group图层来组织各图层的层次关系。这样在存在大量图层的情况下非常有用。
Tile图层提供了一种用存储Tile数据的方法,TIle数据用来填充大面积区域。Tile数据是简单的数组,只包含了Tile的索引,因此每个位置不能存储额外的信息。仅有的额外信息是少量的标志位,这些标志位允许图形的垂直平铺,水平或者反对角翻转(用于支持90度增量旋转)。
渲染每个Tile层所需的信息与地图一起储存。该地图基于方向和其他属性来指定Tile的位置和渲染顺序。
尽管只能够引用tile,tile层也可用于定义您级别中的各种非图形信息。 通常可以使用特殊的tile set来传达碰撞信息,并且任何类型的不需要自定义属性并且始终与网格对齐的对象也可以被放置在瓦片层上。
Oject 层非常有用,因为对象可以存储很多中 Tile 层不能存储的信息。
Objects 可以自由摆放,缩放,旋转。他们可以有独立的自定义属性。
目前有很多Objects:
所有Objects都可以被命名。在这种情况下,他们的名字可以在他们上访的标签中显示(默认只有选中的Objects)。Objects可以被指定一种类型,这非常有用,因为可以给予类型去指定标签的颜色和可用的自定义属性。对于Tile Objects来说,类型可以从他们的Tile继承。
对于大多数地图类型来说,Objects是基于像素去定位的。唯一的例外是 Isometric地图(不是isometric staggered).对于isometri地图来说,他们的位置被存储在投影坐标中。如果你在等距游戏中为对象使用不同的坐标空间,则需要对应的转换这些坐标。
Object的宽和高也基本上基于像素储存。Isometric地图不懂。。。。
Image层提供了一种在地图中包含单一前景/背景图片的快速方法。当前还作用不大,因为如果你将图片作为Tilesets加入,并放在Tile Objects, 你可以自由的缩放和旋转图片。
使用Image层的唯一优点是避免在使用“选择对象”工具时选择/拖动图像。 但是,从Tiled 1.1版本起,也可以通过锁定包含要避免交互的图块对象的对象层来实现。
Group 层类似于文件夹的形式,可以用来将各层组织起来。当地图中包含大量的图层时,这个功能较为有用。
Group层的可见性,透明图,偏移和锁定属性会应用于所有的子层。
可以使用鼠标将图层轻松地拖入group中。 提升层/下层动作还允许移动层进出组。
Layers can be easily dragged in and out of groups with the mouse. The Raise Layer / Lower Layer actions also allow moving layers in and out of groups.
层可以有更多强大的功能:
如果您喜欢任何这些计划,请通过成为顾客来帮助我更快地到达这个计划。 我得到的支持越多,我可以花更多的时间来改善Tiled!
Working with Layers
A Tiled map supports various sorts of content, and this content is organized into various different layers. The most common layers are the Tile Layer and the Object Layer. There is also an Image Layer for including simple foreground or background graphics. The order of the layers determines the rendering order of your content.
Layers can be hidden, made only partially visible and can be locked. Layers also have an offset, which can be used to position them independently of each other, for example to fake depth.
Layers View
The eye and lock icon toggle the visibility and locked state of a layer respectively.
You use Group Layers to organize the layers into a hierarchy. This makes it more comfortable to work with a large amount of layers.
Tile Layers
Tile layers provide an efficient way of storing a large area filled with tile data. The data is a simple array of tile references and as such no additional information can be stored for each location. The only extra information stored are a few flags, that allow tile graphics to be flipped vertically, horizontally or anti-diagonally (to support rotation in 90-degree increments).
The information needed to render each tile layer is stored with the map, which specifies the position and rendering order of the tiles based on the orientation and various other properties.
Despite only being able to refer to tiles, tile layers can also be useful for defining various bits of non-graphical information in your level. Collision information can often be conveyed using a special tileset, and any kind of object that does not need custom properties and is always aligned to the grid can also be placed on a tile layer.
Object Layers
Object layers are useful because they can store many kinds of information that would not fit in a tile layer. Objects can be freely positioned, resized and rotated. They can also have individual custom properties. There are many kinds of objects:
Rectangle - for marking custom rectangular areas
Ellipse - for marking custom ellipse or circular areas
Polygon - for when a rectangle or ellipse doesn’t cut it (often a collision area)
Polyline - can be a path to follow or a wall to collide with
Tile - for freely placing, scaling and rotating your tile graphics
Text - for custom text or notes (since Tiled 1.0)
All objects can be named, in which case their name will show up in a label above them (by default only for selected objects). Objects can also be given a type, which is useful since it can be used to customize the color of their label and the available custom properties for this object type. For tile objects, the type can be inherited from their tile.
For most map types, objects are positioned in plain pixels. The only exception to this are isometric maps (not isometric staggered). For isometric maps, it was deemed useful to store their positions in a projected coordinate space. For this, the isometric tiles are assumed to represent projected squares with both sides equal to the tile height. If you’re using a different coordinate space for objects in your isometric game, you’ll need to convert these coordinates accordingly.
The object width and height is also mostly stored in pixels. For isometric maps, all shape objects (rectangle, ellipse, polygon and polyline) are projected into the same coordinate space described above. This is based on the assumption that these objects are generally used to mark areas on the map.
Image Layers
Image layers provide a way to quickly include a single image as foreground or background of your map. They are currently not so useful, because if you instead add the image as a Tileset and place it as a Tile Object, you gain the ability to freely scale and rotate the image.
The only advantage of using an image layer is that it avoids selecting / dragging the image while using the Select Objects tool. However, since Tiled 1.1 this can also be achieved by locking the object layer containing the tile object you’d like to avoid interacting with.
New in Tiled 1.0
Group Layers
Group layers work like folders and can be used for organizing the layers into a hierarchy. This is mainly useful when your map contains a large amount of layers.
The visibility, opacity, offset and lock of a group layer affects all child layers.
Layers can be easily dragged in and out of groups with the mouse. The Raise Layer / Lower Layer actions also allow moving layers in and out of groups.
Future Extensions
There are many ways in which the layers can be made more powerful:
Ability to lock individual objects (#828).
Moving certain map-global properties to the Tile Layer (#149). It would be useful if one map could accommodate layers of different tile sizes and maybe even of different orientation.
If you like any of these plans, please help me getting around to it faster by becoming a patron. The more support I receive the more time I can afford to spend improving Tiled!
开源java游戏框架libgdx专题-13-开发工具-地图的使用
支持libGDX的地图编辑器有很多种,其中比较常用的工具为Tiled地图工具。Tiled是一款非常好用的地图编辑器。下载地址:http://www.mapeditor.org
TiledMap类:
又称瓦片地图类,主要用于解析地图编辑器所保存的地图文件。
常用方法:
dispose():销毁当前地图中的所有纹理
getLayers():获取当前地图中的所有图层,返回值为MapLayers类型变量
getProperties():获取当前地图的所有属性,返回值为MapProperties类型变量
getTileSets():获取地图瓦片集,返回值为TileDMapTileSets类型变量
OrthogonalTiledMapRenderer类:
地图渲染器,主要负责地图的渲染工作
常用方法:
beginRender():开始绘制
dispose():销毁所有资源对象
endRender():结束绘制,该方法在所有图层绘制之后执行
getMap():获取当前TiledMap对象
getSpriteBatch():获取当前地图的精灵画笔
getUnitScale():获取绘制缩放比例
render():绘制地图
render(int[] layers):绘制地图的制定图层
setMap(TiledMap map):设置绘制的地图
setView(Matrix4 projection,float x,float y,float width,float height):设置视距
setView(OrthographicCamera camera):设置绘制相机
renderTileLayer(TiledMapTileLayer layer):绘制指定图层
代码实例:
1 package com.mygdx.usemap; 2 3 import com.badlogic.gdx.ApplicationAdapter; 4 import com.badlogic.gdx.Gdx; 5 import com.badlogic.gdx.graphics.GL20; 6 import com.badlogic.gdx.graphics.OrthographicCamera; 7 import com.badlogic.gdx.maps.tiled.TiledMap; 8 import com.badlogic.gdx.maps.tiled.TmxMapLoader; 9 import com.badlogic.gdx.maps.tiled.renderers.OrthoCachedTiledMapRenderer; 10 /** 11 * 使用地图 12 * @author Jack(乐智) 13 * @blog dtblog.cn 14 * @qq 984137183 15 */ 16 public class MainGame extends ApplicationAdapter { 17 18 //声明地图加载器 19 private TiledMap map; 20 //声明地图渲染器 21 private OrthoCachedTiledMapRenderer renderer; 22 //声明相机 23 private OrthographicCamera camera; 24 25 @Override 26 public void create() { 27 //加载地图 28 map=new TmxMapLoader().load("test2.tmx"); 29 //实例化地图渲染器 30 renderer=new OrthoCachedTiledMapRenderer(map); 31 //实例化相机 32 camera=new OrthographicCamera(); 33 //设置相机视距 34 camera.setToOrtho(false, 576, 552); 35 } 36 37 @Override 38 public void render() { 39 //设置屏幕背景色为黑色 40 Gdx.gl.glClearColor(1, 1, 1, 1); 41 //清屏 42 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 43 //更新相机状态 44 camera.update(); 45 //渲染地图比例 46 renderer.setView(camera); 47 //渲染地图 48 renderer.render(); 49 } 50 51 }
地图配置文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <map version="1.0" orientation="orthogonal" renderorder="right-down" width="24" height="24" tilewidth="24" tileheight="23" nextobjectid="1"> 3 <tileset firstgid="1" name="test" tilewidth="24" tileheight="23" tilecount="8" columns="8"> 4 <image source="ground248.png" width="192" height="24"/> 5 </tileset> 6 <tileset firstgid="9" name="2" tilewidth="24" tileheight="23" tilecount="8" columns="8"> 7 <image source="ground288.png" width="192" height="24"/> 8 </tileset> 9 <layer name="块层 1" width="24" height="24"> 10 <data encoding="base64" compression="zlib"> 11 eJxjZGBgYBzhmJ0EtcxAzEJDt7ACMdsgCJNRPHCYlmkMlNZhaYyUdE+OPfQIKyYamsuEZD4TkXbB4o5QHDLhwMTGHb5ygljzBmO4D3U8EuoHeuVtemAALkgCuw== 12 </data> 13 </layer> 14 </map>
地图资源文件:
效果:
原文由博主 乐智 编辑撰写,版权归博主所有。
原文地址 http://www.dtblog.cn/1160.html 转载请注明出处!
以上是关于LibGDX开发 - Tiled地图 之 图层的主要内容,如果未能解决你的问题,请参考以下文章