SKTileset

public class SKTileset : NSObject, CustomReflectable, TiledObjectType

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

Tileset Setup

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

  • name: tileset name.
  • tilemap: reference to parent tilemap.
  • tileSize: tile size (in pixels).
  • localRange: range of local tile data id values.
  • globalRange: range of global tile data id values.
  • columns: number of columns.
  • tilecount: tile count.
  • firstGID: first tile global id.
  • lastGID: last tile global id.
  • tileData: set of tile data structures.

Class Functions

  • load(tsxFile:delegate:): Load a tileset from a file.
  • load(tsxFiles:delegate:): Load multiple tilesets.

Instance Methods

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

For more information, see the Working with Tilesets page in the official documentation.

Spacing

Initialization

  • Initialize with basic properties.

    Declaration

    Swift

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

    Parameters

    name

    tileset name.

    size

    tile size.

    firstgid

    first gid value.

    columns

    number of columns.

    offset

    tileset offset value.

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

    Declaration

    Swift

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

    Parameters

    source

    source file name.

    firstgid

    first gid value.

    tilemap

    parent tile map node.

    offset

    tile offset value.

  • Initialize with attributes directly from TMX file.

    Declaration

    Swift

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

    Parameters

    attributes

    attributes dictionary.

    offset

    pixel offset in x/y.

Loading

Textures

  • Create tile texture data from a spritesheet image.

    Declaration

    Swift

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

    Parameters

    source

    image path referenced in the tileset.

    replace

    replace the current texture.

    transparent

    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: UInt32, texture: SKTexture) -> SKTilesetData?

    Parameters

    tileID

    local tile ID.

    texture

    texture for tile at the given id.

    Return Value

    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: UInt32, source: String) -> SKTilesetData?

    Parameters

    tileID

    local tile ID.

    source

    source image name.

    Return Value

    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(tileID: UInt32, texture: SKTexture) -> SKTexture?

    Parameters

    tileID

    tile ID.

    texture

    texture for tile at the given id.

    Return Value

    previous tile data texture.

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

    Declaration

    Swift

    @discardableResult
    public func setDataTexture(tileID: UInt32, imageNamed: String) -> SKTexture?

    Parameters

    tileID

    tile ID.

    imageNamed

    source texture name.

    Return Value

    old tile data texture.

  • Returns true if the tileset contains the global ID.

    Declaration

    Swift

    public func contains(globalID: UInt32) -> Bool

    Parameters

    globalID

    global tile id.

    Return Value

    tileset contains the global id.

  • Returns tile data for the given global tile ID (if it exists).

    Declaration

    Swift

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

    Parameters

    gid

    global tile id.

    Return Value

    tile data object.

  • Returns tile data for the given local tile ID.

    Declaration

    Swift

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

    Parameters

    id

    local tile id.

    Return Value

    tile data.

  • Returns tile data matching the given property.

    Declaration

    Swift

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

    Parameters

    property

    property name.

    Return Value

    array of tile data.

  • Returns tile data with the given property & value.

    Declaration

    Swift

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

    Parameters

    property

    property name.

    value

    property value.

    Return Value

    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

    name

    data name.

    isAnimated

    filter data that is animated.

    Return Value

    array of tile data.

  • Returns tile data with the given type.

    Declaration

    Swift

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

    Parameters

    ofType

    data type.

    Return Value

    array of tile data.

  • Returns animated tile data.

    Declaration

    Swift

    public func getAnimatedTileData() -> [SKTilesetData]

    Return Value

    array of animated tile data.

  • Returns tile data with collision shapes.

    Declaration

    Swift

    public func getTileDataWithCollisionShapes() -> [SKTilesetData]

    Return Value

    array of tile data.

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

    Declaration

    Swift

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

    Parameters

    id

    local id.

    Return Value

    global tile ID.

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

    Declaration

    Swift

    public func getLocalID(globalID: UInt32) -> UInt32

    Parameters

    globalID

    global id.

    Return Value

    local tile id.

Reflection

SKTileset

Extensions

  • String representation of the tileset object.

    Declaration

    Swift

    public override var description: String { get }
  • Debugging string representation of the tileset object.

    Declaration

    Swift

    public override var debugDescription: String { get }
  • Creates and returns a new tile instance with the given global id.

    Declaration

    Swift

    public func newTile(globalID: UInt32, type tileType: SKTile.Type = SKTile.self) -> SKTile?

    Parameters

    localID

    tile global id.

    tileType

    tile object type.

    Return Value

    tile instance, if tile data exists.

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

    Declaration

    Swift

    public func newTile(localID: UInt32, type tileType: SKTile.Type = SKTile.self) -> SKTile?

    Parameters

    localID

    tile local id.

    tileType

    tile object type.

    Return Value

    tile instance, if tile data exists.

  • Returns the internal Tiled node type.

    Declaration

    Swift

    @objc
    public var tiledElementName: String { get }
  • Returns the internal Tiled node type icon.

    Declaration

    Swift

    @objc
    public var tiledIconName: String { get }
  • A description of the node type used for help features.

    Declaration

    Swift

    @objc
    public var tiledHelpDescription: String { get }

Deprecations

  • Initialize with basic properties.

    Declaration

    Swift

    @available(*, deprecated, renamed: "init(name:tileSize:firstgid:columns:offset:﹚")
    public convenience init(name: String,
                tileSize size: CGSize,
                firstgid: Int = 1,
                columns: Int = 0,
                offset: CGPoint = CGPoint.zero)

    Parameters

    name

    tileset name.

    size

    tile size.

    firstgid

    first gid value.

    columns

    number of columns.

    offset

    tileset offset value.

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

    Declaration

    Swift

    @available(*, deprecated, renamed: "init(source:firstgid:tilemap:offset:﹚")
    public convenience init(source: String,
                firstgid: Int,
                tilemap: SKTilemap,
                offset: CGPoint = CGPoint.zero)

    Parameters

    source

    source file name.

    firstgid

    first gid value.

    tilemap

    parent tile map node.

    offset

    tile offset value.

  • Returns tile data for the given global tile ID (if it exists).

    Declaration

    Swift

    @available(*, deprecated, message: "tile ids should be `UInt32` type.")
    public func getTileData(globalID gid: Int) -> SKTilesetData?

    Parameters

    gid

    global tile id.

    Return Value

    tile data object.

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

    Declaration

    Swift

    @available(*, deprecated, message: "tile ids should be `UInt32` type.")
    public func getGlobalID(forLocalID id: Int) -> Int

    Parameters

    id

    local id.

    Return Value

    global tile ID.

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

    Declaration

    Swift

    @available(*, deprecated, renamed: "getLocalID(globalID:﹚")
    public func getLocalID(forGlobalID gid: UInt32) -> UInt32

    Parameters

    gid

    global id.

    Return Value

    local tile id.

  • Returns tile data for the given local tile ID.

    Declaration

    Swift

    @available(*, deprecated, message: "tile ids should be `UInt32` type.")
    public func getTileData(localID id: Int) -> SKTilesetData?

    Parameters

    id

    local tile id.

    Return 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

    @available(*, deprecated, renamed: "addTilesetTile(tileID:texture:﹚")
    public func addTilesetTile(_ tileID: Int, texture: SKTexture) -> SKTilesetData?

    Parameters

    tileID

    local tile ID.

    texture

    texture for tile at the given id.

    Return Value

    tileset data (or nil if the data exists).

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

    Declaration

    Swift

    @available(*, deprecated, renamed: "addTilesetTile(tileID:source:﹚")
    public func addTilesetTile(_ tileID: Int, source: String) -> SKTilesetData?

    Parameters

    tileID

    local tile ID.

    source

    source image name.

    Return Value

    tileset data (or nil if the data exists).

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

    Declaration

    Swift

    @available(*, deprecated, renamed: "setDataTexture(_:texture:﹚")
    @discardableResult
    public func setDataTexture(_ id: Int, texture: SKTexture) -> SKTexture?

    Parameters

    id

    tile ID.

    texture

    texture for tile at the given id.

    Return Value

    previous tile data texture.