3

I'm trying to programmatically create an SKTileMapNode in Swift, by allocating alternating tiles (these will be more complicated combinations later, this is just a trial, this example should display alternating green and yellow patterned tiles). However, when I run the app it just shows a block of colour, not the pattern that's supposed to be shown, not one of tiles repeated, just a block of colour similar to the background colour of one of the tiles. Even trying to just fill the map with one kind of tile doesn't show the pattern on the tile, just a solid block of colour.

The code in my scene that sets up the map in the SKScene didMove(to view: SKView) is as follows:

let tileset = SKTileSet(tileGroups: [
        SKTileGroup(tileDefinition: SKTileDefinition(texture: SKTexture(image: #imageLiteral(resourceName: "Sand3")))),
        SKTileGroup(tileDefinition: SKTileDefinition(texture: SKTexture(image: #imageLiteral(resourceName: "Grass1"))))
])

let tilemapNode = SKTileMapNode(tileSet: tileset, columns: 20, rows: 20, tileSize: CGSize(width: 32.0, height: 32.0))

var x = 0
var y = 0
var sand = false

while x < 20 {
    while y < 20 {
        var tilegroup: SKTileGroup
        if sand {
            tilegroup = tileset.tileGroups[0]
        } else {
            tilegroup = tileset.tileGroups[1]
        }
        tilemapNode.setTileGroup(tilegroup, forColumn: x, row: y)
        y = y + 1
        sand = !sand
    }
    x = x + 1
    y = 0
    sand = !sand
}

self.addChild(tilemapNode)

My view controller:

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view = SKView(frame: UIScreen.main.bounds)

        if let view = self.view as! SKView? {
            let scene = GameScene()
            scene.scaleMode = .aspectFill
            view.presentScene(scene)
        }
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }
}

App delegate:

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)

        let testGameController = GameViewController()

        window?.rootViewController = testGameController
        window?.makeKeyAndVisible()
        return true
    }
//...
}

I can't find any problems similar to this that use the built in SKTileMapNodes.

Cœur
  • 37,241
  • 25
  • 195
  • 267
merithayan
  • 31
  • 3
  • 1
    I would like a working example of this as well!! – Fluidity Jul 30 '17 at 02:25
  • This [SO Question and answer](https://stackoverflow.com/questions/41352251/what-is-the-proper-way-to-programmatically-create-a-spritekit-sktilemap/41434435#41434435) discuss this in detail. It might be the way you are creating the`SKTileGroup` that is causing the problem. – Mark Brownsword Jul 31 '17 at 11:14
  • @MarkBrownsword that seems to be the problem, how the tile group is made, it works when using the default tiles provided with SpriteKit when referenced by name. – merithayan Jul 31 '17 at 17:56

0 Answers0