TiledLayerObject

public class TiledLayerObject : SKEffectNode, CustomReflectable, TiledMappableGeometryType, TiledObjectType

The TiledLayerObject is the base class for all SKTiled layer types.

This class doesn’t specify any object or child types, but provides base behaviors for layered content, including:

  • coordinate transformations
  • validating coordinates
  • positioning and alignment

Properties

  • tilemap: the parent tilemap container.
  • index: layer index. Matches the index of the layer in the source TMX file.
  • mapSize: layer size (in tiles).
  • tileSize: layer tile size (in pixels).
  • anchorPoint: layer anchor point, used to position layers.
  • offset: offset value for tweaking layer position. Matches the layer offset values set in the source TMX file.

Instance Methods

  • 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. (iOS only)
  • coordinateAtTouchLocation(_:): returns the tile coordinate at a touch location. (iOS only)
  • isValid(coord:): returns true if the coordinate is valid.

Usage

All layer types share identical methods for translating screen coordinates to map coordinates (and vice versa):

// assign a position matching a coordinate in the current projection
node.position = tileLayer.pointForCoordinate(2, 1)

// translate a point to map a coordinate
coord = coordinateForPoint(touchPosition)

In addition, all layer types respond to mouse/touch events:

// return the tile coordinate at a touch event (iOS)
coord = imageLayer.coordinateAtTouchLocation(touchPosition)

// return the tile coordinate at a mouse event (macOS)
coord = groupLayer.coordinateAtMouse(event: mouseClicked)

Layer Index

  • An integer representing the layer index value. Matches the id value of the layer in the source TMX file.

    Declaration

    Swift

    public var index: UInt32
  • The xPath attributes is used to signify the path to this layer in the XML document structure. This can be useful for returning a specific layer in a tilemap (see the SKTilemap.getLayer(xPath:) method).

    Declaration

    Swift

    public var xPath: String
  • Translate the parent hierarchy to an layer path string.

    Declaration

    Swift

    public var path: String { get }

Layer Properties

  • Custom layer properties.

    Declaration

    Swift

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

    Declaration

    Swift

    public var ignoreProperties: Bool
  • The TiledLayerType enumeration determines tile offset hints for coordinate conversion.

    Constants

    • 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.
    See more

    Declaration

    Swift

    public enum TileOffset : Int
  • Layer is visible to scene cameras.

    Declaration

    Swift

    @objc
    public var visibleToCamera: Bool

Color Attributes

Shapes

  • Layer bounding shape.

    Declaration

    Swift

    @objc
    public lazy var boundsShape: SKShapeNode? { get set }
  • Object anchor node visualization node.

    Declaration

    Swift

    @objc
    public lazy var anchorShape: SKShapeNode { get set }
  • Layer highlight duration.

    Declaration

    Swift

    public var highlightDuration: TimeInterval
  • Indicates this layer is isolated in the map.

    Declaration

    Swift

    public internal(set) var isIsolated: Bool { get }

Static Layers

  • Layer is static (no animated objects).

    Declaration

    Swift

    public internal(set) var isStatic: Bool { get }

Sizing & Positioning

Geometry

  • Returns the layer bounding rectangle (used to draw bounds).

    Declaration

    Swift

    @objc
    public override var boundingRect: CGRect { get }
  • Returns a rectangle in this node’s parent’s coordinate system. Currently only used in the SKTilemap.absoluteSize attribute.

    Declaration

    Swift

    public override var frame: CGRect { get }
  • This object’s CGPath defining the shape of geometry. Used to draw the bounding shape.

    Declaration

    Swift

    @objc
    public lazy var objectPath: CGPath { get set }
  • Returns the current tilemap tile update mode.

    Declaration

    Swift

    public var updateMode: TileUpdateMode { get }
  • Indicates the current node has received focus or selected.

    Declaration

    Swift

    public var isFocused: Bool { get set }

Initialization

  • Initialize via the tilemap 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])
  • Create a new layer within the parent tilemap node.

    Declaration

    Swift

    public init(layerName: String, tilemap: SKTilemap)

    Parameters

    layerName

    layer name.

    tilemap

    parent tilemap node.

  • Create a new layer within the parent tilemap node.

    Declaration

    Swift

    public init(tilemap: SKTilemap)

    Parameters

    tilemap

    parent tilemap node.

    Return Value

    tile layer object.

  • Instantiate the node with a decoder instance.

    Declaration

    Swift

    required public init?(coder aDecoder: NSCoder)

    Parameters

    aDecoder

    decoder.

Colors

  • Set the layer color with an SKColor.

    Declaration

    Swift

    public func setColor(color: SKColor)

    Parameters

    color

    object color.

  • Set the layer color with a hex string.

    Declaration

    Swift

    public func setColor(hexString: String)

    Parameters

    hexString

    color hex string.

Parents/Children

  • Returns a flattened array of contained child layers.

    Declaration

    Swift

    public var layers: [TiledLayerObject] { get }
  • Returns an array of parent layers.

    Declaration

    Swift

    public var parentLayers: [TiledLayerObject] { get }
  • Returns a flattened array of child layers.

    Declaration

    Swift

    public var childLayers: [TiledLayerObject] { get }

Touch Events

Mouse Events

  • Returns a mouse event location in the current layer.

    Declaration

    Swift

    public func mouseLocation(event: NSEvent) -> CGPoint

    Parameters

    event

    mouse event location.

    Return Value

    converted point in layer coordinate system.

  • Returns the tile coordinate at a mouse event location.

    Declaration

    Swift

    public func coordinateAtMouse(event: NSEvent) -> simd_int2

    Parameters

    event

    mouse event.

    Return Value

    coordinate in layer coordinate system.

Coordinate Conversion

  • Returns true if the coordinate is valid (within map bounds).

    Declaration

    Swift

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

    Parameters

    x

    x-coordinate.

    y

    y-coordinate.

    Return Value

    coordinate is valid.

  • Returns true if the coordinate is valid (within map bounds).

    Declaration

    Swift

    public func isValid(coord: simd_int2) -> Bool

    Parameters

    coord

    tile coordinate.

    Return Value

    coordinate is valid

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

    Declaration

    Swift

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

    Parameters

    point

    input point.

    Return Value

    point with y-value inverted.

Adding & Removing Nodes

  • 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: simd_int2,
                         offset: CGPoint = CGPoint.zero,
                         zpos: CGFloat? = nil)

    Parameters

    node

    SpriteKit node.

    coord

    tile coordinate.

    offset

    offset amount.

    zpos

    optional z-position.

  • 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

    object.

    x

    x-coordinate.

    y

    y-coordinate.

    offset

    offset amount.

    zpos

    optional z-position.

Callbacks

Dynamics

Shaders

  • Set a shader effect for the layer.

    Declaration

    Swift

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

    Parameters

    named

    shader file name.

    uniforms

    array of shader uniforms.

Debugging

  • Highlights the layer with the given color.

    Declaration

    Swift

    @objc
    public override func highlightNode(with color: SKColor, duration: TimeInterval = 0)

    Parameters

    color

    highlight color.

    duration

    duration of highlight effect.

  • Remove the current object’s highlight color.

    Declaration

    Swift

    @objc
    public override func removeHighlight()

Updating

Reflection

Deprecations

  • Initialize SpriteKit animation actions for the layer.

    Declaration

    Swift

    @available(*, deprecated, renamed: "runAnimationAsActions")
    public func runAnimationAsAction()

TiledLayerObject

  • Parse the layer’s properties value.

    Declaration

    Swift

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

    Parameters

    completion

    optional completion closure.

  • Returns a named property for the layer.

    Declaration

    Swift

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

    Parameters

    name

    property name.

    Return Value

    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

    value

    property name.

    name

    property value..

Extensions

  • Layer width (in tiles).

    Declaration

    Swift

    public var width: CGFloat { get }
  • Layer height (in tiles).

    Declaration

    Swift

    public var height: CGFloat { get }
  • Layer tile size width.

    Declaration

    Swift

    public var tileWidth: CGFloat { get }
  • Layer tile size height.

    Declaration

    Swift

    public var tileHeight: CGFloat { get }
  • Layer size, halved.

    Declaration

    Swift

    public var sizeHalved: CGSize { get }
  • Layer tile size width, halved.

    Declaration

    Swift

    public var tileWidthHalf: CGFloat { get }
  • Layer tile size height, halved.

    Declaration

    Swift

    public var tileHeightHalf: CGFloat { get }
  • Layer transparency.

    Declaration

    Swift

    public var opacity: CGFloat { get set }
  • 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

    SpriteKit node.

    x

    x-coordinate.

    y

    y-coordinate.

    dx

    offset x-amount.

    dy

    offset y-amount.

    zpos

    z-position (optional).

  • 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

    x-coordinate.

    y

    y-coordinate.

    offsetX

    x-offset value.

    offsetY

    y-offset value.

    Return Value

    position in layer.

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

    Declaration

    Swift

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

    Parameters

    coord

    tile coordinate.

    offset

    tile offset.

    Return Value

    point in layer.

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

    Declaration

    Swift

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

    Parameters

    coord

    tile coordinate.

    tileOffset

    tile offset hint.

    Return Value

    point in layer.

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

    Declaration

    Swift

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

    Parameters

    x

    x-position.

    y

    y-position.

    Return Value

    position in layer.

  • 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

    Parameters

    pos

    point to measure.

    Return Value

    distance vector.

Deprecations

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

    Declaration

    Swift

    @available(*, deprecated, renamed: "coordinateForPoint(point:﹚")
    public func coordinateForPoint(_ point: CGPoint) -> CGPoint

    Parameters

    point

    point in layer (spritekit space).

    Return Value

    tile coordinate.

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

    Declaration

    Swift

    @available(*, deprecated, message: "use coordinateForPoint(point:")
    public func coordinateForPoint(_ x: Int, _ y: Int) -> CGPoint

    Parameters

    x

    x-position.

    y

    y-position.

    Return Value

    position in layer.

  • Returns a tile coordinate for a given simd_int2 coordinate.

    Declaration

    Swift

    @available(*, deprecated, renamed: "pointForCoordinate(coord:﹚")
    public func pointForCoordinate(vec2: simd_int2, offsetX: CGFloat = 0, offsetY: CGFloat = 0) -> CGPoint

    Parameters

    vec2

    vector simd_int2 coordinate.

    offsetX

    x-offset value.

    offsetY

    y-offset value.

    Return Value

    position in layer.

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

    Declaration

    Swift

    @available(*, deprecated, message: "use coordinateForPoint(point:")
    public func vectorCoordinateForPoint(_ point: CGPoint) -> simd_int2

    Parameters

    point

    point in layer.

    Return Value

    tile coordinate.

  • Returns true if the coordinate is valid (within map bounds).

    Declaration

    Swift

    @available(*, deprecated, renamed: "isValid(_:_:﹚")
    public func isValid(_ x: Int, _ y: Int) -> Bool

    Parameters

    x

    x-coordinate.

    y

    y-coordinate.

    Return Value

    coordinate is valid.

  • Returns true if the coordinate is valid (within map bounds).

    Declaration

    Swift

    @available(*, deprecated, renamed: "isValid(coord:﹚")
    public func isValid(coord: CGPoint) -> Bool

    Parameters

    coord

    CGPoint coordinate.

    Return Value

    coordinate is valid.

  • 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

    @available(*, deprecated, renamed: "addChild(_:coord:offset:zpos:﹚")
    public func addChild(_ node: SKNode, coord: CGPoint, offset: CGPoint = CGPoint.zero, zpos: CGFloat? = nil)

    Parameters

    node

    SpriteKit node.

    coord

    tile coordinate.

    offset

    offset amount.

    zpos

    optional z-position.

  • Returns the tile coordinate at a mouse event location.

    Declaration

    Swift

    @available(*, deprecated, renamed: "coordinateAtMouse(event:﹚")
    public func coordinateAtMouseEvent(event: NSEvent) -> CGPoint

    Parameters

    event

    mouse event.

    Return Value

    converted point in layer coordinate system.

  • Layer position in the render tree.

    Declaration

    Swift

    @available(*, deprecated, message: "This was never fully implemented. The `TiledLayerObject.realIndex` attribute might be more useful.")
    public var rawIndex: UInt32 { get }
  • Represents the container size (in tiles).

    Declaration

    Swift

    @available(*, deprecated, renamed: "mapSize")
    public internal(set) var size: CGSize { get set }
  • Returns a string array containing the current layer statistics.

    Declaration

    Swift

    @available(*, deprecated, renamed: "layerOneLineDescrption")
    public var layerStatsDescription: [String] { get }

    Return Value

    array of statistics.

  • Indicates this layer is isolated in the map.

    Declaration

    Swift

    @available(*, deprecated, renamed: "isIsolated")
    public private(set) var isolated: Bool { get set }