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
}
-
Determines the z-position difference between layers.
Declaration
Swift
@objc optional var zDeltaForLayers: CGFloat { get }
-
Called when the tilemap is instantiated.
Declaration
Swift
@objc optional func didBeginParsing(_ tilemap: SKTilemap)Parameters
tilemaptilemap instance.
-
Called when a tileset is added to a map.
Declaration
Swift
@objc optional func didAddTileset(_ tileset: SKTileset)Parameters
tilesettileset instance.
-
Called when a layer is added to a tilemap.
Declaration
Swift
@objc optional func didAddLayer(_ layer: TiledLayerObject)Parameters
layertilemap instance.
-
Called when the tilemap is finished parsing.
Declaration
Swift
@objc optional func didReadMap(_ tilemap: SKTilemap)Parameters
tilemaptilemap instance.
-
Called when the tilemap layers are finished rendering.
Declaration
Swift
@objc optional func didRenderMap(_ tilemap: SKTilemap)Parameters
tilemaptilemap instance.
-
Called when the a navigation graph is built for a layer.
Declaration
Swift
@objc optional func didAddNavigationGraph(_ graph: GKGridGraph<GKGridGraphNode>)Parameters
graphGKGridGraph<GKGridGraphNode>graph node instance.
-
Specify a custom tile object for use in tile layers.
Declaration
Swift
@objc optional func objectForTileType(named: String?) -> SKTile.TypeParameters
namedoptional 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.TypeParameters
namedoptional 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.TypeParameters
namedoptional class name.
Return Value
pathfinding graph node type.
-
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?) -> UInt32Parameters
globalIDtile global id.
coordtile coordinate (optional).
inoptional 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?) -> UInt32Parameters
globalIDtile global id.
inoptional 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
Parameters
tiletile instance.
coordtile coordinate.
inoptional parent layer name.
completionoptional completion block.
-
Called whem a tile has just been built in a layer.
Declaration
Parameters
tiletile instance.
inoptional parent layer name.
completionoptional 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
typetype value.
namedoptional 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
ofTypepoint object type.
attributesattributes parsed from Tiled reference.
inLayeroptional parent layer name.
View on GitHub
Install in Dash
TilemapDelegate Protocol Reference