Protocols

SKTiled protocols represent blueprints of requirements (methods, properties) to allow users to interact with Tiled content as it is being created.

  • The DebugDrawableType protocol provides an interface to visualizing various object attributes, such as displaying a visual grid over the container, or highlighting object bounds.

    Properties

    • _debugLevel: debugging visualization level.
    See more

    Declaration

    Swift

    @objc
    public protocol DebugDrawableType
  • The TiledCustomReflectableType protocol outlines internal debugging elements that can be used to describe objects in debugging interfaces.

    Properties

    • tiledElementName: Tiled element type.
    • tiledNodeType: SKTiled node type.
    • tiledNodeNiceName: proper node name.
    • tiledIconName: node icon representation.
    • tiledListDescription: description of the node used for list or outline views.
    • tiledMenuItemDescription: description of the node used in dropdown & popu menus.
    • tiledDisplayItemDescription: shortened debug description used for debug output text.
    • tiledHelpDescription: description of the node type used for help features.
    • tiledTooltipDescription: description suitable for a UI widget to display as a tooltip.
    See more

    Declaration

    Swift

    @objc
    public protocol TiledCustomReflectableType
  • The TiledGeometryType protocol describes a drawable type used to manage and render Tiled geometry objects.

    Properties

    • objectPath: the node’s bounding shape
    • boundingRect: the node’s bounding rect
    • boundsShape: the node’s rect shape
    • renderQuality: render scaling quality
    • visibleToCamera: node is visible to camera

    Methods

    • contains(touch: CGPoint): returns true if a point is contained in this shape’s frame.
    • getVertices(offset: CGPoint): return’s the node’s shape points.
    • draw(): draw the object’s contents.
    See more

    Declaration

    Swift

    @objc
    public protocol TiledGeometryType : DebugDrawableType, TiledObjectType, TiledRasterizableType, TiledSelectableType
  • The TiledMappableGeometryType protocol describes a container that is broken up into two-dimensional tiles.

    Properties

    • orientation: Container orientation
    • isInfinite: Container represents and infinite area.
    • mapSize: Container size (in tiles)
    • graph: Container pathfinding graph (optional)
    • childOffset: Child node offset (optional)
    • tileSize: Tile size (in pixels)
    • hexsidelength: Hexagonal side length
    • staggeraxis: Hexagonal stagger axis
    • staggerindex: Hexagonal stagger index
    See more

    Declaration

    Swift

    @objc
    public protocol TiledMappableGeometryType : TiledGeometryType
  • 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.

    See more

    Declaration

    Swift

    @objc
    public protocol TiledObjectType : TiledCustomReflectableType
  • The TiledEventHandler protocol delegates optional handlers for mouse (macOS) and touch (iOS) events.

    Instance Methods

    • mouseOverTileHandler: Custom tile mouse over event handler. macOS
    • mouseOverObjectHandler: Custom object mouse over event handler. macOS
    • tileClickedHandler: Custom tile mouse click event handler. macOS
    • objectClickedHandler: Custom object mouse click event handler. macOS
    • tileTouchedHandler: Custom tile touch event handler. iOS
    • objectTouchedHandler: Custom object touch event handler. iOS

    Usage

    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
        }
    }
    

    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
        }
    }
    
    See more

    Declaration

    Swift

    @objc
    public protocol TiledEventHandler
  • The TiledSceneCameraDelegate protocol defines methods for interacting with the custom SKTiledSceneCamera object. Classes conforming to this protocol are notified of camera position & zoom changes unless the TiledSceneCameraDelegate.receiveCameraUpdates flag is disabled.

    This delegate also receives mouse & touch events and forwards them on to delegates accordingly.

    Tiled Scene Camera Delegate

    Properties

    • receiveCameraUpdates: delegate will receive camera updates.
    • currentCoordinate: currently focused map coordinate.

    Instance Methods

    • containedNodesChanged: nodes visible in camera haved changed.
    • cameraPositionChanged: camera position change.
    • cameraZoomChanged: camera zoom change.
    • cameraBoundsChanged: camera bounds updated.
    • leftMouseDown: scene is clicked (macOS only).
    • rightMouseDown: scene is right-clicked (macOS only).
    • leftMouseUp: left mouse button is released (macOS only).
    • rightMouseUp: right mouse button is released (macOS only).
    • leftMouseDoubleClicked: scene is double-clicked (macOS only).
    • mousePositionChanged: mouse moves in the scene (macOS only).
    • sceneDoubleTapped: scene is double-tapped (iOS only).
    • sceneRotated: scene is rotated via gesture (iOS only).
    See more

    Declaration

    Swift

    @objc
    public protocol TiledSceneCameraDelegate
  • The TiledSceneDelegate Methods for managing SKTilemap nodes in an SpriteKit SKScene scene. This protocol and the SKTiledScene objects are included as a suggested way to use the SKTilemap class, but are not required.

    In this configuration, the tile map is a child of the root node and reference the custom SKTiledSceneCamera camera.

    TiledSceneDelegate Overview

    Properties

    • worldNode: Root container node. Tiled assets are parented to this node.
    • cameraNode: Custom scene camera.
    • tilemap: Tile map node.

    Instance Methods

    See more

    Declaration

    Swift

    public protocol TiledSceneDelegate : AnyObject
  • 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
    }
    
    See more

    Declaration

    Swift

    @objc
    public protocol TilemapDelegate : TiledEventHandler
  • The TilesetDataSource protocol outlines methods which allow the user to dynamically alter the properties of a tileset as it is being created.

    Instance Methods

    Delegate callbacks are called asynchronously as the tileset is being rendered.

    • willAddSpriteSheet: Provide an image name for the tileset before textures are generated.
    • willAddImage: Provide an alernate image name for an image in a collection.

    Usage

    Implementing the TilesetDataSource.willAddSpriteSheet method allows the user to specify different spritesheet images. Take care that these images have the same dimensions & layout.

    extension MyScene: TilesetDataSource {
        func willAddSpriteSheet(to tileset: SKTileset, fileNamed: String) -> String {
            if (currentSeason == .winter) {
                return "winter-tiles-16x16.png"
            }
            if (currentSeason == .summer) {
                return "summer-tiles-16x16.png"
            }
            return fileNamed
        }
    }
    
    See more

    Declaration

    Swift

    public protocol TilesetDataSource : AnyObject