我目前有一个有两种变体的 TileSet:Poison 和 NotPoison。我希望这些变体对被点击做出不同的 react ,并且我还想遍历我的 TileMap 的各个行和列,以计算该行/列中“毒药”图 block 的数量。我以这种方式实现它是因为这种瓦片类型占用了瓦片 map 的某个区域。如何访问代码中的特定变体?是否可以改变变体之间的行为?
最佳答案
您可以通过单击图像 Poison
在 Tileset 中添加 userData并创建 userData
具有键和值的项目,例如'isPoisonKey' 值为 1 和 dataType bool 值。然后在你的gameScene.swift
代码你可以检查一个瓦片是否有这个用户数据。
此示例遍历 SKTileMapNode
中的每一列/行。命名为 background
并在每次找到 key 时打印到调试控制台 isPoisonKey
值为 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)
}
}
}
}
}
关于ios - 代码中的 SKTileSet 平铺变体访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40295719/