TilemapDelegate

@objc
public protocol TilemapDelegate : TiledEventHandler

An interface to a tilemap object that allows the user to interact with it as it is being created as well as customizing its properties & behaviors.

Properties

  • zDeltaForLayers: Default z-distance between layers.

Instance Methods

Delegate callbacks are called asynchronously as the map is being read from disk and rendered:

  • didBeginParsing: called when the tilemap is instantiated.
  • didAddTileset: called when a tileset is added to a map.
  • didAddLayer: called when a layer is added to a tilemap.
  • didReadMap: called when the tilemap is finished parsing.
  • didRenderMap: called when the tilemap layers are finished rendering.
  • didAddNavigationGraph: called when the a navigation graph is built for a layer.
  • objectForTileType: specify a custom tile object for use in tile layers.
  • objectForVectorType: specify a custom object for use in object groups.
  • objectForGraphType: specify a custom graph node object for use in navigation graphs.
  • willAddTile: called when a tile is about to be built.
  • didAddTile: called when a tile has just been built.
  • attributesForNodes: Add custom attributes for Tiled nodes of the given type

Event Handlers

  • mouseOverTileHandler: custom tile mouse event handler (macOS only).
  • mouseOverObjectHandler: custom object mouse event handler (macOS only).
  • tileClickedHandler: custom tile mouse event handler (macOS only).
  • objectClickedHandler: custom object mouse event handler (macOS only).
  • tileTouchedHandler: custom tile touch event handler (iOS only).
  • objectTouchedHandler: custom object touch event handler (iOS only).

Usage

Custom Objects

Custom object methods can be used to substitute your own objects for tiles:

func objectForTileType(named: String? = nil) -> SKTile.Type {
    if (named == "MyTile") {
       return MyTile.self
    }
    return SKTile.self
}

Attribute Overrides

The delegate method attributesForNodes allows custom attributes to be added to an object based on global id, node type or name:

func attributesForNodes(ofType: String?, named: String?, globalIDs: [UInt32]) -> [String : String]? {
    if (ofType == "barrel") {
        if (globalIDs.contains(2)) {
            return ["jumpBonus": "100", "hammerBonus": "300"]
        } else {
            return ["jumpBonus": "300", "hammerBonus": "800"]
        }
    }
    return nil
}

Mouse & Touch Handlers

The optional delegate method mouseOverTileHandler allows mouse and touch event handlers to be added to tiles and vector objects:

@objc public func mouseOverTileHandler(globalID: UInt32, ofType: String?) -> ((SKTile) -> ())? {
    guard let tileType = ofType else {
        return nil
    }

    switch tileType {
        case "floors":
            return { (tile) in tile.tileData.setValue(for: "color", "#308CC6") }

        case "walls":
            return { (tile) in tile.tileData.setValue(for: "color", "#8E6214") }

        default:
            return nil
    }
}

This method allows a developer to filter tiles by global id or type, or simply return a closure for all objects globally (regardless of global ID or type).

The tileClickedHandler allows you to set a custom click handler for tiles & objects:

@objc func tileClickedHandler(globalID: UInt32, ofType: String?, button: UInt8) -> ((SKTile) -> ())? {
    switch globalID {
        case 24, 25:
            return { (tile) in
                tile.tileData.setValue(for: "wasVisited", "true")
        }
        default:
            return nil
    }
}

Global ID Overrides

The delegate method willAddTile allows the global ID of a tile to be modified before the tile is created:

@objc func willAddTile(globalID: UInt32, coord: simd_int2, in: String?) -> UInt32 {
    if (globalID == 217) {
       return 320
    }
    return globalID
}

Tilemap Properties

  • Determines the z-position difference between layers.

    Declaration

    Swift

    @objc
    optional var zDeltaForLayers: CGFloat { get }

Creation & Rendering

Custom Object Types

  • Specify a custom tile object for use in tile layers.

    Declaration

    Swift

    @objc
    optional func objectForTileType(named: String?) -> SKTile.Type

    Parameters

    named

    optional class name.

    Return Value

    tile object type.

  • Specify a custom object for use in object groups.

    Declaration

    Swift

    @objc
    optional func objectForVectorType(named: String?) -> SKTileObject.Type

    Parameters

    named

    optional class name

    Return Value

    vector object type.

  • Specify a custom graph node object for use in navigation graphs.

    Declaration

    Swift

    @objc
    optional func objectForGraphType(named: String?) -> GKGridGraphNode.Type

    Parameters

    named

    optional class name.

    Return Value

    pathfinding graph node type.

User Customization

  • Called whem a tile is about to be built in a layer at a given coordinate. Allows a global ID value to be substituted before the tile is created.

    Declaration

    Swift

    @objc
    optional func willAddTile(globalID: UInt32, coord: simd_int2, in: String?) -> UInt32

    Parameters

    globalID

    tile global id.

    coord

    tile coordinate (optional).

    in

    optional parent layer name.

    Return Value

    tile global id.

  • Called whem a tile is about to be built in a layer. Allows a global ID value to be substituted before the tile is created.

    Declaration

    Swift

    @objc
    optional func willAddTile(globalID: UInt32, in: String?) -> UInt32

    Parameters

    globalID

    tile global id.

    in

    optional parent layer name.

    Return Value

    tile global id.

  • Called whem a tile has just been built in a layer. This method allows for the creation of custom object types at a given coordinate.

    Declaration

    Swift

    @objc
    optional func didAddTile(_ tile: SKTile, coord: simd_int2, in: String?, _ completion: ((_ tile: SKTile) -> Void)?)

    Parameters

    tile

    tile instance.

    coord

    tile coordinate.

    in

    optional parent layer name.

    completion

    optional completion block.

  • Called whem a tile has just been built in a layer.

    Declaration

    Swift

    @objc
    optional func didAddTile(_ tile: SKTile, in: String?, _ completion: ((_ tile: SKTile) -> Void)?)

    Parameters

    tile

    tile instance.

    in

    optional parent layer name.

    completion

    optional completion block.

  • Provides custom attributes for objects for given Tiled types.

    Declaration

    Swift

    @objc
    optional func attributesForNodes(ofType: String?, named: String?, globalIDs: [UInt32]) -> [String : String]?

    Parameters

    type

    type value.

    named

    optional node name.

    Return Value

    custom attributes dictionary.

  • Provides a mechanism for substitute custom SpriteKit node types in place of Tiled point objects.

    Declaration

    Swift

    @objc
    optional func customNodeForPointObject(ofType: String, attributes: [String : String], inLayer: String?) -> SKNode?

    Parameters

    ofType

    point object type.

    attributes

    attributes parsed from Tiled reference.

    inLayer

    optional parent layer name.