SKTiledLayerObject

public class SKTiledLayerObject : SKEffectNode, SKTiledObject

Overview

The SKTiledLayerObject is the generic base class for all layer types. This class doesn’t define any object or child types, but provides base behaviors for layered content, including:

  • coordinate transformations
  • validating coordinates
  • positioning and alignment

Properties

Property Description
tilemap Parent tilemap.
index Layer index. Matches the index of the layer in the source TMX file.
size Layer size (in tiles).
tileSize Layer tile size (in pixels).
anchorPoint Layer anchor point, used to position layers.
origin Layer origin point, used for placing tiles.

Instance Methods

Method Description
pointForCoordinate(coord:offset:) Returns a point for a coordinate in the layer, with optional offset.
coordinateForPoint(_:) Returns a tile coordinate for a given point in the layer.
touchLocation(_:) Returns a converted touch location in map space.
coordinateAtTouchLocation(_:) Returns the tile coordinate at a touch location.
isValid(coord:) Returns true if the coordinate is valid.

Usage

Coordinate transformation functions return points in the current tilemap projection:

node.position = tileLayer.pointForCoordinate(2, 1)

Coordinate transformation functions translate points to map coordinates:

coord = coordinateForPoint(touchPosition)

Return the tile coordinate at a touch event (iOS):

coord = imageLayer.coordinateAtTouchLocation(touchPosition)

Return the tile coordinate at a mouse event (macOS):

coord = groupLayer.coordinateAtMouseEvent(event: mouseClicked)
  • ▶︎tilemap

    Reference to the parent tilemap.

    Declaration

    Swift

    public var tilemap: SKTilemap
  • ▶︎uuid

    Unique layer id.

    Declaration

    Swift

    public var uuid: String
  • ▶︎type

    Layer type.

    Declaration

    Swift

    public var type: String!
  • ▶︎index

    Layer index. Matches the index of the layer in the source TMX file.

    Declaration

    Swift

    public var index: Int
  • ▶︎rawIndex

    Position in the render tree.

    Declaration

    Swift

    public var rawIndex: Int
  • ▶︎properties

    Custom layer properties.

    Declaration

    Swift

    public var properties: [String : String]
  • Ignore custom properties.

    Declaration

    Swift

    public var ignoreProperties: Bool
  • ▶︎TileOffset

    Overview

    Tile offset hint for coordinate conversion.

    Constants

    Property Description
    center Tile is centered.
    top Tile is offset at the top.
    topLeft Tile is offset at the upper left.
    topRight Tile is offset at the upper right.
    bottom Tile is offset at the bottom.
    bottomLeft Tile is offset at the bottom left.
    bottomRight Tile is offset at the bottom right.
    left Tile is offset at the left side.
    right Tile is offset at the right side.

    Declaration

    Swift

    public enum TileOffset : Int
  • ▶︎color

    Layer color.

    Declaration

    Swift

    public dynamic var color: SKColor
  • ▶︎gridColor

    Grid visualization color.

    Declaration

    Swift

    public var gridColor: SKColor
  • ▶︎frameColor

    Bounding box color.

    Declaration

    Swift

    public var frameColor: SKColor
  • Layer highlight color (for highlighting tiles)

    Declaration

    Swift

    public var highlightColor: SKColor
  • Layer highlight duration

    Declaration

    Swift

    public var highlightDuration: TimeInterval
  • ▶︎isolated

    Layer is isolated.

    Declaration

    Swift

    public private(set) var isolated: Bool { get }
  • ▶︎offset

    Layer offset value.

    Declaration

    Swift

    public var offset: CGPoint
  • ▶︎size

    Layer size (in tiles).

    Declaration

    Swift

    public var size: CGSize { get }
  • ▶︎tileSize

    Layer tile size (in pixels).

    Declaration

    Swift

    public var tileSize: CGSize { get }
  • ▶︎anchorPoint

    Layer anchor point, used to position layers.

    Declaration

    Swift

    public var anchorPoint: CGPoint { get }
  • ▶︎graph

    Pathfinding graph.

    Declaration

    Swift

    public var graph: GKGridGraph<GKGridGraphNode>!
  • Debug visualization options.

    Declaration

    Swift

    public var debugDrawOptions: DebugDrawOptions { get set }
  • ▶︎antialiased

    Antialias lines.

    Declaration

    Swift

    public var antialiased: Bool
  • Map color blending value.

    Declaration

    Swift

    public var colorBlendFactor: CGFloat
  • Render scaling property.

    Declaration

    Swift

    public var renderQuality: CGFloat
  • Name used to access navigation graph.

    Declaration

    Swift

    public var navigationKey: String
  • Output current zPosition

    Declaration

    Swift

    public var currentZPosition: CGFloat { get }
  • Optional background color.

    Declaration

    Swift

    public var backgroundColor: SKColor? { get set }
  • ▶︎background

    Layer background sprite.

    Declaration

    Swift

    lazy public var background: SKSpriteNode { get set }

Geometry

  • ▶︎origin

    Returns the position of layer origin point (used to place tiles).

    Declaration

    Swift

    public var origin: CGPoint { get }
  • ▶︎bounds

    Returns the frame rectangle of the layer (used to draw bounds).

    Declaration

    Swift

    public var bounds: CGRect { get }
  • Returns the points of the layer’s bounding shape.

    Declaration

    Swift

    public func getVertices() -> [CGPoint]

    Return Value

    [CGPoint] array of points.

  • ▶︎updateMode

    Tile update mode.

    Declaration

    Swift

    public var updateMode: TileUpdateMode { get }

Init

  • Initialize via the parser.

    This intializer is meant to be called by the SKTilemapParser, you should not use it directly.

    Declaration

    Swift

    public init?(layerName: String, tilemap: SKTilemap, attributes: [String : String])

    Parameters

    layerName

    String layer name.

    tilemap

    SKTilemap parent tilemap node.

    attributes

    [String: String] dictionary of layer attributes.

    Return Value

    SKTiledLayerObject? tiled layer, if initialization succeeds.

  • Create a new layer within the parent tilemap node.

    Declaration

    Swift

    public init(layerName: String, tilemap: SKTilemap)

    Parameters

    layerName

    String layer name.

    tilemap

    SKTilemap parent tilemap node.

    Return Value

    SKTiledLayerObject tiled layer object.

Color

  • Set the layer color with an SKColor.

    Declaration

    Swift

    public func setColor(color: SKColor)

    Parameters

    color

    SKColor object color.

  • Set the layer color with a hex string.

    Declaration

    Swift

    public func setColor(hexString: String)

    Parameters

    hexString

    String color hex string.

Children

  • ▶︎layers

    Child layer array.

    Declaration

    Swift

    public var layers: [SKTiledLayerObject] { get }

Touch Events

Mouse Events

  • Returns a mouse event location in the current layer.

    Declaration

    Swift

    public func mouseLocation(event: NSEvent) -> CGPoint

    Parameters

    event

    NSEvent mouse event location.

    Return Value

    CGPoint converted point in layer coordinate system.

  • Returns the tile coordinate at a mouse event location.

    Declaration

    Swift

    public func coordinateAtMouseEvent(event: NSEvent) -> CGPoint

    Parameters

    event

    NSEvent mouse event location.

    Return Value

    CGPoint converted point in layer coordinate system.

Coordinate Conversion

  • Returns true if the coordinate is valid.

    Declaration

    Swift

    public func isValid(_ x: Int, _ y: Int) -> Bool

    Parameters

    x

    Int x-coordinate.

    y

    Int y-coordinate.

    Return Value

    Bool coodinate is valid.

  • Returns true if the coordinate is valid.

    Declaration

    Swift

    public func isValid(coord: CGPoint) -> Bool

    Parameters

    coord

    CGPoint tile coordinate.

    Return Value

    Bool coodinate is valid.

  • Convert a point into the tile map’s coordinate space.

    Declaration

    Swift

    public func convertPoint(_ point: CGPoint) -> CGPoint

    Parameters

    point

    CGPoint input point.

    Return Value

    CGPoint point with y-value inverted.

  • Returns a point for a given coordinate in the layer, with optional offset values for x/y.

    Declaration

    Swift

    public func pointForCoordinate(coord: CGPoint, offsetX: CGFloat = 0, offsetY: CGFloat = 0) -> CGPoint

    Parameters

    coord

    CGPoint tile coordinate.

    offsetX

    CGFloat x-offset value.

    offsetY

    CGFloat y-offset value.

    Return Value

    CGPoint point in layer (spritekit space).

  • Returns a tile coordinate for a given point in the layer.

    Declaration

    Swift

    public func coordinateForPoint(_ point: CGPoint) -> CGPoint

    Parameters

    point

    CGPoint point in layer (spritekit space).

    Return Value

    CGPoint tile coordinate.

  • Returns a tile coordinate for a given point in the layer as a vector_int2.

    Declaration

    Swift

    public func vectorCoordinateForPoint(_ point: CGPoint) -> int2

    Parameters

    point

    CGPoint point in layer.

    Return Value

    int2 tile coordinate.

Adding & Removing Nodes

  • Add an SKNode child node at the given x/y coordinates. By default, the zPositon will be higher than all of the other nodes in the layer.

    Declaration

    Swift

    public func addChild(_ node: SKNode, x: Int = 0, y: Int = 0, offset: CGPoint = CGPoint.zero, zpos: CGFloat? = nil)

    Parameters

    node

    SKNode object.

    x

    Int x-coordinate.

    y

    Int y-coordinate.

    offset

    CGPoint offset amount.

    zpos

    CGFloat? optional z-position.

  • Add an SKNode child node at the given coordinates. By default, the zPositon will be higher than all of the other nodes in the layer.

    Declaration

    Swift

    public func addChild(_ node: SKNode, coord: CGPoint, offset: CGPoint = CGPoint.zero, zpos: CGFloat? = nil)

    Parameters

    node

    SKNode object.

    coord

    CGPoint tile coordinate.

    offset

    CGPoint offset amount.

    zpos

    CGFloat? optional z-position.

Callbacks

  • Called when the layer is finished rendering.

    Declaration

    Swift

    public func didFinishRendering(duration: TimeInterval = 0)

    Parameters

    duration

    TimeInterval fade-in duration.

Dynamics

Shaders

  • Set a shader effect for the layer.

    Declaration

    Swift

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

    Parameters

    named

    String shader file name.

    uniforms

    [SKUniform] array of shader uniforms.

Debugging

Updating

  • Initialize SpriteKit animation actions for the layer.

    Declaration

    Swift

    public func runAnimationAsActions()
  • Remove SpriteKit animations in the layer.

    Declaration

    Swift

    public func removeAnimationActions(restore: Bool = false)

    Parameters

    restore

    Bool restore tile/obejct texture.

  • ▶︎update(_:)

    Update the layer before each frame is rendered.

    Declaration

    Swift

    public func update(_ currentTime: TimeInterval)

    Parameters

    currentTime

    TimeInterval update interval.

Extensions

  • ▶︎opacity

    Layer transparency.

    Declaration

    Swift

    public var opacity: CGFloat { get set }
  • ▶︎visible

    Layer visibility.

    Declaration

    Swift

    public var visible: Bool { get set }
  • Add a node at the given coordinates. By default, the zPositon will be higher than all of the other nodes in the layer.

    Declaration

    Swift

    public func addChild(_ node: SKNode, _ x: Int, _ y: Int, dx: CGFloat = 0, dy: CGFloat = 0, zpos: CGFloat? = nil)

    Parameters

    node

    SKNode object.

    x

    Int x-coordinate.

    y

    Int y-coordinate.

    dx

    CGFloat offset x-amount.

    dy

    CGFloat offset y-amount.

    zpos

    CGFloat? optional z-position.

  • Returns a point for a given coordinate in the layer, with optional offset values for x/y.

    Declaration

    Swift

    public func pointForCoordinate(_ x: Int, _ y: Int, offsetX: CGFloat = 0, offsetY: CGFloat = 0) -> CGPoint

    Parameters

    x

    Int x-coordinate.

    y

    Int y-coordinate.

    offsetX

    CGFloat x-offset value.

    offsetY

    CGFloat y-offset value.

    Return Value

    CGPoint position in layer.

  • Returns a point for a given coordinate in the layer, with optional offset.

    Declaration

    Swift

    public func pointForCoordinate(coord: CGPoint, offset: CGPoint) -> CGPoint

    Parameters

    coord

    CGPoint tile coordinate.

    offset

    CGPoint tile offset.

    Return Value

    CGPoint point in layer.

  • Returns a point for a given coordinate in the layer, with optional offset.

    Declaration

    Swift

    public func pointForCoordinate(coord: CGPoint, tileOffset: SKTiledLayerObject.TileOffset = .center) -> CGPoint

    Parameters

    coord

    CGPoint tile coordinate.

    offset

    TileOffset tile offset hint.

    Return Value

    CGPoint point in layer.

  • Returns a tile coordinate for a given vector_int2 coordinate.

    Declaration

    Swift

    public func pointForCoordinate(vec2: int2, offsetX: CGFloat = 0, offsetY: CGFloat = 0) -> CGPoint

    Parameters

    vec2

    int2 vector int2 coordinate.

    offsetX

    CGFloat x-offset value.

    offsetY

    CGFloat y-offset value.

    Return Value

    CGPoint position in layer.

  • Returns a tile coordinate for a given point in the layer.

    Declaration

    Swift

    public func coordinateForPoint(_ x: Int, _ y: Int) -> CGPoint

    Parameters

    x

    Int x-position.

    y

    Int y-position.

    Return Value

    CGPoint position in layer.

  • ▶︎center

    Returns the center point of a layer.

    Declaration

    Swift

    public var center: CGPoint { get }
  • Calculate the distance from the layer’s origin

    Declaration

    Swift

    public func distanceFromOrigin(_ pos: CGPoint) -> CGVector
  • Returns a value for use in a dropdown menu.

    Declaration

    Swift

    public var menuDescription: String { get }
  • ▶︎layerName

    String representing the layer name (null if not set).

    Declaration

    Swift

    public var layerName: String { get }
  • ▶︎parents

    Returns an array of parent layers, beginning with the current.

    Declaration

    Swift

    public var parents: [SKNode] { get }
  • ▶︎childLayers

    Returns an array of child layers.

    Declaration

    Swift

    public var childLayers: [SKNode] { get }
  • Returns an array of tiles/objects that conform to the SKTiledGeometry protocol.

    Declaration

    Swift

    public func renderableObjects() -> [SKNode]

    Return Value

    [SKNode] array of child objects.

  • ▶︎isTopLevel

    Indicates the layer is a top-level layer.

    Declaration

    Swift

    public var isTopLevel: Bool { get }
  • ▶︎path

    Translate the parent hierarchy to a path string

    Declaration

    Swift

    public var path: String { get }
  • Returns a string array representing the current layer name & index.

    Declaration

    Swift

    public var layerStatsDescription: [String] { get }
  • Parse the layer’s properties value.

    Declaration

    Swift

    public func parseProperties(completion: (() -> Void)?)
  • Returns a named property for the layer.

    Declaration

    Swift

    public func getValue(forProperty name: String) -> String?

    Parameters

    name

    String property name.

    Return Value

    String? the property value, or nil if it does not exist.

  • Set a property/value pair.

    Declaration

    Swift

    public func setValue(_ value: String, forProperty name: String)

    Parameters

    name

    String property name.

    value

    String property value.