Tiled Files in Entities
Last updated
Last updated
Tiled files (.tmx) can be included in entities to create visuals which are tile-based. When used in entities, .tmx files are loaded into LayeredTileMap instances which are attached to the entity. This means that the entire tile map inherits the following from the entity that it is a part of:
Position
Rotation
Layer
Visibility
To add a .tmx file to an entity, follow these steps:
Create a .tmx file using the Tiled program. Make sure this .tmx file is saved in the folder structure of your project's content folder.
Create an Entity
Drag+drop the .tmx file onto the entity
Drag+drop the file from inside the entity's Files folder onto the entity's Objects folder
Select Entire File (Layered TileMap) in the Source Name dropdown
Click OK
LayeredTileMap objects use their top-left corner as their origin. This means that the center of the entity will align with the top-left corner of the map. We can observe this by adding a Circle object to an entity which also has a Tiled object.
We can center the tiles on the center of the entity by following these steps:
Open the .tmx file in Tiled
Resize the map so that it there is no empty space around the object
Save the file
Add the following code to the entity's CustomInitialize :
Now the center of the tilemap will align with the center of the entity.
Tiled files added to entities can also be optionally loaded. Games may need to optionally load .tmx files if a single entity can have different visuals, such as a Boss entity which may be drawn by one of many .tmx files. To add an optionally .tmx file to an entity, follow these steps:
Drag+drop the .tmx file into the entity
Select the .tmx file in the Files folder
Change its LoadedOnlyWhenReferenced to True
If your entity has a LayeredTileMap object (which we called EntireFile earlier) remove it - it needs to be created in code instead.
Modify your entity so that its custom code creates and destroys the entity in CustomInitialize and CustomDestroy as shown in the following code:
The LayeredTileMap implements the IDrawableBatch interface, which means it is rendered outside of the FlatRedBall engine. This means that adding an instance of a LayeredTileMap to your game will add at least one render break, and possibly two if it is drawn inbetween standard FlatRedBall objects. Furthermore, each instance of an entity using a LayeredTileMap will introduce additional render breaks. Therefore, you may consider other ways to render your entity visuals if you plan on having a large number of entity instances in your game at the same time - especially for mobile platforms.
The entity will now display the contents of the .tmx file when included in a screen.
CircleInstance is centered on the entity, so we can see that the tiled object's top-left corner aligns with the center of the entity. We can further observe the impact of the origin by rotating the entity. We'll do this by adding the following code to the entity's CustomActivity method: