您可以通過例如SKTileMapNode
訪問圖塊定義,例如,當您將SKTileMapNode
添加到地圖編輯器時,它會關聯SKTileSet
。所以如果你的CountTiles.sks
有這樣的結構;
- BackgroundTiles(tile集合)
- 瓷磚(瓷磚集團)
- 瓷磚(瓷磚組規則)
- Tile1(平鋪定義)
- Tile2(平鋪定義)
- Tile3(瓷磚定義)
您可以訪問特定的圖塊定義,例如Tile1具有調用SKTileMapNode
的功能,其名稱爲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")