09. Working with Tiled Shapes

Introduction

Tiled object layers support “tile objects” (images) and geometric shapes. The following geometric shapes can be created in Tiled:

  • Rectangle
  • Ellipse (similar to Oval)
  • Polygon
  • Polyline
  • Tile (Image)

The first four types (geometric shapes) are added to the LayeredTileMap.ShapeCollections list when loaded. These can be accessed and used at run time for custom collision, such as the creation of triggers.

Creating Shapes on an Object Layer

Shapes can be added to any object layer (such as the existing EntityObjectLayer). To keep our objects organized, we’ll create a new object layer:

  1. In Tiled, click the Add Layer button
  2. Select “Add Object Layer”
  3. Enter the name “ShapesLayer”

To add a rectangle:

  1. In Tiled, click the Insert Rectangle button
  2. Push and drag the left mouse button to draw a Rectangle

  3. Enter the name “Rectangle1” for the new rectangle. This is needed to find the shape at run time.

As always, don’t forget to save your changes in Tiled.

Working with ShapeCollections

Each object layer with one or more shape is loaded as a ShapeCollection at runtime. This tutorial covers the basics of working with a ShapeCollection, but more information can be found on the ShapeCollection page.

All ShapeCollections are invisible by default, but can be made visible. Add the following method to GameScreen :

We could also selectively make the shape collections visible:

We need to modify CustomInitialize  to call ShapeTutorialLogic :

Accessing Individual Shapes

All shapes are added to the shape collection’s Polygons property, regardless of shape type. Even rectangles are added as Polygon instances to support rotation.

We can access the Polygon instance as shown in the following code, which is another alternative to making it visible: