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.