SKTile

open class SKTile : SKSpriteNode, CustomReflectable

The SKTile class is a custom SpriteKit sprite node that references its image and animation data from a tileset container. The tile represents a single piece of a larger image stored in a tile layer container.

Tile Data Setup

Properties

  • globalId: tile global id.
  • tileData: tileset tile data reference.
  • tileSize: tile size (in pixels).
  • layer: parent tile layer.

Instance Methods

  • setupPhysics(shapeOf:isDynamic:): setup physics for the tile.
  • setupPhysics(rectSize:isDynamic:): setup physics for the tile.
  • setupPhysics(withSize:isDynamic:): setup physics for the tile.
  • runAnimation(): play tile animation (if animated).
  • removeAnimation(restore:): remove animation.
  • runAnimationAsActions(): runs a SpriteKit action to animate the tile.
  • removeAnimationActions(restore:): remove the animation for the current tile.
  • Tile size (in pixels).

    Declaration

    Swift

    open var tileSize: CGSize
  • Node unique indentifier.

    Declaration

    Swift

    @objc
    public var uuid: String
  • Node type. With a tile, this will never be set.

    Declaration

    Swift

    @objc
    public var type: String!
  • Overview

    The SKTile.tileData property holds a reference to the tile data contained in the referencing tileset. This struct contains attributes like texture, animation frame data and

    Declaration

    Swift

    open var tileData: SKTilesetData { get set }
  • Overview

    Tile global id attribute. This attribute determines the tile data assigned to the tile. Changing this value will update this tiles’ tile data.

    This is a wrapper for the TileID data structure and represents both global ID & tile orientation flags.

    Declaration

    Swift

    @TileID
    open var globalId: UInt32 { get set }
  • Returns the tile data “real value” (global id with flags mask). If tile flip flags have been set in Tiled, this value will match the value set in the parent layer’s tile data array.

    For example, a value of 2684354571 translates to a global id of 11, flipped horizontally & diagonally.

    tile.globalId = 11
    tile.flipFlags = [.flipHorizontal, .flipDiagonal]
    print(tile.maskedTileId)
    // 2684354571
    

    Declaration

    Swift

    public var maskedTileId: UInt32 { get }
  • The tile’s current coordinate.

    Declaration

    Swift

    open var currentCoordinate: simd_int2
  • Reference to the parent layer.

    Declaration

    Swift

    open weak var layer: TiledLayerObject!
  • Parent tile onbject.

    Declaration

    Swift

    open weak var object: SKTileObject?
  • Object is visible in camera.

    Declaration

    Swift

    open var visibleToCamera: Bool
  • Overview

    The tile render mode for this instance.

    • Render Modes
      • default: tile renders at default settings.
      • static: tile ignores any animation data.
      • ignore: tile does not take into account its tile data.
      • animate: animate with a global id value.

    Declaration

    Swift

    open var renderMode: TileRenderMode { get set }
  • Returns true if the tile is part of a tile object.

    Declaration

    Swift

    public internal(set) var isTileObject: Bool { get }
  • Declaration

    Swift

    @objc
    public var _debugLevel: UInt8
  • Debug visualization options.

    Declaration

    Swift

    public var debugDrawOptions: DebugDrawOptions

Properties

  • Ignore custom node properties.

    Declaration

    Swift

    @objc
    public var ignoreProperties: Bool
  • Storage for custom Tiled properties. These properties are set in the Tiled scene.

    Declaration

    Swift

    @objc
    public var properties: [String : String] { get set }
  • Return a string value for the given key, if it exists.

    Usage

     if let characterName = tile["characterName"] {
        print("character name is '\(characterName)'.")
     }
    

    Declaration

    Swift

    public subscript(key: String) -> String? { get set }

    Parameters

    key

    String key to query.

Tile Handlers

  • Indicates the current node has received focus or selected.

    Declaration

    Swift

    public var isFocused: Bool { get set }
  • Alignment hint used to define how to handle tile positioning within layers & objects (in the event the tile size is different than the parent).

    Properties

    • topLeft: Tile is positioned at the upper left.
    • top: Tile is positioned at top.
    • topRight: Tile is positioned at the upper right.
    • left: Tile is positioned at the left.
    • center: Tile is positioned in the center.
    • right: Tile is positioned to the right.
    • bottomLeft: Tile is positioned at the bottom left.
    • bottom: Tile is positioned at the bottom.
    • bottomRight: Tile is positioned at the bottom right.

    Declaration

    Swift

    public enum TileAlignmentHint : UInt8
  • Render scaling property.

    Declaration

    Swift

    @objc
    public var renderQuality: CGFloat { get set }

Color Attributes

Geometry

  • 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 }
  • Tile highlight duration.

    Declaration

    Swift

    open var highlightDuration: TimeInterval
  • Enable tile animation.

    Declaration

    Swift

    open var enableAnimation: Bool { get set }
  • 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 an array of points representing the tile’s bounding shape.

    Declaration

    Swift

    @objc
    open override func getVertices(offset: CGPoint = CGPoint.zero) -> [CGPoint]

    Parameters

    offset

    point offset value.

    Return Value

    array of bounding shape points.

  • Returns the bounding box of the shape.

    Declaration

    Swift

    open override var boundingRect: CGRect { get }

Initialization

  • Instantiate the tile with SKTilesetData data instance.

    Declaration

    Swift

    required public init?(data: SKTilesetData)

    Parameters

    data

    tile data structure.

  • Instantiate the tile with a decoder instance.

    Declaration

    Swift

    required public init?(coder aDecoder: NSCoder)

    Parameters

    aDecoder

    decoder.

  • Instantiate an empty tile.

    Declaration

    Swift

    required public init()
  • Initialize the tile with a tile size.

    Declaration

    Swift

    public init(tileSize size: CGSize)

    Parameters

    size

    tile size in pixels.

  • Initialize the tile with a tile texture.

    Declaration

    Swift

    public init(texture: SKTexture?)

    Parameters

    texture

    tile texture.

  • Removes this node from the scene graph. Signals the tile cache to remove the tile.

    Declaration

    Swift

    open override func destroy()
  • Creates and returns a new tile instance with the given tileset & global id.

    Declaration

    Swift

    public class func newTile(globalID: UInt32, in tileset: SKTileset) -> SKTile

    Parameters

    globalID

    tile global id.

    tileset

    tileset instance.

    Return Value

    tile object with the given data.

  • Creates and returns a new tile instance with the given tileset & local id.

    Declaration

    Swift

    public class func newTile(localID: UInt32, in tileset: SKTileset) -> SKTile

    Parameters

    localID

    tileset local id.

    tileset

    tileset instance.

    Return Value

    tile object with the given data.

Drawing

  • Draw the tile. Forces the tile to update its textures.

    Declaration

    Swift

    @objc
    open func draw()

Physics

  • Describes the tile’s physics shape.

    Properties

    • none: No physics shape.
    • rectangle: Rectangular object shape.
    • ellipse: Circular object shape.
    • texture: Texture-based shape.
    • path: Open path.

    Declaration

    Swift

    public enum PhysicsShape
  • Physics body shape.

    Declaration

    Swift

    open var physicsShape: PhysicsShape
  • Set up the tile’s dynamics body.

    Declaration

    Swift

    open func setupPhysics(shapeOf: PhysicsShape = PhysicsShape.rectangle, isDynamic: Bool = false)

    Parameters

    shapeOf

    tile physics shape type.

    isDynamic

    physics body is active.

  • Set up the tile’s dynamics body with a rectanglular shape.

    Declaration

    Swift

    open func setupPhysics(rectSize: CGSize, isDynamic: Bool = false)

    Parameters

    rectSize

    rectangle size.

    isDynamic

    physics body is active.

  • Set up the tile’s dynamics body with a rectanglular shape.

    Declaration

    Swift

    open func setupPhysics(withSize: CGFloat, isDynamic: Bool = false)

    Parameters

    withSize

    rectangle size.

    isDynamic

    physics body is active.

  • Set up the tile’s dynamics body with a circular shape.

    Declaration

    Swift

    open func setupPhysics(radius: CGFloat, isDynamic: Bool = false)

    Parameters

    radius

    circle radius.

    isDynamic

    physics body is active.

  • Remove tile physics body.

    Declaration

    Swift

    open func removePhysics()
  • Set up the tile collision shape. Offset for tile objects should be zero.

    Declaration

    Swift

    open func setupTileCollisions(offset: CGSize = CGSize.zero)

    Parameters

    zero

    offset value.

Animation

Overlap

  • Set the tile overlap amount.

    Declaration

    Swift

    open func setTileOverlap(_ overlap: CGFloat)

    Parameters

    overlap

    overlap amount.

Events & Handlers

  • Informs the receiver that the mouse has moved.

    Declaration

    Swift

    open override func mouseMoved(with event: NSEvent)

    Parameters

    event

    mouse event.

  • Informs the receiver that the user has pressed the left mouse button.

    Declaration

    Swift

    open override func mouseDown(with event: NSEvent)

    Parameters

    event

    mouse event.

  • Tells this object that one or more new touches occurred in a view or window.

  • Returns true if the touch event (mouse or touch) hits this node.

    Declaration

    Swift

    @objc
    public override func contains(touch: CGPoint) -> Bool

    Parameters

    touch

    touch point in this node.

    Return Value

    node was touched.

Updating

  • Render the tile before each frame is rendered.

    Declaration

    Swift

    open func update(_ deltaTime: TimeInterval)

    Parameters

    deltaTime

    update interval.

Reflection

Convenience Properties

  • A reference to the tile data’s containing tileset.

    Declaration

    Swift

    open var tileset: SKTileset? { get }

    Return Value

    tileset instance, if one exists.

  • Reference to the tile’s parent tilemap.

    Declaration

    Swift

    open var tilemap: SKTilemap? { get }
  • Opacity value of the tile.

    Declaration

    Swift

    open var opacity: CGFloat { get set }
  • Toggle for tile visibility.

    Declaration

    Swift

    open var isVisble: Bool { get set }
  • Tile flip flags.

    Declaration

    Swift

    public var flipFlags: TileFlags { get set }
  • Tile is flipped horizontally.

    Declaration

    Swift

    public var isFlippedHorizontally: Bool { get set }
  • Tile is flipped vertically.

    Declaration

    Swift

    public var isFlippedVertically: Bool { get set }
  • Tile is flipped diagonally.

    Declaration

    Swift

    public var isFlippedDiagonally: Bool { get set }
  • The offset position of the tile.

    Declaration

    Swift

    public var tileOffset: CGPoint { get }
  • Copies the tile object to a generic SpriteKit SKSpriteNode node. If the tile has animation, returns a sprite running a custom SKAction.

    Declaration

    Swift

    public func spriteCopy() -> SKSpriteNode

    Return Value

    sprite copy of tile with current texture/animation.

  • Replace the tile object with sprite copy of the tile.

    Declaration

    Swift

    @discardableResult
    public func replaceWithSpriteCopy() -> SKSpriteNode

    Return Value

    sprite with current animation.

  • Clone the tile data and apply it to this instance.

    Declaration

    Swift

    public func withTileDataClone()
  • Highlight the tile with a given color & duration.

    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()

Deprecations

  • Toggle for tile visibility.

    Declaration

    Swift

    @available(*, deprecated, renamed: "isVisible")
    open var visible: Bool { get set }
  • Returns the tile global id unmasked.

    Declaration

    Swift

    @available(*, deprecated, renamed: "maskedTileId")
    public var realTileId: UInt32 { get }
  • Pauses tile animation

    Declaration

    Swift

    @available(*, deprecated, message: "Use the default `SKNode.isPaused` to pause animation.")
    open var pauseAnimation: Bool { get set }
  • Returns a shortened textual representation for debugging.

    Declaration

    Swift

    @available(*, deprecated, renamed: "description")
    open var shortDescription: String { get }
  • Draw the tile. Forces the tile to update its textures.

    Declaration

    Swift

    @available(*, deprecated, renamed: "draw(﹚")
    @objc
    open func draw(debug: Bool = false)

    Parameters

    debug

    debug draw.

  • Draw the tile. Force the tile to update its textures.

    Declaration

    Swift

    @available(*, deprecated, renamed: "draw(﹚")
    open func draw(in rect: CGRect, debug: Bool = false)

    Parameters

    rect

    rectangle.

    debug

    debug draw.

  • Update the tile’s tile data instance.

    Declaration

    Swift

    @available(*, deprecated, message: "Use the `SKTile.tileData` property.")
    open func setTileData(data: SKTilesetData)

    Parameters

    data

    new tile data.