TiledObjectType

@objc
public protocol TiledObjectType : TiledCustomReflectableType

The TiledObjectType protocol defines a basic node type with a data structure for mapping custom Tiled properties to SpriteKit nodes. The TiledObjectType.parseProperties method maps Tiled node custom properties to SpriteKit/SKTiled properties.

Objects conforming to this protocol will automatically receive properties from the source Tiled scene, unless supressed by setting the nodes’s TiledObjectType.ignoreProperties property.

Properties

  • uuid: unique object id.
  • type: Tiled object type.
  • properties: object of custom Tiled properties.
  • ignoreProperties: ignore Tiled properties.
  • parseProperties: parse Tiled attributes (with optional completion handler).

Usage

Querying a property is simple. Simply use the hasKey function:

tiledObject.hasKey("floorColor")

If a property value is convertible to other types (such as boolean, integer or double), you can use one of several convenience methods to query the value:

// query a Tiled integer property
let score = tiledObject.intForKey("scoreValue") ?? 0

// query a TIled boolean property
let isDynamic = tiledObject.boolForKey("isDynamic") == true

Finally, you can subscript the properties dictionary with any object conforming to the TiledObjectType protocol:

// use a subscript to query a property or add a new one
let floorColor = tiledObject["floorColor"]
tiledObject["orientation"] = "bottom-left"

For more information, see the Tiled Properties page in the official documentation. Also see the Tiled Properties page in the Tiled Documentation.

  • Unique object id (layer & object names may not be unique).

    Declaration

    Swift

    @objc
    var uuid: String { get }
  • Object type property as parsed from the Tiled scene.

    Declaration

    Swift

    @objc
    var type: String! { get set }
  • Storage for custom Tiled properties. These properties are set in the Tiled scene.

    Declaration

    Swift

    @objc
    var properties: [String : String] { get set }
  • Ignore custom node properties.

    Declaration

    Swift

    @objc
    var ignoreProperties: Bool { get set }
  • Parse function (with optional completion block).

    Declaration

    Swift

    @objc
    optional func parseProperties(completion: (() -> Void)?)

Extensions

  • mirrorChildren() Extension method

    Returns a mirror representation of the properties attribute.

    Declaration

    Swift

    public func mirrorChildren() -> [(label: String, value: Any)]

    Return Value

    array of mirror child values.

  • getValue(for:defaultValue:) Extension method

    Query and return a custom property. Optionally setting a default value.

    Declaration

    Swift

    public func getValue(for key: String, defaultValue: String? = nil) -> String?

    Parameters

    key

    property key.

    default

    default value, if the property doesn’t already exist.

    Return Value

    custom property value.

  • setValue(for:_:) Extension method

    Sets a named SKTiled property.

    Declaration

    Swift

    public func setValue(for key: String, _ value: String)

    Parameters

    key

    property key.

    value

    property value.

Properties Parsing

  • subscript(_:) Extension method

    Return a string value for the given key, if it exists.

    Usage

     if let name = tileData["name"] {
        print("tile data is named '\(name)'")
     }
    

    Declaration

    Swift

    public subscript(key: String) -> String? { get set }

    Parameters

    key

    String key to query.

  • hasProperties Extension method

    Returns true if the node has stored properties.

    Declaration

    Swift

    public var hasProperties: Bool { get }
  • propertiesString Extension method

    Returns a string representation of the node’s properties.

    Declaration

    Swift

    public var propertiesString: String { get }
  • Returns an attributed string representation of the node’s properties.

    Declaration

    Swift

    public func propertiesAttributedString(delineator: String?) -> NSAttributedString

    Parameters

    delineator

    param string delineator.

    Return Value

    attributed string.

  • hasKey(_:) Extension method

    Returns true if the node has the given custom SKTiled property (case insensitive).

    Declaration

    Swift

    public func hasKey(_ key: String) -> Bool

    Parameters

    key

    custom property key.

  • boolForKey(_:) Extension method

    Returns a boolean value for the given key.

    Declaration

    Swift

    public func boolForKey(_ key: String) -> Bool

    Parameters

    key

    property key.

  • stringForKey(_:) Extension method

    Returns a string for the given SKTiled key.

    Declaration

    Swift

    public func stringForKey(_ key: String) -> String?

    Parameters

    key

    property key.

  • intForKey(_:) Extension method

    Returns a integer value for the given key.

    Declaration

    Swift

    public func intForKey(_ key: String) -> Int?

    Parameters

    key

    property key.

  • unsignedIntForKey(_:) Extension method

    Returns an unsigned integer value for the given key.

    Declaration

    Swift

    public func unsignedIntForKey(_ key: String) -> UInt?

    Parameters

    key

    property key.

  • doubleForKey(_:) Extension method

    Returns a float value for the given key.

    Declaration

    Swift

    public func doubleForKey(_ key: String) -> Double?

    Parameters

    key

    property key.

    Return Value

    parsed double value.

  • colorForKey(_:) Extension method

    Returns a color given a property string.

    Declaration

    Swift

    public func colorForKey(_ key: String) -> SKColor?

    Parameters

    key

    property key.

    Return Value

    parsed color.

  • removeValue(for:) Extension method

    Remove a named SKTiled property, returns the value as a string (if property exists).

    Declaration

    Swift

    public func removeValue(for key: String) -> String?

    Parameters

    key

    property key.

  • setProperties(_:overwrite:) Extension method

    Set the node’s properties values, optionally overwriting current values.

    Declaration

    Swift

    public func setProperties(_ attrs: [String : String], overwrite: Bool = false)

    Parameters

    attrs

    dictionary of properties.

    overwrite

    overwrite the current value, if one exists.

Deprecations

  • setValue(forKey:_:) Extension method

    Sets a named SKTiled property. Returns the value, or nil if it does not exist.

    Declaration

    Swift

    @available(*, deprecated, renamed: "setValue(for:﹚")
    public func setValue(forKey key: String, _ value: String)

    Parameters

    key

    property key.

    value

    property value.

  • removeProperty(forKey:) Extension method

    Remove a named SKTiled property, returns the value as a string (if property exists).

    Declaration

    Swift

    @available(*, deprecated, renamed: "removeValue(for:﹚")
    public func removeProperty(forKey key: String) -> String?

    Parameters

    key

    property key.

Available where Self: SKNode

  • getChild(uuid:) Extension method

    Returns a Tiled SpriteKit node with a matching unique ID.

    Declaration

    Swift

    public func getChild(uuid: String) -> SKNode?

    Parameters

    uuid

    unique Tiled node id to match.

    Return Value

    child TiledObjectType node with the given unique id.