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. |
-
▶︎urlTileset url (external tileset).
Declaration
Swift
public var url: URL! -
▶︎filenameTiled tsx filename (external tileset).
Declaration
Swift
public var filename: String! -
▶︎uuidUnique object id.
Declaration
Swift
public var uuid: String -
▶︎nameTileset name
Declaration
Swift
public var name: String -
▶︎typeObject 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 -
▶︎tilemapReference to parent tilemap.
Declaration
Swift
public var tilemap: SKTilemap! -
▶︎tileSizeTile size (in pixels).
Declaration
Swift
public var tileSize: CGSize -
▶︎columnsThe number of tile columns.
Declaration
Swift
public var columns: Int -
The number of tiles contained in this set.
Declaration
Swift
public internal(set) var tilecount: Int { get } -
Tile data count.
Declaration
Swift
public var dataCount: Int { get } -
▶︎firstGIDTile offset value.
Declaration
Swift
public var firstGID: Int -
▶︎lastGIDReturns the last global tile id in the tileset.
Declaration
Swift
public var lastGID: Int { get } -
Returns a range of localized tile ids.
Declaration
Swift
public var localRange: ClosedRange<Int> { get } -
Returns a range of global tile id values.
Declaration
Swift
public var globalRange: ClosedRange<Int> { get }
-
▶︎spacingSpritesheet spacing between tiles.
Declaration
Swift
public var spacing: Int -
▶︎marginSpritesheet border margin.
Declaration
Swift
public var margin: Int -
Offset for drawing tiles.
Declaration
Swift
public var tileOffset: CGPoint -
▶︎sourceTexture 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 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
nameStringtileset name.sizeCGSizetile size.firstgidIntfirst gid value.columnsIntnumber of columns.offsetCGPointtileset offset value.Return Value
SKTilesettileset 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
sourceStringsource file name.firstgidIntfirst gid value.tilemapSKTilemapparent tile map node.Return Value
SKTilesettile 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.offsetCGPointpixel offset in x/y. -
Initialize with a TSX file name.
Declaration
Swift
public init(fileNamed: String)Parameters
fileNamedStringtileset file name.delegateSKTilemapDelegate?optional tilemap delegate.
-
Loads Tiled tsx files and returns an array of
SKTilesetobjects.Declaration
Swift
public class func load(tsxFiles: [String], delegate: SKTilemapDelegate? = nil, ignoreProperties noparse: Bool = false) -> [SKTileset]Parameters
tsxFiles[String]Tiled tileset filenames.delegateSKTilemapDelegate?optionalSKTilemapDelegateinstance.ignorePropertiesBoolignore custom properties from Tiled.Return Value
[SKTileset]tileset objects.
-
Add tile texture data from a sprite sheet image.
Declaration
Swift
public func addTextures(fromSpriteSheet source: String, replace: Bool = false, transparent: String? = nil)Parameters
sourceStringimage named referenced in the tileset.replaceBoolreplace the current texture.transparentString?optional 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: Int, texture: SKTexture) -> SKTilesetData?Parameters
tileIDIntlocal tile ID.textureSKTexturetexture 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
tileIDIntlocal tile ID.sourceStringsource 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
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(_ id: Int, texture: SKTexture) -> SKTexture?Parameters
tileIDInttile ID.textureSKTexturetexture 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
tileIDInttile ID.imageNamedStringsource 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) -> BoolParameters
globalIDUInt32global tile id.Return Value
Booltileset 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
globalIDIntglobal 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
localIDIntlocal 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
withPropertyStringproperty 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
namedStringdata name.isAnimatedBoolfilter 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
ofTypeStringdata 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
propertyStringproperty name.valueAnyObjectvalueReturn 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) -> IntParameters
gidIntglobal id.Return Value
Intlocal tile ID. -
Convert a global ID to the tileset’s local ID.
Declaration
Swift
public func getGlobalID(forLocalID id: Int) -> IntParameters
idIntlocal id.Return Value
Intglobal tile ID. -
Parse the tileset’s properties value.
Declaration
Swift
public func parseProperties(completion: (() -> Void)?) -
String representation of the tileset object.
Declaration
Swift
override public var description: String { get }
View on GitHub
SKTileset Class Reference