Creating Entities from Object Layers
Last updated
Last updated
In the previous tutorial we covered how to create entities from tiles. This approach can simplify entity creation, and works great for entities which are uniform (each instance is identical aside from position). Some entities require custom properties per-instance. For example, consider a game where players can walk through doors to enter other maps. In this case, each door entity needs to have a custom property for which level it links to. This walk-through shows how to create object layers in Tiled and how to create entities using these objects.
For this tutorial we will be creating a Door entity which can be used to move the player from one level to the next. This entity will need to have collision so that the player can collide with it and we can respond to the collision. To create a Door entity:
Select the Quick Actions tab in the FRB editor
Click Add Entity
Enter the name Door
Check the Circle check box
Leave the defaults and click OK
Now you will have a Door entity. We should change the color of the door so it doesn't look like the player (which is currently a white circle). To do this, change the CircleInstance's Color value to Green.
We need to create a variable for which level to load when the Player collides with the Door:
Right-click on the Variables folder under the Door entity
Select Add Variable
Select the Create a new variable option
Select string as the Type
Enter the Name LevelToGoTo
Click OK
The name of the variable that we created (LevelToGoTo) needs to match the name of the property that we will be adding to the tile later in this guide. Note that if using a entity types XML, the property will automatically be added.
Tiled object layers allow placing geometric shapes (such as rectangles, circles, and custom polygons) as well as free-floating images (not snapped to the grid). These images (also referred to as "tiles" in the Tiled program) can be used purely for non-interactive visuals or can be used to place entities. Objects can be created and added to tile maps, where each instance has all of its properties set individually, or objects can be given default properties in the tileset. First we'll set up the common properties on our Door tile:
Open TiledIcons.tsx in Tiled (click the edit button if the tsx file is not already open)
Select the door tile
Enter Door as the Type. Note that this may already be the default.
Click the + button to add a new property
Enter the name LevelToGoTo
Verify the type is string
We won't assign the value of LevelToGo yet, we'll do that per-instance. Now that we've defined the Door tile properties, we'll add a Door to the Level2Map.tmx file. Don't forget to save your tileset in Tiled. First we'll make an object layer:
Open Level2Map.tmx in Tiled
Click the Add Layer button
Select Object Layer
Name the new layer GameplayObjectLayer
Next we'll add an instance of the door to the EntityObjectLayer:
Verify that EntityObjectLayer is selected
Select the tile to be placed in the tile set
Click on the tile map to place the tile. Be careful - if you place the door in the same place as where the player starts, the player will immediately collide with the door, and will move to the next level once we add the collision code.
Notice that the placed door may not align with the tiles. If you wish to make it align, you use the Select Objects command to select the tile and either move it visually or change its X an Y properties:
Alternatively, holding the CTRL key will snap objects to the grid while you place them. Now we can add the LevelToGoTo property to our placed tile:
Click the Select Objects button (if not already selected)
Select the placed Door object (if not already selected)
Enter the value Level1 for the LevelToGoTo property. More generally, the door's LevelToGoTo property should be the name of the screen that we want to go to when the Player entity touches the Door.
Don't forget to save the tile map.
If we run our game now, we should see the Door tile that we placed in Tile replaced by a green circle, which is the collision of the Door entity:
If you are not seeing the Door entity in the game, there may be a problem with the Tiled map, the inheritance in your game, or settings on the entity. Fortunately, the process that FlatRedBall uses to create entities on object layers is the same as the process for creating entities on tile layers. Therefore, to troubleshoot the problem you should go back to the previous tutorial and see if you can create entities from tiles. The previous tutorial also includes troubleshooting steps.
We can use the Door entity to navigate between levels. We'll check collision between the Player and all doors in the DoorList object (our game only has one door, but it could be expanded to have more). If a collision occurs, we'll go to the level set by the Door's LevelToGoTo variable. First we'll create a collision relationship between the PlayerList and DoorList in our GameScreen:
Expand the GameScreen Objects folder in Glue
Drag the PlayerList onto the DoorList to create a CollisionRelationship
This time we won't change the Collision Physics because the purpose of the door isn't to block the movement of the player. Instead, we'll create an event which will contain code to move to a different level. To do this:
Scroll down to the bottom of the Collision tab of the newly-created CollisionRelationship
Click the Add Event button
If asked, leave the defaults on the New Event window and click OK
Collision events are added to the Events file for the current screen. In this case, the events are added to GameScreen.Events.cs. To add handle the code:
Open the project in Visual Studio
Open GameScreen.Event.cs. Notice that the newly created collision event will be located under the GameScreen.cs file
Enter the code to move to a screen based on the Door's LevelToGoTo
Now when the player collides with the door in Level2, the game will move to Level1.
Now that we've covered object layers, you can create entities which are either all identical on tile layers or which have custom properties. Here's some things to try next:
Add a door in Level1 which goes back to Level2. Remember, you don't have to set up the CollisionRelationship again. It's already set up in GameScreen which covers all levels.
Try adding collision between the player and monsters. On collision, you can handle the event by restarting the screen (indicating the player has died)