您可以通过SKTileMapNode
例如当您添加SKTileMapNode
到地图编辑器时,它具有关联的SKTileSet
. 所以如果你CountTiles.sks
有这样的结构;
- BackgroundTiles(瓷砖集)
- 瓷砖(瓷砖组)
- 平铺(平铺组规则)
- Tile1(瓷砖定义)
- Tile2(瓷砖定义)
- Tile3(瓷砖定义)
SKTileMapNode
您可以使用调用这样命名的函数访问特定的 Tile 定义,例如 Tile1 background
;
func backgroundTileDefinition(key: String) -> SKTileDefinition {
guard let backgroundLayer = childNode(withName: "background") as? SKTileMapNode else {
fatalError("Background node not loaded")
}
guard let backgroundTile = backgroundLayer.tileSet.tileGroups.first(where: {$0.name == "Tiles"}) else {
fatalError("TileSet not found")
}
guard let backgroundTileSetRule = backgroundTile.rules.first(where: {$0.name == "Tile"}) else {
fatalError("Tileset rule not found")
}
guard let backgroundTileDefinition = backgroundTileSetRule.tileDefinitions.first(where: {$0.name == key}) else {
fatalError("Tile definition not found")
}
return backgroundTileDefinition
}
像这样调用函数;
let backgroundTileDefinition = backgroundTileDefinition(key: "Tile1")