2.4. Working with Objects Document

Previous: Working with Layers - Next: Working with Tile AnimationsReturn to Index


The TKObject class represents an object on a TKObjectLayer. The various types of objects are not subclasses but rather differentiated based on TKObject’s type property, a TKObjectType enum.

The proeprties and methods for object types are largely identical, except for certain special cases where a property may not be valid for a given object type. This is noted in the class reference.

Object Transform

The object’s position is in the layer’s (map’s) coordinate system. Its rotation property is in degrees.

The object’s size determines the width and height of rectangle and tile objects, as well as the shape of ellipse objects. Ellipse objects whose width and height are equal are simply circles.

The path and vertices of an object are in the object’s local coordinate space, ie relative to the object’s position.

Identifying Objects and their Type

To determine an object’s type you have several options:

  • The TKObjectType (rectangle, ellipse, polygon, polyline, tile) assigned to the objectType property.
  • The name property, a string editable in an object’s properties pane in Tiled (labelled “Name”).
  • The type property, a string editable in an object’s properties pane in Tiled (labelled “Type”).
  • The objectID which is a consecutive, unique number applied to every object and shown in the object’s properties pane in Tiled as “ID”.

The name and type properties allow you to customize objects, besides using custom properties. Specifically type is made for differentiating objects, ie indicating a concrete node class or unit type.

Object Type Differences

The objectType property will tell you what to make of certain properties.

  • The position property is set to the first point of a polygon or polyline object.
  • The size property has no meaning (is CGSizeZero) for polygon and polyline objects.
  • The tile property is only valid (non-zero) for tile objects.
  • The vertices property and the createLayerCoordVectorFromVertices will return nil for ellipse objects. Unsurprisingly, vertexCount will be 0 for ellipse objects.
  • The obstacle method will return nil for polyline objects, as well as ellipse objects that aren’t circles (same width and height).

Using the Object’s Shape

There are several ways to use the object’s shape.

  • The path property returns a CGPathRef instance describing the object’s shape.
  • The vertices property returns the object’s vertices as a CGPoint* buffer. The number of vertices is returned by the vertexCount property.
  • The createLayerCoordVectorFromVertices method is the same as vertices except that it’s a vector_float2* buffer for use with GameplayKit.
  • The obstacle method will return a GKPolygonObstacle or GKCircleObstacle for use with GameplayKit.

Previous: Working with Layers - Next: Working with Tile AnimationsReturn to Index