-
Notifications
You must be signed in to change notification settings - Fork 33
Working With Layers
Once a map is loaded, you can interact with the layers within. Layers can be queried either by name, layer index, or type. Layers are parsed and indexed in the order they are read in your TMX document, so the bottom-most layer in Tiled will have an index of 0.
There are several ways to query layers from the SKTilemap
object:
// access a tile layer with a given name
if let backgoundLayer = tilemap.getLayers(named: "Background").first as? SKTileLayer {
backgroundLayer.opacity = 1
}
// filter tile layers based on name
let allBackgroundLayers = tilemap.tileLayers().filter { $0.name == "Background" }
// return tile layers with the custom `type` property
let allWallLayers = tilemap.getLayers(ofType: "walls")
If you need to access a layer before it is rendered, implement the SKTilemapDelegate.didAddLayer
method. See the Setting Up Your Scenes page for more details.
Creating new layers for your content is simple:
// create a new tile layer
let newTileLayer = tilemap.newTileLayer(named: "Walls")
The resulting layer is automatically parented to the SKTilemap
node, but you can also parent it to an existing SKGroupLayer
group node:
// create a new tile layer and parent it to an existing group
let houseGroup = tilemap.groupLayer(named: "HOUSE").first!
let newTileLayer = tilemap.newTileLayer(named: "Walls", group: houseGroup)
Because all SKTiled objects are derived from SpriteKit's default SKNode
type, you can technically parent any object converted from your Tiled scene to any layer type, or have own your sprite objects interacting with SKTile
sprites.
To aid in positioning your objects, every SKTiledLayerObject
layer type contains methods for converting tile coordinates to and from the current projection.
// add a child node
playerLayer.addChild(player)
// set the player position based on coordinate x & y values
player.position = playerLayer.pointForCoordinate(4, 12)
It is also possible to provide an offset value in x/y for more precise positioning:
player.position = playerLayer.pointForCoordinate(4, 12, offsetX: 8.0, offsetY: 4.0)
All SKTiledLayerObject
objects have convenience methods for adding children with coordinate values & optional offset and zPosition values:
playerLayer.addChild(player, 4, 12, zpos: 25.0)
See the Coordinates page for more information.
Group layers are a new feature in Tiled v1.0. Group layers are treated as any other layer type; adding or changing properties on a parent group will affect all child layers.
// query all of the group layers in the map
let groupLayers = tilemap.groupLayers
// access a tile layer with a given name
if let housesGroup = tilemap.getLayers(named: "Houses").first as? SKGroupLayer {
// add custom prop objects
for childLayer in housesGroup.getLayers() {
let lampPost = LampPostProp()
childLayer.addChild(lampPost)
lampPost.position = childLayer.pointForCoordinate(lampCoord.defaultCoords)
}
}
With the addition of group layers, SKTiledLayerObject
objects now have two new properties: SKTiledLayerObject.path
& SKTiledLayerObject.parents
which represent the layer's location in the Tiled scene hierarchy.
// query layers based on path
let wallsLayer = tilemap.getLayers(atPath: "WALLS/INNER/inner_walls").first!
// get an array of the node & its parents
print(wallsLayer.parents)
// ["inner_walls", "INNER", "WALLS"]
© 2016 Michael Fessenden under open source license