I currently have a TileSet with two variants: Poison and NotPoison. I want these variants to react differently to being clicked, and I also want to iterate over individual rows and columns of my TileMap to count the number of "Poison" tiles in that row/column. I implemented it in this way because this tile type takes up a certain area of the tileMap. How can I access the specific variant in the code? Is it possible to alter the behavior between variants?
You can add userData in the Tileset, by clicking on the image Poison
and creating a userData
item with key and value e.g. 'isPoisonKey' with value 1 and dataType boolean. Then in your gameScene.swift
code you can check if a tile has this userData.
This example iterates through every column / row in a SKTileMapNode
named background
and prints to the debug console each time it finds a key isPoisonKey
with a value of true
var backgroundLayer: SKTileMapNode!
override func didMove(to view: SKView) {
guard let backgroundLayer = childNode(withName: "background") as? SKTileMapNode else {
fatalError("Background node not loaded")
}
self.backgroundLayer = backgroundLayer
for row in 0..<self.backgroundLayer.numberOfRows {
for column in 0..<self.backgroundLayer.numberOfColumns {
let backgroundTile = self.backgroundLayer.tileDefinition(atColumn: column, row: row)
let isPoison = backgroundTile?.userData?.value(forKey: "isPoisonKey")
if let countNode = isPoison as? Bool {
// Code here
if countNode {
print(countNode)
}
}
}
}
}