sprite-kitsktilemapnode

Create/Load SKTilemapnode without using SKS/Scene files


I create my sprite kit games programmatically and the sks files end up just confusing me.

Is there a way to load or create a tile map node without having to use the sks/scene file?


Solution

  • I figured out a way to to do it:

    Then grab the tile map from the sks file. Weird trick is that you have to remove it from its parent first:

        guard 
          let tileScene = SKScene.unarchiveFromFile(file: "TileMaps"),
          let testMap = tileScene.childNode(withName: "Dungeon1")
            as? SKTileMapNode else {
                fatalError("Background node not loaded")
        }
        self.testMap = testMap
        self.testMap.removeFromParent()
        self.testMap.zPosition = 1200
    
        self.addChild(testMap)