SKTiledLayerObject
public class SKTiledLayerObject : SKEffectNode, SKTiledObject
Overview
The SKTiledLayerObject is the generic base class for all layer types. This class doesn’t
define any object or child types, but provides base behaviors for layered content, including:
- coordinate transformations
- validating coordinates
- positioning and alignment
Properties
| Property | Description |
|---|---|
| tilemap | Parent tilemap. |
| index | Layer index. Matches the index of the layer in the source TMX file. |
| size | Layer size (in tiles). |
| tileSize | Layer tile size (in pixels). |
| anchorPoint | Layer anchor point, used to position layers. |
| origin | Layer origin point, used for placing tiles. |
Instance Methods
| Method | Description |
|---|---|
| pointForCoordinate(coord:offset:) | Returns a point for a coordinate in the layer, with optional offset. |
| coordinateForPoint(_:) | Returns a tile coordinate for a given point in the layer. |
| touchLocation(_:) | Returns a converted touch location in map space. |
| coordinateAtTouchLocation(_:) | Returns the tile coordinate at a touch location. |
| isValid(coord:) | Returns true if the coordinate is valid. |
Usage
Coordinate transformation functions return points in the current tilemap projection:
node.position = tileLayer.pointForCoordinate(2, 1)
Coordinate transformation functions translate points to map coordinates:
coord = coordinateForPoint(touchPosition)
Return the tile coordinate at a touch event (iOS):
coord = imageLayer.coordinateAtTouchLocation(touchPosition)
Return the tile coordinate at a mouse event (macOS):
coord = groupLayer.coordinateAtMouseEvent(event: mouseClicked)
-
▶︎tilemapReference to the parent tilemap.
Declaration
Swift
public var tilemap: SKTilemap -
▶︎uuidUnique layer id.
Declaration
Swift
public var uuid: String -
▶︎typeLayer type.
Declaration
Swift
public var type: String! -
▶︎indexLayer index. Matches the index of the layer in the source TMX file.
Declaration
Swift
public var index: Int -
▶︎rawIndexPosition in the render tree.
Declaration
Swift
public var rawIndex: Int -
Custom layer properties.
Declaration
Swift
public var properties: [String : String] -
Ignore custom properties.
Declaration
Swift
public var ignoreProperties: Bool -
Overview
Tile offset hint for coordinate conversion.
Constants
Property Description center Tile is centered. top Tile is offset at the top. topLeft Tile is offset at the upper left. topRight Tile is offset at the upper right. bottom Tile is offset at the bottom. bottomLeft Tile is offset at the bottom left. bottomRight Tile is offset at the bottom right. left Tile is offset at the left side. right Tile is offset at the right side. Declaration
Swift
public enum TileOffset : Int -
▶︎colorLayer color.
Declaration
Swift
public dynamic var color: SKColor -
Grid visualization color.
Declaration
Swift
public var gridColor: SKColor -
Bounding box color.
Declaration
Swift
public var frameColor: SKColor -
Layer highlight color (for highlighting tiles)
Declaration
Swift
public var highlightColor: SKColor -
Layer highlight duration
Declaration
Swift
public var highlightDuration: TimeInterval -
▶︎isolatedLayer is isolated.
Declaration
Swift
public private(set) var isolated: Bool { get } -
▶︎offsetLayer offset value.
Declaration
Swift
public var offset: CGPoint -
▶︎sizeLayer size (in tiles).
Declaration
Swift
public var size: CGSize { get } -
▶︎tileSizeLayer tile size (in pixels).
Declaration
Swift
public var tileSize: CGSize { get } -
Layer anchor point, used to position layers.
Declaration
Swift
public var anchorPoint: CGPoint { get } -
▶︎graphPathfinding graph.
Declaration
Swift
public var graph: GKGridGraph<GKGridGraphNode>! -
Debug visualization options.
Declaration
Swift
public var debugDrawOptions: DebugDrawOptions { get set } -
Antialias lines.
Declaration
Swift
public var antialiased: Bool -
Map color blending value.
Declaration
Swift
public var colorBlendFactor: CGFloat -
Render scaling property.
Declaration
Swift
public var renderQuality: CGFloat -
Name used to access navigation graph.
Declaration
Swift
public var navigationKey: String -
Output current zPosition
Declaration
Swift
public var currentZPosition: CGFloat { get } -
Optional background color.
Declaration
Swift
public var backgroundColor: SKColor? { get set } -
Layer background sprite.
Declaration
Swift
lazy public var background: SKSpriteNode { get set }
-
▶︎originReturns the position of layer origin point (used to place tiles).
Declaration
Swift
public var origin: CGPoint { get } -
▶︎boundsReturns the frame rectangle of the layer (used to draw bounds).
Declaration
Swift
public var bounds: CGRect { get } -
Returns the points of the layer’s bounding shape.
Declaration
Swift
public func getVertices() -> [CGPoint]Return Value
[CGPoint]array of points. -
Tile update mode.
Declaration
Swift
public var updateMode: TileUpdateMode { get }
-
Initialize via the parser.
This intializer is meant to be called by the
SKTilemapParser, you should not use it directly.Declaration
Swift
public init?(layerName: String, tilemap: SKTilemap, attributes: [String : String])Parameters
layerNameStringlayer name.tilemapSKTilemapparent tilemap node.attributes[String: String]dictionary of layer attributes.Return Value
SKTiledLayerObject?tiled layer, if initialization succeeds.
-
Set the layer color with an
SKColor.Declaration
Swift
public func setColor(color: SKColor)Parameters
colorSKColorobject color. -
Set the layer color with a hex string.
Declaration
Swift
public func setColor(hexString: String)Parameters
hexStringStringcolor hex string.
-
▶︎layersChild layer array.
Declaration
Swift
public var layers: [SKTiledLayerObject] { get }
-
Returns a converted touch location.
-
Returns the tile coordinate at a touch location.
-
Returns a mouse event location in the current layer.
Declaration
Swift
public func mouseLocation(event: NSEvent) -> CGPointParameters
eventNSEventmouse event location.Return Value
CGPointconverted point in layer coordinate system. -
Returns the tile coordinate at a mouse event location.
Declaration
Swift
public func coordinateAtMouseEvent(event: NSEvent) -> CGPointParameters
eventNSEventmouse event location.Return Value
CGPointconverted point in layer coordinate system.
-
Returns true if the coordinate is valid.
Declaration
Swift
public func isValid(_ x: Int, _ y: Int) -> BoolParameters
xIntx-coordinate.yInty-coordinate.Return Value
Boolcoodinate is valid. -
Returns true if the coordinate is valid.
Declaration
Swift
public func isValid(coord: CGPoint) -> BoolParameters
coordCGPointtile coordinate.Return Value
Boolcoodinate is valid. -
Convert a point into the tile map’s coordinate space.
Declaration
Swift
public func convertPoint(_ point: CGPoint) -> CGPointParameters
pointCGPointinput point.Return Value
CGPointpoint with y-value inverted. -
Returns a point for a given coordinate in the layer, with optional offset values for x/y.
Declaration
Swift
public func pointForCoordinate(coord: CGPoint, offsetX: CGFloat = 0, offsetY: CGFloat = 0) -> CGPointParameters
coordCGPointtile coordinate.offsetXCGFloatx-offset value.offsetYCGFloaty-offset value.Return Value
CGPointpoint in layer (spritekit space). -
Returns a tile coordinate for a given point in the layer.
Declaration
Swift
public func coordinateForPoint(_ point: CGPoint) -> CGPointParameters
pointCGPointpoint in layer (spritekit space).Return Value
CGPointtile coordinate. -
Returns a tile coordinate for a given point in the layer as a
vector_int2.Declaration
Swift
public func vectorCoordinateForPoint(_ point: CGPoint) -> int2Parameters
pointCGPointpoint in layer.Return Value
int2tile coordinate.
-
Add an
SKNodechild node at the given x/y coordinates. By default, the zPositon will be higher than all of the other nodes in the layer.Declaration
Swift
public func addChild(_ node: SKNode, x: Int = 0, y: Int = 0, offset: CGPoint = CGPoint.zero, zpos: CGFloat? = nil)Parameters
nodeSKNodeobject.xIntx-coordinate.yInty-coordinate.offsetCGPointoffset amount.zposCGFloat?optional z-position. -
Add an
SKNodechild node at the given coordinates. By default, the zPositon will be higher than all of the other nodes in the layer.Declaration
Swift
public func addChild(_ node: SKNode, coord: CGPoint, offset: CGPoint = CGPoint.zero, zpos: CGFloat? = nil)Parameters
nodeSKNodeobject.coordCGPointtile coordinate.offsetCGPointoffset amount.zposCGFloat?optional z-position.
-
Called when the layer is finished rendering.
Declaration
Swift
public func didFinishRendering(duration: TimeInterval = 0)Parameters
durationTimeIntervalfade-in duration.
-
Set up physics boundary for the entire layer.
Declaration
Swift
public func setupLayerPhysicsBoundary(isDynamic: Bool = false)Parameters
isDynamicBoollayer is dynamic. -
Set up physics for child objects.
Declaration
Swift
public func setupPhysics()
-
Set a shader effect for the layer.
Declaration
Swift
public func setShader(named: String, uniforms: [SKUniform] = [])Parameters
namedStringshader file name.uniforms[SKUniform]array of shader uniforms.
-
Visualize the layer’s boundary shape.
Declaration
Swift
public func drawBounds() -
Toggle layer isolation on/off.
Declaration
Swift
public func isolateLayer(duration: TimeInterval = 0)
-
Initialize SpriteKit animation actions for the layer.
Declaration
Swift
public func runAnimationAsActions() -
Remove SpriteKit animations in the layer.
Declaration
Swift
public func removeAnimationActions(restore: Bool = false)Parameters
restoreBoolrestore tile/obejct texture. -
Update the layer before each frame is rendered.
Declaration
Swift
public func update(_ currentTime: TimeInterval)Parameters
currentTimeTimeIntervalupdate interval.
-
▶︎opacityLayer transparency.
Declaration
Swift
public var opacity: CGFloat { get set } -
▶︎visibleLayer visibility.
Declaration
Swift
public var visible: Bool { get set } -
Add a node at the given coordinates. By default, the zPositon will be higher than all of the other nodes in the layer.
Declaration
Swift
public func addChild(_ node: SKNode, _ x: Int, _ y: Int, dx: CGFloat = 0, dy: CGFloat = 0, zpos: CGFloat? = nil)Parameters
nodeSKNodeobject.xIntx-coordinate.yInty-coordinate.dxCGFloatoffset x-amount.dyCGFloatoffset y-amount.zposCGFloat?optional z-position. -
Returns a point for a given coordinate in the layer, with optional offset values for x/y.
Declaration
Swift
public func pointForCoordinate(_ x: Int, _ y: Int, offsetX: CGFloat = 0, offsetY: CGFloat = 0) -> CGPointParameters
xIntx-coordinate.yInty-coordinate.offsetXCGFloatx-offset value.offsetYCGFloaty-offset value.Return Value
CGPointposition in layer. -
Returns a point for a given coordinate in the layer, with optional offset.
Declaration
Swift
public func pointForCoordinate(coord: CGPoint, offset: CGPoint) -> CGPointParameters
coordCGPointtile coordinate.offsetCGPointtile offset.Return Value
CGPointpoint in layer. -
Returns a point for a given coordinate in the layer, with optional offset.
Declaration
Swift
public func pointForCoordinate(coord: CGPoint, tileOffset: SKTiledLayerObject.TileOffset = .center) -> CGPointParameters
coordCGPointtile coordinate.offsetTileOffsettile offset hint.Return Value
CGPointpoint in layer. -
Returns a tile coordinate for a given vector_int2 coordinate.
Declaration
Swift
public func pointForCoordinate(vec2: int2, offsetX: CGFloat = 0, offsetY: CGFloat = 0) -> CGPointParameters
vec2int2vector int2 coordinate.offsetXCGFloatx-offset value.offsetYCGFloaty-offset value.Return Value
CGPointposition in layer. -
Returns a tile coordinate for a given point in the layer.
Declaration
Swift
public func coordinateForPoint(_ x: Int, _ y: Int) -> CGPointParameters
xIntx-position.yInty-position.Return Value
CGPointposition in layer. -
▶︎centerReturns the center point of a layer.
Declaration
Swift
public var center: CGPoint { get } -
Calculate the distance from the layer’s origin
Declaration
Swift
public func distanceFromOrigin(_ pos: CGPoint) -> CGVector -
Returns a value for use in a dropdown menu.
Declaration
Swift
public var menuDescription: String { get } -
String representing the layer name (null if not set).
Declaration
Swift
public var layerName: String { get } -
▶︎parentsReturns an array of parent layers, beginning with the current.
Declaration
Swift
public var parents: [SKNode] { get } -
Returns an array of child layers.
Declaration
Swift
public var childLayers: [SKNode] { get } -
Returns an array of tiles/objects that conform to the
SKTiledGeometryprotocol.Declaration
Swift
public func renderableObjects() -> [SKNode]Return Value
[SKNode]array of child objects. -
Indicates the layer is a top-level layer.
Declaration
Swift
public var isTopLevel: Bool { get } -
▶︎pathTranslate the parent hierarchy to a path string
Declaration
Swift
public var path: String { get } -
Returns a string array representing the current layer name & index.
Declaration
Swift
public var layerStatsDescription: [String] { get } -
Parse the layer’s properties value.
Declaration
Swift
public func parseProperties(completion: (() -> Void)?) -
Returns a named property for the layer.
Declaration
Swift
public func getValue(forProperty name: String) -> String?Parameters
nameStringproperty name.Return Value
String?the property value, or nil if it does not exist. -
Set a property/value pair.
Declaration
Swift
public func setValue(_ value: String, forProperty name: String)Parameters
nameStringproperty name.valueStringproperty value.
View on GitHub
SKTiledLayerObject Class Reference