I want to change the zPosition of some tiles in my tile map. I use the following function to achieve this:
static func setZPosition(to tileMap: SKTileMapNode, and tileInfo: String){
let tileSize = tileMap.tileSize
let halfWidth = CGFloat(tileMap.numberOfColumns) / 2 * tileSize.width
let halfHeight = CGFloat(tileMap.numberOfRows) / 2 * tileSize.height
for column in 0..<tileMap.numberOfColumns{
for row in 0..<tileMap.numberOfRows{
let tileDefinition = tileMap.tileDefinition(atColumn: column, row: row)
let isCorrectTile = tileDefinition?.userData?[tileInfo] as? Bool
if isCorrectTile ?? false{
let x = CGFloat(column) * tileSize.width - halfWidth
let y = CGFloat(row) * tileSize.height - halfHeight
let rect = CGRect(x: 0, y: 0, width: tileSize.width, height: tileSize.height)
let tileNode = SKShapeNode(rect: rect)
tileNode.position = CGPoint(x: x, y: y)
tileNode.zPosition = 100
tileMap.addChild(tileNode)
print(tileNode.zPosition)
}
}
}
}
When I use this function, the print statetement shows me 5 times "100". This is correct because I want to change the zPosition of 5 tiles with the userData name "Z", which I use as the tileInfo parameter. The player sprite has a ZPosition of 10. But if I walk now with the player over a tile with zPosition 100, the player is still above the Tile. I want him to be behind the tile. What I'm doing wrong here?
There is nothing in the definition that allows for zPosition change (Which would make sense since the idea is to draw the tiles as fast as possible)
I would recommend having another tile set overlay it, or have individual sprites that pop out.