> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/flatredball/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/flatredball/tiled-plugin/using-the-tiled-plugin/09-working-with-tiled-shapes.md).

# Working with Tiled Shapes

### Introduction

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

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

![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-391eb7453787b47457d4be170884522e57c300b5%2F2016-08-img_57b76aa31dfed.png?alt=media)

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

### Map.ShapeCollections vs. Collidable Entities

As shown in the [Creating Entities from Object Layers page](/flatredball/tiled-plugin/using-the-tiled-plugin/07-creating-entities-from-object-layers.md), it is possible to create collidable entities using Tiled. These collidable entities ultimately hold references to shapes which can be accessed in code. Some situations (such as triggers in a level) will be easier to create with "raw shapes" rather than entities. These free floating shapes can be of any shape or size, and creating them in Tiled lets us visualize the shapes in context of the entire level.

### Creating Shapes on an Object Layer

Shapes can be added to any object layer (such as the existing GameplayObjectLayer in Level2Map), but we'll keep things organized by creating a dedicated ShapesLayer.

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

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

To add a rectangle:

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

<figure><img src="https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-5bfca6665f3de999111810a928a73babccf170c1%2F2016-08-2021_February_20_153435.gif?alt=media" alt=""><figcaption></figcaption></figure>

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

   ![](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-6ed44bb45e7d011fac1af3ffbfaa76e13cc8e920%2F2016-08-img_57b770362661b.png?alt=media)

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 separate ShapeCollection at runtime. This tutorial covers the basics of working with a ShapeCollection, but more information can be found on the [ShapeCollection page](/flatredball/api/flatredball/math/geometry/shapecollection.md). All ShapeCollections are invisible by default, but can be made visible. Add the following method to GameScreen :

```csharp
private void ShapeTutorialLogic()
{
    foreach(var shapeCollection in Map.ShapeCollections)
    {
        shapeCollection.Visible = true;
    }
}
```

We could also selectively make the shape collections visible:

```csharp
private void ShapeTutorialLogic()
{
    var shapeCollection =
    Map.ShapeCollections
        // This name matches the layer name 
        .FirstOrDefault(item => item.Name == "ShapesLayer");

    if(shapeCollection != null)
    {
        shapeCollection.Visible = true;
    }
}
```

We need to modify CustomInitialize to call ShapeTutorialLogic :

```csharp
void CustomInitialize()
{
    ...
    ShapeTutorialLogic();
}
```

![White rectangle from the Map's ShapeCollection](https://951240982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fye9Ufg3vzJxwX5Hk%2Fuploads%2Fgit-blob-de842dd74a7e11fc24c8aa73952d9722f66df81d%2F2021-02-img_60318fee8cc7f.png?alt=media)

### Accessing Individual Shapes

The TMX loading code performs the following logic when deciding where to add shapes:

* A perfect circle (not ellipse) are added to the ShapeCollection's Circles list
* An un-rotated rectangle are added to the ShapeCollection's Rectangles list
* All other shapes (ovals, rotated rectangles, and polygons) are added to the ShapeCollection's Polygons list

We can perform shape-specific logic by accessing the individual lists. For example, we can access the Rectangles instance as shown in the following code:

```csharp
private void ShapeTutorialLogic()
{
    var shapeCollection = Map.ShapeCollections
        // This name matches the layer name 
        .FirstOrDefault(item => item.Name == "ShapesLayer");

    var rectangle = shapeCollection?.AxisAlignedRectangles
        ?.FirstOrDefault(item => item.Name == "Rectangle1");

    if(rectangle != null)
    {
        rectangle.Visible = true;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.flatredball.com/flatredball/tiled-plugin/using-the-tiled-plugin/09-working-with-tiled-shapes.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
