您可以通过例如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")