SKTileset

public class SKTileset : NSObject, SKTiledObject

Overview

The tileset class manages a set of SKTilesetData objects, which store tile data including global id, texture and animation.

Tile data is accessed via a local id, and tiles can be instantiated with the resulting SKTilesetData instance:

if let data = tileset.getTileData(localID: 56) {
   let tile = SKTile(data: data)
}

Properties

Property Description
name Tileset name.
tilemap Reference to parent tilemap.
tileSize Tile size (in pixels).
columns Number of columns.
tilecount Tile count.
firstGID First tile global id.
lastGID Last tile global id.
tileData Set of tile data structures.

Instance Methods

Method Description
addTextures() Generate textures from a spritesheet image.
addTilesetTile() Add & return new tile data object.

Spacing

  • ▶︎spacing

    Spritesheet spacing between tiles.

    Declaration

    Swift

    public var spacing: Int
  • ▶︎margin

    Spritesheet border margin.

    Declaration

    Swift

    public var margin: Int
  • ▶︎tileOffset

    Offset for drawing tiles.

    Declaration

    Swift

    public var tileOffset: CGPoint
  • ▶︎source

    Texture name (if created from source)

    Declaration

    Swift

    public var source: String!
  • Indicates the tileset is a collection of images.

    Declaration

    Swift

    public var isImageCollection: Bool
  • The tileset is stored in an external file.

    Declaration

    Swift

    public var isExternalTileset: Bool { get }
  • Source image transparency color.

    Declaration

    Swift

    public var transparentColor: SKColor?
  • ▶︎isRendered

    Indicates all of the tile data textures have been set.

    Declaration

    Swift

    public internal(set) var isRendered: Bool { get }
  • Scaling factor for text objects, etc.

    Declaration

    Swift

    public var renderQuality: CGFloat { get set }
  • Initialize with basic properties.

    Declaration

    Swift

    public init(name: String, tileSize size: CGSize,
                firstgid: Int = 1, columns: Int = 0,
                offset: CGPoint = CGPoint.zero)

    Parameters

    name

    String tileset name.

    size

    CGSize tile size.

    firstgid

    Int first gid value.

    columns

    Int number of columns.

    offset

    CGPoint tileset offset value.

    Return Value

    SKTileset tileset object.

  • Initialize with an external tileset (only source and first gid are given).

    Declaration

    Swift

    public init(source: String, firstgid: Int,
                tilemap: SKTilemap, offset: CGPoint = CGPoint.zero)

    Parameters

    source

    String source file name.

    firstgid

    Int first gid value.

    tilemap

    SKTilemap parent tile map node.

    Return Value

    SKTileset tile set.

  • Initialize with attributes directly from TMX file.

    Declaration

    Swift

    public init?(attributes: [String: String],
                 offset: CGPoint = CGPoint.zero)

    Parameters

    attributes

    [String: String] attributes dictionary.

    offset

    CGPoint pixel offset in x/y.

  • Initialize with a TSX file name.

    Declaration

    Swift

    public init(fileNamed: String)

    Parameters

    fileNamed

    String tileset file name.

    delegate

    SKTilemapDelegate? optional tilemap delegate.

Loading

Textures

  • Add tile texture data from a sprite sheet image.

    Declaration

    Swift

    public func addTextures(fromSpriteSheet source: String, replace: Bool = false, transparent: String? = nil)

    Parameters

    source

    String image named referenced in the tileset.

    replace

    Bool replace the current texture.

    transparent

    String? optional transparent color hex value.

Tile Data

  • Add tileset tile data attributes. Returns a new SKTilesetData object, or nil if tile data already exists with the given id.

    Declaration

    Swift

    public func addTilesetTile(_ tileID: Int, texture: SKTexture) -> SKTilesetData?

    Parameters

    tileID

    Int local tile ID.

    texture

    SKTexture texture for tile at the given id.

    Return Value

    SKTilesetData? tileset data (or nil if the data exists).

  • Add tileset data from an image source (tileset is a collections tileset).

    Declaration

    Swift

    public func addTilesetTile(_ tileID: Int, source: String) -> SKTilesetData?

    Parameters

    tileID

    Int local tile ID.

    source

    String source image name.

    Return Value

    SKTilesetData? tileset data (or nil if the data exists).

  • Create new data from the given texture image path. Returns a new tileset data instance & the local id associated with it.

    Declaration

    Swift

    public func addTilesetTile(source: String) -> (id: UInt32, data: SKTilesetData)?

    Parameters

    source

    source image name.

    Return Value

    local tile id & tileset data (or nil if the data exists).

  • Set(replace) the texture for a given tile id.

    Declaration

    Swift

    @discardableResult
    public func setDataTexture(_ id: Int, texture: SKTexture) -> SKTexture?

    Parameters

    tileID

    Int tile ID.

    texture

    SKTexture texture for tile at the given id.

    Return Value

    SKTexture? previous tile data texture.

  • Set(replace) the texture for a given tile id.

    Declaration

    Swift

    @discardableResult
    public func setDataTexture(_ id: Int, imageNamed: String) -> SKTexture?

    Parameters

    tileID

    Int tile ID.

    imageNamed

    String source texture name.

    Return Value

    SKTexture? old tile data texture.

  • Returns true if the tileset contains the global ID.

    Declaration

    Swift

    public func contains(globalID gid: UInt32) -> Bool

    Parameters

    globalID

    UInt32 global tile id.

    Return Value

    Bool tileset contains the global id.

  • Returns tile data for the given global tile ID.

    ** Tiled ID == GID + 1

    Declaration

    Swift

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

    Parameters

    globalID

    Int global tile id.

    Return Value

    SKTilesetData? tile data object.

  • Returns tile data for the given local tile ID.

    Declaration

    Swift

    public func getTileData(localID id: Int) -> SKTilesetData?

    Parameters

    localID

    Int local tile id.

    Return Value

    SKTilesetData? tile data object.

  • Returns tile data with the given property.

    Declaration

    Swift

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

    Parameters

    withProperty

    String property name.

    Return Value

    [SKTilesetData] array of tile data.

  • Returns tile data with the given name & animated state.

    Declaration

    Swift

    public func getTileData(named name: String, isAnimated: Bool = false) -> [SKTilesetData]

    Parameters

    named

    String data name.

    isAnimated

    Bool filter data that is animated.

    Return Value

    [SKTilesetData] array of tile data.

  • Returns tile data with the given type.

    Declaration

    Swift

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

    Parameters

    ofType

    String data type.

    Return Value

    [SKTilesetData] array of tile data.

  • Returns tile data with the given property.

    Declaration

    Swift

    public func getTileData(withProperty property: String, _ value: Any) -> [SKTilesetData]

    Parameters

    property

    String property name.

    value

    AnyObject value

    Return Value

    [SKTilesetData] array of tile data.

  • Returns animated tile data.

    Declaration

    Swift

    public func getAnimatedTileData() -> [SKTilesetData]

    Return Value

    [SKTilesetData] array of animated tile data.

  • Convert a global ID to the tileset’s local ID.

    Declaration

    Swift

    public func getLocalID(forGlobalID gid: Int) -> Int

    Parameters

    gid

    Int global id.

    Return Value

    Int local tile ID.

  • Convert a global ID to the tileset’s local ID.

    Declaration

    Swift

    public func getGlobalID(forLocalID id: Int) -> Int

    Parameters

    id

    Int local id.

    Return Value

    Int global tile ID.

  • Parse the tileset’s properties value.

    Declaration

    Swift

    public func parseProperties(completion: (() -> Void)?)
  • ▶︎description

    String representation of the tileset object.

    Declaration

    Swift

    override public var description: String { get }