SKTileLayer

public class SKTileLayer : TiledLayerObject

Subclass of TiledLayerObject, 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

  • tileCount: returns a count of valid tiles.

Instance Methods

  • 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")

Colors

  • Overview

    Set the layer tint color. Tiles contained in this layer will be tinted with the given color.

    Declaration

    Swift

    public override var tintColor: SKColor? { get set }

Initialization

Infinite Map/Chunks

  • Returns the number of chunks contained in this layer.

    Declaration

    Swift

    public var chunkCount: Int { get }

Tiles

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

    Declaration

    Swift

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

    Parameters

    x

    x-coordinate.

    y

    y-coordinate.

    Return Value

    tile object, if it exists.

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

    Declaration

    Swift

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

    Parameters

    coord

    tile coordinate.

    Return Value

    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

    screen point.

    offset

    pixel offset.

    Return Value

    tile object, if it exists.

  • Returns an array of all tiles in the layer.

    Declaration

    Swift

    public func getTiles() -> [SKTile]

    Return Value

    array of tiles.

  • Returns all of the tiles with a property of the given type.

    Declaration

    Swift

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

    Parameters

    ofType

    tile type.

    Return Value

    array of tiles.

  • Returns tiles with a property matching the given name.

    Declaration

    Swift

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

    Parameters

    named

    property name.

    Return Value

    array of tiles.

  • Returns tiles matching the given global id.

    Declaration

    Swift

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

    Parameters

    globalID

    tile global id.

    Return Value

    array of tiles.

  • Returns tiles with a property matching the given name & value.

    Declaration

    Swift

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

    Parameters

    named

    property name.

    value

    property value.

    Return Value

    array of tiles matching the given property name/value.

  • Returns an array of all the animated tiles in the layer.

    Declaration

    Swift

    public func animatedTiles() -> [SKTile]

    Return Value

    array of animated tiles.

  • Return tile data matching a global id.

    Declaration

    Swift

    public func getTileData(globalID: UInt32) -> SKTilesetData?

    Parameters

    globalID

    global tile id.

    Return Value

    tile data (for valid id).

  • Returns tiles with a property of the given type.

    Declaration

    Swift

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

    Parameters

    named

    type.

    Return Value

    array of tiles.

Layer Data

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

    Declaration

    Swift

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

    Parameters

    data

    tile data.

    Return Value

    data was successfully added.

  • 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: simd_int2,
                          globalID: UInt32? = nil,
                          tileType: String? = nil) -> SKTile?

    Parameters

    coord

    tile coordinate

    gid

    tile id.

    tileType

    optional tile class name.

    Return Value

    newly created tile (if successful).

  • 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: simd_int2,
                          texture: SKTexture? = nil,
                          tileType: String? = nil) -> SKTile?

    Parameters

    coord

    tile coordinate.

    texture

    optional tile texture.

    tileType

    optional tile type.

    Return Value

    newly created 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, globalID: UInt32? = nil) -> SKTile?

    Parameters

    x

    x-coordinate.

    y

    y-coordinate.

    gid

    tile global id.

    Return Value

    newly created tile (if successful).

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

    Declaration

    Swift

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

    Parameters

    x

    x-coordinate.

    y

    y-coordinate.

    texture

    optional tile texture.

    Return Value

    newly created tile (if successful).

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

    Declaration

    Swift

    public func replaceTileAt(coord: simd_int2,
                          globalID: UInt32? = nil,
                          tileType: String? = nil) -> (old: SKTile?, new: SKTile?)

    Parameters

    coord

    tile coordinate

    gid

    tile id.

    tileType

    optional tile class name.

    Return Value

    tuple of current tile (if it exists) & newly created tile (if successful).

  • Clear all tiles.

    Declaration

    Swift

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

    Declaration

    Swift

    @discardableResult
    public func removeTile(at coord: simd_int2) -> SKTile?

    Parameters

    coord

    map coordinate.

    Return Value

    removed tile, if one exists.

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

    Declaration

    Swift

    @discardableResult
    public func removeTile(_ x: Int, _ y: Int) -> SKTile?

    Parameters

    x

    x-coordinate.

    y

    y-coordinate.

    Return Value

    removed tile, if one exists.

  • Set a tile at the given coordinate.

    Declaration

    Swift

    @discardableResult
    public func setTile(coord: simd_int2, tile: SKTile? = nil) -> SKTile?

    Parameters

    coord

    coordinate.

    tile

    tile instance.

    Return Value

    tile instance.

  • Set a tile at the given coordinate.

    Declaration

    Swift

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

    Parameters

    x

    x-coordinate.

    y

    y-coordinate.

    tile

    tile instance.

    Return Value

    tile instance.

Physics

Overlap

  • Set the tile overlap. Valid values are (0 - 1.0).

    Declaration

    Swift

    public func setTileOverlap(_ overlap: CGFloat)

    Parameters

    overlap

    tile overlap value.

Callbacks

  • Called when the layer is finished rendering.

    Declaration

    Swift

    public override func didFinishRendering(duration: TimeInterval = 0)

    Parameters

    duration

    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

    sktiles

    tiles to apply shader to.

    named

    shader file name.

    uniforms

    array of shader uniforms.

Rasterization

  • Rasterize a static layer into an image. The resulting texture is stored in the TiledLayerObject.staticTexture property.

    Declaration

    Swift

    public override func rasterizeStaticLayer()

Updating

  • Run animation actions on all of the tiles in this layer.

    Declaration

    Swift

    public override func runAnimationAsActions()
  • Remove tile animations.

    Declaration

    Swift

    public override func removeAnimationActions(restore: Bool = false)

    Parameters

    restore

    restore tile/object texture.

  • Update the tile layer before each frame is rendered.

    Declaration

    Swift

    public override func update(_ currentTime: TimeInterval)

    Parameters

    currentTime

    update interval.

Reflection

  • Returns a custom mirror for this layer.

    Declaration

    Swift

    public override var customMirror: Mirror { get }

Extensions

Deprecations

  • Returns an array of valid tiles.

    Declaration

    Swift

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

    Return Value

    array of current tiles.

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

    Declaration

    Swift

    @available(*, deprecated, renamed: "tileAt(_:_:﹚")
    public func tileAt(_ x: Int, _ y: Int) -> SKTile?

    Parameters

    x

    x-coordinate.

    y

    y-coordinate.

    Return Value

    tile object, if it exists.

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

    Declaration

    Swift

    @available(*, deprecated, renamed: "tileAt(coord:﹚")
    public func tileAt(coord: CGPoint) -> SKTile?

    Parameters

    coord

    tile coordinate.

    Return Value

    tile object, if it exists.

  • Returns tiles matching the given global id.

    Declaration

    Swift

    @available(*, deprecated, renamed: "getTiles(globalID:﹚")
    public func getTiles(globalID: Int) -> [SKTile]

    Parameters

    globalID

    tile global id.

    Return Value

    array of tiles.

  • 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

    @available(*, deprecated, renamed: "addTileAt(coord:globalID:tileType:﹚")
    public func addTileAt(coord: CGPoint, gid: Int? = nil, tileType: String? = nil) -> SKTile?

    Parameters

    coord

    tile coordinate

    gid

    tile id.

    tileType

    optional tile class name.

    Return Value

    newly created tile (if successful).

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

    Declaration

    Swift

    @available(*, deprecated, renamed: "addTileAt(coord:texture:tileType:﹚")
    public func addTileAt(coord: CGPoint, texture: SKTexture? = nil, tileType: String? = nil) -> SKTile?

    Parameters

    coord

    tile coordinate.

    texture

    optional tile texture.

    tileType

    optional tile type.

    Return Value

    newly created 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

    @available(*, deprecated, renamed: "addTileAt(x:y:gid:﹚")
    public func addTileAt(_ x: Int, _ y: Int, gid: Int? = nil) -> SKTile?

    Parameters

    x

    x-coordinate.

    y

    y-coordinate.

    gid

    tile global id.

    Return Value

    newly created tile (if successful).

  • Remove the tile at a given coordinate.

    Declaration

    Swift

    @available(*, deprecated, renamed: "removeTile(at:﹚")
    public func removeTileAt(coord: CGPoint) -> SKTile?

    Parameters

    coord

    map coordinate.

    Return Value

    removed tile, if one exists.

  • Remove the tile at a given coordinate.

    Declaration

    Swift

    @available(*, deprecated, renamed: "removeTile(at:﹚")
    public func removeTileAt(coord: simd_int2) -> SKTile?

    Parameters

    coord

    map coordinate.

    Return Value

    removed tile, if one exists.

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

    Declaration

    Swift

    @available(*, deprecated, renamed: "removeTile(_:﹚")
    public func removeTileAt(_ x: Int, _ y: Int) -> SKTile?

    Parameters

    x

    x-coordinate.

    y

    y-coordinate.

    Return Value

    removed tile, if one exists.

  • Set a tile at the given coordinate.

    Declaration

    Swift

    @available(*, deprecated, renamed: "setTile(coord:tile:﹚")
    public func setTile(at coord: CGPoint, tile: SKTile? = nil) -> SKTile?

    Parameters

    coord

    tile coordinate.

    tile

    tile instance.

    Return Value

    tile instance.

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

    Declaration

    Swift

    @available(*, deprecated, renamed: "setLayerData(_:﹚")
    @discardableResult
    public func setLayerData(_ data: [UInt32], debug: Bool = false) -> Bool

    Parameters

    data

    tile data.

    debug

    debug mode.

    Return Value

    data was successfully added.

  • Dump the contents of the tile data array to the console.

    Declaration

    Swift

    @available(*, deprecated, renamed: "dumpTileLayerData(spacing:﹚")
    public func dumpLayerData(spacing: Int = 3)

    Parameters

    spacing

    spacing length.

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

    Declaration

    Swift

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

    Parameters

    walkable

    array of walkable tiles.

    obstacles

    array of obstacle tiles.

    diagonalsAllowed

    allow diagonal movement in the grid.

    withName

    optional graph name for identifying in scene.

    nodeClass

    graph node type.

    Return Value

    navigation graph, if created.

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

    Declaration

    Swift

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

    Return Value

    navigation graph, if created.

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

    Declaration

    Swift

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

    Parameters

    walkableIDs

    array of walkable tile ids.

    obstacleIDs

    array of obstacle tile ids.

    diagonalsAllowed

    allow diagonal movement in the grid.

    nodeClass

    graph node type.

    Return Value

    navigation graph, if created.

  • Return tiles with walkable data attributes.

    Declaration

    Swift

    public func gatherWalkable() -> [SKTile]

    Return Value

    array of tiles with walkable attribute.

  • Return tiles with obstacle data attributes.

    Declaration

    Swift

    public func gatherObstacles() -> [SKTile]

    Return Value

    array of tiles with obstacle attribute.

SKTileLayer

  • Parse the tile layer’s properties.

    Declaration

    Swift

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

    Parameters

    completion

    optional completion closure.