SKTileLayer
public class SKTileLayer : SKTiledLayerObject
Overview
Subclass of SKTiledLayerObject
, the tile layer is a container for an array of tiles (sprites). Tiles maintain a link to the map’s tileset via their SKTilesetData
property.
Properties
Property | Description |
---|---|
tileCount | Returns a count of valid tiles. |
Instance Methods
Method | Description |
---|---|
getTiles() | Returns an array of current tiles. |
getTiles(ofType:) | Returns tiles of the given type. |
getTiles(globalID:) | Returns all tiles matching a global id. |
getTilesWithProperty(:) | Returns tiles matching the given property & value. |
animatedTiles() | Returns all animated tiles. |
getTileData(globalID:) | Returns all tiles matching a global id. |
tileAt(coord:) | Returns a tile at the given coordinate, if one exists. |
Usage
Accessing a tile at a given coordinate:
let tile = tileLayer.tileAt(2, 6)!
Query tiles of a certain type:
let floorTiles = tileLayer.getTiles(ofType: "Floor")
-
Returns a count of valid tiles.
Declaration
Swift
public var tileCount: Int { get }
-
Debug visualization options.
Declaration
Swift
override public var debugDrawOptions: DebugDrawOptions { get set }
-
Tile highlight duration
Declaration
Swift
override public var highlightDuration: TimeInterval { get set }
-
Returns a tile at the given coordinate, if one exists.
Declaration
Swift
public func tileAt(_ x: Int, _ y: Int) -> SKTile?
Parameters
x
Int
y-coordinate.y
Int
x-coordinate.Return Value
SKTile?
tile object, if it exists. -
Returns a tile at the given coordinate, if one exists.
Declaration
Swift
public func tileAt(coord: CGPoint) -> SKTile?
Parameters
coord
CGPoint
tile coordinate.Return Value
SKTile?
tile object, if it exists. -
Returns a tile at the given screen position, if one exists.
Declaration
Swift
public func tileAt(point: CGPoint, offset: CGPoint = CGPoint.zero) -> SKTile?
Parameters
point
CGPoint
screen point.offset
CGPoint
pixel offset.Return Value
SKTile?
tile object, if it exists. -
Returns an array of current tiles.
Declaration
Swift
public func getTiles() -> [SKTile]
Return Value
[SKTile]
array of tiles. -
Returns tiles with a property of the given type.
Declaration
Swift
public func getTiles(ofType: String) -> [SKTile]
Parameters
ofType
String
type.Return Value
[SKTile]
array of tiles. -
Returns tiles matching the given global id.
Declaration
Swift
public func getTiles(globalID: Int) -> [SKTile]
Parameters
globalID
Int
tile global id.Return Value
[SKTile]
array of tiles. -
Returns tiles with a property of the given type.
Declaration
Swift
public func getTilesWithProperty(_ named: String, _ value: Any) -> [SKTile]
Parameters
type
String
type.Return Value
[SKTile]
array of tiles. -
Returns all tiles with animation.
Declaration
Swift
public func animatedTiles() -> [SKTile]
Return Value
[SKTile]
array of animated tiles. -
Return tile data from a global id.
Declaration
Swift
public func getTileData(globalID gid: Int) -> SKTilesetData?
Parameters
globalID
Int
global tile id.Return Value
SKTilesetData?
tile data (for valid id). -
Returns tiles with a property of the given type.
Declaration
Swift
public func getTileData(withProperty named: String) -> [SKTilesetData]
Parameters
type
String
type.Return Value
[SKTile]
array of tiles.
-
Add tile data array to the layer and render it.
Declaration
Swift
@discardableResult public func setLayerData(_ data: [UInt32], debug: Bool = false) -> Bool
Parameters
data
[UInt32]
tile data.debug
Bool
debug mode.Return Value
Bool
data was successfully added. -
Clear the layer of tiles.
Declaration
Swift
public func clearLayer()
-
Build an empty tile at the given coordinates. Returns an existing tile if one already exists, or nil if the coordinate is invalid.
Declaration
Swift
public func addTileAt(coord: CGPoint, gid: Int? = nil, tileType: String? = nil) -> SKTile?
Parameters
coord
CGPoint
tile coordinategid
Int?
tile id.tileType
String
optional tile class name.Return Value
SKTile?
tile. -
Build an empty tile at the given coordinates with a custom texture. Returns
nil
if the coordinate is invalid.Declaration
Swift
public func addTileAt(coord: CGPoint, texture: SKTexture? = nil, tileType: String? = nil) -> SKTile?
Parameters
coord
CGPoint
tile coordinate.texture
SKTexture?
optional tile texture.Return Value
SKTile?
tile. -
Build an empty tile at the given coordinates. Returns an existing tile if one already exists, or nil if the coordinate is invalid.
Declaration
Swift
public func addTileAt(_ x: Int, _ y: Int, gid: Int? = nil) -> SKTile?
Parameters
x
Int
x-coordinatey
Int
y-coordinategid
Int?
tile id.Return Value
SKTile?
tile. -
Build an empty tile at the given coordinates with a custom texture. Returns nil is the coordinate is invalid.
Declaration
Swift
public func addTileAt(_ x: Int, _ y: Int, texture: SKTexture? = nil) -> SKTile?
Parameters
x
Int
x-coordinatey
Int
y-coordinatetexture
SKTexture?
optional tile texture.Return Value
SKTile?
tile. -
Remove the tile at a given x/y coordinates.
Declaration
Swift
public func removeTileAt(_ x: Int, _ y: Int) -> SKTile?
Parameters
x
Int
x-coordinatey
Int
y-coordinateReturn Value
SKTile?
removed tile. -
Clear all tiles.
Declaration
Swift
public func clearTiles()
-
Remove the tile at a given coordinate.
Declaration
Swift
public func removeTileAt(coord: CGPoint) -> SKTile?
Parameters
coord
CGPoint
tile coordinate.Return Value
SKTile?
removed tile. -
Set a tile at the given coordinate.
Parameters
x
Int
x-coordinatey
Int
y-coordinateReturn Value
SKTile?
tile. -
Set a tile at the given coordinate.
Parameters
coord
CGPoint
tile coordinate.Return Value
SKTile?
tile.
-
Set the tile overlap. Only accepts a value between 0 - 1.0
Declaration
Swift
public func setTileOverlap(_ overlap: CGFloat)
Parameters
overlap
CGFloat
tile overlap value.
-
Called when the layer is finished rendering.
Declaration
Swift
override public func didFinishRendering(duration: TimeInterval = 0)
Parameters
duration
TimeInterval
fade-in duration.
-
Set a shader for tiles in this layer.
Declaration
Swift
public func setShader(for sktiles: [SKTile], named: String, uniforms: [SKUniform] = [])
Parameters
for
[SKTile]
tiles to apply shader to.named
String
shader file name.uniforms
[SKUniform]
array of shader uniforms.
-
Visualize the layer’s boundary shape.
Declaration
Swift
override public func drawBounds()
-
Run animation actions on all tiles layer.
Declaration
Swift
override public func runAnimationAsActions()
-
Remove tile animations.
Declaration
Swift
override public func removeAnimationActions(restore: Bool = false)
Parameters
restore
Bool
restore tile/obejct texture. -
Update the tile layer before each frame is rendered.
Declaration
Swift
override public func update(_ currentTime: TimeInterval)
Parameters
currentTime
TimeInterval
update interval.
-
Returns an array of valid tiles.
Declaration
Swift
@available(*, deprecated, message: "use `getTiles(﹚` instead") public func validTiles() -> [SKTile]
Return Value
[SKTile]
array of current tiles. -
Initialize this layer’s grid graph with an array of walkable tiles.
Declaration
Parameters
walkable
[SKTile]
array of walkable tiles.obstacles
[SKTile]
array of obstacle tiles.diagonalsAllowed
Bool
allow diagonal movement in the grid.withName
String?
optional graph name for identifying in scene.nodeClass
String?
graph node type.Return Value
GKGridGraph<GKGridGraphNode>?
navigation graph, if created. -
Initialize layer navigation graph by not specifying tiles to utilize.
Declaration
Swift
public func initializeGraph() -> GKGridGraph<GKGridGraphNode>?
Return Value
GKGridGraph<GKGridGraphNode>?
navigation graph, if created. -
Initialize this layer’s grid graph with an array of walkable & obstacle tile ids.
Declaration
Swift
public func initializeGraph(walkableIDs: [Int], obstacleIDs: [Int] = [], diagonalsAllowed: Bool = false, nodeClass: String? = nil) -> GKGridGraph<GKGridGraphNode>?
Parameters
walkableIDs
[Int]
array of walkable tile ids.obstacleIDs
[Int]
array of obstacle tile ids.diagonalsAllowed
Bool
allow diagonal movement in the grid.nodeClass
String?
graph node type.Return Value
GKGridGraph<GKGridGraphNode>?
navigation graph, if created. -
Return tiles with walkable data attributes.
Declaration
Swift
public func gatherWalkable() -> [SKTile]
Return Value
[SKTile]
array of tiles with walkable attribute. -
Return tiles with obstacle data attributes.
Declaration
Swift
public func gatherObstacles() -> [SKTile]
Return Value
[SKTile]
array of tiles with obstacle attribute. -
Parse the tile layer’s properties.
Declaration
Swift
override public func parseProperties(completion: (() -> Void)?)