Adding Collision
Last updated
Last updated
Collisions are used in almost every game. Collisions are often used for the following functionality:
Solid collision to keep players, enemies, and bullets from moving between rooms or out of bounds
Damage collision to deal damage to players, slow movement, or heal players
This tutorial will show how to work with the solid collision, and how to add new types of collision to your game.
By default our game already has SolidCollision defined in the GameScreen. If you used the New Project Wizard, then GameScreen already has a SolidCollision object. If you followed the manual steps in the previous tutorial, then you will have added SolidCollision manually.
Until now the SolidCollisions have been invisible, and we haven't created anything to collide against. Therefore, there hasn't been any functionality yet either. We can verify that the SolidCollision shape collection is actually being created by making it visible. To do this:
Expand the GameScreen in FlatRedBall
Select the SolidCollision object. Note that it is blue, indicating that it is visible to children screens such as Level1
Click the Variables tab
Check the Visible property
If you run the game now, you will see that all solid collision tiles are outlined in white. This is the SolidCollision object.
You may want to keep collision visibility set to true to help diagnose problems in your game, but you should turn it to false when your game is ready to distribute.
As mentioned above, the SolidCollision shape collection is a common object, so the FRB editor provides quick defaults for this type. Your game may need additional TileShapeCollections for additional types of collision. For example, your game may need special collision for spikes that the player can walk over, but which deal damage to the player.
For the rest of this tutorial we will cover how to create a new TileShapeCollection. At a high level, the steps are:
Decide on a tile to use for collision
Set the Type on the tile
Create a new TileShapeCollection in GameScreen
Configure the TileShapeCollection to be created from the tile type specified earlier
The first step is to decide which tile you would like to use for your collision. As we mentioned before, all gameplay tiles (such as collision) should be on the TiledIcons. To mark a tile as collidable:
Open Tiled
Click the TiledIcons tileset
Click the Edit icon to edit the TiledIcons tileset
Select a Tile that you would like to use for collision
Enter a Class for that tile.
Don't forget to save your tileset file
Now that the type has been set and the tileset has been saved, you can place the tile in your level. Make sure to place it on the GameplayLayer in case your game has multiple layers.
Be sure to save your map after adding tiles.
Now that we have a new tile type in our game, we can add another TileShapeCollection. To do this:
In FlatRedBall, select the GameScreen
Click the Quick Actions tab
Select the Add Object to GameScreen button
Type or look for TileShapeCollection in the window and select this option
Enter the name for your tileset. Usually this should match the type of your tile.
Click OK
TileShapeCollections usually come from specific tiles in tile maps. We'll set up the TileShapeCollection created in the previous section here. To do this:
Select the newly-created TileShapeCollection
Click the TileShapeCollection Properties tab
Select the From Type option
Select Map as the source TMX
Select the matching type in the Type dropdown
Notice that Remove Tiles is checked by default. Uncheck this option if you would like to see the tiles in game. We should also turn on collision visibility to make sure it is created as we expect.
If you run your game, you will see the collision in game. The tiles will be removed if the Remove Tiles checkbox was left checked.
As mentioned earlier, TileShapeCollection instances can also be created in code. For more information on creating collision code from Tiles, see these links: