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.
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.
-
Tileset url (external tileset).
Declaration
Swift
public var url: URL! -
Unique object id.
Declaration
Swift
public var uuid: String -
Tiled tsx filename (external tileset, ie: “tileset.tsx”).
Declaration
Swift
public var filename: String! -
Tileset name
Declaration
Swift
public var name: String -
Object type.
Declaration
Swift
public var type: String! -
Custom tileset properties.
Declaration
Swift
public var properties: [String : String] -
Ignore custom properties.
Declaration
Swift
public var ignoreProperties: Bool -
Reference to parent tilemap.
Declaration
Swift
public weak var tilemap: SKTilemap! -
Tileset tile size (in pixels).
Declaration
Swift
public var tileSize: CGSize -
Number of tileset columns.
Declaration
Swift
public var columns: Int -
Tileset tile count.
Declaration
Swift
public internal(set) var tilecount: Int { get } -
Tileset tile data count (not the same as tile count).
Declaration
Swift
public var dataCount: Int { get } -
Tileset first gid.
Declaration
Swift
public internal(set) var firstGID: UInt32 { get } -
Returns the last global tile id in the tileset.
Declaration
Swift
public var lastGID: UInt32 { get } -
Returns a range of localized tile id values.
Declaration
Swift
public var localRange: ClosedRange<UInt32> { get } -
Returns a range of global tile id values.
Declaration
Swift
public var globalRange: ClosedRange<UInt32> { get }
-
Spritesheet spacing between tiles.
Declaration
Swift
public var spacing: Int -
Spritesheet border margin.
Declaration
Swift
public var margin: Int -
Offset for drawing tiles.
Declaration
Swift
public var tileOffset: CGPoint -
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? -
Indicates tileset is rendered.
Declaration
Swift
public var isRendered: Bool -
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: UInt32 = 1, columns: Int = 0, offset: CGPoint = CGPoint.zero)Parameters
nametileset name.
sizetile size.
firstgidfirst gid value.
columnsnumber of columns.
offsettileset 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
sourcesource file name.
firstgidfirst gid value.
tilemapparent tile map node.
offsettile offset value.
-
Initialize with attributes directly from TMX file.
Declaration
Swift
public init?(attributes: [String: String], offset: CGPoint = CGPoint.zero)Parameters
attributesattributes dictionary.
offsetpixel offset in x/y.
-
Loads Tiled tsx files and returns an array of
SKTilesetobjects.Declaration
Swift
public class func load(tsxFiles: [String], delegate: TilemapDelegate? = nil, ignoreProperties noparse: Bool = false) -> [SKTileset]Parameters
tsxFilesTiled tileset filenames.
delegatenoparseignore custom properties from Tiled.
Return Value
array of tilesets.
-
Load a Tiled tsx file and return a
SKTilesetobject.Declaration
Swift
public class func load(tsxFile: String, dataSource: TilesetDataSource? = nil, ignoreProperties noparse: Bool = false) -> SKTileset?Parameters
tsxFiletileset filename.
dataSourcetileset data source delegate.
noparseignore custom properties.
Return Value
tileset, if created.
-
Create tile texture data from a spritesheet image.
Declaration
Swift
public func addTextures(fromSpriteSheet source: String, replace: Bool = false, transparent: String? = nil)Parameters
sourceimage path referenced in the tileset.
replacereplace the current texture.
transparentoptional transparent color hex value.
-
Add tileset tile data attributes. Returns a new
SKTilesetDataobject, or nil if tile data already exists with the given id.Declaration
Swift
public func addTilesetTile(tileID: UInt32, texture: SKTexture) -> SKTilesetData?Parameters
tileIDlocal tile ID.
texturetexture 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
tileIDlocal tile ID.
sourcesource 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
sourcesource 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
tileIDtile ID.
texturetexture 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
tileIDtile ID.
imageNamedsource texture name.
Return Value
old tile data texture.
-
Returns true if the tileset contains the global ID.
Declaration
Swift
public func contains(globalID: UInt32) -> BoolParameters
globalIDglobal 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
gidglobal 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
idlocal tile id.
Return Value
tile data.
-
Returns tile data matching the given property.
Declaration
Swift
public func getTileData(withProperty property: String) -> [SKTilesetData]Parameters
propertyproperty 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
propertyproperty name.
valueproperty 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
namedata name.
isAnimatedfilter 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
ofTypedata 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) -> UInt32Parameters
idlocal id.
Return Value
global tile ID.
-
Convert a global ID to the tileset’s local ID.
Declaration
Swift
public func getLocalID(globalID: UInt32) -> UInt32Parameters
globalIDglobal id.
Return Value
local tile id.
-
Returns a custom mirror for this object.
Declaration
Swift
public var customMirror: Mirror { get }
-
Parse the tileset’s properties value.
Declaration
Swift
public func parseProperties(completion: (() -> Void)?)Parameters
completionoptional completion closure.
-
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
Parameters
localIDtile global id.
tileTypetile object type.
Return Value
tile instance, if tile data exists.
-
Creates and returns a new tile instance with the given local id.
Declaration
Parameters
localIDtile local id.
tileTypetile 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 }
-
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
nametileset name.
sizetile size.
firstgidfirst gid value.
columnsnumber of columns.
offsettileset 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
sourcesource file name.
firstgidfirst gid value.
tilemapparent tile map node.
offsettile 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
gidglobal 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) -> IntParameters
idlocal 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) -> UInt32Parameters
gidglobal 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
idlocal tile id.
Return Value
tile data.
-
Add tileset tile data attributes. Returns a new
SKTilesetDataobject, 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
tileIDlocal tile ID.
texturetexture 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
tileIDlocal tile ID.
sourcesource 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
idtile ID.
texturetexture for tile at the given id.
Return Value
previous tile data texture.
View on GitHub
Install in Dash
SKTileset Class Reference