# LayeredTileMap

### Introduction

The LayeredTileMap object is the runtime type for the TMX file format (the native file format for Tiled). Whenever a TMX file is added to FlatRedBall, it will (by default) be loaded into a LayeredTileMap at runtime. The LayeredTileMap represents a collection of [MapDrawableBatches](https://github.com/flatredball/FlatRedBallDocs/blob/main/frb/docs/index.php). It contains one [MapDrawableBatch](https://github.com/flatredball/FlatRedBallDocs/blob/main/frb/docs/index.php) per layer in the source TMX file. The LayeredTileMap class inherits from [FlatRedBall.PositionedObject](https://github.com/flatredball/FlatRedBallDocs/blob/main/frb/docs/index.php) so it can be moved and attached to other [PositionedObjects](https://github.com/flatredball/FlatRedBallDocs/blob/main/frb/docs/index.php). Each MapDrawableBatch is attached to its parent LayeredTileMap, and can be moved independently by changing its [RelativePosition](https://github.com/flatredball/FlatRedBallDocs/blob/main/frb/docs/index.php).

### Common Usage

The easiest way to use a TMX file is to create a standard GameScreen and Level. This can be accomplished using one of the following methods:

1. Run the Wizard when creating a new project - this will automatically add TMX files to your project.

   ![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-6baefdc77d5601c2946c09ae0b0db9ce7aaa7b49%2F2021-10-img_6166edc84073b.png?alt=media) Existing (empty) projects can also run the wizard through the Quick Actions tab.

   ![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-9dbef57ab88719a4ee80c82392c0227b2686580b%2F2021-10-img_6166ee164da74.png?alt=media)
2. Create a GameScreen and Level screens and check the options for creating TMX files. ![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-fcd27ee502f39c0ffcda451ec31f42a5a5e0a677%2F2021-10-img_6166ee3ba4c28.png?alt=media)

   ![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-657c9381ff82edc999a818cbbcd6a8adc0863c0a%2F2021-10-img_6166ee644e2ae.png?alt=media)

### Manually Adding a MapDrawableBatch From TMX to a Screen

If you are not using the GameScreen/Level pattern, you can add your own TMX files to Screens manually. To add a LayeredTileMap from an existing TMX:

1. Create a TMX file in Tiled
2. Save the TMX file in a location relative to your game's content folder. Be sure that all PNG files and TSX files (tileset files) are also relative to the game content folder
3. Create a game screen
4. Drag+drop the TMX file into your Screen's **Files** folder

<figure><img src="https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-21bb838d12937a928e658d0b88cfea75da4008e3%2F2016-01-2020_February_04_172542.gif?alt=media" alt=""><figcaption></figcaption></figure>

No additional code is necessary - your map will now show up in the screen.

### Creating Entities From a TMX

If using the GameScreen/Level pattern, FlatRedBall will automatically set up a Map object which can be used to create entities. If you are creating your own map object manually and you would like to create entities from this map, a small amount of extra work is needed.

#### Creating a Map object for Entity Creation in Glue

Once you have a TMX file in your screen's Files folder, you can create a Map object by drag+dropping the TMX onto the Screen's Objects folder.

<figure><img src="https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-2c63f3e914882def7541b7dc01bb774f2121fca8%2F2016-01-13_08-39-26.gif?alt=media" alt=""><figcaption></figcaption></figure>

Once the Map object has been created, its **Create Entities From Tiles** property can be checked.

![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-3ee05ca937f0c15a61f3926e6ec567bf524e1b66%2F2021-10-img_6166f03c272a0.png?alt=media)

### LayeredTileMap as a list of MapDrawableBatches

The LayeredTileMap is mostly a List of MapDrawableBatches. For example, you can print out information about a MapDrawableBatch as follows:

```
string information = "This map has " + TestLevel.MapLayers.Count + " layers\n";

foreach (var layer in TestLevel.MapLayers)
{
    information += "Number of verts: " + layer.Vertices.Length + "\n";

}
```

![MapLayerInfo.PNG](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-94d217ed120c67716af15cbfe5b452a417e6d217%2Fmigrated_media-MapLayerInfo.PNG?alt=media) \[subpages depth="1"]
