SKTileLayer

public class SKTileLayer : SKTiledLayerObject

Overview

Subclass of SKTiledLayerObject, the tile layer is a container for an array of tiles (sprites). Tiles maintain a link to the map’s tileset via their SKTilesetData property.

Properties

Property Description
tileCount Returns a count of valid tiles.

Instance Methods

Method Description
getTiles() Returns an array of current tiles.
getTiles(ofType:) Returns tiles of the given type.
getTiles(globalID:) Returns all tiles matching a global id.
getTilesWithProperty(:) Returns tiles matching the given property & value.
animatedTiles() Returns all animated tiles.
getTileData(globalID:) Returns all tiles matching a global id.
tileAt(coord:) Returns a tile at the given coordinate, if one exists.

Usage

Accessing a tile at a given coordinate:

let tile = tileLayer.tileAt(2, 6)!

Query tiles of a certain type:

let floorTiles = tileLayer.getTiles(ofType: "Floor")

Init

Tiles

  • ▶︎tileAt(_:_:)

    Returns a tile at the given coordinate, if one exists.

    Declaration

    Swift

    public func tileAt(_ x: Int, _ y: Int) -> SKTile?

    Parameters

    x

    Int y-coordinate.

    y

    Int x-coordinate.

    Return Value

    SKTile? tile object, if it exists.

  • Returns a tile at the given coordinate, if one exists.

    Declaration

    Swift

    public func tileAt(coord: CGPoint) -> SKTile?

    Parameters

    coord

    CGPoint tile coordinate.

    Return Value

    SKTile? tile object, if it exists.

  • Returns a tile at the given screen position, if one exists.

    Declaration

    Swift

    public func tileAt(point: CGPoint, offset: CGPoint = CGPoint.zero) -> SKTile?

    Parameters

    point

    CGPoint screen point.

    offset

    CGPoint pixel offset.

    Return Value

    SKTile? tile object, if it exists.

  • ▶︎getTiles()

    Returns an array of current tiles.

    Declaration

    Swift

    public func getTiles() -> [SKTile]

    Return Value

    [SKTile] array of tiles.

  • Returns tiles with a property of the given type.

    Declaration

    Swift

    public func getTiles(ofType: String) -> [SKTile]

    Parameters

    ofType

    String type.

    Return Value

    [SKTile] array of tiles.

  • Returns tiles matching the given global id.

    Declaration

    Swift

    public func getTiles(globalID: Int) -> [SKTile]

    Parameters

    globalID

    Int tile global id.

    Return Value

    [SKTile] array of tiles.

  • Returns tiles with a property of the given type.

    Declaration

    Swift

    public func getTilesWithProperty(_ named: String, _ value: Any) -> [SKTile]

    Parameters

    type

    String type.

    Return Value

    [SKTile] array of tiles.

  • Returns all tiles with animation.

    Declaration

    Swift

    public func animatedTiles() -> [SKTile]

    Return Value

    [SKTile] array of animated tiles.

  • Return tile data from a global id.

    Declaration

    Swift

    public func getTileData(globalID gid: Int) -> SKTilesetData?

    Parameters

    globalID

    Int global tile id.

    Return Value

    SKTilesetData? tile data (for valid id).

  • Returns tiles with a property of the given type.

    Declaration

    Swift

    public func getTileData(withProperty named: String) -> [SKTilesetData]

    Parameters

    type

    String type.

    Return Value

    [SKTile] array of tiles.

Layer Data

  • Add tile data array to the layer and render it.

    Declaration

    Swift

    @discardableResult
    public func setLayerData(_ data: [UInt32], debug: Bool = false) -> Bool

    Parameters

    data

    [UInt32] tile data.

    debug

    Bool debug mode.

    Return Value

    Bool data was successfully added.

  • ▶︎clearLayer()

    Clear the layer of tiles.

    Declaration

    Swift

    public func clearLayer()
  • Build an empty tile at the given coordinates. Returns an existing tile if one already exists, or nil if the coordinate is invalid.

    Declaration

    Swift

    public func addTileAt(coord: CGPoint,
                          gid: Int? = nil,
                          tileType: String? = nil) -> SKTile?

    Parameters

    coord

    CGPoint tile coordinate

    gid

    Int? tile id.

    tileType

    String optional tile class name.

    Return Value

    SKTile? tile.

  • Build an empty tile at the given coordinates with a custom texture. Returns nil if the coordinate is invalid.

    Declaration

    Swift

    public func addTileAt(coord: CGPoint,
                          texture: SKTexture? = nil,
                          tileType: String? = nil) -> SKTile?

    Parameters

    coord

    CGPoint tile coordinate.

    texture

    SKTexture? optional tile texture.

    Return Value

    SKTile? tile.

  • Build an empty tile at the given coordinates. Returns an existing tile if one already exists, or nil if the coordinate is invalid.

    Declaration

    Swift

    public func addTileAt(_ x: Int, _ y: Int, gid: Int? = nil) -> SKTile?

    Parameters

    x

    Int x-coordinate

    y

    Int y-coordinate

    gid

    Int? tile id.

    Return Value

    SKTile? tile.

  • Build an empty tile at the given coordinates with a custom texture. Returns nil is the coordinate is invalid.

    Declaration

    Swift

    public func addTileAt(_ x: Int, _ y: Int, texture: SKTexture? = nil) -> SKTile?

    Parameters

    x

    Int x-coordinate

    y

    Int y-coordinate

    texture

    SKTexture? optional tile texture.

    Return Value

    SKTile? tile.

  • Remove the tile at a given x/y coordinates.

    Declaration

    Swift

    public func removeTileAt(_ x: Int, _ y: Int) -> SKTile?

    Parameters

    x

    Int x-coordinate

    y

    Int y-coordinate

    Return Value

    SKTile? removed tile.

  • ▶︎clearTiles()

    Clear all tiles.

    Declaration

    Swift

    public func clearTiles()
  • Remove the tile at a given coordinate.

    Declaration

    Swift

    public func removeTileAt(coord: CGPoint) -> SKTile?

    Parameters

    coord

    CGPoint tile coordinate.

    Return Value

    SKTile? removed tile.

  • Set a tile at the given coordinate.

    Declaration

    Swift

    public func setTile(_ x: Int, _ y: Int, tile: SKTile? = nil) -> SKTile?

    Parameters

    x

    Int x-coordinate

    y

    Int y-coordinate

    Return Value

    SKTile? tile.

  • Set a tile at the given coordinate.

    Declaration

    Swift

    public func setTile(at coord: CGPoint, tile: SKTile? = nil) -> SKTile?

    Parameters

    coord

    CGPoint tile coordinate.

    Return Value

    SKTile? tile.

Overlap

  • Set the tile overlap. Only accepts a value between 0 - 1.0

    Declaration

    Swift

    public func setTileOverlap(_ overlap: CGFloat)

    Parameters

    overlap

    CGFloat tile overlap value.

Callbacks

  • Called when the layer is finished rendering.

    Declaration

    Swift

    override public func didFinishRendering(duration: TimeInterval = 0)

    Parameters

    duration

    TimeInterval fade-in duration.

Shaders

  • Set a shader for tiles in this layer.

    Declaration

    Swift

    public func setShader(for sktiles: [SKTile], named: String, uniforms: [SKUniform] = [])

    Parameters

    for

    [SKTile] tiles to apply shader to.

    named

    String shader file name.

    uniforms

    [SKUniform] array of shader uniforms.

Debugging

Updating: Tile Layer

  • Run animation actions on all tiles layer.

    Declaration

    Swift

    override public func runAnimationAsActions()
  • Remove tile animations.

    Declaration

    Swift

    override public func removeAnimationActions(restore: Bool = false)

    Parameters

    restore

    Bool restore tile/obejct texture.

  • ▶︎update(_:)

    Update the tile layer before each frame is rendered.

    Declaration

    Swift

    override public func update(_ currentTime: TimeInterval)

    Parameters

    currentTime

    TimeInterval update interval.

Deprecated

  • ▶︎validTiles()

    Returns an array of valid tiles.

    Declaration

    Swift

    @available(*, deprecated, message: "use `getTiles(﹚` instead")
    public func validTiles() -> [SKTile]

    Return Value

    [SKTile] array of current tiles.

  • Initialize this layer’s grid graph with an array of walkable tiles.

    Declaration

    Swift

    public func initializeGraph(walkable: [SKTile],
                                obstacles: [SKTile] = [],
                                diagonalsAllowed: Bool = false,
                                withName: String? = nil,
                                nodeClass: String? = nil) -> GKGridGraph<GKGridGraphNode>?

    Parameters

    walkable

    [SKTile] array of walkable tiles.

    obstacles

    [SKTile] array of obstacle tiles.

    diagonalsAllowed

    Bool allow diagonal movement in the grid.

    withName

    String? optional graph name for identifying in scene.

    nodeClass

    String? graph node type.

    Return Value

    GKGridGraph<GKGridGraphNode>? navigation graph, if created.

  • Initialize layer navigation graph by not specifying tiles to utilize.

    Declaration

    Swift

    public func initializeGraph() -> GKGridGraph<GKGridGraphNode>?

    Return Value

    GKGridGraph<GKGridGraphNode>? navigation graph, if created.

  • Initialize this layer’s grid graph with an array of walkable & obstacle tile ids.

    Declaration

    Swift

    public func initializeGraph(walkableIDs: [Int],
                                obstacleIDs: [Int] = [],
                                diagonalsAllowed: Bool = false,
                                nodeClass: String? = nil) -> GKGridGraph<GKGridGraphNode>?

    Parameters

    walkableIDs

    [Int] array of walkable tile ids.

    obstacleIDs

    [Int] array of obstacle tile ids.

    diagonalsAllowed

    Bool allow diagonal movement in the grid.

    nodeClass

    String? graph node type.

    Return Value

    GKGridGraph<GKGridGraphNode>? navigation graph, if created.

  • Return tiles with walkable data attributes.

    Declaration

    Swift

    public func gatherWalkable() -> [SKTile]

    Return Value

    [SKTile] array of tiles with walkable attribute.

  • Return tiles with obstacle data attributes.

    Declaration

    Swift

    public func gatherObstacles() -> [SKTile]

    Return Value

    [SKTile] array of tiles with obstacle attribute.

  • Parse the tile layer’s properties.

    Declaration

    Swift

    override public func parseProperties(completion: (() -> Void)?)